blob: fb95d91b8949046583d1b401bafc0e2e91f5d44b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1995-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package java.awt;
26
27import java.awt.peer.TextFieldPeer;
28import java.awt.event.*;
29import java.util.EventListener;
30import java.io.ObjectOutputStream;
31import java.io.ObjectInputStream;
32import java.io.IOException;
33import javax.accessibility.*;
34
35
36/**
37 * A <code>TextField</code> object is a text component
38 * that allows for the editing of a single line of text.
39 * <p>
40 * For example, the following image depicts a frame with four
41 * text fields of varying widths. Two of these text fields
42 * display the predefined text <code>"Hello"</code>.
43 * <p>
44 * <img src="doc-files/TextField-1.gif" alt="The preceding text describes this image."
45 * ALIGN=center HSPACE=10 VSPACE=7>
46 * <p>
47 * Here is the code that produces these four text fields:
48 * <p>
49 * <hr><blockquote><pre>
50 * TextField tf1, tf2, tf3, tf4;
51 * // a blank text field
52 * tf1 = new TextField();
53 * // blank field of 20 columns
54 * tf2 = new TextField("", 20);
55 * // predefined text displayed
56 * tf3 = new TextField("Hello!");
57 * // predefined text in 30 columns
58 * tf4 = new TextField("Hello", 30);
59 * </pre></blockquote><hr>
60 * <p>
61 * Every time the user types a key in the text field, one or
62 * more key events are sent to the text field. A <code>KeyEvent</code>
63 * may be one of three types: keyPressed, keyReleased, or keyTyped.
64 * The properties of a key event indicate which of these types
65 * it is, as well as additional information about the event,
66 * such as what modifiers are applied to the key event and the
67 * time at which the event occurred.
68 * <p>
69 * The key event is passed to every <code>KeyListener</code>
70 * or <code>KeyAdapter</code> object which registered to receive such
71 * events using the component's <code>addKeyListener</code> method.
72 * (<code>KeyAdapter</code> objects implement the
73 * <code>KeyListener</code> interface.)
74 * <p>
75 * It is also possible to fire an <code>ActionEvent</code>.
76 * If action events are enabled for the text field, they may
77 * be fired by pressing the <code>Return</code> key.
78 * <p>
79 * The <code>TextField</code> class's <code>processEvent</code>
80 * method examines the action event and passes it along to
81 * <code>processActionEvent</code>. The latter method redirects the
82 * event to any <code>ActionListener</code> objects that have
83 * registered to receive action events generated by this
84 * text field.
85 *
86 * @author Sami Shaio
87 * @see java.awt.event.KeyEvent
88 * @see java.awt.event.KeyAdapter
89 * @see java.awt.event.KeyListener
90 * @see java.awt.event.ActionEvent
91 * @see java.awt.Component#addKeyListener
92 * @see java.awt.TextField#processEvent
93 * @see java.awt.TextField#processActionEvent
94 * @see java.awt.TextField#addActionListener
95 * @since JDK1.0
96 */
97public class TextField extends TextComponent {
98
99 /**
100 * The number of columns in the text field.
101 * A column is an approximate average character
102 * width that is platform-dependent.
103 * Guaranteed to be non-negative.
104 *
105 * @serial
106 * @see #setColumns(int)
107 * @see #getColumns()
108 */
109 int columns;
110
111 /**
112 * The echo character, which is used when
113 * the user wishes to disguise the characters
114 * typed into the text field.
115 * The disguises are removed if echoChar = <code>0</code>.
116 *
117 * @serial
118 * @see #getEchoChar()
119 * @see #setEchoChar(char)
120 * @see #echoCharIsSet()
121 */
122 char echoChar;
123
124 transient ActionListener actionListener;
125
126 private static final String base = "textfield";
127 private static int nameCounter = 0;
128
129 /*
130 * JDK 1.1 serialVersionUID
131 */
132 private static final long serialVersionUID = -2966288784432217853L;
133
134 /**
135 * Initialize JNI field and method ids
136 */
137 private static native void initIDs();
138
139 static {
140 /* ensure that the necessary native libraries are loaded */
141 Toolkit.loadLibraries();
142 if (!GraphicsEnvironment.isHeadless()) {
143 initIDs();
144 }
145 }
146
147 /**
148 * Constructs a new text field.
149 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
150 * returns true.
151 * @see java.awt.GraphicsEnvironment#isHeadless
152 */
153 public TextField() throws HeadlessException {
154 this("", 0);
155 }
156
157 /**
158 * Constructs a new text field initialized with the specified text.
159 * @param text the text to be displayed. If
160 * <code>text</code> is <code>null</code>, the empty
161 * string <code>""</code> will be displayed.
162 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
163 * returns true.
164 * @see java.awt.GraphicsEnvironment#isHeadless
165 */
166 public TextField(String text) throws HeadlessException {
167 this(text, (text != null) ? text.length() : 0);
168 }
169
170 /**
171 * Constructs a new empty text field with the specified number
172 * of columns. A column is an approximate average character
173 * width that is platform-dependent.
174 * @param columns the number of columns. If
175 * <code>columns</code> is less than <code>0</code>,
176 * <code>columns</code> is set to <code>0</code>.
177 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
178 * returns true.
179 * @see java.awt.GraphicsEnvironment#isHeadless
180 */
181 public TextField(int columns) throws HeadlessException {
182 this("", columns);
183 }
184
185 /**
186 * Constructs a new text field initialized with the specified text
187 * to be displayed, and wide enough to hold the specified
188 * number of columns. A column is an approximate average character
189 * width that is platform-dependent.
190 * @param text the text to be displayed. If
191 * <code>text</code> is <code>null</code>, the empty
192 * string <code>""</code> will be displayed.
193 * @param columns the number of columns. If
194 * <code>columns</code> is less than <code>0</code>,
195 * <code>columns</code> is set to <code>0</code>.
196 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
197 * returns true.
198 * @see java.awt.GraphicsEnvironment#isHeadless
199 */
200 public TextField(String text, int columns) throws HeadlessException {
201 super(text);
202 this.columns = (columns >= 0) ? columns : 0;
203 }
204
205 /**
206 * Construct a name for this component. Called by getName() when the
207 * name is null.
208 */
209 String constructComponentName() {
210 synchronized (TextField.class) {
211 return base + nameCounter++;
212 }
213 }
214
215 /**
216 * Creates the TextField's peer. The peer allows us to modify the
217 * appearance of the TextField without changing its functionality.
218 */
219 public void addNotify() {
220 synchronized (getTreeLock()) {
221 if (peer == null)
222 peer = getToolkit().createTextField(this);
223 super.addNotify();
224 }
225 }
226
227 /**
228 * Gets the character that is to be used for echoing.
229 * <p>
230 * An echo character is useful for text fields where
231 * user input should not be echoed to the screen, as in
232 * the case of a text field for entering a password.
233 * If <code>echoChar</code> = <code>0</code>, user
234 * input is echoed to the screen unchanged.
235 * <p>
236 * A Java platform implementation may support only a limited,
237 * non-empty set of echo characters. This function returns the
238 * echo character originally requested via setEchoChar(). The echo
239 * character actually used by the TextField implementation might be
240 * different.
241 * @return the echo character for this text field.
242 * @see java.awt.TextField#echoCharIsSet
243 * @see java.awt.TextField#setEchoChar
244 */
245 public char getEchoChar() {
246 return echoChar;
247 }
248
249 /**
250 * Sets the echo character for this text field.
251 * <p>
252 * An echo character is useful for text fields where
253 * user input should not be echoed to the screen, as in
254 * the case of a text field for entering a password.
255 * Setting <code>echoChar</code> = <code>0</code> allows
256 * user input to be echoed to the screen again.
257 * <p>
258 * A Java platform implementation may support only a limited,
259 * non-empty set of echo characters. Attempts to set an
260 * unsupported echo character will cause the default echo
261 * character to be used instead. Subsequent calls to getEchoChar()
262 * will return the echo character originally requested. This might
263 * or might not be identical to the echo character actually
264 * used by the TextField implementation.
265 * @param c the echo character for this text field.
266 * @see java.awt.TextField#echoCharIsSet
267 * @see java.awt.TextField#getEchoChar
268 * @since JDK1.1
269 */
270 public void setEchoChar(char c) {
271 setEchoCharacter(c);
272 }
273
274 /**
275 * @deprecated As of JDK version 1.1,
276 * replaced by <code>setEchoChar(char)</code>.
277 */
278 @Deprecated
279 public synchronized void setEchoCharacter(char c) {
280 if (echoChar != c) {
281 echoChar = c;
282 TextFieldPeer peer = (TextFieldPeer)this.peer;
283 if (peer != null) {
284 peer.setEchoCharacter(c);
285 }
286 }
287 }
288
289 /**
290 * Sets the text that is presented by this
291 * text component to be the specified text.
292 * @param t the new text.
293 * @see java.awt.TextComponent#getText
294 */
295 public void setText(String t) {
296 super.setText(t);
297
298 // This could change the preferred size of the Component.
299 if (valid) {
300 invalidate();
301 }
302 }
303
304 /**
305 * Indicates whether or not this text field has a
306 * character set for echoing.
307 * <p>
308 * An echo character is useful for text fields where
309 * user input should not be echoed to the screen, as in
310 * the case of a text field for entering a password.
311 * @return <code>true</code> if this text field has
312 * a character set for echoing;
313 * <code>false</code> otherwise.
314 * @see java.awt.TextField#setEchoChar
315 * @see java.awt.TextField#getEchoChar
316 */
317 public boolean echoCharIsSet() {
318 return echoChar != 0;
319 }
320
321 /**
322 * Gets the number of columns in this text field. A column is an
323 * approximate average character width that is platform-dependent.
324 * @return the number of columns.
325 * @see java.awt.TextField#setColumns
326 * @since JDK1.1
327 */
328 public int getColumns() {
329 return columns;
330 }
331
332 /**
333 * Sets the number of columns in this text field. A column is an
334 * approximate average character width that is platform-dependent.
335 * @param columns the number of columns.
336 * @see java.awt.TextField#getColumns
337 * @exception IllegalArgumentException if the value
338 * supplied for <code>columns</code>
339 * is less than <code>0</code>.
340 * @since JDK1.1
341 */
342 public void setColumns(int columns) {
343 int oldVal;
344 synchronized (this) {
345 oldVal = this.columns;
346 if (columns < 0) {
347 throw new IllegalArgumentException("columns less than zero.");
348 }
349 if (columns != oldVal) {
350 this.columns = columns;
351 }
352 }
353
354 if (columns != oldVal) {
355 invalidate();
356 }
357 }
358
359 /**
360 * Gets the preferred size of this text field
361 * with the specified number of columns.
362 * @param columns the number of columns
363 * in this text field.
364 * @return the preferred dimensions for
365 * displaying this text field.
366 * @since JDK1.1
367 */
368 public Dimension getPreferredSize(int columns) {
369 return preferredSize(columns);
370 }
371
372 /**
373 * @deprecated As of JDK version 1.1,
374 * replaced by <code>getPreferredSize(int)</code>.
375 */
376 @Deprecated
377 public Dimension preferredSize(int columns) {
378 synchronized (getTreeLock()) {
379 TextFieldPeer peer = (TextFieldPeer)this.peer;
380 return (peer != null) ?
381 peer.preferredSize(columns) :
382 super.preferredSize();
383 }
384 }
385
386 /**
387 * Gets the preferred size of this text field.
388 * @return the preferred dimensions for
389 * displaying this text field.
390 * @since JDK1.1
391 */
392 public Dimension getPreferredSize() {
393 return preferredSize();
394 }
395
396 /**
397 * @deprecated As of JDK version 1.1,
398 * replaced by <code>getPreferredSize()</code>.
399 */
400 @Deprecated
401 public Dimension preferredSize() {
402 synchronized (getTreeLock()) {
403 return (columns > 0) ?
404 preferredSize(columns) :
405 super.preferredSize();
406 }
407 }
408
409 /**
410 * Gets the minumum dimensions for a text field with
411 * the specified number of columns.
412 * @param columns the number of columns in
413 * this text field.
414 * @since JDK1.1
415 */
416 public Dimension getMinimumSize(int columns) {
417 return minimumSize(columns);
418 }
419
420 /**
421 * @deprecated As of JDK version 1.1,
422 * replaced by <code>getMinimumSize(int)</code>.
423 */
424 @Deprecated
425 public Dimension minimumSize(int columns) {
426 synchronized (getTreeLock()) {
427 TextFieldPeer peer = (TextFieldPeer)this.peer;
428 return (peer != null) ?
429 peer.minimumSize(columns) :
430 super.minimumSize();
431 }
432 }
433
434 /**
435 * Gets the minumum dimensions for this text field.
436 * @return the minimum dimensions for
437 * displaying this text field.
438 * @since JDK1.1
439 */
440 public Dimension getMinimumSize() {
441 return minimumSize();
442 }
443
444 /**
445 * @deprecated As of JDK version 1.1,
446 * replaced by <code>getMinimumSize()</code>.
447 */
448 @Deprecated
449 public Dimension minimumSize() {
450 synchronized (getTreeLock()) {
451 return (columns > 0) ?
452 minimumSize(columns) :
453 super.minimumSize();
454 }
455 }
456
457 /**
458 * Adds the specified action listener to receive
459 * action events from this text field.
460 * If l is null, no exception is thrown and no action is performed.
461 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
462 * >AWT Threading Issues</a> for details on AWT's threading model.
463 *
464 * @param l the action listener.
465 * @see #removeActionListener
466 * @see #getActionListeners
467 * @see java.awt.event.ActionListener
468 * @since JDK1.1
469 */
470 public synchronized void addActionListener(ActionListener l) {
471 if (l == null) {
472 return;
473 }
474 actionListener = AWTEventMulticaster.add(actionListener, l);
475 newEventsOnly = true;
476 }
477
478 /**
479 * Removes the specified action listener so that it no longer
480 * receives action events from this text field.
481 * If l is null, no exception is thrown and no action is performed.
482 * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
483 * >AWT Threading Issues</a> for details on AWT's threading model.
484 *
485 * @param l the action listener.
486 * @see #addActionListener
487 * @see #getActionListeners
488 * @see java.awt.event.ActionListener
489 * @since JDK1.1
490 */
491 public synchronized void removeActionListener(ActionListener l) {
492 if (l == null) {
493 return;
494 }
495 actionListener = AWTEventMulticaster.remove(actionListener, l);
496 }
497
498 /**
499 * Returns an array of all the action listeners
500 * registered on this textfield.
501 *
502 * @return all of this textfield's <code>ActionListener</code>s
503 * or an empty array if no action
504 * listeners are currently registered
505 *
506 * @see #addActionListener
507 * @see #removeActionListener
508 * @see java.awt.event#ActionListener
509 * @since 1.4
510 */
511 public synchronized ActionListener[] getActionListeners() {
512 return (ActionListener[])(getListeners(ActionListener.class));
513 }
514
515 /**
516 * Returns an array of all the objects currently registered
517 * as <code><em>Foo</em>Listener</code>s
518 * upon this <code>TextField</code>.
519 * <code><em>Foo</em>Listener</code>s are registered using the
520 * <code>add<em>Foo</em>Listener</code> method.
521 *
522 * <p>
523 * You can specify the <code>listenerType</code> argument
524 * with a class literal, such as
525 * <code><em>Foo</em>Listener.class</code>.
526 * For example, you can query a
527 * <code>TextField</code> <code>t</code>
528 * for its action listeners with the following code:
529 *
530 * <pre>ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));</pre>
531 *
532 * If no such listeners exist, this method returns an empty array.
533 *
534 * @param listenerType the type of listeners requested; this parameter
535 * should specify an interface that descends from
536 * <code>java.util.EventListener</code>
537 * @return an array of all objects registered as
538 * <code><em>Foo</em>Listener</code>s on this textfield,
539 * or an empty array if no such
540 * listeners have been added
541 * @exception ClassCastException if <code>listenerType</code>
542 * doesn't specify a class or interface that implements
543 * <code>java.util.EventListener</code>
544 *
545 * @see #getActionListeners
546 * @since 1.3
547 */
548 public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
549 EventListener l = null;
550 if (listenerType == ActionListener.class) {
551 l = actionListener;
552 } else {
553 return super.getListeners(listenerType);
554 }
555 return AWTEventMulticaster.getListeners(l, listenerType);
556 }
557
558 // REMIND: remove when filtering is done at lower level
559 boolean eventEnabled(AWTEvent e) {
560 if (e.id == ActionEvent.ACTION_PERFORMED) {
561 if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
562 actionListener != null) {
563 return true;
564 }
565 return false;
566 }
567 return super.eventEnabled(e);
568 }
569
570 /**
571 * Processes events on this text field. If the event
572 * is an instance of <code>ActionEvent</code>,
573 * it invokes the <code>processActionEvent</code>
574 * method. Otherwise, it invokes <code>processEvent</code>
575 * on the superclass.
576 * <p>Note that if the event parameter is <code>null</code>
577 * the behavior is unspecified and may result in an
578 * exception.
579 *
580 * @param e the event
581 * @see java.awt.event.ActionEvent
582 * @see java.awt.TextField#processActionEvent
583 * @since JDK1.1
584 */
585 protected void processEvent(AWTEvent e) {
586 if (e instanceof ActionEvent) {
587 processActionEvent((ActionEvent)e);
588 return;
589 }
590 super.processEvent(e);
591 }
592
593 /**
594 * Processes action events occurring on this text field by
595 * dispatching them to any registered
596 * <code>ActionListener</code> objects.
597 * <p>
598 * This method is not called unless action events are
599 * enabled for this component. Action events are enabled
600 * when one of the following occurs:
601 * <p><ul>
602 * <li>An <code>ActionListener</code> object is registered
603 * via <code>addActionListener</code>.
604 * <li>Action events are enabled via <code>enableEvents</code>.
605 * </ul>
606 * <p>Note that if the event parameter is <code>null</code>
607 * the behavior is unspecified and may result in an
608 * exception.
609 *
610 * @param e the action event
611 * @see java.awt.event.ActionListener
612 * @see java.awt.TextField#addActionListener
613 * @see java.awt.Component#enableEvents
614 * @since JDK1.1
615 */
616 protected void processActionEvent(ActionEvent e) {
617 ActionListener listener = actionListener;
618 if (listener != null) {
619 listener.actionPerformed(e);
620 }
621 }
622
623 /**
624 * Returns a string representing the state of this <code>TextField</code>.
625 * This method is intended to be used only for debugging purposes, and the
626 * content and format of the returned string may vary between
627 * implementations. The returned string may be empty but may not be
628 * <code>null</code>.
629 *
630 * @return the parameter string of this text field
631 */
632 protected String paramString() {
633 String str = super.paramString();
634 if (echoChar != 0) {
635 str += ",echo=" + echoChar;
636 }
637 return str;
638 }
639
640
641 /*
642 * Serialization support.
643 */
644 /**
645 * The textField Serialized Data Version.
646 *
647 * @serial
648 */
649 private int textFieldSerializedDataVersion = 1;
650
651 /**
652 * Writes default serializable fields to stream. Writes
653 * a list of serializable ActionListener(s) as optional data.
654 * The non-serializable ActionListener(s) are detected and
655 * no attempt is made to serialize them.
656 *
657 * @serialData Null terminated sequence of zero or more pairs.
658 * A pair consists of a String and Object.
659 * The String indicates the type of object and
660 * is one of the following :
661 * ActionListenerK indicating and ActionListener object.
662 *
663 * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
664 * @see java.awt.Component#actionListenerK
665 */
666 private void writeObject(ObjectOutputStream s)
667 throws IOException
668 {
669 s.defaultWriteObject();
670
671 AWTEventMulticaster.save(s, actionListenerK, actionListener);
672 s.writeObject(null);
673 }
674
675 /**
676 * Read the ObjectInputStream and if it isn't null,
677 * add a listener to receive action events fired by the
678 * TextField. Unrecognized keys or values will be
679 * ignored.
680 *
681 * @exception HeadlessException if
682 * <code>GraphicsEnvironment.isHeadless()</code> returns
683 * <code>true</code>
684 * @see #removeActionListener(ActionListener)
685 * @see #addActionListener(ActionListener)
686 * @see java.awt.GraphicsEnvironment#isHeadless
687 */
688 private void readObject(ObjectInputStream s)
689 throws ClassNotFoundException, IOException, HeadlessException
690 {
691 // HeadlessException will be thrown by TextComponent's readObject
692 s.defaultReadObject();
693
694 // Make sure the state we just read in for columns has legal values
695 if (columns < 0) {
696 columns = 0;
697 }
698
699 // Read in listeners, if any
700 Object keyOrNull;
701 while(null != (keyOrNull = s.readObject())) {
702 String key = ((String)keyOrNull).intern();
703
704 if (actionListenerK == key) {
705 addActionListener((ActionListener)(s.readObject()));
706 } else {
707 // skip value for unrecognized key
708 s.readObject();
709 }
710 }
711 }
712
713
714/////////////////
715// Accessibility support
716////////////////
717
718
719 /**
720 * Gets the AccessibleContext associated with this TextField.
721 * For text fields, the AccessibleContext takes the form of an
722 * AccessibleAWTTextField.
723 * A new AccessibleAWTTextField instance is created if necessary.
724 *
725 * @return an AccessibleAWTTextField that serves as the
726 * AccessibleContext of this TextField
727 * @since 1.3
728 */
729 public AccessibleContext getAccessibleContext() {
730 if (accessibleContext == null) {
731 accessibleContext = new AccessibleAWTTextField();
732 }
733 return accessibleContext;
734 }
735
736 /**
737 * This class implements accessibility support for the
738 * <code>TextField</code> class. It provides an implementation of the
739 * Java Accessibility API appropriate to text field user-interface elements.
740 * @since 1.3
741 */
742 protected class AccessibleAWTTextField extends AccessibleAWTTextComponent
743 {
744 /*
745 * JDK 1.3 serialVersionUID
746 */
747 private static final long serialVersionUID = 6219164359235943158L;
748
749 /**
750 * Gets the state set of this object.
751 *
752 * @return an instance of AccessibleStateSet describing the states
753 * of the object
754 * @see AccessibleState
755 */
756 public AccessibleStateSet getAccessibleStateSet() {
757 AccessibleStateSet states = super.getAccessibleStateSet();
758 states.add(AccessibleState.SINGLE_LINE);
759 return states;
760 }
761 }
762
763}