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)
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
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. */
40 import java
.awt
.Dimension
;
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
58 * AccessibleJTextField
60 protected class AccessibleJTextField
extends AccessibleJTextComponent
62 private static final long serialVersionUID
= 8255147276740453036L;
65 * Constructor AccessibleJTextField
67 protected AccessibleJTextField()
72 * getAccessibleStateSet
73 * @return AccessibleStateSet
75 public AccessibleStateSet
getAccessibleStateSet()
81 private static final long serialVersionUID
= 353853209832607592L;
83 public static final String notifyAction
= "notify-field-accept";
89 private int scrollOffset
;
92 private Action action
;
95 private String actionCommand
;
97 private PropertyChangeListener actionPropertyChangeListener
;
100 * Creates a new instance of <code>JTextField</code>.
108 * Creates a new instance of <code>JTextField</code>.
110 * @param text the initial text
112 public JTextField(String text
)
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
)
154 throw new IllegalArgumentException();
156 this.columns
= columns
;
158 setDocument(doc
== null ?
createDefaultModel() : doc
);
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
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()
240 public void setColumns(int columns
)
243 throw new IllegalArgumentException();
245 this.columns
= columns
;
250 public int getHorizontalAlignment()
255 public void setHorizontalAlignment(int newAlign
)
257 if (align
== newAlign
)
260 int oldAlign
= align
;
262 firePropertyChange("horizontalAlignment", oldAlign
, newAlign
);
267 public void setFont(Font newFont
)
269 super.setFont(newFont
);
273 public Dimension
getPreferredSize()
276 FontMetrics fm
= getFontMetrics(getFont());
277 int fontHeight
= fm
.getMaxAscent() + fm
.getMaxDescent();
278 int columnWidth
= fm
.charWidth('m');
282 size
= new Dimension(columns
* columnWidth
+ 4, fontHeight
+ 4);
286 size
= new Dimension(10, 10);
293 * Returns the scroll offset in pixels.
295 * @return the scroll offset
297 public int getScrollOffset()
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
);
324 public Action
getAction()
332 public void setAction(Action newAction
)
334 if (action
== newAction
)
339 removeActionListener(action
);
340 action
.removePropertyChangeListener(actionPropertyChangeListener
);
341 actionPropertyChangeListener
= null;
344 Action oldAction
= action
;
349 addActionListener(action
);
350 actionPropertyChangeListener
=
351 createActionPropertyChangeListener(action
);
352 action
.addPropertyChangeListener(actionPropertyChangeListener
);
355 firePropertyChange("horizontalAlignment", oldAction
, newAction
);
361 public String
getActionCommand()
363 return actionCommand
;
369 public void setActionCommand(String command
)
371 this.actionCommand
= command
;
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());
402 protected void configurePropertiesFromAction(Action action
)
406 setEnabled(action
.isEnabled());
407 setToolTipText((String
) action
.getValue(Action
.SHORT_DESCRIPTION
));
412 setToolTipText(null);
416 protected int getColumnWidth()
418 FontMetrics metrics
= getToolkit().getFontMetrics(getFont());
419 return metrics
.charWidth('m');