46 |
import java.beans.PropertyChangeEvent; |
import java.beans.PropertyChangeEvent; |
47 |
import java.beans.PropertyChangeListener; |
import java.beans.PropertyChangeListener; |
48 |
|
|
49 |
|
import javax.accessibility.AccessibleContext; |
50 |
import javax.accessibility.AccessibleStateSet; |
import javax.accessibility.AccessibleStateSet; |
|
import javax.swing.text.AttributeSet; |
|
|
import javax.swing.text.BadLocationException; |
|
51 |
import javax.swing.text.Document; |
import javax.swing.text.Document; |
52 |
import javax.swing.text.JTextComponent; |
import javax.swing.text.JTextComponent; |
53 |
import javax.swing.text.PlainDocument; |
import javax.swing.text.PlainDocument; |
68 |
*/ |
*/ |
69 |
protected AccessibleJTextField() |
protected AccessibleJTextField() |
70 |
{ |
{ |
71 |
|
super(); |
72 |
} |
} |
73 |
|
|
74 |
/** |
/** |
75 |
* getAccessibleStateSet |
* Returns the accessible state of this <code>AccessibleJTextField</code>. |
76 |
* @return AccessibleStateSet |
* |
77 |
|
* @return the accessible state of this <code>AccessibleJTextField</code> |
78 |
*/ |
*/ |
79 |
public AccessibleStateSet getAccessibleStateSet() |
public AccessibleStateSet getAccessibleStateSet() |
80 |
{ |
{ |
81 |
return null; |
AccessibleStateSet state = super.getAccessibleStateSet(); |
82 |
|
// TODO: Figure out what state must be added here to the super's state. |
83 |
|
return state; |
84 |
} |
} |
85 |
} |
} |
86 |
|
|
95 |
public static final String notifyAction = "notify-field-accept"; |
public static final String notifyAction = "notify-field-accept"; |
96 |
|
|
97 |
static |
static |
98 |
{ |
{ |
99 |
actions = new Action[1]; |
actions = new Action[1]; |
100 |
actions[0] = new TextAction(notifyAction) |
actions[0] = new TextAction(notifyAction) |
101 |
{ |
{ |
102 |
public void actionPerformed(ActionEvent event) |
public void actionPerformed(ActionEvent event) |
103 |
{ |
{ |
104 |
JTextField textField = (JTextField) event.getSource(); |
JTextField textField = (JTextField) event.getSource(); |
105 |
textField.fireActionPerformed(); |
textField.fireActionPerformed(); |
106 |
} |
} |
107 |
}; |
}; |
108 |
} |
} |
109 |
|
|
110 |
private int columns; |
private int columns; |
111 |
private int align; |
private int align; |
120 |
private PropertyChangeListener actionPropertyChangeListener; |
private PropertyChangeListener actionPropertyChangeListener; |
121 |
|
|
122 |
/** |
/** |
123 |
|
* The horizontal visibility of the textfield. |
124 |
|
*/ |
125 |
|
private BoundedRangeModel horizontalVisibility; |
126 |
|
|
127 |
|
/** |
128 |
* Creates a new instance of <code>JTextField</code>. |
* Creates a new instance of <code>JTextField</code>. |
129 |
*/ |
*/ |
130 |
public JTextField() |
public JTextField() |
180 |
{ |
{ |
181 |
if (columns < 0) |
if (columns < 0) |
182 |
throw new IllegalArgumentException(); |
throw new IllegalArgumentException(); |
183 |
|
|
184 |
this.columns = columns; |
this.columns = columns; |
185 |
|
|
186 |
setDocument(doc == null ? createDefaultModel() : doc); |
setDocument(doc == null ? createDefaultModel() : doc); |
187 |
|
|
188 |
if (text != null) |
if (text != null) |
190 |
|
|
191 |
// default value for alignment |
// default value for alignment |
192 |
align = LEADING; |
align = LEADING; |
193 |
|
|
194 |
|
// Initialize the horizontal visibility model. |
195 |
|
horizontalVisibility = new DefaultBoundedRangeModel(); |
196 |
} |
} |
197 |
|
|
198 |
/** |
/** |
203 |
*/ |
*/ |
204 |
protected Document createDefaultModel() |
protected Document createDefaultModel() |
205 |
{ |
{ |
206 |
// subclassed to swallow newlines |
return new PlainDocument(); |
|
return new PlainDocument() { |
|
|
public void insertString(int offset, String str, AttributeSet a) |
|
|
throws BadLocationException |
|
|
{ |
|
|
if (str != null && str.indexOf('\n') == -1) |
|
|
super.insertString(offset, str, a); |
|
|
} |
|
|
}; |
|
207 |
} |
} |
208 |
|
|
209 |
/** |
/** |
271 |
return columns; |
return columns; |
272 |
} |
} |
273 |
|
|
274 |
|
/** |
275 |
|
* Sets the number of columns and then invalidates the layout. |
276 |
|
* @param columns the number of columns |
277 |
|
* @throws IllegalArgumentException if columns < 0 |
278 |
|
*/ |
279 |
public void setColumns(int columns) |
public void setColumns(int columns) |
280 |
{ |
{ |
281 |
if (columns < 0) |
if (columns < 0) |
283 |
|
|
284 |
this.columns = columns; |
this.columns = columns; |
285 |
invalidate(); |
invalidate(); |
286 |
|
//FIXME: do we need this repaint call? |
287 |
repaint(); |
repaint(); |
288 |
} |
} |
289 |
|
|
290 |
|
/** |
291 |
|
* Returns the horizontal alignment, which is one of: JTextField.LEFT, |
292 |
|
* JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING, |
293 |
|
* JTextField.TRAILING. |
294 |
|
* @return the horizontal alignment |
295 |
|
*/ |
296 |
public int getHorizontalAlignment() |
public int getHorizontalAlignment() |
297 |
{ |
{ |
298 |
return align; |
return align; |
299 |
} |
} |
300 |
|
|
301 |
|
/** |
302 |
|
* Sets the horizontal alignment of the text. Calls invalidate and repaint |
303 |
|
* and fires a property change event. |
304 |
|
* @param newAlign must be one of: JTextField.LEFT, JTextField.CENTER, |
305 |
|
* JTextField.RIGHT, JTextField.LEADING, JTextField.TRAILING. |
306 |
|
* @throws IllegalArgumentException if newAlign is not one of the above. |
307 |
|
*/ |
308 |
public void setHorizontalAlignment(int newAlign) |
public void setHorizontalAlignment(int newAlign) |
309 |
{ |
{ |
310 |
|
//FIXME: should throw an IllegalArgumentException if newAlign is invalid |
311 |
if (align == newAlign) |
if (align == newAlign) |
312 |
return; |
return; |
313 |
|
|
318 |
repaint(); |
repaint(); |
319 |
} |
} |
320 |
|
|
321 |
|
/** |
322 |
|
* Sets the current font and revalidates so the font will take effect. |
323 |
|
*/ |
324 |
public void setFont(Font newFont) |
public void setFont(Font newFont) |
325 |
{ |
{ |
326 |
super.setFont(newFont); |
super.setFont(newFont); |
327 |
revalidate(); |
revalidate(); |
328 |
} |
} |
329 |
|
|
330 |
|
/** |
331 |
|
* Returns the preferred size. If there is a non-zero number of columns, |
332 |
|
* this is the number of columns multiplied by the column width, otherwise |
333 |
|
* it returns super.getPreferredSize(). |
334 |
|
*/ |
335 |
public Dimension getPreferredSize() |
public Dimension getPreferredSize() |
336 |
{ |
{ |
337 |
Dimension size = super.getPreferredSize(); |
Dimension size = super.getPreferredSize(); |
349 |
*/ |
*/ |
350 |
public int getScrollOffset() |
public int getScrollOffset() |
351 |
{ |
{ |
352 |
|
//FIXME: this should return horizontalVisibility's value |
353 |
return scrollOffset; |
return scrollOffset; |
354 |
} |
} |
355 |
|
|
360 |
*/ |
*/ |
361 |
public void setScrollOffset(int offset) |
public void setScrollOffset(int offset) |
362 |
{ |
{ |
363 |
|
//FIXME: this should actualy scroll the field if needed |
364 |
scrollOffset = offset; |
scrollOffset = offset; |
365 |
} |
} |
366 |
|
|
367 |
|
/** |
368 |
|
* Returns the set of Actions that are commands for the editor. |
369 |
|
* This is the actions supported by this editor plus the actions |
370 |
|
* of the UI (returned by JTextComponent.getActions()). |
371 |
|
*/ |
372 |
public Action[] getActions() |
public Action[] getActions() |
373 |
{ |
{ |
374 |
return TextAction.augmentList(super.getActions(), actions); |
return TextAction.augmentList(super.getActions(), actions); |
402 |
|
|
403 |
if (action != null) |
if (action != null) |
404 |
{ |
{ |
405 |
removeActionListener(action); |
removeActionListener(action); |
406 |
action.removePropertyChangeListener(actionPropertyChangeListener); |
action.removePropertyChangeListener(actionPropertyChangeListener); |
407 |
actionPropertyChangeListener = null; |
actionPropertyChangeListener = null; |
408 |
} |
} |
409 |
|
|
410 |
Action oldAction = action; |
Action oldAction = action; |
411 |
action = newAction; |
action = newAction; |
412 |
|
|
413 |
if (action != null) |
if (action != null) |
414 |
{ |
{ |
415 |
addActionListener(action); |
addActionListener(action); |
416 |
actionPropertyChangeListener = |
actionPropertyChangeListener = createActionPropertyChangeListener(action); |
417 |
createActionPropertyChangeListener(action); |
action.addPropertyChangeListener(actionPropertyChangeListener); |
|
action.addPropertyChangeListener(actionPropertyChangeListener); |
|
418 |
} |
} |
419 |
|
|
420 |
|
//FIXME: is this a hack? The horizontal alignment hasn't changed |
421 |
firePropertyChange("horizontalAlignment", oldAction, newAction); |
firePropertyChange("horizontalAlignment", oldAction, newAction); |
422 |
} |
} |
423 |
|
|
424 |
/** |
/** |
425 |
|
* Sets the command string used in action events. |
426 |
* @since 1.3 |
* @since 1.3 |
427 |
*/ |
*/ |
428 |
public void setActionCommand(String command) |
public void setActionCommand(String command) |
436 |
protected PropertyChangeListener createActionPropertyChangeListener(Action action) |
protected PropertyChangeListener createActionPropertyChangeListener(Action action) |
437 |
{ |
{ |
438 |
return new PropertyChangeListener() |
return new PropertyChangeListener() |
439 |
|
{ |
440 |
|
public void propertyChange(PropertyChangeEvent event) |
441 |
{ |
{ |
442 |
public void propertyChange(PropertyChangeEvent event) |
// Update properties "action" and "horizontalAlignment". |
443 |
{ |
String name = event.getPropertyName(); |
444 |
// Update properties "action" and "horizontalAlignment". |
|
445 |
String name = event.getPropertyName(); |
if (name.equals("enabled")) |
446 |
|
{ |
447 |
if (name.equals("enabled")) |
boolean enabled = ((Boolean) event.getNewValue()).booleanValue(); |
448 |
{ |
JTextField.this.setEnabled(enabled); |
449 |
boolean enabled = ((Boolean) event.getNewValue()).booleanValue(); |
} |
450 |
JTextField.this.setEnabled(enabled); |
else if (name.equals(Action.SHORT_DESCRIPTION)) |
451 |
} |
{ |
452 |
else if (name.equals(Action.SHORT_DESCRIPTION)) |
JTextField.this.setToolTipText((String) event.getNewValue()); |
453 |
{ |
} |
454 |
JTextField.this.setToolTipText((String) event.getNewValue()); |
} |
455 |
} |
}; |
|
} |
|
|
}; |
|
456 |
} |
} |
457 |
|
|
458 |
/** |
/** |
459 |
|
* |
460 |
* @since 1.3 |
* @since 1.3 |
461 |
*/ |
*/ |
462 |
protected void configurePropertiesFromAction(Action action) |
protected void configurePropertiesFromAction(Action action) |
463 |
{ |
{ |
464 |
if (action != null) |
if (action != null) |
465 |
{ |
{ |
466 |
setEnabled(action.isEnabled()); |
setEnabled(action.isEnabled()); |
467 |
setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION)); |
468 |
} |
} |
469 |
else |
else |
470 |
{ |
{ |
471 |
setEnabled(true); |
setEnabled(true); |
472 |
setToolTipText(null); |
setToolTipText(null); |
473 |
} |
} |
474 |
} |
} |
475 |
|
|
476 |
|
/** |
477 |
|
* Returns the column width, which is the width of the character m |
478 |
|
* for the font in use. |
479 |
|
* @return the width of the character m for the font in use. |
480 |
|
*/ |
481 |
protected int getColumnWidth() |
protected int getColumnWidth() |
482 |
{ |
{ |
483 |
FontMetrics metrics = getToolkit().getFontMetrics(getFont()); |
FontMetrics metrics = getToolkit().getFontMetrics(getFont()); |
484 |
return metrics.charWidth('m'); |
return metrics.charWidth('m'); |
485 |
} |
} |
486 |
|
|
487 |
|
/** |
488 |
|
* Returns the accessible context associated with the <code>JTextField</code>. |
489 |
|
* |
490 |
|
* @return the accessible context associated with the <code>JTextField</code> |
491 |
|
*/ |
492 |
|
public AccessibleContext getAccessibleContext() |
493 |
|
{ |
494 |
|
if (accessibleContext == null) |
495 |
|
accessibleContext = new AccessibleJTextField(); |
496 |
|
return accessibleContext; |
497 |
|
} |
498 |
|
|
499 |
|
/** |
500 |
|
* Returns the bounded range model that describes the horizontal visibility |
501 |
|
* of the text field in the case when the text does not fit into the |
502 |
|
* available space. The actual values of this model are managed by the look |
503 |
|
* and feel implementation. |
504 |
|
* |
505 |
|
* @return the bounded range model that describes the horizontal visibility |
506 |
|
*/ |
507 |
|
public BoundedRangeModel getHorizontalVisibility() |
508 |
|
{ |
509 |
|
// TODO: The real implementation of this property is still missing. |
510 |
|
// However, this is not done in JTextField but must instead be handled in |
511 |
|
// javax.swing.text.FieldView. |
512 |
|
return horizontalVisibility; |
513 |
|
} |
514 |
} |
} |