svn-gvsig-desktop / branches / F2 / extensions / extJCRS / install / IzPack / src / lib / com / izforge / izpack / panels / RuleTextField.java @ 20036
History | View | Annotate | Download (6.92 KB)
1 |
/*
|
---|---|
2 |
* $Id: RuleTextField.java 5819 2006-06-14 07:29:09Z cesar $
|
3 |
* Copyright (C) 2002 Elmar Grom
|
4 |
*
|
5 |
* File : RuleInputField.java
|
6 |
* Description : A Java component that serves as a text input field
|
7 |
* with the abilty to impose limitations on the type
|
8 |
* of data that can be entered.
|
9 |
* Author's email : elmar@grom.net
|
10 |
* Author's Website : http://www.izforge.com
|
11 |
*
|
12 |
* This program is free software; you can redistribute it and/or
|
13 |
* modify it under the terms of the GNU General Public License
|
14 |
* as published by the Free Software Foundation; either version 2
|
15 |
* of the License, or any later version.
|
16 |
*
|
17 |
* This program is distributed in the hope that it will be useful,
|
18 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
* GNU General Public License for more details.
|
21 |
*
|
22 |
* You should have received a copy of the GNU General Public License
|
23 |
* along with this program; if not, write to the Free Software
|
24 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
25 |
*/
|
26 |
|
27 |
package com.izforge.izpack.panels; |
28 |
|
29 |
import java.awt.Toolkit; |
30 |
|
31 |
import javax.swing.JTextField; |
32 |
import javax.swing.text.AttributeSet; |
33 |
import javax.swing.text.BadLocationException; |
34 |
import javax.swing.text.Document; |
35 |
import javax.swing.text.PlainDocument; |
36 |
|
37 |
/*---------------------------------------------------------------------------*/
|
38 |
/**
|
39 |
* One line synopsis.
|
40 |
* <BR><BR>
|
41 |
* Enter detailed class description here.
|
42 |
*
|
43 |
* @see UserInputPanel
|
44 |
*
|
45 |
* @version 0.0.1 / 10/20/02
|
46 |
* @author Elmar Grom
|
47 |
*/
|
48 |
/*---------------------------------------------------------------------------*/
|
49 |
public class RuleTextField extends JTextField |
50 |
{ |
51 |
/** Used to specify numeric input only */
|
52 |
public static final int N = 1; |
53 |
/** Used to specify hexadecimal input only */
|
54 |
public static final int H = 2; |
55 |
/** Used to specify alphabetic input only */
|
56 |
public static final int A = 3; |
57 |
/** Used to specify open input (no restrictions) */
|
58 |
public static final int O = 4; |
59 |
/** Used to specify alpha-numeric input only */
|
60 |
public static final int AN = 5; |
61 |
|
62 |
private int columns; |
63 |
private int editLength; |
64 |
private int type; |
65 |
private boolean unlimitedEdit; |
66 |
private Toolkit toolkit; |
67 |
private Rule rule;
|
68 |
|
69 |
public RuleTextField (int digits, |
70 |
int editLength,
|
71 |
int type,
|
72 |
boolean unlimitedEdit,
|
73 |
Toolkit toolkit)
|
74 |
{ |
75 |
super (digits + 1); |
76 |
|
77 |
columns = digits; |
78 |
this.toolkit = toolkit;
|
79 |
this.type = type;
|
80 |
this.editLength = editLength;
|
81 |
this.unlimitedEdit = unlimitedEdit;
|
82 |
rule = new Rule();
|
83 |
rule.setRuleType (type, editLength, unlimitedEdit); |
84 |
setDocument(rule); |
85 |
} |
86 |
|
87 |
protected Document createDefaultModel () |
88 |
{ |
89 |
rule = new Rule ();
|
90 |
return (rule);
|
91 |
} |
92 |
|
93 |
public int getColumns () |
94 |
{ |
95 |
return (columns);
|
96 |
} |
97 |
|
98 |
public int getEditLength () |
99 |
{ |
100 |
return (editLength);
|
101 |
} |
102 |
|
103 |
public boolean unlimitedEdit () |
104 |
{ |
105 |
return (unlimitedEdit);
|
106 |
} |
107 |
|
108 |
public void setColumns (int columns) |
109 |
{ |
110 |
super.setColumns (columns + 1); |
111 |
this.columns = columns;
|
112 |
} |
113 |
|
114 |
|
115 |
// --------------------------------------------------------------------------
|
116 |
//
|
117 |
// --------------------------------------------------------------------------
|
118 |
|
119 |
class Rule extends PlainDocument |
120 |
{ |
121 |
private int editLength; |
122 |
private int type; |
123 |
private boolean unlimitedEdit; |
124 |
|
125 |
public void setRuleType (int type, |
126 |
int editLength,
|
127 |
boolean unlimitedEdit)
|
128 |
{ |
129 |
this.type = type;
|
130 |
this.editLength = editLength;
|
131 |
this.unlimitedEdit = unlimitedEdit;
|
132 |
} |
133 |
|
134 |
public void insertString (int offs, |
135 |
String str,
|
136 |
AttributeSet a) throws BadLocationException |
137 |
{ |
138 |
// --------------------------------------------------
|
139 |
// don't process if we get a null reference
|
140 |
// --------------------------------------------------
|
141 |
if (str == null) |
142 |
{ |
143 |
return;
|
144 |
} |
145 |
|
146 |
// --------------------------------------------------
|
147 |
// Compute the total length the string would become
|
148 |
// if the insert request were be honored. If this
|
149 |
// size is within the specified limits, apply further
|
150 |
// rules, otherwise give an error signal and return.
|
151 |
// --------------------------------------------------
|
152 |
int totalSize = getLength () + str.length ();
|
153 |
|
154 |
if ((totalSize <= editLength) || (unlimitedEdit))
|
155 |
{ |
156 |
boolean error = false; |
157 |
|
158 |
// test for numeric type
|
159 |
if (type == N)
|
160 |
{ |
161 |
for (int i = 0; i < str.length (); i++) |
162 |
{ |
163 |
if (!Character.isDigit (str.charAt (i))) |
164 |
{ |
165 |
error = true;
|
166 |
} |
167 |
} |
168 |
} |
169 |
// test for hex type
|
170 |
else if (type == H) |
171 |
{ |
172 |
for (int i = 0; i < str.length (); i++) |
173 |
{ |
174 |
char focusChar = Character.toUpperCase (str.charAt (i)); |
175 |
if (!Character.isDigit (focusChar) && |
176 |
(focusChar != 'A') &&
|
177 |
(focusChar != 'B') &&
|
178 |
(focusChar != 'C') &&
|
179 |
(focusChar != 'D') &&
|
180 |
(focusChar != 'E') &&
|
181 |
(focusChar != 'F') )
|
182 |
{ |
183 |
error = true;
|
184 |
} |
185 |
} |
186 |
} |
187 |
// test for alpha type
|
188 |
else if (type == A) |
189 |
{ |
190 |
for (int i = 0; i < str.length (); i++) |
191 |
{ |
192 |
if (!Character.isLetter (str.charAt (i))) |
193 |
{ |
194 |
error = true;
|
195 |
} |
196 |
} |
197 |
} |
198 |
// test for alpha-numeric type
|
199 |
else if (type == AN) |
200 |
{ |
201 |
for (int i = 0; i < str.length (); i++) |
202 |
{ |
203 |
if (!Character.isLetterOrDigit (str.charAt (i))) |
204 |
{ |
205 |
error = true;
|
206 |
} |
207 |
} |
208 |
} |
209 |
// test for 'open' -> no limiting rule at all
|
210 |
else if (type == O) |
211 |
{ |
212 |
// let it slide...
|
213 |
} |
214 |
else
|
215 |
{ |
216 |
System.out.println ("type = " + type); |
217 |
} |
218 |
|
219 |
// ------------------------------------------------
|
220 |
// if we had no error when applying the rules, we
|
221 |
// are ready to insert the string, otherwise give
|
222 |
// an error signal.
|
223 |
// ------------------------------------------------
|
224 |
if (!error)
|
225 |
{ |
226 |
super.insertString (offs, str, a);
|
227 |
} |
228 |
else
|
229 |
{ |
230 |
toolkit.beep (); |
231 |
} |
232 |
} |
233 |
else
|
234 |
{ |
235 |
toolkit.beep (); |
236 |
} |
237 |
} |
238 |
} |
239 |
} |
240 |
/*---------------------------------------------------------------------------*/
|