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 =
158 new StringBuffer( fb.getDocument().getLength() + s.length() );
159
160 b.append( fb.getDocument().getText( 0, fb.getDocument().getLength() ) );
161 b.replace( o, o + s.length(), s );
162
163 try
164 {
165 AlphaNumericText27.parse( b.toString() );
166 }
167 catch ( ParseException e )
168 {
169 invalidEdit();
170 return;
171 }
172 }
173
174 super.replace( fb, o, l, s, a );
175 }
176
177 };
178 }
179
180 };
181 }
182
183 } );
184 }
185
186
187
188
189
190
191 public AlphaNumericText27 getAlphaNumericText27()
192 {
193 return (AlphaNumericText27) this.getValue();
194 }
195
196
197
198
199
200
201
202 public boolean isNormalizing()
203 {
204 if ( this.normalizing == null )
205 {
206 this.normalizing = this.isDefaultNormalizing();
207 }
208
209 return this.normalizing.booleanValue();
210 }
211
212
213
214
215
216
217
218 public void setNormalizing( final boolean value )
219 {
220 this.normalizing = Boolean.valueOf( value );
221 }
222
223
224
225
226
227
228
229 public boolean isValidating()
230 {
231 if ( this.validating == null )
232 {
233 this.validating = this.isDefaultValidating();
234 }
235
236 return this.validating.booleanValue();
237 }
238
239
240
241
242
243
244 public void setValidating( boolean value )
245 {
246 this.validating = Boolean.valueOf( value );
247 }
248
249
250
251
252
253
254
255
256
257
258
259 private java.lang.Boolean isDefaultValidating()
260 {
261 return (java.lang.Boolean) ContainerFactory.getContainer().
262 getProperty( this, "defaultValidating" );
263
264 }
265
266
267
268
269
270
271 private java.lang.Boolean isDefaultNormalizing()
272 {
273 return (java.lang.Boolean) ContainerFactory.getContainer().
274 getProperty( this, "defaultNormalizing" );
275
276 }
277
278
279
280
281 }