Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/swing/InputVerifier.java
41153 views
1
/*
2
* Copyright (c) 1999, 2016, 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 javax.swing;
27
28
import java.util.*;
29
30
/**
31
* This class provides the validation mechanism for Swing components. GUIs often
32
* need to ensure that the components are in a valid state before allowing the
33
* user to navigate the input focus. To do this, clients create a subclass of
34
* {@code InputVerifier} and, using {@code JComponent}'s
35
* {@code setInputVerifier} method, attach an instance of their subclass to
36
* the {@code JComponent} which is the source of the focus transfer operation.
37
* The {@code InputVerifier} also provides the possibility to validate against
38
* the target of the focus transfer which may reject the focus.
39
* Before focus is transferred from the source Swing component to the target
40
* Swing component, the input verifier's
41
* {@code shouldYieldFocus(source, target)} method is called. Focus is
42
* transferred only if that method returns
43
* {@code true}.
44
* <p>
45
* The following example has two text fields, with the first one expecting
46
* the string "pass" to be entered by the user. If either that string is entered
47
* in the first text field or the second text field contains "accept" string,
48
* then the user can advance focus to the second text field by clicking in it or
49
* by pressing TAB.
50
* However, if another string is entered in the first text field and the second
51
* text field does not contain "accept", then the user will be unable to
52
* transfer focus to the second text field.
53
*
54
* <pre>
55
* import java.awt.*;
56
* import javax.swing.*;
57
*
58
* // This program demonstrates the use of the Swing InputVerifier class.
59
* // It creates two text fields; the first of the text fields expects the
60
* // string "pass" as input, and will allow focus to advance to the second text
61
* // field if either that string is typed in by the user or the second
62
* // field contains "accept" string.
63
*
64
* public class VerifierTest extends JFrame {
65
*
66
* public VerifierTest() {
67
* JTextField field1 = new JTextField("Type \"pass\" here");
68
* JTextField field2 = new JTextField("or \"accept\" here");
69
* getContentPane().add(field1, BorderLayout.NORTH);
70
* getContentPane().add(field2, BorderLayout.SOUTH);
71
*
72
* field1.setInputVerifier(new InputVerifier() {
73
* public boolean verify(JComponent input) {
74
* return "pass".equals(((JTextField) input).getText());
75
* }
76
*
77
* public boolean verifyTarget(JComponent input) {
78
* return "accept".equals(((JTextField) input).getText());
79
* }
80
*
81
* public boolean shouldYieldFocus(JComponent source,
82
* JComponent target) {
83
* return verify(source) || verifyTarget(target);
84
* }
85
* });
86
*
87
* pack();
88
* setVisible(true);
89
* }
90
*
91
* public static void main(String[] args) {
92
* SwingUtilities.invokeLater(VerifierTest::new);
93
* }
94
* }
95
* </pre>
96
*
97
* @since 1.3
98
*/
99
public abstract class InputVerifier {
100
101
/**
102
* Constructor for subclasses to call.
103
*/
104
protected InputVerifier() {}
105
106
/**
107
* Checks whether the JComponent's input is valid. This method should
108
* have no side effects. It returns a boolean indicating the status
109
* of the argument's input.
110
*
111
* @param input the JComponent to verify
112
* @return {@code true} when valid, {@code false} when invalid
113
* @see JComponent#setInputVerifier
114
* @see JComponent#getInputVerifier
115
*/
116
public abstract boolean verify(JComponent input);
117
118
/**
119
* Calls {@code verify(input)} to ensure that the input is valid.
120
* This method can have side effects. In particular, this method
121
* is called when the user attempts to advance focus out of the
122
* argument component into another Swing component in this window.
123
* If this method returns {@code true}, then the focus is transferred
124
* normally; if it returns {@code false}, then the focus remains in
125
* the argument component.
126
*
127
* @param input the JComponent to verify
128
* @return {@code true} when valid, {@code false} when invalid
129
* @see JComponent#setInputVerifier
130
* @see JComponent#getInputVerifier
131
*
132
* @deprecated use {@link #shouldYieldFocus(JComponent, JComponent)}
133
* instead.
134
*/
135
@Deprecated(since = "9")
136
public boolean shouldYieldFocus(JComponent input) {
137
return verify(input);
138
}
139
140
/**
141
* Checks whether the target JComponent that will be receiving the focus
142
* is ready to accept it. This method should be over-ridden only if it is
143
* necessary to validate the target of the focus transfer.
144
* This method should have no side effects. It returns a boolean
145
* indicating the status of the argument's input.
146
*
147
* @implSpec By default this method returns {@code true}.
148
*
149
* @param target the target JComponent to verify
150
* @return {@code true} when valid, {@code false} when invalid
151
* @see JComponent#setInputVerifier
152
* @see JComponent#getInputVerifier
153
* @since 9
154
*/
155
public boolean verifyTarget(JComponent target) {
156
return true;
157
}
158
159
/**
160
* Is called by Swing if this {@code InputVerifier} is assigned to the
161
* {@code source} Swing component to check whether the requested focus
162
* transfer from the {@code source} to {@code target} is allowed.
163
* This method can have side effects.
164
* If this method returns {@code true}, then the focus is transferred
165
* normally; if it returns {@code false}, then the focus remains in
166
* the first argument component.
167
*
168
* @implSpec The basic implementation of this method returns the conjunction
169
* of results obtained from {@code verify(input)} and
170
* {@code verifyTarget(input)} to ensure that both the source and the target
171
* components are in valid state.
172
*
173
* @param source the source JComponent of the focus transfer
174
* @param target the target JComponent of the focus transfer
175
* @return {@code true} when valid, {@code false} when invalid
176
* @see JComponent#setInputVerifier
177
* @see JComponent#getInputVerifier
178
* @since 9
179
*/
180
public boolean shouldYieldFocus(JComponent source, JComponent target) {
181
return shouldYieldFocus(source) && verifyTarget(target);
182
}
183
}
184
185