package framework; import java.awt.event.*; import java.awt.*; import javax.swing.*; /** Base class for many controllers.
The controller component receives commands from the GUI and executes them. Programmersneed to seek a balance between a centralized controller and controllers for each use case.
Note: This controller only works for ActionEvent
Example:
public class MyController extends Controller {
public MyController(AppWindow aw) {
super(aw);
// etc.
}
public void actionPerformed(ActionEvent e) {
try {
String arg = e.getActionCommand();
if (arg.equals("Command 1")) handleCommand1();
else if (arg.equals("Command 2")) handleCommand2();
else if (arg.equals("Coimmand 3")) handleCommand3();
// etc.
} catch (Exception x) {
FrameworkUtils.error("Exception thrown: " + x);
}
}
private void handleCommand1() {
MyModel m = (MyModel)appWindow.getModel();
// update m
}
private void handleCommand2() { ... }
private void handleCommand3() { ... }
// etc.
}
*/
public abstract class Controller implements ActionListener {
/**
The AppWindow that this controller is attached to.
From this we can get the model.
*/
protected AppWindow appWindow;
/**
Initializes appWindow.
@param aw the appWindow that creates this controller.
*/
public Controller(AppWindow aw) {
appWindow = aw;
}
}