jdk/src/share/classes/javax/swing/InputVerifier.java
author rriggs
Tue, 03 Jun 2014 11:18:03 -0400
changeset 25122 1ecc464c69d2
parent 23010 6dadb192ad81
permissions -rw-r--r--
8044460: Cleanup new Boolean and single character strings Reviewed-by: pchelko, serb, rriggs Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21982
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The purpose of this class is to help clients support smooth focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * navigation through GUIs with text fields. Such GUIs often need
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * to ensure that the text entered by the user is valid (for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * that it's in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * the proper format) before allowing the user to navigate out of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the text field. To do this, clients create a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>InputVerifier</code> and, using <code>JComponent</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>setInputVerifier</code> method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * attach an instance of their subclass to the <code>JComponent</code> whose input they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * want to validate. Before focus is transfered to another Swing component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * that requests it, the input verifier's <code>shouldYieldFocus</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * called.  Focus is transfered only if that method returns <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The following example has two text fields, with the first one expecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the string "pass" to be entered by the user. If that string is entered in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the first text field, then the user can advance to the second text field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * either by clicking in it or by pressing TAB. However, if another string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * is entered in the first text field, then the user will be unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * transfer focus to the second text field.
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 5506
diff changeset
    50
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * // This program demonstrates the use of the Swing InputVerifier class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * // It creates two text fields; the first of the text fields expects the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * // string "pass" as input, and will allow focus to advance out of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * // only after that string is typed in by the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * public class VerifierTest extends JFrame {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     public VerifierTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         JTextField tf1 = new JTextField ("Type \"pass\" here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *         getContentPane().add (tf1, BorderLayout.NORTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *         tf1.setInputVerifier(new PassVerifier());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         JTextField tf2 = new JTextField ("TextField2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *         getContentPane().add (tf2, BorderLayout.SOUTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *         WindowListener l = new WindowAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *             public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *                 System.exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *         addWindowListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     class PassVerifier extends InputVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *         public boolean verify(JComponent input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *             JTextField tf = (JTextField) input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *             return "pass".equals(tf.getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *         Frame f = new VerifierTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *         f.pack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *         f.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *  @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
public abstract class InputVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
   * Checks whether the JComponent's input is valid. This method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
   * have no side effects. It returns a boolean indicating the status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
   * of the argument's input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
   * @param input the JComponent to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
   * @return <code>true</code> when valid, <code>false</code> when invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
   * @see JComponent#setInputVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
   * @see JComponent#getInputVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  public abstract boolean verify(JComponent input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
   * Calls <code>verify(input)</code> to ensure that the input is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
   * This method can have side effects. In particular, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
   * is called when the user attempts to advance focus out of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
   * argument component into another Swing component in this window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
   * If this method returns <code>true</code>, then the focus is transfered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
   * normally; if it returns <code>false</code>, then the focus remains in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
   * the argument component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
   * @param input the JComponent to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
   * @return <code>true</code> when valid, <code>false</code> when invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
   * @see JComponent#setInputVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
   * @see JComponent#getInputVerifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
   *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
  public boolean shouldYieldFocus(JComponent input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    return verify(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}