source: trunk/src/gcc/libjava/java/awt/TextField.java@ 736

Last change on this file since 736 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 11.7 KB
Line 
1/* TextField.java -- A one line text entry field
2 Copyright (C) 1999 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 ([email protected])
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 */
91public
92TextField()
93{
94 this("", 1);
95}
96
97/*************************************************************************/
98
99/**
100 * Initializes a new instance of <code>TextField</code> containing
101 * the specified text. The number of columns will be equal to the
102 * length of the text string.
103 *
104 * @param text The text to display in the field.
105 */
106public
107TextField(String text)
108{
109 this(text, text.length());
110}
111
112/*************************************************************************/
113
114/**
115 * Initializes a new instance of <code>TextField</code> that is empty
116 * and has the specified number of columns.
117 *
118 * @param columns The number of columns in the text field.
119 */
120public
121TextField(int columns)
122{
123 this("", columns);
124}
125
126/*************************************************************************/
127
128/**
129 * Initializes a new instance of <code>TextField</code> with the
130 * specified text and number of columns.
131 *
132 * @param text The text to display in the field.
133 * @param columns The number of columns in the field.
134 */
135public
136TextField(String text, int columns)
137{
138 super(text);
139 this.columns = columns;
140}
141
142/*************************************************************************/
143
144/*
145 * Instance Methods
146 */
147
148/**
149 * Returns the number of columns in the field.
150 *
151 * @return The number of columns in the field.
152 */
153public int
154getColumns()
155{
156 return(columns);
157}
158
159/*************************************************************************/
160
161/**
162 * Sets the number of columns in this field to the specified value.
163 *
164 * @param columns The new number of columns in the field.
165 *
166 * @exception IllegalArgumentException If columns is less than zero.
167 */
168public synchronized void
169setColumns(int columns)
170{
171 if (columns < 0)
172 throw new IllegalArgumentException("Value is less than zero: " +
173 columns);
174
175 this.columns = columns;
176 // FIXME: How to we communicate this to our peer?
177}
178
179/*************************************************************************/
180
181/**
182 * Returns the character that is echoed to the screen when a text
183 * field is protected (such as when a password is being entered).
184 *
185 * @return The echo character for this text field.
186 */
187public char
188getEchoChar()
189{
190 return(echoChar);
191}
192
193/*************************************************************************/
194
195/**
196 * Sets the character that is echoed when protected input such as
197 * a password is displayed.
198 *
199 * @param echoChar The new echo character.
200 */
201public void
202setEchoChar(char echoChar)
203{
204 this.echoChar = echoChar;
205
206 TextFieldPeer tfp = (TextFieldPeer)getPeer();
207 if (tfp != null)
208 tfp.setEchoChar(echoChar);
209}
210
211/*************************************************************************/
212
213/**
214 * Sets the character that is echoed when protected input such as
215 * a password is displayed.
216 *
217 * @param echoChar The new echo character.
218 *
219 * @deprecated This method is deprecated in favor of
220 * <code>setEchoChar()</code>
221 */
222public void
223setEchoCharacter(char echoChar)
224{
225 setEchoChar(echoChar);
226}
227
228/*************************************************************************/
229
230/**
231 * Tests whether or not this text field has an echo character set
232 * so that characters the user type are not echoed to the screen.
233 *
234 * @return <code>true</code> if an echo character is set,
235 * <code>false</code> otherwise.
236 */
237public boolean
238echoCharIsSet()
239{
240 if (echoChar == '\u0000')
241 return(false);
242 else
243 return(true);
244}
245
246/*************************************************************************/
247
248/**
249 * Returns the minimum size for this text field.
250 *
251 * @return The minimum size for this text field.
252 */
253public Dimension
254getMinimumSize()
255{
256 return(getMinimumSize(getColumns()));
257}
258
259/*************************************************************************/
260
261/**
262 * Returns the minimum size of a text field with the specified number
263 * of columns.
264 *
265 * @param columns The number of columns to get the minimum size for.
266 */
267public Dimension
268getMinimumSize(int columns)
269{
270 TextFieldPeer tfp = (TextFieldPeer)getPeer();
271 if (tfp == null)
272 return(null); // FIXME: What do we do if there is no peer?
273
274 return(tfp.getMinimumSize(columns));
275}
276
277/*************************************************************************/
278
279/**
280 * Returns the minimum size for this text field.
281 *
282 * @return The minimum size for this text field.
283 *
284 * @deprecated This method is depcreated in favor of
285 * <code>getMinimumSize()</code>.
286 */
287public Dimension
288minimumSize()
289{
290 return(getMinimumSize(getColumns()));
291}
292
293/*************************************************************************/
294
295/**
296 * Returns the minimum size of a text field with the specified number
297 * of columns.
298 *
299 * @param columns The number of columns to get the minimum size for.
300 *
301 * @deprecated This method is deprecated in favor of
302 * <code>getMinimumSize(int)</code>.
303 */
304public Dimension
305minimumSize(int columns)
306{
307 return(getMinimumSize(columns));
308}
309
310/*************************************************************************/
311
312/**
313 * Returns the preferred size for this text field.
314 *
315 * @return The preferred size for this text field.
316 */
317public Dimension
318getPreferredSize()
319{
320 return(getPreferredSize(getColumns()));
321}
322
323/*************************************************************************/
324
325/**
326 * Returns the preferred size of a text field with the specified number
327 * of columns.
328 *
329 * @param columns The number of columns to get the preferred size for.
330 */
331public Dimension
332getPreferredSize(int columns)
333{
334 TextFieldPeer tfp = (TextFieldPeer)getPeer();
335 if (tfp == null)
336 return(null); // FIXME: What do we do if there is no peer?
337
338 return(tfp.getPreferredSize(columns));
339}
340
341/*************************************************************************/
342
343/**
344 * Returns the preferred size for this text field.
345 *
346 * @return The preferred size for this text field.
347 *
348 * @deprecated This method is deprecated in favor of
349 * <code>getPreferredSize()</code>.
350 */
351public Dimension
352preferredSize()
353{
354 return(getPreferredSize(getColumns()));
355}
356
357/*************************************************************************/
358
359/**
360 * Returns the preferred size of a text field with the specified number
361 * of columns.
362 *
363 * @param columns The number of columns to get the preferred size for.
364 *
365 * @deprecated This method is deprecated in favor of
366 * <code>getPreferredSize(int)</code>.
367 */
368public Dimension
369preferredSize(int columns)
370{
371 return(getPreferredSize(columns));
372}
373
374/*************************************************************************/
375
376/**
377 * Notifies this object that it should create its native peer.
378 */
379public void
380addNotify()
381{
382 if (getPeer() != null)
383 return;
384
385 setPeer((ComponentPeer)getToolkit().createTextField(this));
386}
387
388/*************************************************************************/
389
390/**
391 * Addes a new listener to the list of action listeners for this
392 * object.
393 *
394 * @param listener The listener to add to the list.
395 */
396public synchronized void
397addActionListener(ActionListener listener)
398{
399 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
400
401 enableEvents(AWTEvent.ACTION_EVENT_MASK);
402}
403
404/*************************************************************************/
405
406/**
407 * Removes the specified listener from the list of action listeners
408 * for this object.
409 *
410 * @param listener The listener to remove from the list.
411 */
412public synchronized void
413removeActionListener(ActionListener listener)
414{
415 action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
416}
417
418/*************************************************************************/
419
420/**
421 * Processes the specified event. If the event is an instance of
422 * <code>ActionEvent</code> then <code>processActionEvent()</code> is
423 * called to process it, otherwise the event is sent to the
424 * superclass.
425 *
426 * @param event The event to process.
427 */
428protected void
429processEvent(AWTEvent event)
430{
431 if (event instanceof ActionEvent)
432 processActionEvent((ActionEvent)event);
433 else
434 super.processEvent(event);
435}
436
437/*************************************************************************/
438
439/**
440 * Processes an action event by calling any registered listeners.
441 * Note to subclasses: This method is not called unless action events
442 * are enabled on this object. This will be true if any listeners
443 * are registered, or if action events were specifically enabled
444 * using <code>enableEvents()</code>.
445 *
446 * @param event The event to process.
447 */
448protected void
449processActionEvent(ActionEvent event)
450{
451 if (action_listeners != null)
452 action_listeners.actionPerformed(event);
453}
454
455/*************************************************************************/
456
457/**
458 * Returns a debug string for this object.
459 *
460 * @return A debug string for this object.
461 */
462protected String
463paramString()
464{
465 return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
466 getEchoChar());
467}
468
469} // class TextField
Note: See TracBrowser for help on using the repository browser.