01: package jimm.datavision.gui.cmd;
02:
03: import java.util.ArrayList;
04: import java.util.Iterator;
05: import java.util.ListIterator; // For reverse traversal
06:
07: /**
08: * A compound command holds a list of commands and allows their use as
09: * one single command.
10: *
11: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
12: */
13: public class CompoundCommand extends CommandAdapter {
14:
15: protected ArrayList commands;
16:
17: public CompoundCommand(String name) {
18: super (name);
19: commands = new ArrayList();
20: }
21:
22: public void add(Command c) {
23: commands.add(c);
24: }
25:
26: public int numCommands() {
27: return commands.size();
28: }
29:
30: public void perform() {
31: for (Iterator iter = commands.iterator(); iter.hasNext();)
32: ((Command) iter.next()).perform();
33: }
34:
35: public void undo() {
36: for (ListIterator iter = commands.listIterator(commands.size()); iter
37: .hasPrevious();)
38: ((Command) iter.previous()).undo();
39: }
40:
41: public void redo() {
42: for (Iterator iter = commands.iterator(); iter.hasNext();)
43: ((Command) iter.next()).redo();
44: }
45:
46: }
|