1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57: public class JTextField extends JTextComponent
58: implements SwingConstants
59: {
60:
63: protected class AccessibleJTextField extends AccessibleJTextComponent
64: {
65: private static final long serialVersionUID = 8255147276740453036L;
66:
67:
70: protected AccessibleJTextField()
71: {
72: }
73:
74:
78: public AccessibleStateSet getAccessibleStateSet()
79: {
80: return null;
81: }
82: }
83:
84: private static final long serialVersionUID = 353853209832607592L;
85:
86: private static final Action[] actions;
87:
88:
92: public static final String notifyAction = "notify-field-accept";
93:
94: static
95: {
96: actions = new Action[1];
97: actions[0] = new TextAction(notifyAction)
98: {
99: public void actionPerformed(ActionEvent event)
100: {
101: JTextField textField = (JTextField) event.getSource();
102: textField.fireActionPerformed();
103: }
104: };
105: }
106:
107: private int columns;
108: private int align;
109: private int scrollOffset;
110:
111:
112: private Action action;
113:
114:
115: private String actionCommand;
116:
117: private PropertyChangeListener actionPropertyChangeListener;
118:
119:
122: public JTextField()
123: {
124: this(null, null, 0);
125: }
126:
127:
132: public JTextField(String text)
133: {
134: this(null, text, 0);
135: }
136:
137:
144: public JTextField(int columns)
145: {
146: this(null, null, columns);
147: }
148:
149:
157: public JTextField(String text, int columns)
158: {
159: this(null, text, columns);
160: }
161:
162:
171: public JTextField(Document doc, String text, int columns)
172: {
173: if (columns < 0)
174: throw new IllegalArgumentException();
175:
176: this.columns = columns;
177:
178: setDocument(doc == null ? createDefaultModel() : doc);
179:
180: if (text != null)
181: setText(text);
182:
183:
184: align = LEADING;
185: }
186:
187:
193: protected Document createDefaultModel()
194: {
195:
196: return new PlainDocument() {
197: public void insertString(int offset, String str, AttributeSet a)
198: throws BadLocationException
199: {
200: if (str != null && str.indexOf('\n') == -1)
201: super.insertString(offset, str, a);
202: }
203: };
204: }
205:
206:
211: public String getUIClassID()
212: {
213: return "TextFieldUI";
214: }
215:
216:
221: public void addActionListener(ActionListener listener)
222: {
223: listenerList.add(ActionListener.class, listener);
224: }
225:
226:
231: public void removeActionListener(ActionListener listener)
232: {
233: listenerList.remove(ActionListener.class, listener);
234: }
235:
236:
243: public ActionListener[] getActionListeners()
244: {
245: return (ActionListener[]) getListeners(ActionListener.class);
246: }
247:
248:
252: protected void fireActionPerformed()
253: {
254: ActionEvent event = new ActionEvent(this, 0, notifyAction);
255: ActionListener[] listeners = getActionListeners();
256:
257: for (int index = 0; index < listeners.length; ++index)
258: listeners[index].actionPerformed(event);
259: }
260:
261:
266: public int getColumns()
267: {
268: return columns;
269: }
270:
271: public void setColumns(int columns)
272: {
273: if (columns < 0)
274: throw new IllegalArgumentException();
275:
276: this.columns = columns;
277: invalidate();
278: repaint();
279: }
280:
281: public int getHorizontalAlignment()
282: {
283: return align;
284: }
285:
286: public void setHorizontalAlignment(int newAlign)
287: {
288: if (align == newAlign)
289: return;
290:
291: int oldAlign = align;
292: align = newAlign;
293: firePropertyChange("horizontalAlignment", oldAlign, newAlign);
294: invalidate();
295: repaint();
296: }
297:
298: public void setFont(Font newFont)
299: {
300: super.setFont(newFont);
301: revalidate();
302: }
303:
304: public Dimension getPreferredSize()
305: {
306: Dimension size = super.getPreferredSize();
307:
308: if (columns != 0)
309: size.width = columns * getColumnWidth();
310:
311: return size;
312: }
313:
314:
319: public int getScrollOffset()
320: {
321: return scrollOffset;
322: }
323:
324:
329: public void setScrollOffset(int offset)
330: {
331: scrollOffset = offset;
332: }
333:
334: public Action[] getActions()
335: {
336: return TextAction.augmentList(super.getActions(), actions);
337: }
338:
339: public void postActionEvent()
340: {
341: String command = actionCommand != null ? actionCommand : getText();
342: ActionEvent event = new ActionEvent(this, 0, command);
343: ActionListener[] listeners = getActionListeners();
344:
345: for (int index = 0; index < listeners.length; ++index)
346: listeners[index].actionPerformed(event);
347: }
348:
349:
352: public Action getAction()
353: {
354: return action;
355: }
356:
357:
360: public void setAction(Action newAction)
361: {
362: if (action == newAction)
363: return;
364:
365: if (action != null)
366: {
367: removeActionListener(action);
368: action.removePropertyChangeListener(actionPropertyChangeListener);
369: actionPropertyChangeListener = null;
370: }
371:
372: Action oldAction = action;
373: action = newAction;
374:
375: if (action != null)
376: {
377: addActionListener(action);
378: actionPropertyChangeListener =
379: createActionPropertyChangeListener(action);
380: action.addPropertyChangeListener(actionPropertyChangeListener);
381: }
382:
383: firePropertyChange("horizontalAlignment", oldAction, newAction);
384: }
385:
386:
389: public void setActionCommand(String command)
390: {
391: actionCommand = command;
392: }
393:
394:
397: protected PropertyChangeListener createActionPropertyChangeListener(Action action)
398: {
399: return new PropertyChangeListener()
400: {
401: public void propertyChange(PropertyChangeEvent event)
402: {
403:
404: String name = event.getPropertyName();
405:
406: if (name.equals("enabled"))
407: {
408: boolean enabled = ((Boolean) event.getNewValue()).booleanValue();
409: JTextField.this.setEnabled(enabled);
410: }
411: else if (name.equals(Action.SHORT_DESCRIPTION))
412: {
413: JTextField.this.setToolTipText((String) event.getNewValue());
414: }
415: }
416: };
417: }
418:
419:
422: protected void configurePropertiesFromAction(Action action)
423: {
424: if (action != null)
425: {
426: setEnabled(action.isEnabled());
427: setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
428: }
429: else
430: {
431: setEnabled(true);
432: setToolTipText(null);
433: }
434: }
435:
436: protected int getColumnWidth()
437: {
438: FontMetrics metrics = getToolkit().getFontMetrics(getFont());
439: return metrics.charWidth('m');
440: }
441: }