KickJava   Java API By Example, From Geeks To Geeks.

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


1 package jimm.datavision.gui;
2 import jimm.datavision.field.Field;
3 import jimm.datavision.gui.cmd.TypingCommand;
4 import jimm.util.I18N;
5 import java.awt.Cursor JavaDoc;
6 import java.awt.Font JavaDoc;
7 import java.awt.FontMetrics JavaDoc;
8 import java.awt.event.MouseEvent JavaDoc;
9 import java.awt.event.KeyEvent JavaDoc;
10 import java.awt.event.ActionEvent JavaDoc;
11 import javax.swing.JTextPane JavaDoc;
12 import javax.swing.text.BadLocationException JavaDoc;
13
14 /**
15  *
16  * A text field widget is a field widget that is editable.
17  *
18  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
19  */

20 public class TextFieldWidget extends FieldWidget {
21
22 protected int origHeight;
23 protected int lineHeight;
24 protected boolean changingEditState;
25 protected TypingCommand typingCommand;
26
27 /**
28  * Constructor.
29  *
30  * @param sw section widget in which the field's new widget will reside
31  * @param field a report field
32  */

33 public TextFieldWidget(SectionWidget sw, Field field) {
34     super(sw, field);
35
36     // Calculate line height.
37
Font JavaDoc f = field.getFormat().getFont();
38     FontMetrics JavaDoc fm = swingField.getComponent().getFontMetrics(f);
39     lineHeight = fm.getHeight();
40 }
41
42 protected String JavaDoc getPopupNameText() {
43     // This string is now found in the menu properties file.
44
return I18N.get(I18N.MENU_FILE_PREFIX, "TextFieldWidget.popup_name");
45 }
46
47 protected void addCustomPopupItems() {
48     MenuUtils.addToMenu(this, popup, "TextFieldWidget.popup_edit", POPUP_FONT);
49     popup.addSeparator();
50 }
51
52 /**
53  * Performs some action based on the action command string (the menu
54  * item text).
55  */

56 public void actionPerformed(ActionEvent JavaDoc e) {
57     String JavaDoc command = e.getActionCommand();
58     if (command == null) return;
59
60     if ("edit".equals(command))
61     startEditing();
62     else
63     super.actionPerformed(e);
64 }
65
66 /**
67  * if this is a double-click, start editing; else handle the mouse event
68  * like a normal field widget.
69  *
70  * @param e mouse event
71  */

72 public void mouseClicked(MouseEvent JavaDoc e) {
73     if (e.getClickCount() == 2)
74     startEditing();
75     else
76     super.mouseClicked(e);
77 }
78
79 /**
80  * Makes our text editable and starts editing.
81  */

82 public void startEditing() {
83     // Bug fix: endEditing is called while startEditing is running
84
synchronized(this) {
85     if (changingEditState)
86         return;
87     changingEditState = true;
88     }
89
90     if (!selected)
91     select(true);
92
93     JTextPane JavaDoc textPane = (JTextPane JavaDoc)getComponent();
94     textPane.setEditable(true);
95     textPane.getCaret().setVisible(true);
96     textPane.requestFocus();
97     origHeight = textPane.getBounds().height;
98
99     sectionWidget.designer.setIgnoreKeys(true);
100
101     textPane.removeMouseListener(this);
102     textPane.removeMouseMotionListener(this);
103
104     changingEditState = false;
105
106     typingCommand = new TypingCommand(this, origHeight);
107 }
108
109 /**
110  * Stores new value and bounds from self into field and makes text
111  * non-editable.
112  */

113 protected void endEditing() {
114     // Bug fix: endEditing is called while startEditing is running
115
synchronized(this) {
116     if (changingEditState)
117         return;
118     changingEditState = true;
119     }
120
121     if (typingCommand != null) {
122     getSectionWidget().performCommand(typingCommand);
123     typingCommand = null;
124     }
125
126     changingEditState = false;
127 }
128
129 /**
130  * Perform selection; if becoming deselected, ends editing.
131  *
132  * @param makeSelected new selection state
133  */

134 protected void doSelect(boolean makeSelected) {
135     if (makeSelected == false)
136     endEditing();
137     super.doSelect(makeSelected);
138 }
139
140 /**
141  * If this field is being edited, show text cursor. Else call superclass
142  * method.
143  *
144  * @param e a mouse event
145  */

146 protected void cursorForPosition(MouseEvent JavaDoc e) {
147     JTextPane JavaDoc textPane = (JTextPane JavaDoc)getComponent();
148     if (textPane.isEditable())
149     textPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
150     else
151     super.cursorForPosition(e);
152 }
153
154 /**
155  * Handles return key by expanding height of editor and backspace and
156  * delete by shrinking height if newline is deleted.
157  *
158  * @param e key event
159  */

160 public void keyTyped(KeyEvent JavaDoc e) {
161     char c = e.getKeyChar();
162     int pos;
163     java.awt.Rectangle JavaDoc bounds;
164     JTextPane JavaDoc textPane = (JTextPane JavaDoc)getComponent();
165
166     switch (c) {
167     case KeyEvent.VK_ENTER:
168     // We've just grown. Make sure we still fit.
169
bounds = getComponent().getBounds();
170     bounds.height += lineHeight;
171     getComponent().setBounds(bounds);
172
173     // Make sure we still fit in section
174
sectionWidget.growToFit();
175     break;
176     case KeyEvent.VK_BACK_SPACE:
177     try {
178         pos = textPane.getCaretPosition();
179         if (pos > 0 && textPane.getText(pos - 1, 1).equals("\n")) {
180         bounds = textPane.getBounds();
181         bounds.height -= lineHeight;
182         textPane.setBounds(bounds);
183         }
184     }
185     catch (BadLocationException JavaDoc ex) {}
186     break;
187     case KeyEvent.VK_DELETE:
188     try {
189         pos = textPane.getCaretPosition();
190         if (pos < textPane.getText().length()
191         && textPane.getText(pos, 1).equals("\n"))
192         {
193         bounds = textPane.getBounds();
194         bounds.height -= lineHeight;
195         textPane.setBounds(bounds);
196         }
197     }
198     catch (BadLocationException JavaDoc ex2) {}
199     break;
200     }
201 }
202
203 }
204
Popular Tags