KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > EditWin


1 package jimm.datavision.gui;
2 import jimm.datavision.gui.cmd.CompoundCommand;
3 import jimm.util.I18N;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 /**
8  * The abstract parent of all edit windows except the main design window.
9  * Handles common behavior.
10  *
11  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
12  */

13 public abstract class EditWin extends JDialog implements ActionListener
14 {
15
16 protected Designer designer;
17 protected JButton revertButton;
18 protected CompoundCommand commands;
19
20 /**
21  * Constructor for a non-modal dialog.
22  *
23  * @param designer the design window to which this dialog belongs
24  * @param title window title
25  * @param commandNameKey the {@link jimm.util.I18N} lookup key for the command
26  * name
27  */

28 public EditWin(Designer designer, String JavaDoc title, String JavaDoc commandNameKey) {
29     this(designer, title, commandNameKey, false);
30 }
31
32 /**
33  * Constructor.
34  *
35  * @param designer the window to which this dialog belongs
36  * @param title window title
37  * @param commandNameKey the {@link jimm.util.I18N} lookup key for the command
38  * name
39  * @param modal passed on to superclass
40  */

41 public EditWin(Designer designer, String JavaDoc title, String JavaDoc commandNameKey,
42            boolean modal)
43 {
44     super(designer.getFrame(), title, modal);
45     this.designer = designer;
46     commands = new CompoundCommand(I18N.get(commandNameKey));
47 }
48
49 /**
50  * Builds and returns a panel containing the bottom four buttons.
51  *
52  * @return the four buttons OK, Apply, Revert, and Cancel
53  */

54 protected JPanel closeButtonPanel() {
55     JPanel buttonPanel = new JPanel();
56     JButton button;
57
58     buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
59     button.addActionListener(this);
60     button.setDefaultCapable(true);
61
62     buttonPanel.add(button = new JButton(I18N.get("GUI.apply")));
63     button.addActionListener(this);
64
65     buttonPanel.add(revertButton = new JButton(I18N.get("GUI.revert")));
66     revertButton.addActionListener(this);
67     revertButton.setEnabled(false);
68
69     buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
70     button.addActionListener(this);
71
72     return buttonPanel;
73 }
74
75 /**
76  * Handles the four buttons.
77  *
78  * @param e action event
79  */

80 public void actionPerformed(ActionEvent e) {
81     String JavaDoc cmd = e.getActionCommand();
82     if (I18N.get("GUI.ok").equals(cmd)) {
83     save(true); // Should add commands
84
dispose();
85     }
86     else if (I18N.get("GUI.apply").equals(cmd)) {
87     save(false); // Should add commands
88
}
89     else if (I18N.get("GUI.revert").equals(cmd)) {
90     revert();
91     }
92     else if (I18N.get("GUI.cancel").equals(cmd)) {
93     revert();
94     dispose();
95     }
96 }
97
98 /**
99  * Saves all data by creating and performing a command. If not, that means we
100  * are done and we should give the compound command to the design window.
101  * <p>
102  * Subclasses that implement {@link #doSave} should probably create a command,
103  * call its <code>perform</code> method, and add it to the <var>commands</var>
104  * compound command.
105  *
106  * @param closing passed through to <code>doSave</code>
107  */

108 protected void save(boolean closing) {
109     doSave();
110     revertButton.setEnabled(true);
111
112     if (closing && commands.numCommands() > 0)
113     designer.addCommand(commands);
114 }
115
116 /**
117  * Saves all data by creating a new command, performing it, and adding it
118  * to <var>commands</var>.
119  */

120 protected abstract void doSave();
121
122 /**
123  * Reverts all state information by undoing any commands previously performed
124  * and emptying the compound command.
125  *
126  * @see #doRevert
127  */

128 protected void revert() {
129     commands.undo();
130     commands = new CompoundCommand(commands.getName());
131     doRevert();
132 }
133
134 /**
135  * Gives subclasses a chance to clean up their GUI.
136  */

137 protected abstract void doRevert();
138
139 }
140
Popular Tags