public interface Resource{
}
Another abstraction we have introduced is the Manager interface:
public interface Manager {
public void addResource(Resource
resource);
public Iterator
iterator();
}
This interface prescribes to behavior methods: how to add the Resource,
and how to supply the abstract iterator which will not reveal the underlying
implementation of the Resource.
class ActionManager implements Manager{
private Vector
resources;
public ActionManager(){
resources = new Vector();
}
public void addResource(Resource
resourceIn){
resources.add(resourceIn);
}
public Iterator
iterator(){
return resources.iterator();
}
}
The resources are managed by putting them inside a very simple collection
-- a Vector.
Resources are implemented by extending the GUI framework abstraction -- AbstractAction; for instance, DeleteAction class will implement the customized code that will handle the delete operation in the actionPerformed method:
class DeleteAction extends javax.swing.AbstractAction
implements Resource{
public DeleteAction(String
name, Icon icon){
super(name, icon);
}
public void actionPerformed(ActionEvent
e){
// place your custom delete code here...
}
}
The Application class will instantiate the Manager and will add the appropriate Resources to it; then it will instantiate the GUI and will set the ActionManager for the GUI:
class Application{
private Manager
manager;
private GUI gui;
public Application(){
manager = new ActionManager();
manager.addResource(new SaveAction("Save", new ImageIcon("save.gif")));
manager.addResource(new DeleteAction("Delete", new ImageIcon("delete.jpg")));
manager.addResource(new NewAction("New", new ImageIcon("new.gif")));
manager.addResource(new CloneAction("Cloning", new ImageIcon("clone.gif")));
gui = new GUI("Action Framework");
gui.setActionManager(manager);
}
public static void
main(String[] args) {
new Application();
}
}
Finally, the GUI class will set up the graphical framework, and then interrogate the Manager in order to include all the Resources managed by the Manager:
import java.awt.*;
import javax.swing.*;
import java.util.*;
class GUI extends JFrame {
private Manager
manager;
private JMenu menu;
private JToolBar
toolbar;
private Action
action;
public GUI(){
setUp();
}
public GUI(String
title){
super(title);
setUp();
}
public void setActionManager(Manager
manager){
Iterator iterator = manager.iterator();
while(iterator.hasNext()){
action = (Action)iterator.next();
menu.add(action);
toolbar.add(action);
}
}
private void setUp(){
getContentPane().setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
menu = new JMenu("File");
menuBar.add(menu);
toolbar = new JToolBar();
getContentPane().add(toolbar, "North");
setBounds(20, 40, 350, 150);
setVisible(true);
}
}
It is important to note that in the above framework, we cannot find any place where the decisions as to which Action to run are being made. Nowhere can we find and if-then-else constructs. The decision is actualy made by the framework.