This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libjava / javax / swing / JTextField.java
blob5fe104b50932d2b57b275052e9443b1fa46fb8df
1 /* JTextField.java --
2 Copyright (C) 2002, 2004 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. */
38 package javax.swing;
40 import java.awt.Dimension;
41 import java.awt.Font;
42 import java.awt.FontMetrics;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.beans.PropertyChangeEvent;
46 import java.beans.PropertyChangeListener;
48 import javax.accessibility.AccessibleStateSet;
49 import javax.swing.text.Document;
50 import javax.swing.text.JTextComponent;
51 import javax.swing.text.PlainDocument;
54 public class JTextField extends JTextComponent
55 implements SwingConstants
57 /**
58 * AccessibleJTextField
60 protected class AccessibleJTextField extends AccessibleJTextComponent
62 private static final long serialVersionUID = 8255147276740453036L;
64 /**
65 * Constructor AccessibleJTextField
67 protected AccessibleJTextField()
71 /**
72 * getAccessibleStateSet
73 * @return AccessibleStateSet
75 public AccessibleStateSet getAccessibleStateSet()
77 return null;
81 private static final long serialVersionUID = 353853209832607592L;
83 public static final String notifyAction = "notify-field-accept";
85 private int columns;
87 private int align;
89 private int scrollOffset;
91 /** @since 1.3 */
92 private Action action;
94 /** @since 1.3 */
95 private String actionCommand;
97 private PropertyChangeListener actionPropertyChangeListener;
99 /**
100 * Creates a new instance of <code>JTextField</code>.
102 public JTextField()
104 this(null, null, 0);
108 * Creates a new instance of <code>JTextField</code>.
110 * @param text the initial text
112 public JTextField(String text)
114 this(null, text, 0);
118 * Creates a new instance of <code>JTextField</code>.
120 * @param columns the number of columns
122 * @exception IllegalArgumentException if columns %lt; 0
124 public JTextField(int columns)
126 this(null, null, columns);
130 * Creates a new instance of <code>JTextField</code>.
132 * @param text the initial text
133 * @param columns the number of columns
135 * @exception IllegalArgumentException if columns %lt; 0
137 public JTextField(String text, int columns)
139 this(null, text, columns);
143 * Creates a new instance of <code>JTextField</code>.
145 * @param doc the document to use
146 * @param text the initial text
147 * @param columns the number of columns
149 * @exception IllegalArgumentException if columns %lt; 0
151 public JTextField(Document doc, String text, int columns)
153 if (columns < 0)
154 throw new IllegalArgumentException();
156 this.columns = columns;
158 setDocument(doc == null ? createDefaultModel() : doc);
160 if (text != null)
161 setText(text);
165 * Creates the default model for this text field.
166 * This implementation returns an instance of <code>PlainDocument</code>.
168 * @return a new instance of the default model
170 protected Document createDefaultModel()
172 return new PlainDocument();
176 * Returns the class ID for the UI.
178 * @return "TextFieldUI";
180 public String getUIClassID()
182 return "TextFieldUI";
186 * Adds a new listener object to this text field.
188 * @param listener the listener to add
190 public void addActionListener(ActionListener listener)
192 listenerList.add(ActionListener.class, listener);
196 * Removes a listener object from this text field.
198 * @param listener the listener to remove
200 public void removeActionListener(ActionListener listener)
202 listenerList.remove(ActionListener.class, listener);
206 * Returns all registered <code>ActionListener</code> objects.
208 * @return an array of listeners
210 * @since 1.4
212 public ActionListener[] getActionListeners()
214 return (ActionListener[]) getListeners(ActionListener.class);
218 * Sends an action event to all registered
219 * <code>ActionListener</code> objects.
221 protected void fireActionPerformed()
223 ActionEvent event = new ActionEvent(this, 0, notifyAction);
224 ActionListener[] listeners = getActionListeners();
226 for (int index = 0; index < listeners.length; ++index)
227 listeners[index].actionPerformed(event);
231 * Returns the number of columns of this text field.
233 * @return the number of columns
235 public int getColumns()
237 return columns;
240 public void setColumns(int columns)
242 if (columns < 0)
243 throw new IllegalArgumentException();
245 this.columns = columns;
246 invalidate();
247 repaint();
250 public int getHorizontalAlignment()
252 return align;
255 public void setHorizontalAlignment(int newAlign)
257 if (align == newAlign)
258 return;
260 int oldAlign = align;
261 align = newAlign;
262 firePropertyChange("horizontalAlignment", oldAlign, newAlign);
263 invalidate();
264 repaint();
267 public void setFont(Font newFont)
269 super.setFont(newFont);
270 revalidate();
273 public Dimension getPreferredSize()
275 Dimension size;
276 FontMetrics fm = getFontMetrics(getFont());
277 int fontHeight = fm.getMaxAscent() + fm.getMaxDescent();
278 int columnWidth = fm.charWidth('m');
280 if (columns != 0)
282 size = new Dimension(columns * columnWidth + 4, fontHeight + 4);
284 else
286 size = new Dimension(10, 10);
289 return size;
293 * Returns the scroll offset in pixels.
295 * @return the scroll offset
297 public int getScrollOffset()
299 return scrollOffset;
303 * Sets the scroll offset in pixels.
305 * @param offset the scroll offset
307 public void setScrollOffset(int offset)
309 scrollOffset = offset;
312 public void postActionEvent()
314 ActionEvent event = new ActionEvent(this, 0, actionCommand);
315 ActionListener[] listeners = getActionListeners();
317 for (int index = 0; index < listeners.length; ++index)
318 listeners[index].actionPerformed(event);
322 * @since 1.3
324 public Action getAction()
326 return action;
330 * @since 1.3
332 public void setAction(Action newAction)
334 if (action == newAction)
335 return;
337 if (action != null)
339 removeActionListener(action);
340 action.removePropertyChangeListener(actionPropertyChangeListener);
341 actionPropertyChangeListener = null;
344 Action oldAction = action;
345 action = newAction;
347 if (action != null)
349 addActionListener(action);
350 actionPropertyChangeListener =
351 createActionPropertyChangeListener(action);
352 action.addPropertyChangeListener(actionPropertyChangeListener);
355 firePropertyChange("horizontalAlignment", oldAction, newAction);
359 * @since 1.3
361 public String getActionCommand()
363 return actionCommand;
367 * @since 1.3
369 public void setActionCommand(String command)
371 this.actionCommand = command;
375 * @since 1.3
377 protected PropertyChangeListener createActionPropertyChangeListener(Action action)
379 return new PropertyChangeListener()
381 public void propertyChange(PropertyChangeEvent event)
383 // Update properties "action" and "horizontalAlignment".
384 String name = event.getPropertyName();
386 if (name.equals("enabled"))
388 boolean enabled = ((Boolean) event.getNewValue()).booleanValue();
389 JTextField.this.setEnabled(enabled);
391 else if (name.equals(Action.SHORT_DESCRIPTION))
393 JTextField.this.setToolTipText((String) event.getNewValue());
400 * @since 1.3
402 protected void configurePropertiesFromAction(Action action)
404 if (action != null)
406 setEnabled(action.isEnabled());
407 setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
409 else
411 setEnabled(true);
412 setToolTipText(null);
416 protected int getColumnWidth()
418 FontMetrics metrics = getToolkit().getFontMetrics(getFont());
419 return metrics.charWidth('m');