001: package jimm.datavision.gui;
002:
003: import jimm.datavision.gui.cmd.CompoundCommand;
004: import jimm.util.I18N;
005: import java.awt.event.*;
006: import javax.swing.*;
007:
008: /**
009: * The abstract parent of all edit windows except the main design window.
010: * Handles common behavior.
011: *
012: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
013: */
014: public abstract class EditWin extends JDialog implements ActionListener {
015:
016: protected Designer designer;
017: protected JButton revertButton;
018: protected CompoundCommand commands;
019:
020: /**
021: * Constructor for a non-modal dialog.
022: *
023: * @param designer the design window to which this dialog belongs
024: * @param title window title
025: * @param commandNameKey the {@link jimm.util.I18N} lookup key for the command
026: * name
027: */
028: public EditWin(Designer designer, String title,
029: String commandNameKey) {
030: this (designer, title, commandNameKey, false);
031: }
032:
033: /**
034: * Constructor.
035: *
036: * @param designer the window to which this dialog belongs
037: * @param title window title
038: * @param commandNameKey the {@link jimm.util.I18N} lookup key for the command
039: * name
040: * @param modal passed on to superclass
041: */
042: public EditWin(Designer designer, String title,
043: String commandNameKey, boolean modal) {
044: super (designer.getFrame(), title, modal);
045: this .designer = designer;
046: commands = new CompoundCommand(I18N.get(commandNameKey));
047: }
048:
049: /**
050: * Builds and returns a panel containing the bottom four buttons.
051: *
052: * @return the four buttons OK, Apply, Revert, and Cancel
053: */
054: protected JPanel closeButtonPanel() {
055: JPanel buttonPanel = new JPanel();
056: JButton button;
057:
058: buttonPanel.add(button = new JButton(I18N.get("GUI.ok")));
059: button.addActionListener(this );
060: button.setDefaultCapable(true);
061:
062: buttonPanel.add(button = new JButton(I18N.get("GUI.apply")));
063: button.addActionListener(this );
064:
065: buttonPanel.add(revertButton = new JButton(I18N
066: .get("GUI.revert")));
067: revertButton.addActionListener(this );
068: revertButton.setEnabled(false);
069:
070: buttonPanel.add(button = new JButton(I18N.get("GUI.cancel")));
071: button.addActionListener(this );
072:
073: return buttonPanel;
074: }
075:
076: /**
077: * Handles the four buttons.
078: *
079: * @param e action event
080: */
081: public void actionPerformed(ActionEvent e) {
082: String cmd = e.getActionCommand();
083: if (I18N.get("GUI.ok").equals(cmd)) {
084: save(true); // Should add commands
085: dispose();
086: } else if (I18N.get("GUI.apply").equals(cmd)) {
087: save(false); // Should add commands
088: } else if (I18N.get("GUI.revert").equals(cmd)) {
089: revert();
090: } else if (I18N.get("GUI.cancel").equals(cmd)) {
091: revert();
092: dispose();
093: }
094: }
095:
096: /**
097: * Saves all data by creating and performing a command. If not, that means we
098: * are done and we should give the compound command to the design window.
099: * <p>
100: * Subclasses that implement {@link #doSave} should probably create a command,
101: * call its <code>perform</code> method, and add it to the <var>commands</var>
102: * compound command.
103: *
104: * @param closing passed through to <code>doSave</code>
105: */
106: protected void save(boolean closing) {
107: doSave();
108: revertButton.setEnabled(true);
109:
110: if (closing && commands.numCommands() > 0)
111: designer.addCommand(commands);
112: }
113:
114: /**
115: * Saves all data by creating a new command, performing it, and adding it
116: * to <var>commands</var>.
117: */
118: protected abstract void doSave();
119:
120: /**
121: * Reverts all state information by undoing any commands previously performed
122: * and emptying the compound command.
123: *
124: * @see #doRevert
125: */
126: protected void revert() {
127: commands.undo();
128: commands = new CompoundCommand(commands.getName());
129: doRevert();
130: }
131:
132: /**
133: * Gives subclasses a chance to clean up their GUI.
134: */
135: protected abstract void doRevert();
136:
137: }
|