1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.banking.util.swing;
22
23 import java.text.ParseException;
24 import javax.swing.JFormattedTextField;
25 import javax.swing.JFormattedTextField.AbstractFormatter;
26 import javax.swing.text.AttributeSet;
27 import javax.swing.text.BadLocationException;
28 import javax.swing.text.DocumentFilter;
29 import org.jdtaus.banking.AlphaNumericText27;
30 import org.jdtaus.core.container.ContainerFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public final class AlphaNumericText27TextField extends JFormattedTextField
45 {
46
47
48 private static final long serialVersionUID = -8152767220100367519L;
49
50
51
52
53
54 private Boolean normalizing;
55
56
57
58
59
60 private Boolean validating;
61
62
63 public AlphaNumericText27TextField()
64 {
65 super();
66 this.setColumns( AlphaNumericText27.MAX_LENGTH );
67 this.setFormatterFactory( new AbstractFormatterFactory()
68 {
69
70 public AbstractFormatter getFormatter( final JFormattedTextField ftf )
71 {
72 return new AbstractFormatter()
73 {
74
75 public Object stringToValue( final String text ) throws ParseException
76 {
77 Object value = null;
78
79 if ( text != null && text.trim().length() > 0 )
80 {
81 value = AlphaNumericText27.parse(
82 isNormalizing() ? AlphaNumericText27.normalize( text ) : text );
83
84 }
85
86 return value;
87 }
88
89 public String valueToString( final Object value ) throws ParseException
90 {
91 String ret = null;
92
93 if ( value instanceof AlphaNumericText27 )
94 {
95 final AlphaNumericText27 txt = (AlphaNumericText27) value;
96 ret = txt.isEmpty() ? null : txt.format().trim();
97 }
98
99 return ret;
100 }
101
102 protected DocumentFilter getDocumentFilter()
103 {
104 return new DocumentFilter()
105 {
106
107 public void insertString( final FilterBypass fb, final int o, String s,
108 final AttributeSet a ) throws BadLocationException
109 {
110 if ( isValidating() )
111 {
112 if ( isNormalizing() )
113 {
114 final char[] chars = s.toCharArray();
115 for ( int i = chars.length - 1; i >= 0; i-- )
116 {
117 chars[i] = Character.toUpperCase( chars[i] );
118 }
119 s = String.valueOf( chars );
120 }
121
122 final StringBuffer b =
123 new StringBuffer( fb.getDocument().getLength() + s.length() );
124
125 b.append( fb.getDocument().getText( 0, fb.getDocument().getLength() ) );
126 b.insert( o, s );
127
128 try
129 {
130 AlphaNumericText27.parse( b.toString() );
131 }
132 catch ( ParseException e )
133 {
134 invalidEdit();
135 return;
136 }
137 }
138
139 super.insertString( fb, o, s, a );
140 }
141
142 public void replace( final FilterBypass fb, final int o, final int l, String s,
143 final AttributeSet a ) throws BadLocationException
144 {
145 if ( isValidating() )
146 {
147 if ( isNormalizing() )
148 {
149 final char[] chars = s.toCharArray();
150 for ( int i = chars.length - 1; i >= 0; i-- )
151 {
152 chars[i] = Character.toUpperCase( chars[i] );
153 }
154 s = String.valueOf( chars );
155 }
156
157 final StringBuffer b = new StringBuffer(
158 fb.getDocument().getText( 0, fb.getDocument().getLength() ) );
159
160 b.delete( o, o + l );
161
162 if ( s != null )
163 {
164 b.insert( o, s );
165 }
166
167 try
168 {
169 AlphaNumericText27.parse( b.toString() );
170 }
171 catch ( ParseException e )
172 {
173 invalidEdit();
174 return;
175 }
176 }
177
178 super.replace( fb, o, l, s, a );
179 }
180
181 };
182 }
183
184 };
185 }
186
187 } );
188 }
189
190
191
192
193
194
195 public AlphaNumericText27 getAlphaNumericText27()
196 {
197 return (AlphaNumericText27) this.getValue();
198 }
199
200
201
202
203
204
205
206 public boolean isNormalizing()
207 {
208 if ( this.normalizing == null )
209 {
210 this.normalizing = this.isDefaultNormalizing();
211 }
212
213 return this.normalizing.booleanValue();
214 }
215
216
217
218
219
220
221
222 public void setNormalizing( final boolean value )
223 {
224 this.normalizing = Boolean.valueOf( value );
225 }
226
227
228
229
230
231
232
233 public boolean isValidating()
234 {
235 if ( this.validating == null )
236 {
237 this.validating = this.isDefaultValidating();
238 }
239
240 return this.validating.booleanValue();
241 }
242
243
244
245
246
247
248 public void setValidating( boolean value )
249 {
250 this.validating = Boolean.valueOf( value );
251 }
252
253
254
255
256
257
258
259
260
261
262
263 private java.lang.Boolean isDefaultValidating()
264 {
265 return (java.lang.Boolean) ContainerFactory.getContainer().
266 getProperty( this, "defaultValidating" );
267
268 }
269
270
271
272
273
274
275 private java.lang.Boolean isDefaultNormalizing()
276 {
277 return (java.lang.Boolean) ContainerFactory.getContainer().
278 getProperty( this, "defaultNormalizing" );
279
280 }
281
282
283
284
285 }