1 /*
   2  * Copyright (c) 1995, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.awt;
  27 
  28 import java.awt.*;
  29 
  30 /**
  31  * FocusingTextField: a subclass of java.awt.TextField that handles moving the
  32  * input focus from field to field, as when the user hits 'return.'
  33  *
  34  * @author Herb Jellinek
  35  */
  36 
  37 public class FocusingTextField extends TextField {
  38 
  39     /** The field to move to on 'return' - can be null. */
  40     TextField next;
  41 
  42     /** If true, select the contents of the field when it gets the focus. */
  43     boolean willSelect;
  44 
  45     /**
  46      * Create a FocusingTextField.
  47      * @param cols number of columns of text.
  48      */
  49     public FocusingTextField(int cols) {
  50         super("", cols);
  51     }
  52 
  53     /**
  54      * Create a FocusingTextField.
  55      * @param cols number of columns of text.
  56      * @param willSelect if true, will select all contents of field when
  57      * focus is gained.
  58      */
  59     public FocusingTextField(int cols, boolean willSelect) {
  60         this(cols);
  61         this.willSelect = willSelect;
  62     }
  63 
  64     public void setWillSelect(boolean will) {
  65         willSelect = will;
  66     }
  67 
  68     public boolean getWillSelect() {
  69         return willSelect;
  70     }
  71 
  72     /**
  73      * Call this to set the next field to receive the input focus.
  74      * @param next the next TextField in order - can be null.
  75      */
  76     public void setNextField(TextField next) {
  77         this.next = next;
  78     }
  79 
  80     /**
  81      * We got the focus.  If willSelect is true, select everything.
  82      */
  83     public boolean gotFocus(Event e, Object arg) {
  84         if (willSelect) {
  85             select(0, getText().length());
  86         }
  87         return true;
  88     }
  89 
  90     /**
  91      * We lost the focus.  If willSelect is true, deselect everything.
  92      */
  93     public boolean lostFocus(Event e, Object arg) {
  94         if (willSelect) {
  95             select(0, 0);
  96         }
  97         return true;
  98     }
  99 
 100     /**
 101      * Pass the focus to the next guy, if any.
 102      */
 103     public void nextFocus() {
 104         if (next != null) {
 105             next.requestFocus();
 106         }
 107         super.nextFocus();
 108     }
 109 }