Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / JTextField.java
blob01c5c06a3509f1d14d86498abbac13147cf94478
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.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
60 /**
61 * AccessibleJTextField
63 protected class AccessibleJTextField extends AccessibleJTextComponent
65 private static final long serialVersionUID = 8255147276740453036L;
67 /**
68 * Constructor AccessibleJTextField
70 protected AccessibleJTextField()
72 super();
75 /**
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.
84 return state;
88 private static final long serialVersionUID = 353853209832607592L;
90 private static final Action[] actions;
92 /**
93 * Name of the action that gets sent when the content of the text field
94 * gets accepted.
96 public static final String notifyAction = "notify-field-accept";
98 static
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();
111 private int columns;
112 private int align;
113 private int scrollOffset;
115 /** @since 1.3 */
116 private Action action;
118 /** @since 1.3 */
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>.
131 public JTextField()
133 this(null, null, 0);
137 * Creates a new instance of <code>JTextField</code>.
139 * @param text the initial text
141 public JTextField(String text)
143 this(null, text, 0);
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)
182 if (columns < 0)
183 throw new IllegalArgumentException();
185 this.columns = columns;
187 setDocument(doc == null ? createDefaultModel() : doc);
189 if (text != null)
190 setText(text);
192 // default value for alignment
193 align = LEADING;
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
260 * @since 1.4
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()
287 return columns;
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)
297 if (columns < 0)
298 throw new IllegalArgumentException();
300 this.columns = columns;
301 invalidate();
302 //FIXME: do we need this repaint call?
303 repaint();
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()
314 return align;
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)
328 return;
330 int oldAlign = align;
331 align = newAlign;
332 firePropertyChange("horizontalAlignment", oldAlign, newAlign);
333 invalidate();
334 repaint();
338 * Sets the current font and revalidates so the font will take effect.
340 public void setFont(Font newFont)
342 super.setFont(newFont);
343 revalidate();
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();
355 if (columns != 0)
357 Insets i = getInsets();
358 size.width = columns * getColumnWidth() + i.left + i.right;
361 return size;
365 * Returns the scroll offset in pixels.
367 * @return the scroll offset
369 public int getScrollOffset()
371 //FIXME: this should return horizontalVisibility's value
372 return scrollOffset;
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);
407 * @since 1.3
409 public Action getAction()
411 return action;
415 * @since 1.3
417 public void setAction(Action newAction)
419 if (action == newAction)
420 return;
422 if (action != null)
424 removeActionListener(action);
425 action.removePropertyChangeListener(actionPropertyChangeListener);
426 actionPropertyChangeListener = null;
429 Action oldAction = action;
430 action = newAction;
432 if (action != null)
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.
445 * @since 1.3
447 public void setActionCommand(String command)
449 actionCommand = command;
453 * @since 1.3
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());
479 * @since 1.3
481 protected void configurePropertiesFromAction(Action action)
483 if (action != null)
485 setEnabled(action.isEnabled());
486 setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
488 else
490 setEnabled(true);
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
537 * validating.
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);