2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.awt
.Dimension
;
43 import java
.awt
.FontMetrics
;
44 import java
.awt
.Insets
;
45 import java
.awt
.event
.ActionEvent
;
46 import java
.awt
.event
.ActionListener
;
47 import java
.beans
.PropertyChangeEvent
;
48 import java
.beans
.PropertyChangeListener
;
50 import javax
.accessibility
.AccessibleContext
;
51 import javax
.accessibility
.AccessibleStateSet
;
52 import javax
.swing
.text
.Document
;
53 import javax
.swing
.text
.JTextComponent
;
54 import javax
.swing
.text
.PlainDocument
;
55 import javax
.swing
.text
.TextAction
;
57 public class JTextField
extends JTextComponent
58 implements SwingConstants
61 * AccessibleJTextField
63 protected class AccessibleJTextField
extends AccessibleJTextComponent
65 private static final long serialVersionUID
= 8255147276740453036L;
68 * Constructor AccessibleJTextField
70 protected AccessibleJTextField()
76 * Returns the accessible state of this <code>AccessibleJTextField</code>.
78 * @return the accessible state of this <code>AccessibleJTextField</code>
80 public AccessibleStateSet
getAccessibleStateSet()
82 AccessibleStateSet state
= super.getAccessibleStateSet();
83 // TODO: Figure out what state must be added here to the super's state.
88 private static final long serialVersionUID
= 353853209832607592L;
90 private static final Action
[] actions
;
93 * Name of the action that gets sent when the content of the text field
96 public static final String notifyAction
= "notify-field-accept";
100 actions
= new Action
[1];
101 actions
[0] = new TextAction(notifyAction
)
103 public void actionPerformed(ActionEvent event
)
105 JTextField textField
= (JTextField
) event
.getSource();
106 textField
.fireActionPerformed();
113 private int scrollOffset
;
116 private Action action
;
119 private String actionCommand
;
121 private PropertyChangeListener actionPropertyChangeListener
;
124 * The horizontal visibility of the textfield.
126 private BoundedRangeModel horizontalVisibility
;
129 * Creates a new instance of <code>JTextField</code>.
137 * Creates a new instance of <code>JTextField</code>.
139 * @param text the initial text
141 public JTextField(String text
)
147 * Creates a new instance of <code>JTextField</code>.
149 * @param columns the number of columns
151 * @exception IllegalArgumentException if columns %lt; 0
153 public JTextField(int columns
)
155 this(null, null, columns
);
159 * Creates a new instance of <code>JTextField</code>.
161 * @param text the initial text
162 * @param columns the number of columns
164 * @exception IllegalArgumentException if columns %lt; 0
166 public JTextField(String text
, int columns
)
168 this(null, text
, columns
);
172 * Creates a new instance of <code>JTextField</code>.
174 * @param doc the document to use
175 * @param text the initial text
176 * @param columns the number of columns
178 * @exception IllegalArgumentException if columns %lt; 0
180 public JTextField(Document doc
, String text
, int columns
)
183 throw new IllegalArgumentException();
185 this.columns
= columns
;
187 setDocument(doc
== null ?
createDefaultModel() : doc
);
192 // default value for alignment
195 // Initialize the horizontal visibility model.
196 horizontalVisibility
= new DefaultBoundedRangeModel();
200 * Creates the default model for this text field.
201 * This implementation returns an instance of <code>PlainDocument</code>.
203 * @return a new instance of the default model
205 protected Document
createDefaultModel()
207 return new PlainDocument();
211 * Sets the document to be used for this JTextField.
213 * This sets the document property <code>filterNewlines</code> to
214 * <code>true</code> and then calls the super behaviour to setup a view and
215 * revalidate the text field.
217 * @param doc the document to set
219 public void setDocument(Document doc
)
221 doc
.putProperty("filterNewlines", Boolean
.TRUE
);
222 super.setDocument(doc
);
226 * Returns the class ID for the UI.
228 * @return "TextFieldUI";
230 public String
getUIClassID()
232 return "TextFieldUI";
236 * Adds a new listener object to this text field.
238 * @param listener the listener to add
240 public void addActionListener(ActionListener listener
)
242 listenerList
.add(ActionListener
.class, listener
);
246 * Removes a listener object from this text field.
248 * @param listener the listener to remove
250 public void removeActionListener(ActionListener listener
)
252 listenerList
.remove(ActionListener
.class, listener
);
256 * Returns all registered <code>ActionListener</code> objects.
258 * @return an array of listeners
262 public ActionListener
[] getActionListeners()
264 return (ActionListener
[]) getListeners(ActionListener
.class);
268 * Sends an action event to all registered
269 * <code>ActionListener</code> objects.
271 protected void fireActionPerformed()
273 ActionEvent event
= new ActionEvent(this, 0, notifyAction
);
274 ActionListener
[] listeners
= getActionListeners();
276 for (int index
= 0; index
< listeners
.length
; ++index
)
277 listeners
[index
].actionPerformed(event
);
281 * Returns the number of columns of this text field.
283 * @return the number of columns
285 public int getColumns()
291 * Sets the number of columns and then invalidates the layout.
292 * @param columns the number of columns
293 * @throws IllegalArgumentException if columns < 0
295 public void setColumns(int columns
)
298 throw new IllegalArgumentException();
300 this.columns
= columns
;
302 //FIXME: do we need this repaint call?
307 * Returns the horizontal alignment, which is one of: JTextField.LEFT,
308 * JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING,
309 * JTextField.TRAILING.
310 * @return the horizontal alignment
312 public int getHorizontalAlignment()
318 * Sets the horizontal alignment of the text. Calls invalidate and repaint
319 * and fires a property change event.
320 * @param newAlign must be one of: JTextField.LEFT, JTextField.CENTER,
321 * JTextField.RIGHT, JTextField.LEADING, JTextField.TRAILING.
322 * @throws IllegalArgumentException if newAlign is not one of the above.
324 public void setHorizontalAlignment(int newAlign
)
326 //FIXME: should throw an IllegalArgumentException if newAlign is invalid
327 if (align
== newAlign
)
330 int oldAlign
= align
;
332 firePropertyChange("horizontalAlignment", oldAlign
, newAlign
);
338 * Sets the current font and revalidates so the font will take effect.
340 public void setFont(Font newFont
)
342 super.setFont(newFont
);
347 * Returns the preferred size. If there is a non-zero number of columns,
348 * this is the number of columns multiplied by the column width, otherwise
349 * it returns super.getPreferredSize().
351 public Dimension
getPreferredSize()
353 Dimension size
= super.getPreferredSize();
357 Insets i
= getInsets();
358 size
.width
= columns
* getColumnWidth() + i
.left
+ i
.right
;
365 * Returns the scroll offset in pixels.
367 * @return the scroll offset
369 public int getScrollOffset()
371 //FIXME: this should return horizontalVisibility's value
376 * Sets the scroll offset in pixels.
378 * @param offset the scroll offset
380 public void setScrollOffset(int offset
)
382 //FIXME: this should actualy scroll the field if needed
383 scrollOffset
= offset
;
387 * Returns the set of Actions that are commands for the editor.
388 * This is the actions supported by this editor plus the actions
389 * of the UI (returned by JTextComponent.getActions()).
391 public Action
[] getActions()
393 return TextAction
.augmentList(super.getActions(), actions
);
396 public void postActionEvent()
398 String command
= actionCommand
!= null ? actionCommand
: getText();
399 ActionEvent event
= new ActionEvent(this, 0, command
);
400 ActionListener
[] listeners
= getActionListeners();
402 for (int index
= 0; index
< listeners
.length
; ++index
)
403 listeners
[index
].actionPerformed(event
);
409 public Action
getAction()
417 public void setAction(Action newAction
)
419 if (action
== newAction
)
424 removeActionListener(action
);
425 action
.removePropertyChangeListener(actionPropertyChangeListener
);
426 actionPropertyChangeListener
= null;
429 Action oldAction
= action
;
434 addActionListener(action
);
435 actionPropertyChangeListener
= createActionPropertyChangeListener(action
);
436 action
.addPropertyChangeListener(actionPropertyChangeListener
);
439 //FIXME: is this a hack? The horizontal alignment hasn't changed
440 firePropertyChange("horizontalAlignment", oldAction
, newAction
);
444 * Sets the command string used in action events.
447 public void setActionCommand(String command
)
449 actionCommand
= command
;
455 protected PropertyChangeListener
createActionPropertyChangeListener(Action action
)
457 return new PropertyChangeListener()
459 public void propertyChange(PropertyChangeEvent event
)
461 // Update properties "action" and "horizontalAlignment".
462 String name
= event
.getPropertyName();
464 if (name
.equals("enabled"))
466 boolean enabled
= ((Boolean
) event
.getNewValue()).booleanValue();
467 JTextField
.this.setEnabled(enabled
);
469 else if (name
.equals(Action
.SHORT_DESCRIPTION
))
471 JTextField
.this.setToolTipText((String
) event
.getNewValue());
481 protected void configurePropertiesFromAction(Action action
)
485 setEnabled(action
.isEnabled());
486 setToolTipText((String
) action
.getValue(Action
.SHORT_DESCRIPTION
));
491 setToolTipText(null);
496 * Returns the column width, which is the width of the character m
497 * for the font in use.
498 * @return the width of the character m for the font in use.
500 protected int getColumnWidth()
502 FontMetrics metrics
= getToolkit().getFontMetrics(getFont());
503 return metrics
.charWidth('m');
507 * Returns the accessible context associated with the <code>JTextField</code>.
509 * @return the accessible context associated with the <code>JTextField</code>
511 public AccessibleContext
getAccessibleContext()
513 if (accessibleContext
== null)
514 accessibleContext
= new AccessibleJTextField();
515 return accessibleContext
;
519 * Returns the bounded range model that describes the horizontal visibility
520 * of the text field in the case when the text does not fit into the
521 * available space. The actual values of this model are managed by the look
522 * and feel implementation.
524 * @return the bounded range model that describes the horizontal visibility
526 public BoundedRangeModel
getHorizontalVisibility()
528 // TODO: The real implementation of this property is still missing.
529 // However, this is not done in JTextField but must instead be handled in
530 // javax.swing.text.FieldView.
531 return horizontalVisibility
;
535 * Returns <code>true</code>, unless this is embedded in a
536 * <code>JViewport</code> in which case the viewport takes responsibility of
539 * @return <code>true</code>, unless this is embedded in a
540 * <code>JViewport</code> in which case the viewport takes
541 * responsibility of validating
543 public boolean isValidateRoot()
545 return ! (getParent() instanceof JViewport
);