KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jimm.datavision.gui;
2 import jimm.datavision.field.Field;
3 import jimm.datavision.gui.cmd.WidgetRenameCommand;
4 import java.awt.event.MouseEvent JavaDoc;
5 import java.awt.event.ActionEvent JavaDoc;
6 import javax.swing.JDialog JavaDoc;
7
8 /**
9  *
10  * An abstract superclass for widgets that open separate windows used to
11  * edit the widget.
12  *
13  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
14  */

15 public abstract class EditWinWidget extends FieldWidget {
16
17 protected JDialog JavaDoc editor;
18
19 /**
20  * Constructor.
21  *
22  * @param sw section widget in which the field's new widget will reside
23  * @param field a report field
24  */

25 EditWinWidget(SectionWidget sw, Field field) {
26     super(sw, field);
27 }
28
29 protected void addCustomPopupItems() {
30     MenuUtils.addToMenu(this, popup, "EditWinWidget.popup_edit", POPUP_FONT);
31     MenuUtils.addToMenu(this, popup, "EditWinWidget.popup_rename", POPUP_FONT);
32     popup.addSeparator();
33 }
34
35 /**
36  * Performs some action based on the action command string (the menu
37  * item text).
38  */

39 public void actionPerformed(ActionEvent JavaDoc e) {
40     String JavaDoc command = e.getActionCommand();
41     if (command == null) return;
42
43     if ("edit".equals(command))
44     openEditor();
45     else if ("rename".equals(command))
46     rename();
47     else
48     super.actionPerformed(e);
49 }
50
51 /**
52  * if this is a double-click, start editing; else handle the mouse event
53  * like a normal field widget.
54  *
55  * @param e mouse event
56  */

57 public void mouseClicked(MouseEvent JavaDoc e) {
58     if (sectionWidget.designer.isPlacingNewTextField())
59     sectionWidget.createNewTextField(e);
60     if (e.getClickCount() == 2)
61     openEditor();
62     else
63     super.mouseClicked(e);
64 }
65
66 /**
67  * Makes our text editable and starts editing.
68  */

69 public void openEditor() {
70     if (editor == null)
71     editor = createEditor();
72     else
73     updateEditor();
74     editor.setVisible(true);
75     editor.toFront();
76 }
77
78 /**
79  * Creates and returns a new frame suitable for editing this widget.
80  *
81  * @return a frame (window or dialog) used to edit the widget
82  */

83 protected abstract JDialog JavaDoc createEditor();
84
85 /**
86  * Updates the editor. This method supplies default do-nothing behavior.
87  */

88 protected void updateEditor() {}
89
90 /**
91  * Opens a name editor.
92  */

93 protected void rename() {
94     Designer designer = sectionWidget.designer;
95     String JavaDoc name = new AskStringDialog(designer.getFrame(), getEditorTitle(),
96                       getEditorLabel(), getWidgetName())
97     .getString();
98     if (name != null)
99     designer.performCommand(new WidgetRenameCommand(this, getWidgetName(),
100                             name));
101 }
102
103 /**
104  * Returns the name string.
105  *
106  * @return the name to be edited
107  */

108 protected abstract String JavaDoc getWidgetName();
109
110 /**
111  * Returns the name edit window's title.
112  */

113 protected abstract String JavaDoc getEditorTitle();
114
115 /**
116  * Returns the name edit window's label (the prompt that goes before the
117  * text edit field).
118  */

119 protected abstract String JavaDoc getEditorLabel();
120
121 /**
122  * Set editable object's name. The new name is guaranteed not to be
123  * <code>null</code>.
124  *
125  * @param newName the new name string
126  */

127 public abstract void setWidgetName(String JavaDoc newName);
128
129 }
130
Popular Tags