KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > cmd > CommandHistory


1 package jimm.datavision.gui.cmd;
2 import jimm.util.I18N;
3 import java.util.ArrayList JavaDoc;
4 import javax.swing.JMenuItem JavaDoc;
5
6 /**
7  * A command history holds comands and manages undo and redo behavior. To
8  * use a new command for the first time, pass it to <code>perform</code>.
9  * You can then call <code>undo</code> and <code>redo</code> to walk the
10  * command history chain.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public class CommandHistory {
15
16 protected ArrayList JavaDoc commands;
17 protected int commandIndex;
18 /**
19  * If the command index is different than the baseline index then something
20  * has changed, and <code>isChanged</code> will return <code>true</code>.
21  */

22 protected int baselineIndex;
23 protected JMenuItem JavaDoc undoMenuItem;
24 protected JMenuItem JavaDoc redoMenuItem;
25
26 public CommandHistory() {
27     this(null, null);
28 }
29
30 public CommandHistory(JMenuItem JavaDoc undoMenuItem, JMenuItem JavaDoc redoMenuItem) {
31     this.undoMenuItem = undoMenuItem;
32     this.redoMenuItem = redoMenuItem;
33     commands = new ArrayList JavaDoc();
34     commandIndex = 0;
35     baselineIndex = 0;
36 }
37
38 public void setMenuItems(JMenuItem JavaDoc undoMenuItem, JMenuItem JavaDoc redoMenuItem) {
39     this.undoMenuItem = undoMenuItem;
40     this.redoMenuItem = redoMenuItem;
41 }
42
43 /** Answers the question, "Is there anything to undo?" */
44 public boolean canUndo() { return commandIndex > 0; }
45
46 /** Answers the question, "Is there anything to redo?" */
47 public boolean canRedo() { return commandIndex < commands.size(); }
48
49 /**
50  * Resets the baseline index. The method <code>isChanged</code> returns
51  * <code>false</code> only when the baseline index is equal to the
52  * current command index.
53  */

54 public void setBaseline() { baselineIndex = commandIndex; }
55     
56
57 /** Answers the question, "Has anything changed?" */
58 public boolean isChanged() { return baselineIndex != commandIndex; }
59
60 /**
61  * Add a command to the history list without performing it. If the history
62  * cursor is not at the top of the stack, first erase all of the
63  * commands after the cursor.
64  *
65  * @param command the command to add
66  */

67 public synchronized void add(Command command) {
68     // Erase commands after this one, if any
69
for (int i = commands.size() - 1; i >= commandIndex; --i)
70     commands.remove(i);
71
72     // Add new command to end of history list
73
commands.add(command);
74     ++commandIndex;
75
76     updateMenu();
77 }
78
79 /**
80  * Perform a command and add it to the history list. If the history
81  * cursor is not at the top of the stack, first erase all of the
82  * commands after the cursor.
83  *
84  * @param command the command to perform
85  */

86 public synchronized void perform(Command command) {
87     command.perform();
88     add(command);
89 }
90
91 /** Undo the command at the history cursor. */
92 public synchronized void undo() {
93     if (commandIndex > 0) {
94     --commandIndex;
95     ((Command)commands.get(commandIndex)).undo();
96     updateMenu();
97     }
98 }
99
100 /** Redo the command under the the history cursor. */
101 public synchronized void redo() {
102     if (commandIndex < commands.size()) {
103     ((Command)commands.get(commandIndex)).redo();
104     ++commandIndex;
105     updateMenu();
106     }
107 }
108
109 /**
110  * Return the name of the command that would be undone were one to call
111  * undo(). Returns <code>null</code> if there is no such command.
112  */

113 public String JavaDoc getUndoName() {
114     return commandIndex > 0 ? getCommandName(commandIndex - 1) : null;
115 }
116
117 /**
118  * Return the name of the command that would be redone were one to call
119  * redo(). Returns <code>null</code> if there is no such command.
120  */

121 public String JavaDoc getRedoName() {
122     return commandIndex < commands.size()
123     ? getCommandName(commandIndex) : null;
124 }
125
126 /**
127  * Return the name of the command at index. Returns <code>null</code>
128  * if there is no such command.
129  */

130 public String JavaDoc getCommandName(int index) {
131     Command cmd = null;
132     synchronized(this) {
133     cmd = (Command)commands.get(index);
134     }
135     return (cmd != null) ? cmd.getName() : null;
136 }
137
138 protected void updateMenu() {
139     if (undoMenuItem != null)
140     updateMenuItem(undoMenuItem, I18N.get("CommandHistory.undo"),
141                getUndoName(), canUndo());
142     if (redoMenuItem != null)
143     updateMenuItem(redoMenuItem, I18N.get("CommandHistory.redo"),
144                getRedoName(), canRedo());
145 }
146
147 protected void updateMenuItem(JMenuItem JavaDoc item, String JavaDoc verb, String JavaDoc cmdName,
148                   boolean canDo)
149 {
150     if (canDo)
151     item.setText(verb + " " + cmdName);
152     else
153     item.setText(verb);
154
155     item.setEnabled(canDo);
156 }
157
158 }
159
Popular Tags