Source for java.awt.TextField

   1: /* TextField.java -- A one line text entry field
   2:    Copyright (C) 1999, 2002, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package java.awt;
  40: 
  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;
  46: 
  47: import javax.accessibility.AccessibleContext;
  48: import javax.accessibility.AccessibleStateSet;
  49: 
  50: /**
  51:   * This class implements a single line text entry field widget
  52:   *
  53:   * @author Aaron M. Renn (arenn@urbanophile.com)
  54:   */
  55: public class TextField extends TextComponent
  56: {
  57: 
  58: /*
  59:  * Static Variables
  60:  */
  61: 
  62: // Serialization constant
  63: private static final long serialVersionUID = -2966288784432217853L;
  64: 
  65: /*************************************************************************/
  66: 
  67: /*
  68:  * Instance Variables
  69:  */
  70: 
  71: /**
  72:   * @serial The number of columns in the text entry field.
  73:   */
  74: private int columns;
  75: 
  76: /**
  77:   * @serial The character that is echoed when doing protected input
  78:   */
  79: private char echoChar;
  80: 
  81: // List of registered ActionListener's for this object.
  82: private ActionListener action_listeners;
  83: 
  84: /*************************************************************************/
  85: 
  86: /*
  87:  * Constructors
  88:  */
  89: 
  90: /**
  91:  * Initializes a new instance of <code>TextField</code> that is empty
  92:  * and has one column.
  93:  *
  94:  * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
  95:  */
  96: public
  97: TextField()
  98: {
  99:   this("", 1);
 100: }
 101: 
 102: /*************************************************************************/
 103: 
 104: /**
 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.
 108:   *
 109:   * @param text The text to display in the field.
 110:   *
 111:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
 112:   */
 113: public
 114: TextField(String text)
 115: {
 116:   this(text, text.length());
 117: }
 118: 
 119: /*************************************************************************/
 120: 
 121: /**
 122:   * Initializes a new instance of <code>TextField</code> that is empty
 123:   * and has the specified number of columns.
 124:   *
 125:   * @param columns The number of columns in the text field.
 126:   *
 127:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
 128:   */
 129: public
 130: TextField(int columns)
 131: {
 132:   this("", columns);
 133: }
 134: 
 135: /*************************************************************************/
 136: 
 137: /**
 138:   * Initializes a new instance of <code>TextField</code> with the
 139:   * specified text and number of columns.
 140:   *
 141:   * @param text The text to display in the field.
 142:   * @param columns The number of columns in the field.
 143:   *
 144:   * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true,
 145:   */
 146: public
 147: TextField(String text, int columns)
 148: {
 149:   super(text);
 150:   this.columns = columns;
 151: 
 152:   if (GraphicsEnvironment.isHeadless())
 153:     throw new HeadlessException ();
 154: }
 155: 
 156: /*************************************************************************/
 157: 
 158: /*
 159:  * Instance Methods
 160:  */
 161: 
 162: /**
 163:   * Returns the number of columns in the field.
 164:   *
 165:   * @return The number of columns in the field.
 166:   */
 167: public int
 168: getColumns()
 169: {
 170:   return(columns);
 171: }
 172: 
 173: /*************************************************************************/
 174: 
 175: /**
 176:   * Sets the number of columns in this field to the specified value.
 177:   *
 178:   * @param columns The new number of columns in the field.
 179:   *
 180:   * @exception IllegalArgumentException If columns is less than zero.
 181:   */
 182: public synchronized void
 183: setColumns(int columns)
 184: {
 185:   if (columns < 0)
 186:     throw new IllegalArgumentException("Value is less than zero: " +
 187:                                        columns);
 188: 
 189:   this.columns = columns;
 190:   // FIXME: How to we communicate this to our peer?
 191: }
 192: 
 193: /*************************************************************************/
 194: 
 195: /**
 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).
 198:   *
 199:   * @return The echo character for this text field.
 200:   */
 201: public char
 202: getEchoChar()
 203: {
 204:   return(echoChar);
 205: }
 206: 
 207: /*************************************************************************/
 208: 
 209: /**
 210:   * Sets the character that is echoed when protected input such as
 211:   * a password is displayed.
 212:   *
 213:   * @param echoChar The new echo character.
 214:   */
 215: public void
 216: setEchoChar(char echoChar)
 217: {
 218:   setEchoCharacter (echoChar);
 219: }
 220: 
 221: /*************************************************************************/
 222: 
 223: /**
 224:   * Sets the character that is echoed when protected input such as
 225:   * a password is displayed.
 226:   *
 227:   * @param echoChar The new echo character.
 228:   *
 229:   * @deprecated This method is deprecated in favor of 
 230:   * <code>setEchoChar()</code>
 231:   */
 232: public void
 233: setEchoCharacter(char echoChar)
 234: {
 235:   this.echoChar = echoChar;
 236: 
 237:   TextFieldPeer peer = (TextFieldPeer) getPeer ();
 238:   if (peer != null)
 239:     peer.setEchoChar (echoChar);
 240: }
 241: 
 242: /*************************************************************************/
 243: 
 244: /**
 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.
 247:   *
 248:   * @return <code>true</code> if an echo character is set,
 249:   * <code>false</code> otherwise.
 250:   */
 251: public boolean
 252: echoCharIsSet()
 253: {
 254:   if (echoChar == '\u0000')
 255:     return(false);
 256:   else
 257:     return(true);
 258: }
 259: 
 260: /*************************************************************************/
 261: 
 262: /**
 263:   * Returns the minimum size for this text field.
 264:   *
 265:   * @return The minimum size for this text field.
 266:   */
 267: public Dimension
 268: getMinimumSize()
 269: {
 270:   return getMinimumSize (getColumns ());
 271: }
 272: 
 273: /*************************************************************************/
 274: 
 275: /**
 276:   * Returns the minimum size of a text field with the specified number
 277:   * of columns.
 278:   *
 279:   * @param columns The number of columns to get the minimum size for.
 280:   */
 281: public Dimension
 282: getMinimumSize(int columns)
 283: {
 284:   return minimumSize (columns);
 285: }
 286: 
 287: /*************************************************************************/
 288: 
 289: /**
 290:   * Returns the minimum size for this text field.
 291:   *
 292:   * @return The minimum size for this text field.
 293:   *
 294:   * @deprecated This method is deprecated in favor of
 295:   * <code>getMinimumSize()</code>.
 296:   */
 297: public Dimension
 298: minimumSize()
 299: {
 300:   return minimumSize (getColumns ());
 301: }
 302: 
 303: /*************************************************************************/
 304: 
 305: /**
 306:   * Returns the minimum size of a text field with the specified number
 307:   * of columns.
 308:   *
 309:   * @param columns The number of columns to get the minimum size for.
 310:   *
 311:   * @deprecated This method is deprecated in favor of 
 312:   * <code>getMinimumSize(int)</code>.
 313:   */
 314: public Dimension
 315: minimumSize(int columns)
 316: {
 317:   TextFieldPeer peer = (TextFieldPeer) getPeer ();
 318:   if (peer == null)
 319:     return null; // FIXME: What do we do if there is no peer?
 320: 
 321:   return peer.getMinimumSize (columns);
 322: }
 323: 
 324: /*************************************************************************/
 325: 
 326: /**
 327:   * Returns the preferred size for this text field.
 328:   *
 329:   * @return The preferred size for this text field.
 330:   */
 331: public Dimension
 332: getPreferredSize()
 333: {
 334:   return getPreferredSize (getColumns ());
 335: }
 336: 
 337: /*************************************************************************/
 338: 
 339: /**
 340:   * Returns the preferred size of a text field with the specified number
 341:   * of columns.
 342:   *
 343:   * @param columns The number of columns to get the preferred size for.
 344:   */
 345: public Dimension
 346: getPreferredSize(int columns)
 347: {
 348:   return preferredSize (columns);
 349: }
 350: 
 351: /*************************************************************************/
 352: 
 353: /**
 354:   * Returns the preferred size for this text field.
 355:   *
 356:   * @return The preferred size for this text field.
 357:   *
 358:   * @deprecated This method is deprecated in favor of 
 359:   * <code>getPreferredSize()</code>.
 360:   */
 361: public Dimension
 362: preferredSize()
 363: {
 364:   return preferredSize (getColumns ());
 365: }
 366: 
 367: /*************************************************************************/
 368: 
 369: /**
 370:   * Returns the preferred size of a text field with the specified number
 371:   * of columns.
 372:   *
 373:   * @param columns The number of columns to get the preferred size for.
 374:   *
 375:   * @deprecated This method is deprecated in favor of 
 376:   * <code>getPreferredSize(int)</code>.
 377:   */
 378: public Dimension
 379: preferredSize(int columns)
 380: {
 381:   TextFieldPeer peer = (TextFieldPeer) getPeer ();
 382:   if (peer == null)
 383:     return new Dimension (0, 0);
 384: 
 385:   return peer.getPreferredSize (columns);
 386: }
 387: 
 388: /*************************************************************************/
 389: 
 390: /**
 391:   * Notifies this object that it should create its native peer.
 392:   */
 393: public void
 394: addNotify()
 395: {
 396:   if (getPeer() != null)
 397:     return;
 398: 
 399:   setPeer((ComponentPeer)getToolkit().createTextField(this));
 400: }
 401: 
 402: /*************************************************************************/
 403: 
 404: /**
 405:   * Addes a new listener to the list of action listeners for this
 406:   * object.
 407:   *
 408:   * @param listener The listener to add to the list.
 409:   */
 410: public synchronized void
 411: addActionListener(ActionListener listener)
 412: {
 413:   action_listeners = AWTEventMulticaster.add(action_listeners, listener);
 414: 
 415:   enableEvents(AWTEvent.ACTION_EVENT_MASK);
 416: }
 417: 
 418: /*************************************************************************/
 419: 
 420: /**
 421:   * Removes the specified listener from the list of action listeners
 422:   * for this object.
 423:   *
 424:   * @param listener The listener to remove from the list.
 425:   */
 426: public synchronized void
 427: removeActionListener(ActionListener listener)
 428: {
 429:   action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
 430: }
 431: 
 432: /*************************************************************************/
 433: 
 434: /**
 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.
 439:   *
 440:   * @param event The event to process.
 441:   */
 442: protected void
 443: processEvent(AWTEvent event)
 444: {
 445:   if (event instanceof ActionEvent)
 446:     processActionEvent((ActionEvent)event);
 447:   else
 448:     super.processEvent(event);
 449: }
 450: 
 451: /*************************************************************************/
 452: 
 453: /**
 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>.
 459:   * 
 460:   * @param event The event to process.
 461:   */
 462: protected void
 463: processActionEvent(ActionEvent event)
 464: {
 465:   if (action_listeners != null)
 466:     action_listeners.actionPerformed(event);
 467: }
 468: 
 469: void
 470: dispatchEventImpl(AWTEvent e)
 471: {
 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);
 479: }
 480: 
 481: /*************************************************************************/
 482: 
 483: /**
 484:   * Returns a debug string for this object.
 485:   *
 486:   * @return A debug string for this object.
 487:   */
 488: protected String
 489: paramString()
 490: {
 491:   return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
 492:          getEchoChar());
 493: }
 494: 
 495:   /**
 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.
 499:    *
 500:    * @exception ClassCastException If listenerType doesn't specify a class or
 501:    * interface that implements java.util.EventListener.
 502:    *
 503:    * @since 1.3
 504:    */
 505:   public EventListener[] getListeners (Class listenerType)
 506:   {
 507:     if (listenerType == ActionListener.class)
 508:       return AWTEventMulticaster.getListeners (action_listeners, listenerType);
 509: 
 510:     return super.getListeners (listenerType);
 511:   }
 512: 
 513:   /**
 514:    * Return all ActionListeners register to this <code>TextField</code> object
 515:    * as an array.
 516:    *
 517:    * @since 1.4
 518:    */
 519:   public ActionListener[] getActionListeners ()
 520:   {
 521:     return (ActionListener[]) getListeners (ActionListener.class);
 522:   }
 523: 
 524:   protected class AccessibleAWTTextField extends AccessibleAWTTextComponent
 525:   {
 526:     private static final long serialVersionUID = 6219164359235943158L;
 527: 
 528:     protected AccessibleAWTTextField()
 529:     {
 530:     }
 531:     
 532:     public AccessibleStateSet getAccessibleStateSet()
 533:     {
 534:       return super.getAccessibleStateSet();
 535:     }
 536:   }
 537:   
 538:   public AccessibleContext getAccessibleContext()
 539:   {
 540:     return new AccessibleAWTTextField();
 541:   }
 542: 
 543: } // class TextField