Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / JTextField.java
blob409e2a5970f73a7abab129a25454203bc35b1b47
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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.AccessibleStateSet;
50 import javax.swing.text.Document;
51 import javax.swing.text.JTextComponent;
52 import javax.swing.text.PlainDocument;
53 import javax.swing.text.TextAction;
55 public class JTextField extends JTextComponent
56 implements SwingConstants
58 /**
59 * AccessibleJTextField
61 protected class AccessibleJTextField extends AccessibleJTextComponent
63 private static final long serialVersionUID = 8255147276740453036L;
65 /**
66 * Constructor AccessibleJTextField
68 protected AccessibleJTextField()
72 /**
73 * getAccessibleStateSet
74 * @return AccessibleStateSet
76 public AccessibleStateSet getAccessibleStateSet()
78 return null;
82 private static final long serialVersionUID = 353853209832607592L;
84 private static final Action[] actions;
86 /**
87 * Name of the action that gets sent when the content of the text field
88 * gets accepted.
90 public static final String notifyAction = "notify-field-accept";
92 static
94 actions = new Action[1];
95 actions[0] = new TextAction(notifyAction)
97 public void actionPerformed(ActionEvent event)
99 JTextField textField = (JTextField) event.getSource();
100 textField.fireActionPerformed();
105 private int columns;
106 private int align;
107 private int scrollOffset;
109 /** @since 1.3 */
110 private Action action;
112 /** @since 1.3 */
113 private String actionCommand;
115 private PropertyChangeListener actionPropertyChangeListener;
118 * Creates a new instance of <code>JTextField</code>.
120 public JTextField()
122 this(null, null, 0);
126 * Creates a new instance of <code>JTextField</code>.
128 * @param text the initial text
130 public JTextField(String text)
132 this(null, text, 0);
136 * Creates a new instance of <code>JTextField</code>.
138 * @param columns the number of columns
140 * @exception IllegalArgumentException if columns %lt; 0
142 public JTextField(int columns)
144 this(null, null, columns);
148 * Creates a new instance of <code>JTextField</code>.
150 * @param text the initial text
151 * @param columns the number of columns
153 * @exception IllegalArgumentException if columns %lt; 0
155 public JTextField(String text, int columns)
157 this(null, text, columns);
161 * Creates a new instance of <code>JTextField</code>.
163 * @param doc the document to use
164 * @param text the initial text
165 * @param columns the number of columns
167 * @exception IllegalArgumentException if columns %lt; 0
169 public JTextField(Document doc, String text, int columns)
171 if (columns < 0)
172 throw new IllegalArgumentException();
174 this.columns = columns;
176 setDocument(doc == null ? createDefaultModel() : doc);
178 if (text != null)
179 setText(text);
183 * Creates the default model for this text field.
184 * This implementation returns an instance of <code>PlainDocument</code>.
186 * @return a new instance of the default model
188 protected Document createDefaultModel()
190 return new PlainDocument();
194 * Returns the class ID for the UI.
196 * @return "TextFieldUI";
198 public String getUIClassID()
200 return "TextFieldUI";
204 * Adds a new listener object to this text field.
206 * @param listener the listener to add
208 public void addActionListener(ActionListener listener)
210 listenerList.add(ActionListener.class, listener);
214 * Removes a listener object from this text field.
216 * @param listener the listener to remove
218 public void removeActionListener(ActionListener listener)
220 listenerList.remove(ActionListener.class, listener);
224 * Returns all registered <code>ActionListener</code> objects.
226 * @return an array of listeners
228 * @since 1.4
230 public ActionListener[] getActionListeners()
232 return (ActionListener[]) getListeners(ActionListener.class);
236 * Sends an action event to all registered
237 * <code>ActionListener</code> objects.
239 protected void fireActionPerformed()
241 ActionEvent event = new ActionEvent(this, 0, notifyAction);
242 ActionListener[] listeners = getActionListeners();
244 for (int index = 0; index < listeners.length; ++index)
245 listeners[index].actionPerformed(event);
249 * Returns the number of columns of this text field.
251 * @return the number of columns
253 public int getColumns()
255 return columns;
258 public void setColumns(int columns)
260 if (columns < 0)
261 throw new IllegalArgumentException();
263 this.columns = columns;
264 invalidate();
265 repaint();
268 public int getHorizontalAlignment()
270 return align;
273 public void setHorizontalAlignment(int newAlign)
275 if (align == newAlign)
276 return;
278 int oldAlign = align;
279 align = newAlign;
280 firePropertyChange("horizontalAlignment", oldAlign, newAlign);
281 invalidate();
282 repaint();
285 public void setFont(Font newFont)
287 super.setFont(newFont);
288 revalidate();
291 public Dimension getPreferredSize()
293 Dimension size = super.getPreferredSize();
295 if (columns != 0)
296 size.width = columns * getColumnWidth();
298 return size;
302 * Returns the scroll offset in pixels.
304 * @return the scroll offset
306 public int getScrollOffset()
308 return scrollOffset;
312 * Sets the scroll offset in pixels.
314 * @param offset the scroll offset
316 public void setScrollOffset(int offset)
318 scrollOffset = offset;
321 public Action[] getActions()
323 return TextAction.augmentList(super.getActions(), actions);
326 public void postActionEvent()
328 String command = actionCommand != null ? actionCommand : getText();
329 ActionEvent event = new ActionEvent(this, 0, command);
330 ActionListener[] listeners = getActionListeners();
332 for (int index = 0; index < listeners.length; ++index)
333 listeners[index].actionPerformed(event);
337 * @since 1.3
339 public Action getAction()
341 return action;
345 * @since 1.3
347 public void setAction(Action newAction)
349 if (action == newAction)
350 return;
352 if (action != null)
354 removeActionListener(action);
355 action.removePropertyChangeListener(actionPropertyChangeListener);
356 actionPropertyChangeListener = null;
359 Action oldAction = action;
360 action = newAction;
362 if (action != null)
364 addActionListener(action);
365 actionPropertyChangeListener =
366 createActionPropertyChangeListener(action);
367 action.addPropertyChangeListener(actionPropertyChangeListener);
370 firePropertyChange("horizontalAlignment", oldAction, newAction);
374 * @since 1.3
376 public void setActionCommand(String command)
378 actionCommand = command;
382 * @since 1.3
384 protected PropertyChangeListener createActionPropertyChangeListener(Action action)
386 return new PropertyChangeListener()
388 public void propertyChange(PropertyChangeEvent event)
390 // Update properties "action" and "horizontalAlignment".
391 String name = event.getPropertyName();
393 if (name.equals("enabled"))
395 boolean enabled = ((Boolean) event.getNewValue()).booleanValue();
396 JTextField.this.setEnabled(enabled);
398 else if (name.equals(Action.SHORT_DESCRIPTION))
400 JTextField.this.setToolTipText((String) event.getNewValue());
407 * @since 1.3
409 protected void configurePropertiesFromAction(Action action)
411 if (action != null)
413 setEnabled(action.isEnabled());
414 setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
416 else
418 setEnabled(true);
419 setToolTipText(null);
423 protected int getColumnWidth()
425 FontMetrics metrics = getToolkit().getFontMetrics(getFont());
426 return metrics.charWidth('m');