source: trunk/gcc/libjava/java/awt/TextField.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 12.4 KB
Line 
1/* TextField.java -- A one line text entry field
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.awt.event.ActionEvent;
42import java.awt.event.ActionListener;
43import java.awt.peer.TextFieldPeer;
44import java.awt.peer.TextComponentPeer;
45import java.awt.peer.ComponentPeer;
46
47/**
48 * This class implements a single line text entry field widget
49 *
50 * @author Aaron M. Renn (arenn@urbanophile.com)
51 */
52public class TextField extends TextComponent implements java.io.Serializable
53{
54
55/*
56 * Static Variables
57 */
58
59// Serialization constant
60private static final long serialVersionUID = -2966288784432217853L;
61
62/*************************************************************************/
63
64/*
65 * Instance Variables
66 */
67
68/**
69 * @serial The number of columns in the text entry field.
70 */
71private int columns;
72
73/**
74 * @serial The character that is echoed when doing protected input
75 */
76private char echoChar;
77
78// List of registered ActionListener's for this object.
79private ActionListener action_listeners;
80
81/*************************************************************************/
82
83/*
84 * Constructors
85 */
86
87/**
88 * Initializes a new instance of <code>TextField</code> that is empty
89 * and has one column.
90 *
91 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
92 */
93public
94TextField()
95{
96 this("", 1);
97}
98
99/*************************************************************************/
100
101/**
102 * Initializes a new instance of <code>TextField</code> containing
103 * the specified text. The number of columns will be equal to the
104 * length of the text string.
105 *
106 * @param text The text to display in the field.
107 *
108 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
109 */
110public
111TextField(String text)
112{
113 this(text, text.length());
114}
115
116/*************************************************************************/
117
118/**
119 * Initializes a new instance of <code>TextField</code> that is empty
120 * and has the specified number of columns.
121 *
122 * @param columns The number of columns in the text field.
123 *
124 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
125 */
126public
127TextField(int columns)
128{
129 this("", columns);
130}
131
132/*************************************************************************/
133
134/**
135 * Initializes a new instance of <code>TextField</code> with the
136 * specified text and number of columns.
137 *
138 * @param text The text to display in the field.
139 * @param columns The number of columns in the field.
140 *
141 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
142 */
143public
144TextField(String text, int columns)
145{
146 super(text);
147 this.columns = columns;
148
149 if (GraphicsEnvironment.isHeadless())
150 throw new HeadlessException ();
151}
152
153/*************************************************************************/
154
155/*
156 * Instance Methods
157 */
158
159/**
160 * Returns the number of columns in the field.
161 *
162 * @return The number of columns in the field.
163 */
164public int
165getColumns()
166{
167 return(columns);
168}
169
170/*************************************************************************/
171
172/**
173 * Sets the number of columns in this field to the specified value.
174 *
175 * @param columns The new number of columns in the field.
176 *
177 * @exception IllegalArgumentException If columns is less than zero.
178 */
179public synchronized void
180setColumns(int columns)
181{
182 if (columns < 0)
183 throw new IllegalArgumentException("Value is less than zero: " +
184 columns);
185
186 this.columns = columns;
187 // FIXME: How to we communicate this to our peer?
188}
189
190/*************************************************************************/
191
192/**
193 * Returns the character that is echoed to the screen when a text
194 * field is protected (such as when a password is being entered).
195 *
196 * @return The echo character for this text field.
197 */
198public char
199getEchoChar()
200{
201 return(echoChar);
202}
203
204/*************************************************************************/
205
206/**
207 * Sets the character that is echoed when protected input such as
208 * a password is displayed.
209 *
210 * @param echoChar The new echo character.
211 */
212public void
213setEchoChar(char echoChar)
214{
215 this.echoChar = echoChar;
216
217 TextFieldPeer tfp = (TextFieldPeer)getPeer();
218 if (tfp != null)
219 tfp.setEchoChar(echoChar);
220}
221
222/*************************************************************************/
223
224/**
225 * Sets the character that is echoed when protected input such as
226 * a password is displayed.
227 *
228 * @param echoChar The new echo character.
229 *
230 * @deprecated This method is deprecated in favor of
231 * <code>setEchoChar()</code>
232 */
233public void
234setEchoCharacter(char echoChar)
235{
236 setEchoChar(echoChar);
237}
238
239/*************************************************************************/
240
241/**
242 * Tests whether or not this text field has an echo character set
243 * so that characters the user type are not echoed to the screen.
244 *
245 * @return <code>true</code> if an echo character is set,
246 * <code>false</code> otherwise.
247 */
248public boolean
249echoCharIsSet()
250{
251 if (echoChar == '\u0000')
252 return(false);
253 else
254 return(true);
255}
256
257/*************************************************************************/
258
259/**
260 * Returns the minimum size for this text field.
261 *
262 * @return The minimum size for this text field.
263 */
264public Dimension
265getMinimumSize()
266{
267 return(getMinimumSize(getColumns()));
268}
269
270/*************************************************************************/
271
272/**
273 * Returns the minimum size of a text field with the specified number
274 * of columns.
275 *
276 * @param columns The number of columns to get the minimum size for.
277 */
278public Dimension
279getMinimumSize(int columns)
280{
281 TextFieldPeer tfp = (TextFieldPeer)getPeer();
282 if (tfp == null)
283 return(null); // FIXME: What do we do if there is no peer?
284
285 return(tfp.getMinimumSize(columns));
286}
287
288/*************************************************************************/
289
290/**
291 * Returns the minimum size for this text field.
292 *
293 * @return The minimum size for this text field.
294 *
295 * @deprecated This method is depcreated in favor of
296 * <code>getMinimumSize()</code>.
297 */
298public Dimension
299minimumSize()
300{
301 return(getMinimumSize(getColumns()));
302}
303
304/*************************************************************************/
305
306/**
307 * Returns the minimum size of a text field with the specified number
308 * of columns.
309 *
310 * @param columns The number of columns to get the minimum size for.
311 *
312 * @deprecated This method is deprecated in favor of
313 * <code>getMinimumSize(int)</code>.
314 */
315public Dimension
316minimumSize(int columns)
317{
318 return(getMinimumSize(columns));
319}
320
321/*************************************************************************/
322
323/**
324 * Returns the preferred size for this text field.
325 *
326 * @return The preferred size for this text field.
327 */
328public Dimension
329getPreferredSize()
330{
331 return(getPreferredSize(getColumns()));
332}
333
334/*************************************************************************/
335
336/**
337 * Returns the preferred size of a text field with the specified number
338 * of columns.
339 *
340 * @param columns The number of columns to get the preferred size for.
341 */
342public Dimension
343getPreferredSize(int columns)
344{
345 TextFieldPeer tfp = (TextFieldPeer)getPeer();
346 if (tfp == null)
347 return(null); // FIXME: What do we do if there is no peer?
348
349 return(tfp.getPreferredSize(columns));
350}
351
352/*************************************************************************/
353
354/**
355 * Returns the preferred size for this text field.
356 *
357 * @return The preferred size for this text field.
358 *
359 * @deprecated This method is deprecated in favor of
360 * <code>getPreferredSize()</code>.
361 */
362public Dimension
363preferredSize()
364{
365 return(getPreferredSize(getColumns()));
366}
367
368/*************************************************************************/
369
370/**
371 * Returns the preferred size of a text field with the specified number
372 * of columns.
373 *
374 * @param columns The number of columns to get the preferred size for.
375 *
376 * @deprecated This method is deprecated in favor of
377 * <code>getPreferredSize(int)</code>.
378 */
379public Dimension
380preferredSize(int columns)
381{
382 return(getPreferredSize(columns));
383}
384
385/*************************************************************************/
386
387/**
388 * Notifies this object that it should create its native peer.
389 */
390public void
391addNotify()
392{
393 if (getPeer() != null)
394 return;
395
396 setPeer((ComponentPeer)getToolkit().createTextField(this));
397}
398
399/*************************************************************************/
400
401/**
402 * Addes a new listener to the list of action listeners for this
403 * object.
404 *
405 * @param listener The listener to add to the list.
406 */
407public synchronized void
408addActionListener(ActionListener listener)
409{
410 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
411
412 enableEvents(AWTEvent.ACTION_EVENT_MASK);
413}
414
415/*************************************************************************/
416
417/**
418 * Removes the specified listener from the list of action listeners
419 * for this object.
420 *
421 * @param listener The listener to remove from the list.
422 */
423public synchronized void
424removeActionListener(ActionListener listener)
425{
426 action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
427}
428
429/*************************************************************************/
430
431/**
432 * Processes the specified event. If the event is an instance of
433 * <code>ActionEvent</code> then <code>processActionEvent()</code> is
434 * called to process it, otherwise the event is sent to the
435 * superclass.
436 *
437 * @param event The event to process.
438 */
439protected void
440processEvent(AWTEvent event)
441{
442 if (event instanceof ActionEvent)
443 processActionEvent((ActionEvent)event);
444 else
445 super.processEvent(event);
446}
447
448/*************************************************************************/
449
450/**
451 * Processes an action event by calling any registered listeners.
452 * Note to subclasses: This method is not called unless action events
453 * are enabled on this object. This will be true if any listeners
454 * are registered, or if action events were specifically enabled
455 * using <code>enableEvents()</code>.
456 *
457 * @param event The event to process.
458 */
459protected void
460processActionEvent(ActionEvent event)
461{
462 if (action_listeners != null)
463 action_listeners.actionPerformed(event);
464}
465
466void
467dispatchEventImpl(AWTEvent e)
468{
469 if (e.id <= ActionEvent.ACTION_LAST
470 && e.id >= ActionEvent.ACTION_FIRST
471 && (action_listeners != null
472 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
473 processEvent(e);
474 else
475 super.dispatchEventImpl(e);
476}
477
478/*************************************************************************/
479
480/**
481 * Returns a debug string for this object.
482 *
483 * @return A debug string for this object.
484 */
485protected String
486paramString()
487{
488 return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
489 getEchoChar());
490}
491
492} // class TextField
Note: See TracBrowser for help on using the repository browser.