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