001: package jimm.datavision.gui;
002:
003: import jimm.datavision.field.Field;
004: import jimm.datavision.gui.cmd.WidgetRenameCommand;
005: import java.awt.event.MouseEvent;
006: import java.awt.event.ActionEvent;
007: import javax.swing.JDialog;
008:
009: /**
010: *
011: * An abstract superclass for widgets that open separate windows used to
012: * edit the widget.
013: *
014: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
015: */
016: public abstract class EditWinWidget extends FieldWidget {
017:
018: protected JDialog editor;
019:
020: /**
021: * Constructor.
022: *
023: * @param sw section widget in which the field's new widget will reside
024: * @param field a report field
025: */
026: EditWinWidget(SectionWidget sw, Field field) {
027: super (sw, field);
028: }
029:
030: protected void addCustomPopupItems() {
031: MenuUtils.addToMenu(this , popup, "EditWinWidget.popup_edit",
032: POPUP_FONT);
033: MenuUtils.addToMenu(this , popup, "EditWinWidget.popup_rename",
034: POPUP_FONT);
035: popup.addSeparator();
036: }
037:
038: /**
039: * Performs some action based on the action command string (the menu
040: * item text).
041: */
042: public void actionPerformed(ActionEvent e) {
043: String command = e.getActionCommand();
044: if (command == null)
045: return;
046:
047: if ("edit".equals(command))
048: openEditor();
049: else if ("rename".equals(command))
050: rename();
051: else
052: super .actionPerformed(e);
053: }
054:
055: /**
056: * if this is a double-click, start editing; else handle the mouse event
057: * like a normal field widget.
058: *
059: * @param e mouse event
060: */
061: public void mouseClicked(MouseEvent e) {
062: if (sectionWidget.designer.isPlacingNewTextField())
063: sectionWidget.createNewTextField(e);
064: if (e.getClickCount() == 2)
065: openEditor();
066: else
067: super .mouseClicked(e);
068: }
069:
070: /**
071: * Makes our text editable and starts editing.
072: */
073: public void openEditor() {
074: if (editor == null)
075: editor = createEditor();
076: else
077: updateEditor();
078: editor.setVisible(true);
079: editor.toFront();
080: }
081:
082: /**
083: * Creates and returns a new frame suitable for editing this widget.
084: *
085: * @return a frame (window or dialog) used to edit the widget
086: */
087: protected abstract JDialog createEditor();
088:
089: /**
090: * Updates the editor. This method supplies default do-nothing behavior.
091: */
092: protected void updateEditor() {
093: }
094:
095: /**
096: * Opens a name editor.
097: */
098: protected void rename() {
099: Designer designer = sectionWidget.designer;
100: String name = new AskStringDialog(designer.getFrame(),
101: getEditorTitle(), getEditorLabel(), getWidgetName())
102: .getString();
103: if (name != null)
104: designer.performCommand(new WidgetRenameCommand(this ,
105: getWidgetName(), name));
106: }
107:
108: /**
109: * Returns the name string.
110: *
111: * @return the name to be edited
112: */
113: protected abstract String getWidgetName();
114:
115: /**
116: * Returns the name edit window's title.
117: */
118: protected abstract String getEditorTitle();
119:
120: /**
121: * Returns the name edit window's label (the prompt that goes before the
122: * text edit field).
123: */
124: protected abstract String getEditorLabel();
125:
126: /**
127: * Set editable object's name. The new name is guaranteed not to be
128: * <code>null</code>.
129: *
130: * @param newName the new name string
131: */
132: public abstract void setWidgetName(String newName);
133:
134: }
|