| 1 | /* TextField.java -- A one line text entry field
|
|---|
| 2 | Copyright (C) 1999, 2002 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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.TextFieldPeer;
|
|---|
| 44 | import java.awt.peer.TextComponentPeer;
|
|---|
| 45 | import 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 | */
|
|---|
| 52 | public class TextField extends TextComponent implements java.io.Serializable
|
|---|
| 53 | {
|
|---|
| 54 |
|
|---|
| 55 | /*
|
|---|
| 56 | * Static Variables
|
|---|
| 57 | */
|
|---|
| 58 |
|
|---|
| 59 | // Serialization constant
|
|---|
| 60 | private 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 | */
|
|---|
| 71 | private int columns;
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * @serial The character that is echoed when doing protected input
|
|---|
| 75 | */
|
|---|
| 76 | private char echoChar;
|
|---|
| 77 |
|
|---|
| 78 | // List of registered ActionListener's for this object.
|
|---|
| 79 | private 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 | */
|
|---|
| 93 | public
|
|---|
| 94 | TextField()
|
|---|
| 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 | */
|
|---|
| 110 | public
|
|---|
| 111 | TextField(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 | */
|
|---|
| 126 | public
|
|---|
| 127 | TextField(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 | */
|
|---|
| 143 | public
|
|---|
| 144 | TextField(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 | */
|
|---|
| 164 | public int
|
|---|
| 165 | getColumns()
|
|---|
| 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 | */
|
|---|
| 179 | public synchronized void
|
|---|
| 180 | setColumns(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 | */
|
|---|
| 198 | public char
|
|---|
| 199 | getEchoChar()
|
|---|
| 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 | */
|
|---|
| 212 | public void
|
|---|
| 213 | setEchoChar(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 | */
|
|---|
| 233 | public void
|
|---|
| 234 | setEchoCharacter(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 | */
|
|---|
| 248 | public boolean
|
|---|
| 249 | echoCharIsSet()
|
|---|
| 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 | */
|
|---|
| 264 | public Dimension
|
|---|
| 265 | getMinimumSize()
|
|---|
| 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 | */
|
|---|
| 278 | public Dimension
|
|---|
| 279 | getMinimumSize(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 | */
|
|---|
| 298 | public Dimension
|
|---|
| 299 | minimumSize()
|
|---|
| 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 | */
|
|---|
| 315 | public Dimension
|
|---|
| 316 | minimumSize(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 | */
|
|---|
| 328 | public Dimension
|
|---|
| 329 | getPreferredSize()
|
|---|
| 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 | */
|
|---|
| 342 | public Dimension
|
|---|
| 343 | getPreferredSize(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 | */
|
|---|
| 362 | public Dimension
|
|---|
| 363 | preferredSize()
|
|---|
| 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 | */
|
|---|
| 379 | public Dimension
|
|---|
| 380 | preferredSize(int columns)
|
|---|
| 381 | {
|
|---|
| 382 | return(getPreferredSize(columns));
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | /*************************************************************************/
|
|---|
| 386 |
|
|---|
| 387 | /**
|
|---|
| 388 | * Notifies this object that it should create its native peer.
|
|---|
| 389 | */
|
|---|
| 390 | public void
|
|---|
| 391 | addNotify()
|
|---|
| 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 | */
|
|---|
| 407 | public synchronized void
|
|---|
| 408 | addActionListener(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 | */
|
|---|
| 423 | public synchronized void
|
|---|
| 424 | removeActionListener(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 | */
|
|---|
| 439 | protected void
|
|---|
| 440 | processEvent(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 | */
|
|---|
| 459 | protected void
|
|---|
| 460 | processActionEvent(ActionEvent event)
|
|---|
| 461 | {
|
|---|
| 462 | if (action_listeners != null)
|
|---|
| 463 | action_listeners.actionPerformed(event);
|
|---|
| 464 | }
|
|---|
| 465 |
|
|---|
| 466 | void
|
|---|
| 467 | dispatchEventImpl(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 | */
|
|---|
| 485 | protected String
|
|---|
| 486 | paramString()
|
|---|
| 487 | {
|
|---|
| 488 | return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
|
|---|
| 489 | getEchoChar());
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | } // class TextField
|
|---|