001: /*
002: * Copyright 1995 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.awt.*;
029:
030: /**
031: * FocusingTextField: a subclass of java.awt.TextField that handles moving the
032: * input focus from field to field, as when the user hits 'return.'
033: *
034: * @version 1.19, 05/05/07
035: * @author Herb Jellinek
036: */
037:
038: public class FocusingTextField extends TextField {
039:
040: /** The field to move to on 'return' - can be null. */
041: TextField next;
042:
043: /** If true, select the contents of the field when it gets the focus. */
044: boolean willSelect;
045:
046: /**
047: * Create a FocusingTextField.
048: * @param cols number of columns of text.
049: */
050: public FocusingTextField(int cols) {
051: super ("", cols);
052: }
053:
054: /**
055: * Create a FocusingTextField.
056: * @param cols number of columns of text.
057: * @param willSelect if true, will select all contents of field when
058: * focus is gained.
059: */
060: public FocusingTextField(int cols, boolean willSelect) {
061: this (cols);
062: this .willSelect = willSelect;
063: }
064:
065: public void setWillSelect(boolean will) {
066: willSelect = will;
067: }
068:
069: public boolean getWillSelect() {
070: return willSelect;
071: }
072:
073: /**
074: * Call this to set the next field to receive the input focus.
075: * @param next the next TextField in order - can be null.
076: */
077: public void setNextField(TextField next) {
078: this .next = next;
079: }
080:
081: /**
082: * We got the focus. If willSelect is true, select everything.
083: */
084: public boolean gotFocus(Event e, Object arg) {
085: if (willSelect) {
086: select(0, getText().length());
087: }
088: return true;
089: }
090:
091: /**
092: * We lost the focus. If willSelect is true, deselect everything.
093: */
094: public boolean lostFocus(Event e, Object arg) {
095: if (willSelect) {
096: select(0, 0);
097: }
098: return true;
099: }
100:
101: /**
102: * Pass the focus to the next guy, if any.
103: */
104: public void nextFocus() {
105: if (next != null) {
106: next.requestFocus();
107: }
108: super.nextFocus();
109: }
110: }
|