001: /*
002: * @(#)TextField.java 1.65 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package java.awt;
028:
029: import sun.awt.peer.TextFieldPeer;
030: import sun.awt.PeerBasedToolkit;
031: import java.awt.event.*;
032: import java.io.ObjectOutputStream;
033: import java.io.ObjectInputStream;
034: import java.io.IOException;
035: import java.util.EventListener;
036: import java.awt.AWTEventMulticaster;
037:
038: /**
039: * A <code>TextField</code> object is a text component
040: * that allows for the editing of a single line of text.
041: * <p>
042: * For example, the following image depicts a frame with four
043: * text fields of varying widths. Two of these text fields
044: * display the predefined text <code>"Hello"</code>.
045: * <p>
046: * <img src="images-awt/TextField-1.gif"
047: * ALIGN=center HSPACE=10 VSPACE=7>
048: * <p>
049: * Here is the code that produces these four text fields:
050: * <p>
051: * <hr><blockquote><pre>
052: * TextField tf1, tf2, tf3, tf4;
053: * // a blank text field
054: * tf1 = new TextField();
055: * // blank field of 20 columns
056: * tf2 = new TextField("", 20);
057: * // predefined text displayed
058: * tf3 = new TextField("Hello!");
059: * // predefined text in 30 columns
060: * tf4 = new TextField("Hello", 30);
061: * </pre></blockquote><hr>
062: * <p>
063: * Every time the user types a key in the text field, AWT
064: * sends two action events to the text field. The first
065: * one represents the key press and the second one,
066: * the key release. Each action event embodies the state
067: * of the system at the time that some action occurred.
068: * The properties of an action event indicate which
069: * key was pressed, what modifier keys were also pressed,
070: * and the time at which the event occurred.
071: * <p>
072: * Since the event is an instance of <code>ActionEvent</code>,
073: * the <code>TextField</code> class's <code>processEvent</code>
074: * method examines the event and passes it along to
075: * <code>processActionEvent</code>. The latter method redirects the
076: * event to any <code>ActionListener</code> objects that have
077: * registered an interest in action events generated by this
078: * text field.
079: *
080: * @version 1.59, 08/19/02
081: * @author Sami Shaio
082: * @see java.awt.event.ActionEvent
083: * @see java.awt.TextField#processEvent
084: * @see java.awt.TextField#processActionEvent
085: * @since JDK1.0
086: */
087: public class TextField extends TextComponent {
088: /**
089: * The number of columns in the TextField.
090: */
091: int columns;
092: /**
093: * The echo character.
094: */
095: char echoChar;
096: transient ActionListener actionListener;
097: private static final String base = "textfield";
098: private static int nameCounter = 0;
099: /*
100: * JDK 1.1 serialVersionUID
101: */
102: private static final long serialVersionUID = -2966288784432217853L;
103:
104: /**
105: * Constructs a new text field.
106: * @since JDK1.0
107: */
108: public TextField() {
109: this ("", 0);
110: }
111:
112: /**
113: * Constructs a new text field initialized with the specified text.
114: * @param text the text to be displayed.
115: * @since JDK1.0
116: */
117: public TextField(String text) {
118: this (text, (text != null) ? text.length() : 0);
119: }
120:
121: /**
122: * Constructs a new empty TextField with the specified number of columns.
123: * @param columns the number of columns
124: */
125: public TextField(int columns) {
126: this ("", columns);
127: }
128:
129: /**
130: * Constructs a new text field initialized with the specified text
131: * to be displayed, and wide enough to hold the specified
132: * number of characters.
133: * @param text the text to be displayed.
134: * @param columns the number of characters.
135: * @since JDK1.0
136: */
137: public TextField(String text, int columns) {
138: super (text);
139: this .columns = (columns >= 0) ? columns : 0;
140: }
141:
142: /**
143: * Construct a name for this component. Called by getName() when the
144: * name is null.
145: */
146: String constructComponentName() {
147: return base + nameCounter++;
148: }
149:
150: /**
151: * Creates the TextField's peer. The peer allows us to modify the
152: * appearance of the TextField without changing its functionality.
153: */
154: public void addNotify() {
155: synchronized (getTreeLock()) {
156: if (peer == null)
157: peer = ((PeerBasedToolkit) getToolkit())
158: .createTextField(this );
159: super .addNotify();
160: }
161: }
162:
163: /**
164: * Gets the character that is to be used for echoing.
165: * <p>
166: * An echo character is useful for text fields where
167: * user input should not be echoed to the screen, as in
168: * the case of a text field for entering a password.
169: * @return the echo character for this text field.
170: * @see java.awt.TextField#echoCharIsSet
171: * @see java.awt.TextField#setEchoChar
172: * @since JDK1.0
173: */
174: public char getEchoChar() {
175: return echoChar;
176: }
177:
178: /**
179: * Sets the echo character for this text field.
180: * <p>
181: * An echo character is useful for text fields where
182: * user input should not be echoed to the screen, as in
183: * the case of a text field for entering a password.
184: * @param c the echo character for this text field.
185: * @see java.awt.TextField#echoCharIsSet
186: * @see java.awt.TextField#getEchoChar
187: * @since JDK1.1
188: */
189: public void setEchoChar(char c) {
190: setEchoCharacter(c);
191: }
192:
193: /**
194: * @deprecated As of JDK version 1.1,
195: * replaced by <code>setEchoChar(char)</code>.
196: */
197: public synchronized void setEchoCharacter(char c) {
198: if (echoChar != c) {
199: echoChar = c;
200: TextFieldPeer peer = (TextFieldPeer) this .peer;
201: if (peer != null) {
202: peer.setEchoChar(c);
203: }
204: }
205: }
206:
207: /**
208: * Indicates whether or not this text field has a
209: * character set for echoing.
210: * <p>
211: * An echo character is useful for text fields where
212: * user input should not be echoed to the screen, as in
213: * the case of a text field for entering a password.
214: * @return <code>true</code> if this text field has
215: * a character set for echoing;
216: * <code>false</code> otherwise.
217: * @see java.awt.TextField#setEchoChar
218: * @see java.awt.TextField#getEchoChar
219: * @since JDK1.0
220: */
221: public boolean echoCharIsSet() {
222: return echoChar != 0;
223: }
224:
225: /**
226: * Gets the number of columns in this text field.
227: * @return the number of columns.
228: * @see java.awt.TextField#setColumns
229: * @since JDK1.1ld.
230: */
231: public int getColumns() {
232: return columns;
233: }
234:
235: /**
236: * Sets the number of columns in this text field.
237: * @param columns the number of columns.
238: * @see java.awt.TextField#getColumns
239: * @exception IllegalArgumentException if the value
240: * supplied for <code>columns</code>
241: * is less than zero.
242: * @since JDK1.1
243: */
244: public synchronized void setColumns(int columns) {
245: int oldVal = this .columns;
246: if (columns < 0) {
247: throw new IllegalArgumentException(
248: "columns less than zero.");
249: }
250: if (columns != oldVal) {
251: this .columns = columns;
252: invalidate();
253: }
254: }
255:
256: /**
257: * Gets the preferred size of this text field
258: * with the specified number of columns.
259: * @param columns the number of columns
260: * in this text field.
261: * @return the preferred dimensions for
262: * displaying this text field.
263: * @since JDK1.1
264: */
265: public Dimension getPreferredSize(int columns) {
266: return preferredSize(columns);
267: }
268:
269: /**
270: * @deprecated As of JDK version 1.1,
271: * replaced by <code>getPreferredSize(int)</code>.
272: */
273: public Dimension preferredSize(int columns) {
274: synchronized (getTreeLock()) {
275: TextFieldPeer peer = (TextFieldPeer) this .peer;
276: return (peer != null) ? peer.getPreferredSize(columns)
277: : super .preferredSize();
278: }
279: }
280:
281: /**
282: * Gets the preferred size of this text field.
283: * @return the preferred dimensions for
284: * displaying this text field.
285: * @since JDK1.1
286: */
287: public Dimension getPreferredSize() {
288: return preferredSize();
289: }
290:
291: /**
292: * @deprecated As of JDK version 1.1,
293: * replaced by <code>getPreferredSize()</code>.
294: */
295: public Dimension preferredSize() {
296: synchronized (getTreeLock()) {
297: return (columns > 0) ? preferredSize(columns) : super
298: .preferredSize();
299: }
300: }
301:
302: /**
303: * Gets the minumum dimensions for a text field with
304: * the specified number of columns.
305: * @param columns the number of columns in
306: * this text field.
307: * @since JDK1.1
308: */
309: public Dimension getMinimumSize(int columns) {
310: return minimumSize(columns);
311: }
312:
313: /**
314: * @deprecated As of JDK version 1.1,
315: * replaced by <code>getMinimumSize(int)</code>.
316: */
317: public Dimension minimumSize(int columns) {
318: synchronized (getTreeLock()) {
319: TextFieldPeer peer = (TextFieldPeer) this .peer;
320: return (peer != null) ? peer.getMinimumSize(columns)
321: : super .minimumSize();
322: }
323: }
324:
325: /**
326: * Gets the minumum dimensions for this text field.
327: * @return the minimum dimensions for
328: * displaying this text field.
329: * @since JDK1.1
330: */
331: public Dimension getMinimumSize() {
332: return minimumSize();
333: }
334:
335: /**
336: * @deprecated As of JDK version 1.1,
337: * replaced by <code>getMinimumSize()</code>.
338: */
339: public Dimension minimumSize() {
340: synchronized (getTreeLock()) {
341: return (columns > 0) ? minimumSize(columns) : super
342: .minimumSize();
343: }
344: }
345:
346: /**
347: * Adds the specified action listener to recieve
348: * action events from this text field.
349: * @param l the action listener.
350: * @see java.awt.event.ActionListener
351: * @see java.awt.TextField#removeActionListener
352: * @since JDK1.1
353: */
354: public synchronized void addActionListener(ActionListener l) {
355: actionListener = AWTEventMulticaster.add(actionListener, l);
356: newEventsOnly = true;
357: }
358:
359: /**
360: * Removes the specified action listener so that it no longer
361: * receives action events from this text field.
362: * @param l the action listener.
363: * @see java.awt.event.ActionListener
364: * @see java.awt.TextField#addActionListener
365: * @since JDK1.1
366: */
367: public synchronized void removeActionListener(ActionListener l) {
368: actionListener = AWTEventMulticaster.remove(actionListener, l);
369: }
370:
371: /**
372: * Returns an array of all the action listeners
373: * registered on this textfield.
374: *
375: * @return all of this textfield's <code>ActionListener</code>s
376: * or an empty array if no action
377: * listeners are currently registered
378: *
379: * @see #addActionListener
380: * @see #removeActionListener
381: * @see java.awt.event#ActionListener
382: * @since 1.4
383: */
384: public synchronized ActionListener[] getActionListeners() {
385: return (ActionListener[]) AWTEventMulticaster.getListeners(
386: (EventListener) actionListener, ActionListener.class);
387: }
388:
389: // NOTE remove when filtering is done at lower level
390: boolean eventEnabled(AWTEvent e) {
391: if (e.id == ActionEvent.ACTION_PERFORMED) {
392: if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0
393: || actionListener != null) {
394: return true;
395: }
396: return false;
397: }
398: return super .eventEnabled(e);
399: }
400:
401: /**
402: * Processes events on this text field. If the event
403: * is an instance of <code>ActionEvent</code>,
404: * it invokes the <code>processActionEvent</code>
405: * method. Otherwise, it invokes <code>processEvent</code>
406: * on the superclass.
407: * @param e the event.
408: * @see java.awt.event.ActionEvent
409: * @see java.awt.TextField#processActionEvent
410: * @since JDK1.1
411: */
412: protected void processEvent(AWTEvent e) {
413: if (e instanceof ActionEvent) {
414: processActionEvent((ActionEvent) e);
415: return;
416: }
417: super .processEvent(e);
418: }
419:
420: /**
421: * Processes action events occurring on this text field by
422: * dispatching them to any registered
423: * <code>ActionListener</code> objects.
424: * <p>
425: * This method is not called unless action events are
426: * enabled for this component. Action events are enabled
427: * when one of the following occurs:
428: * <p><ul>
429: * <li>An <code>ActionListener</code> object is registered
430: * via <code>addActionListener</code>.
431: * <li>Action events are enabled via <code>enableEvents</code>.
432: * </ul>
433: * @param e the action event.
434: * @see java.awt.event.ActionListener
435: * @see java.awt.TextField#addActionListener
436: * @see java.awt.Component#enableEvents
437: * @since JDK1.1
438: */
439: protected void processActionEvent(ActionEvent e) {
440: if (actionListener != null) {
441: actionListener.actionPerformed(e);
442: }
443: }
444:
445: /**
446: * Returns the parameter string representing the state of this
447: * text field. This string is useful for debugging.
448: * @return the parameter string of this text field.
449: * @since JDK1.0
450: */
451: protected String paramString() {
452: String str = super .paramString();
453: if (echoChar != 0) {
454: str += ",echo=" + echoChar;
455: }
456: return str;
457: }
458:
459: /* Serialization support.
460: */
461:
462: private int textFieldSerializedDataVersion = 1;
463:
464: private void writeObject(ObjectOutputStream s) throws IOException {
465: s.defaultWriteObject();
466: AWTEventMulticaster.save(s, actionListenerK, actionListener);
467: s.writeObject(null);
468: }
469:
470: private void readObject(ObjectInputStream s)
471: throws ClassNotFoundException, IOException {
472: s.defaultReadObject();
473: Object keyOrNull;
474: while (null != (keyOrNull = s.readObject())) {
475: String key = ((String) keyOrNull).intern();
476: if (actionListenerK == key)
477: addActionListener((ActionListener) (s.readObject()));
478: else
479: // skip value for unrecognized key
480: s.readObject();
481: }
482: }
483: }
|