import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* A panel containing the input controls for one account
*/
public class Controller extends JPanel implements ActionListener {
/**
* The Model (Account) we are controlling
*/
private Model model; //*1 Knows about model, but not vice versa
/**
* Save these so actionPerformed() can access
*/
private JTextField textField;
private JButton depositButton, withdrawButton;
public Controller (Model model) {
// Stash
this.model = model; //*1
// Layout of our panel
setBorder (new TitledBorder ("Controller"));
setLayout (new GridLayout (0, 1, 20, 20));
// Our widget for entering amount of deposit or withdrawal
textField = new JTextField ("0", 6); //*2 Provide control widgets
add (textField);
// Our deposit button
depositButton = new JButton ("Deposit"); //*2
depositButton.addActionListener (this);
add (depositButton); //*2
// Withdraw button
withdrawButton = new JButton ("Withdraw"); //*2
withdrawButton.addActionListener (this);
add (withdrawButton); //*2
}
/**
* Common callback for both buttons,
* we use ivar to disambiguate.
*/
public void actionPerformed (ActionEvent e) { //*3 Button action = call model for data
if (e.getSource()==depositButton) //*3
model.deposit (Integer.parseInt (textField.getText())); //*3
else if (e.getSource()==withdrawButton) //*3
model.withdraw (Integer.parseInt (textField.getText())); //*3
}
}