Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / awt / TextField.java
blob5c84aab0965382781244a26caa2533800feb01c8
1 /* TextField.java -- A one line text entry field
2 Copyright (C) 1999, 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. */
39 package java.awt;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.peer.ComponentPeer;
44 import java.awt.peer.TextFieldPeer;
45 import java.util.EventListener;
47 import javax.accessibility.AccessibleContext;
48 import javax.accessibility.AccessibleStateSet;
50 /**
51 * This class implements a single line text entry field widget
53 * @author Aaron M. Renn (arenn@urbanophile.com)
55 public class TextField extends TextComponent
59 * Static Variables
62 // Serialization constant
63 private static final long serialVersionUID = -2966288784432217853L;
65 /*************************************************************************/
68 * Instance Variables
71 /**
72 * @serial The number of columns in the text entry field.
74 private int columns;
76 /**
77 * @serial The character that is echoed when doing protected input
79 private char echoChar;
81 // List of registered ActionListener's for this object.
82 private ActionListener action_listeners;
84 /*************************************************************************/
87 * Constructors
90 /**
91 * Initializes a new instance of <code>TextField</code> that is empty
92 * and has one column.
94 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
96 public
97 TextField()
99 this("", 1);
102 /*************************************************************************/
105 * Initializes a new instance of <code>TextField</code> containing
106 * the specified text. The number of columns will be equal to the
107 * length of the text string.
109 * @param text The text to display in the field.
111 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
113 public
114 TextField(String text)
116 this(text, text.length());
119 /*************************************************************************/
122 * Initializes a new instance of <code>TextField</code> that is empty
123 * and has the specified number of columns.
125 * @param columns The number of columns in the text field.
127 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
129 public
130 TextField(int columns)
132 this("", columns);
135 /*************************************************************************/
138 * Initializes a new instance of <code>TextField</code> with the
139 * specified text and number of columns.
141 * @param text The text to display in the field.
142 * @param columns The number of columns in the field.
144 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
146 public
147 TextField(String text, int columns)
149 super(text);
150 this.columns = columns;
152 if (GraphicsEnvironment.isHeadless())
153 throw new HeadlessException ();
156 /*************************************************************************/
159 * Instance Methods
163 * Returns the number of columns in the field.
165 * @return The number of columns in the field.
167 public int
168 getColumns()
170 return(columns);
173 /*************************************************************************/
176 * Sets the number of columns in this field to the specified value.
178 * @param columns The new number of columns in the field.
180 * @exception IllegalArgumentException If columns is less than zero.
182 public synchronized void
183 setColumns(int columns)
185 if (columns < 0)
186 throw new IllegalArgumentException("Value is less than zero: " +
187 columns);
189 this.columns = columns;
190 // FIXME: How to we communicate this to our peer?
193 /*************************************************************************/
196 * Returns the character that is echoed to the screen when a text
197 * field is protected (such as when a password is being entered).
199 * @return The echo character for this text field.
201 public char
202 getEchoChar()
204 return(echoChar);
207 /*************************************************************************/
210 * Sets the character that is echoed when protected input such as
211 * a password is displayed.
213 * @param echoChar The new echo character.
215 public void
216 setEchoChar(char echoChar)
218 setEchoCharacter (echoChar);
221 /*************************************************************************/
224 * Sets the character that is echoed when protected input such as
225 * a password is displayed.
227 * @param echoChar The new echo character.
229 * @deprecated This method is deprecated in favor of
230 * <code>setEchoChar()</code>
232 public void
233 setEchoCharacter(char echoChar)
235 this.echoChar = echoChar;
237 TextFieldPeer peer = (TextFieldPeer) getPeer ();
238 if (peer != null)
239 peer.setEchoChar (echoChar);
242 /*************************************************************************/
245 * Tests whether or not this text field has an echo character set
246 * so that characters the user type are not echoed to the screen.
248 * @return <code>true</code> if an echo character is set,
249 * <code>false</code> otherwise.
251 public boolean
252 echoCharIsSet()
254 if (echoChar == '\u0000')
255 return(false);
256 else
257 return(true);
260 /*************************************************************************/
263 * Returns the minimum size for this text field.
265 * @return The minimum size for this text field.
267 public Dimension
268 getMinimumSize()
270 return getMinimumSize (getColumns ());
273 /*************************************************************************/
276 * Returns the minimum size of a text field with the specified number
277 * of columns.
279 * @param columns The number of columns to get the minimum size for.
281 public Dimension
282 getMinimumSize(int columns)
284 return minimumSize (columns);
287 /*************************************************************************/
290 * Returns the minimum size for this text field.
292 * @return The minimum size for this text field.
294 * @deprecated This method is deprecated in favor of
295 * <code>getMinimumSize()</code>.
297 public Dimension
298 minimumSize()
300 return minimumSize (getColumns ());
303 /*************************************************************************/
306 * Returns the minimum size of a text field with the specified number
307 * of columns.
309 * @param columns The number of columns to get the minimum size for.
311 * @deprecated This method is deprecated in favor of
312 * <code>getMinimumSize(int)</code>.
314 public Dimension
315 minimumSize(int columns)
317 TextFieldPeer peer = (TextFieldPeer) getPeer ();
318 if (peer == null)
319 return null; // FIXME: What do we do if there is no peer?
321 return peer.getMinimumSize (columns);
324 /*************************************************************************/
327 * Returns the preferred size for this text field.
329 * @return The preferred size for this text field.
331 public Dimension
332 getPreferredSize()
334 return getPreferredSize (getColumns ());
337 /*************************************************************************/
340 * Returns the preferred size of a text field with the specified number
341 * of columns.
343 * @param columns The number of columns to get the preferred size for.
345 public Dimension
346 getPreferredSize(int columns)
348 return preferredSize (columns);
351 /*************************************************************************/
354 * Returns the preferred size for this text field.
356 * @return The preferred size for this text field.
358 * @deprecated This method is deprecated in favor of
359 * <code>getPreferredSize()</code>.
361 public Dimension
362 preferredSize()
364 return preferredSize (getColumns ());
367 /*************************************************************************/
370 * Returns the preferred size of a text field with the specified number
371 * of columns.
373 * @param columns The number of columns to get the preferred size for.
375 * @deprecated This method is deprecated in favor of
376 * <code>getPreferredSize(int)</code>.
378 public Dimension
379 preferredSize(int columns)
381 TextFieldPeer peer = (TextFieldPeer) getPeer ();
382 if (peer == null)
383 return new Dimension (0, 0);
385 return peer.getPreferredSize (columns);
388 /*************************************************************************/
391 * Notifies this object that it should create its native peer.
393 public void
394 addNotify()
396 if (getPeer() != null)
397 return;
399 setPeer((ComponentPeer)getToolkit().createTextField(this));
402 /*************************************************************************/
405 * Addes a new listener to the list of action listeners for this
406 * object.
408 * @param listener The listener to add to the list.
410 public synchronized void
411 addActionListener(ActionListener listener)
413 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
415 enableEvents(AWTEvent.ACTION_EVENT_MASK);
418 /*************************************************************************/
421 * Removes the specified listener from the list of action listeners
422 * for this object.
424 * @param listener The listener to remove from the list.
426 public synchronized void
427 removeActionListener(ActionListener listener)
429 action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
432 /*************************************************************************/
435 * Processes the specified event. If the event is an instance of
436 * <code>ActionEvent</code> then <code>processActionEvent()</code> is
437 * called to process it, otherwise the event is sent to the
438 * superclass.
440 * @param event The event to process.
442 protected void
443 processEvent(AWTEvent event)
445 if (event instanceof ActionEvent)
446 processActionEvent((ActionEvent)event);
447 else
448 super.processEvent(event);
451 /*************************************************************************/
454 * Processes an action event by calling any registered listeners.
455 * Note to subclasses: This method is not called unless action events
456 * are enabled on this object. This will be true if any listeners
457 * are registered, or if action events were specifically enabled
458 * using <code>enableEvents()</code>.
460 * @param event The event to process.
462 protected void
463 processActionEvent(ActionEvent event)
465 if (action_listeners != null)
466 action_listeners.actionPerformed(event);
469 void
470 dispatchEventImpl(AWTEvent e)
472 if (e.id <= ActionEvent.ACTION_LAST
473 && e.id >= ActionEvent.ACTION_FIRST
474 && (action_listeners != null
475 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
476 processEvent(e);
477 else
478 super.dispatchEventImpl(e);
481 /*************************************************************************/
484 * Returns a debug string for this object.
486 * @return A debug string for this object.
488 protected String
489 paramString()
491 return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
492 getEchoChar());
496 * Returns an array of all the objects currently registered as FooListeners
497 * upon this <code>TextField</code>. FooListeners are registered using the
498 * addFooListener method.
500 * @exception ClassCastException If listenerType doesn't specify a class or
501 * interface that implements java.util.EventListener.
503 * @since 1.3
505 public EventListener[] getListeners (Class listenerType)
507 if (listenerType == ActionListener.class)
508 return AWTEventMulticaster.getListeners (action_listeners, listenerType);
510 return super.getListeners (listenerType);
514 * Return all ActionListeners register to this <code>TextField</code> object
515 * as an array.
517 * @since 1.4
519 public ActionListener[] getActionListeners ()
521 return (ActionListener[]) getListeners (ActionListener.class);
524 protected class AccessibleAWTTextField extends AccessibleAWTTextComponent
526 protected AccessibleAWTTextField()
530 public AccessibleStateSet getAccessibleStateSet()
532 return super.getAccessibleStateSet();
536 public AccessibleContext getAccessibleContext()
538 return new AccessibleAWTTextField();
541 } // class TextField