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.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.AttributeSet;
51 import javax.swing.text.BadLocationException;
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()
75 * getAccessibleStateSet
76 * @return AccessibleStateSet
78 public AccessibleStateSet getAccessibleStateSet()
84 private static final long serialVersionUID = 353853209832607592L;
86 private static final Action[] actions;
89 * Name of the action that gets sent when the content of the text field
92 public static final String notifyAction = "notify-field-accept";
96 actions = new Action[1];
97 actions[0] = new TextAction(notifyAction)
99 public void actionPerformed(ActionEvent event)
101 JTextField textField = (JTextField) event.getSource();
102 textField.fireActionPerformed();
109 private int scrollOffset;
112 private Action action;
115 private String actionCommand;
117 private PropertyChangeListener actionPropertyChangeListener;
120 * Creates a new instance of <code>JTextField</code>.
128 * Creates a new instance of <code>JTextField</code>.
130 * @param text the initial text
132 public JTextField(String text)
138 * Creates a new instance of <code>JTextField</code>.
140 * @param columns the number of columns
142 * @exception IllegalArgumentException if columns %lt; 0
144 public JTextField(int columns)
146 this(null, null, columns);
150 * Creates a new instance of <code>JTextField</code>.
152 * @param text the initial text
153 * @param columns the number of columns
155 * @exception IllegalArgumentException if columns %lt; 0
157 public JTextField(String text, int columns)
159 this(null, text, columns);
163 * Creates a new instance of <code>JTextField</code>.
165 * @param doc the document to use
166 * @param text the initial text
167 * @param columns the number of columns
169 * @exception IllegalArgumentException if columns %lt; 0
171 public JTextField(Document doc, String text, int columns)
174 throw new IllegalArgumentException();
176 this.columns = columns;
178 setDocument(doc == null ? createDefaultModel() : doc);
183 // default value for alignment
188 * Creates the default model for this text field.
189 * This implementation returns an instance of <code>PlainDocument</code>.
191 * @return a new instance of the default model
193 protected Document createDefaultModel()
195 // subclassed to swallow newlines
196 return new PlainDocument() {
197 public void insertString(int offset, String str, AttributeSet a)
198 throws BadLocationException
200 if (str != null && str.indexOf('\n') == -1)
201 super.insertString(offset, str, a);
207 * Returns the class ID for the UI.
209 * @return "TextFieldUI";
211 public String getUIClassID()
213 return "TextFieldUI";
217 * Adds a new listener object to this text field.
219 * @param listener the listener to add
221 public void addActionListener(ActionListener listener)
223 listenerList.add(ActionListener.class, listener);
227 * Removes a listener object from this text field.
229 * @param listener the listener to remove
231 public void removeActionListener(ActionListener listener)
233 listenerList.remove(ActionListener.class, listener);
237 * Returns all registered <code>ActionListener</code> objects.
239 * @return an array of listeners
243 public ActionListener[] getActionListeners()
245 return (ActionListener[]) getListeners(ActionListener.class);
249 * Sends an action event to all registered
250 * <code>ActionListener</code> objects.
252 protected void fireActionPerformed()
254 ActionEvent event = new ActionEvent(this, 0, notifyAction);
255 ActionListener[] listeners = getActionListeners();
257 for (int index = 0; index < listeners.length; ++index)
258 listeners[index].actionPerformed(event);
262 * Returns the number of columns of this text field.
264 * @return the number of columns
266 public int getColumns()
271 public void setColumns(int columns)
274 throw new IllegalArgumentException();
276 this.columns = columns;
281 public int getHorizontalAlignment()
286 public void setHorizontalAlignment(int newAlign)
288 if (align == newAlign)
291 int oldAlign = align;
293 firePropertyChange("horizontalAlignment", oldAlign, newAlign);
298 public void setFont(Font newFont)
300 super.setFont(newFont);
304 public Dimension getPreferredSize()
306 Dimension size = super.getPreferredSize();
309 size.width = columns * getColumnWidth();
315 * Returns the scroll offset in pixels.
317 * @return the scroll offset
319 public int getScrollOffset()
325 * Sets the scroll offset in pixels.
327 * @param offset the scroll offset
329 public void setScrollOffset(int offset)
331 scrollOffset = offset;
334 public Action[] getActions()
336 return TextAction.augmentList(super.getActions(), actions);
339 public void postActionEvent()
341 String command = actionCommand != null ? actionCommand : getText();
342 ActionEvent event = new ActionEvent(this, 0, command);
343 ActionListener[] listeners = getActionListeners();
345 for (int index = 0; index < listeners.length; ++index)
346 listeners[index].actionPerformed(event);
352 public Action getAction()
360 public void setAction(Action newAction)
362 if (action == newAction)
367 removeActionListener(action);
368 action.removePropertyChangeListener(actionPropertyChangeListener);
369 actionPropertyChangeListener = null;
372 Action oldAction = action;
377 addActionListener(action);
378 actionPropertyChangeListener =
379 createActionPropertyChangeListener(action);
380 action.addPropertyChangeListener(actionPropertyChangeListener);
383 firePropertyChange("horizontalAlignment", oldAction, newAction);
389 public void setActionCommand(String command)
391 actionCommand = command;
397 protected PropertyChangeListener createActionPropertyChangeListener(Action action)
399 return new PropertyChangeListener()
401 public void propertyChange(PropertyChangeEvent event)
403 // Update properties "action" and "horizontalAlignment".
404 String name = event.getPropertyName();
406 if (name.equals("enabled"))
408 boolean enabled = ((Boolean) event.getNewValue()).booleanValue();
409 JTextField.this.setEnabled(enabled);
411 else if (name.equals(Action.SHORT_DESCRIPTION))
413 JTextField.this.setToolTipText((String) event.getNewValue());
422 protected void configurePropertiesFromAction(Action action)
426 setEnabled(action.isEnabled());
427 setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
432 setToolTipText(null);
436 protected int getColumnWidth()
438 FontMetrics metrics = getToolkit().getFontMetrics(getFont());
439 return metrics.charWidth('m');