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