KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > panels > RuleTextField


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2002 Elmar Grom
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.panels;
23
24 import java.awt.Toolkit JavaDoc;
25
26 import javax.swing.JTextField JavaDoc;
27 import javax.swing.text.AttributeSet JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import javax.swing.text.PlainDocument JavaDoc;
31
32 /*---------------------------------------------------------------------------*/
33 /**
34  * One line synopsis. <BR>
35  * <BR>
36  * Enter detailed class description here.
37  *
38  * @see UserInputPanel
39  *
40  * @version 0.0.1 / 10/20/02
41  * @author Elmar Grom
42  */

43 /*---------------------------------------------------------------------------*/
44 public class RuleTextField extends JTextField JavaDoc
45 {
46
47     /**
48      *
49      */

50     private static final long serialVersionUID = 3976731454594365493L;
51
52     /** Used to specify numeric input only */
53     public static final int N = 1;
54
55     /** Used to specify hexadecimal input only */
56     public static final int H = 2;
57
58     /** Used to specify alphabetic input only */
59     public static final int A = 3;
60
61     /** Used to specify open input (no restrictions) */
62     public static final int O = 4;
63
64     /** Used to specify alpha-numeric input only */
65     public static final int AN = 5;
66
67     private int columns;
68
69     private int editLength;
70
71     private boolean unlimitedEdit;
72
73     private Toolkit JavaDoc toolkit;
74
75     public RuleTextField(int digits, int editLength, int type, boolean unlimitedEdit,
76                          Toolkit JavaDoc toolkit)
77     {
78         super(digits + 1);
79
80         setColumns(digits);
81         this.toolkit = toolkit;
82         this.editLength = editLength;
83         this.unlimitedEdit = unlimitedEdit;
84         Rule rule = new Rule();
85         rule.setRuleType(type, editLength, unlimitedEdit);
86         setDocument(rule);
87     }
88
89     protected Document JavaDoc createDefaultModel()
90     {
91         Rule rule = new Rule();
92         return (rule);
93     }
94
95     public int getColumns()
96     {
97         return (columns);
98     }
99
100     public int getEditLength()
101     {
102         return (editLength);
103     }
104
105     public boolean unlimitedEdit()
106     {
107         return (unlimitedEdit);
108     }
109
110     public void setColumns(int columns)
111     {
112         super.setColumns(columns + 1);
113         this.columns = columns;
114     }
115
116     // --------------------------------------------------------------------------
117
//
118
// --------------------------------------------------------------------------
119

120     class Rule extends PlainDocument JavaDoc
121     {
122
123         /**
124          *
125          */

126         private static final long serialVersionUID = 3258134643651063862L;
127
128         private int editLength;
129
130         private int type;
131
132         private boolean unlimitedEdit;
133
134         public void setRuleType(int type, int editLength, boolean unlimitedEdit)
135         {
136             this.type = type;
137             this.editLength = editLength;
138             this.unlimitedEdit = unlimitedEdit;
139         }
140
141         public void insertString(int offs, String JavaDoc str, AttributeSet JavaDoc a) throws BadLocationException JavaDoc
142         {
143             // --------------------------------------------------
144
// don't process if we get a null reference
145
// --------------------------------------------------
146
if (str == null) { return; }
147
148             // --------------------------------------------------
149
// Compute the total length the string would become
150
// if the insert request were be honored. If this
151
// size is within the specified limits, apply further
152
// rules, otherwise give an error signal and return.
153
// --------------------------------------------------
154
int totalSize = getLength() + str.length();
155
156             if ((totalSize <= editLength) || (unlimitedEdit))
157             {
158                 boolean error = false;
159
160                 // test for numeric type
161
if (type == N)
162                 {
163                     for (int i = 0; i < str.length(); i++)
164                     {
165                         if (!Character.isDigit(str.charAt(i)))
166                         {
167                             error = true;
168                         }
169                     }
170                 }
171                 // test for hex type
172
else if (type == H)
173                 {
174                     for (int i = 0; i < str.length(); i++)
175                     {
176                         char focusChar = Character.toUpperCase(str.charAt(i));
177                         if (!Character.isDigit(focusChar) && (focusChar != 'A')
178                                 && (focusChar != 'B') && (focusChar != 'C') && (focusChar != 'D')
179                                 && (focusChar != 'E') && (focusChar != 'F'))
180                         {
181                             error = true;
182                         }
183                     }
184                 }
185                 // test for alpha type
186
else if (type == A)
187                 {
188                     for (int i = 0; i < str.length(); i++)
189                     {
190                         if (!Character.isLetter(str.charAt(i)))
191                         {
192                             error = true;
193                         }
194                     }
195                 }
196                 // test for alpha-numeric type
197
else if (type == AN)
198                 {
199                     for (int i = 0; i < str.length(); i++)
200                     {
201                         if (!Character.isLetterOrDigit(str.charAt(i)))
202                         {
203                             error = true;
204                         }
205                     }
206                 }
207                 // test for 'open' -> no limiting rule at all
208
else if (type == O)
209                 {
210                     // let it slide...
211
}
212                 else
213                 {
214                     System.out.println("type = " + type);
215                 }
216
217                 // ------------------------------------------------
218
// if we had no error when applying the rules, we
219
// are ready to insert the string, otherwise give
220
// an error signal.
221
// ------------------------------------------------
222
if (!error)
223                 {
224                     super.insertString(offs, str, a);
225                 }
226                 else
227                 {
228                     toolkit.beep();
229                 }
230             }
231             else
232             {
233                 toolkit.beep();
234             }
235         }
236     }
237 }
238 /*---------------------------------------------------------------------------*/
239
Popular Tags