| 1 | /* TextField.java -- A one line text entry field
|
|---|
| 2 | Copyright (C) 1999 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 | public
|
|---|
| 92 | TextField()
|
|---|
| 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 | */
|
|---|
| 106 | public
|
|---|
| 107 | TextField(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 | */
|
|---|
| 120 | public
|
|---|
| 121 | TextField(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 | */
|
|---|
| 135 | public
|
|---|
| 136 | TextField(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 | */
|
|---|
| 153 | public int
|
|---|
| 154 | getColumns()
|
|---|
| 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 | */
|
|---|
| 168 | public synchronized void
|
|---|
| 169 | setColumns(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 | */
|
|---|
| 187 | public char
|
|---|
| 188 | getEchoChar()
|
|---|
| 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 | */
|
|---|
| 201 | public void
|
|---|
| 202 | setEchoChar(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 | */
|
|---|
| 222 | public void
|
|---|
| 223 | setEchoCharacter(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 | */
|
|---|
| 237 | public boolean
|
|---|
| 238 | echoCharIsSet()
|
|---|
| 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 | */
|
|---|
| 253 | public Dimension
|
|---|
| 254 | getMinimumSize()
|
|---|
| 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 | */
|
|---|
| 267 | public Dimension
|
|---|
| 268 | getMinimumSize(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 | */
|
|---|
| 287 | public Dimension
|
|---|
| 288 | minimumSize()
|
|---|
| 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 | */
|
|---|
| 304 | public Dimension
|
|---|
| 305 | minimumSize(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 | */
|
|---|
| 317 | public Dimension
|
|---|
| 318 | getPreferredSize()
|
|---|
| 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 | */
|
|---|
| 331 | public Dimension
|
|---|
| 332 | getPreferredSize(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 | */
|
|---|
| 351 | public Dimension
|
|---|
| 352 | preferredSize()
|
|---|
| 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 | */
|
|---|
| 368 | public Dimension
|
|---|
| 369 | preferredSize(int columns)
|
|---|
| 370 | {
|
|---|
| 371 | return(getPreferredSize(columns));
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | /*************************************************************************/
|
|---|
| 375 |
|
|---|
| 376 | /**
|
|---|
| 377 | * Notifies this object that it should create its native peer.
|
|---|
| 378 | */
|
|---|
| 379 | public void
|
|---|
| 380 | addNotify()
|
|---|
| 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 | */
|
|---|
| 396 | public synchronized void
|
|---|
| 397 | addActionListener(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 | */
|
|---|
| 412 | public synchronized void
|
|---|
| 413 | removeActionListener(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 | */
|
|---|
| 428 | protected void
|
|---|
| 429 | processEvent(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 | */
|
|---|
| 448 | protected void
|
|---|
| 449 | processActionEvent(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 | */
|
|---|
| 462 | protected String
|
|---|
| 463 | paramString()
|
|---|
| 464 | {
|
|---|
| 465 | return(getClass().getName() + "(columns=" + getColumns() + ",echoChar=" +
|
|---|
| 466 | getEchoChar());
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | } // class TextField
|
|---|