Merge from mainline
[official-gcc.git] / libjava / classpath / javax / swing / JTextField.java
blobc4903106131eafa56e1e201a6fc11f405b4796aa
1 /* JTextField.java --
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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. */
39 package javax.swing;
41 import java.awt.Dimension;
42 import java.awt.Font;
43 import java.awt.FontMetrics;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.beans.PropertyChangeEvent;
47 import java.beans.PropertyChangeListener;
49 import javax.accessibility.AccessibleContext;
50 import javax.accessibility.AccessibleStateSet;
51 import javax.swing.text.Document;
52 import javax.swing.text.JTextComponent;
53 import javax.swing.text.PlainDocument;
54 import javax.swing.text.TextAction;
56 public class JTextField extends JTextComponent
57 implements SwingConstants
59 /**
60 * AccessibleJTextField
62 protected class AccessibleJTextField extends AccessibleJTextComponent
64 private static final long serialVersionUID = 8255147276740453036L;
66 /**
67 * Constructor AccessibleJTextField
69 protected AccessibleJTextField()
71 super();
74 /**
75 * Returns the accessible state of this <code>AccessibleJTextField</code>.
77 * @return the accessible state of this <code>AccessibleJTextField</code>
79 public AccessibleStateSet getAccessibleStateSet()
81 AccessibleStateSet state = super.getAccessibleStateSet();
82 // TODO: Figure out what state must be added here to the super's state.
83 return state;
87 private static final long serialVersionUID = 353853209832607592L;
89 private static final Action[] actions;
91 /**
92 * Name of the action that gets sent when the content of the text field
93 * gets accepted.
95 public static final String notifyAction = "notify-field-accept";
97 static
99 actions = new Action[1];
100 actions[0] = new TextAction(notifyAction)
102 public void actionPerformed(ActionEvent event)
104 JTextField textField = (JTextField) event.getSource();
105 textField.fireActionPerformed();
110 private int columns;
111 private int align;
112 private int scrollOffset;
114 /** @since 1.3 */
115 private Action action;
117 /** @since 1.3 */
118 private String actionCommand;
120 private PropertyChangeListener actionPropertyChangeListener;
123 * The horizontal visibility of the textfield.
125 private BoundedRangeModel horizontalVisibility;
128 * Creates a new instance of <code>JTextField</code>.
130 public JTextField()
132 this(null, null, 0);
136 * Creates a new instance of <code>JTextField</code>.
138 * @param text the initial text
140 public JTextField(String text)
142 this(null, text, 0);
146 * Creates a new instance of <code>JTextField</code>.
148 * @param columns the number of columns
150 * @exception IllegalArgumentException if columns %lt; 0
152 public JTextField(int columns)
154 this(null, null, columns);
158 * Creates a new instance of <code>JTextField</code>.
160 * @param text the initial text
161 * @param columns the number of columns
163 * @exception IllegalArgumentException if columns %lt; 0
165 public JTextField(String text, int columns)
167 this(null, text, columns);
171 * Creates a new instance of <code>JTextField</code>.
173 * @param doc the document to use
174 * @param text the initial text
175 * @param columns the number of columns
177 * @exception IllegalArgumentException if columns %lt; 0
179 public JTextField(Document doc, String text, int columns)
181 if (columns < 0)
182 throw new IllegalArgumentException();
184 this.columns = columns;
186 setDocument(doc == null ? createDefaultModel() : doc);
188 if (text != null)
189 setText(text);
191 // default value for alignment
192 align = LEADING;
194 // Initialize the horizontal visibility model.
195 horizontalVisibility = new DefaultBoundedRangeModel();
199 * Creates the default model for this text field.
200 * This implementation returns an instance of <code>PlainDocument</code>.
202 * @return a new instance of the default model
204 protected Document createDefaultModel()
206 return new PlainDocument();
210 * Sets the document to be used for this JTextField.
212 * This sets the document property <code>filterNewlines</code> to
213 * <code>true</code> and then calls the super behaviour to setup a view and
214 * revalidate the text field.
216 * @param doc the document to set
218 public void setDocument(Document doc)
220 doc.putProperty("filterNewlines", Boolean.TRUE);
221 super.setDocument(doc);
225 * Returns the class ID for the UI.
227 * @return "TextFieldUI";
229 public String getUIClassID()
231 return "TextFieldUI";
235 * Adds a new listener object to this text field.
237 * @param listener the listener to add
239 public void addActionListener(ActionListener listener)
241 listenerList.add(ActionListener.class, listener);
245 * Removes a listener object from this text field.
247 * @param listener the listener to remove
249 public void removeActionListener(ActionListener listener)
251 listenerList.remove(ActionListener.class, listener);
255 * Returns all registered <code>ActionListener</code> objects.
257 * @return an array of listeners
259 * @since 1.4
261 public ActionListener[] getActionListeners()
263 return (ActionListener[]) getListeners(ActionListener.class);
267 * Sends an action event to all registered
268 * <code>ActionListener</code> objects.
270 protected void fireActionPerformed()
272 ActionEvent event = new ActionEvent(this, 0, notifyAction);
273 ActionListener[] listeners = getActionListeners();
275 for (int index = 0; index < listeners.length; ++index)
276 listeners[index].actionPerformed(event);
280 * Returns the number of columns of this text field.
282 * @return the number of columns
284 public int getColumns()
286 return columns;
290 * Sets the number of columns and then invalidates the layout.
291 * @param columns the number of columns
292 * @throws IllegalArgumentException if columns < 0
294 public void setColumns(int columns)
296 if (columns < 0)
297 throw new IllegalArgumentException();
299 this.columns = columns;
300 invalidate();
301 //FIXME: do we need this repaint call?
302 repaint();
306 * Returns the horizontal alignment, which is one of: JTextField.LEFT,
307 * JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING,
308 * JTextField.TRAILING.
309 * @return the horizontal alignment
311 public int getHorizontalAlignment()
313 return align;
317 * Sets the horizontal alignment of the text. Calls invalidate and repaint
318 * and fires a property change event.
319 * @param newAlign must be one of: JTextField.LEFT, JTextField.CENTER,
320 * JTextField.RIGHT, JTextField.LEADING, JTextField.TRAILING.
321 * @throws IllegalArgumentException if newAlign is not one of the above.
323 public void setHorizontalAlignment(int newAlign)
325 //FIXME: should throw an IllegalArgumentException if newAlign is invalid
326 if (align == newAlign)
327 return;
329 int oldAlign = align;
330 align = newAlign;
331 firePropertyChange("horizontalAlignment", oldAlign, newAlign);
332 invalidate();
333 repaint();
337 * Sets the current font and revalidates so the font will take effect.
339 public void setFont(Font newFont)
341 super.setFont(newFont);
342 revalidate();
346 * Returns the preferred size. If there is a non-zero number of columns,
347 * this is the number of columns multiplied by the column width, otherwise
348 * it returns super.getPreferredSize().
350 public Dimension getPreferredSize()
352 Dimension size = super.getPreferredSize();
354 if (columns != 0)
355 size.width = columns * getColumnWidth();
357 return size;
361 * Returns the scroll offset in pixels.
363 * @return the scroll offset
365 public int getScrollOffset()
367 //FIXME: this should return horizontalVisibility's value
368 return scrollOffset;
372 * Sets the scroll offset in pixels.
374 * @param offset the scroll offset
376 public void setScrollOffset(int offset)
378 //FIXME: this should actualy scroll the field if needed
379 scrollOffset = offset;
383 * Returns the set of Actions that are commands for the editor.
384 * This is the actions supported by this editor plus the actions
385 * of the UI (returned by JTextComponent.getActions()).
387 public Action[] getActions()
389 return TextAction.augmentList(super.getActions(), actions);
392 public void postActionEvent()
394 String command = actionCommand != null ? actionCommand : getText();
395 ActionEvent event = new ActionEvent(this, 0, command);
396 ActionListener[] listeners = getActionListeners();
398 for (int index = 0; index < listeners.length; ++index)
399 listeners[index].actionPerformed(event);
403 * @since 1.3
405 public Action getAction()
407 return action;
411 * @since 1.3
413 public void setAction(Action newAction)
415 if (action == newAction)
416 return;
418 if (action != null)
420 removeActionListener(action);
421 action.removePropertyChangeListener(actionPropertyChangeListener);
422 actionPropertyChangeListener = null;
425 Action oldAction = action;
426 action = newAction;
428 if (action != null)
430 addActionListener(action);
431 actionPropertyChangeListener = createActionPropertyChangeListener(action);
432 action.addPropertyChangeListener(actionPropertyChangeListener);
435 //FIXME: is this a hack? The horizontal alignment hasn't changed
436 firePropertyChange("horizontalAlignment", oldAction, newAction);
440 * Sets the command string used in action events.
441 * @since 1.3
443 public void setActionCommand(String command)
445 actionCommand = command;
449 * @since 1.3
451 protected PropertyChangeListener createActionPropertyChangeListener(Action action)
453 return new PropertyChangeListener()
455 public void propertyChange(PropertyChangeEvent event)
457 // Update properties "action" and "horizontalAlignment".
458 String name = event.getPropertyName();
460 if (name.equals("enabled"))
462 boolean enabled = ((Boolean) event.getNewValue()).booleanValue();
463 JTextField.this.setEnabled(enabled);
465 else if (name.equals(Action.SHORT_DESCRIPTION))
467 JTextField.this.setToolTipText((String) event.getNewValue());
475 * @since 1.3
477 protected void configurePropertiesFromAction(Action action)
479 if (action != null)
481 setEnabled(action.isEnabled());
482 setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
484 else
486 setEnabled(true);
487 setToolTipText(null);
492 * Returns the column width, which is the width of the character m
493 * for the font in use.
494 * @return the width of the character m for the font in use.
496 protected int getColumnWidth()
498 FontMetrics metrics = getToolkit().getFontMetrics(getFont());
499 return metrics.charWidth('m');
503 * Returns the accessible context associated with the <code>JTextField</code>.
505 * @return the accessible context associated with the <code>JTextField</code>
507 public AccessibleContext getAccessibleContext()
509 if (accessibleContext == null)
510 accessibleContext = new AccessibleJTextField();
511 return accessibleContext;
515 * Returns the bounded range model that describes the horizontal visibility
516 * of the text field in the case when the text does not fit into the
517 * available space. The actual values of this model are managed by the look
518 * and feel implementation.
520 * @return the bounded range model that describes the horizontal visibility
522 public BoundedRangeModel getHorizontalVisibility()
524 // TODO: The real implementation of this property is still missing.
525 // However, this is not done in JTextField but must instead be handled in
526 // javax.swing.text.FieldView.
527 return horizontalVisibility;