View Javadoc

1   /*
2    *  jDTAUS Banking Utilities
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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   * {@code JFormattedTextField} supporting the {@code Referenznummer11} type.
36   * <p>This textfield uses the {@link Referenznummer11} type for parsing and formatting. An empty string value is treated
37   * as {@code null}. Property {@code format} controls formatting and takes one of the format constants defined in class
38   * {@code Referenznummer11}. By default the {@code ELECTRONIC_FORMAT} is used. The {@code validating} flag controls
39   * validation of values entered into the textfield. If {@code true} (default), a {@code DocumentFilter} is registered
40   * with the textfield disallowing invalid values, that is, values which are not {@code null} and not empty strings and
41   * for which the {@link Referenznummer11#parse(String)} method throws a {@code ParseException}.</p>
42   *
43   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
44   * @version $JDTAUS: Referenznummer11TextField.java 8661 2012-09-27 11:29:58Z schulte $
45   */
46  public final class Referenznummer11TextField extends JFormattedTextField
47  {
48  
49      /** Serial version UID for backwards compatibility with 1.1.x classes. */
50      private static final long serialVersionUID = 47899058115334464L;
51  
52      /**
53       * Format used when formatting Referenznummer11 instances.
54       * @serial
55       */
56      private Integer format;
57  
58      /**
59       * Flag indicating if validation is performed.
60       * @serial
61       */
62      private Boolean validating;
63  
64      /** Creates a new default {@code Referenznummer11TextField} instance. */
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( fb.getDocument().getLength() + s.length() );
138                                     b.append( fb.getDocument().getText( 0, fb.getDocument().getLength() ) );
139                                     b.replace( o, o + s.length(), s );
140 
141                                     try
142                                     {
143                                         Referenznummer11.parse( b.toString() );
144                                     }
145                                     catch ( ParseException e )
146                                     {
147                                         invalidEdit();
148                                         return;
149                                     }
150                                 }
151 
152                                 super.replace( fb, o, l, s, a );
153                             }
154 
155                         };
156                     }
157 
158                 };
159             }
160 
161         } );
162     }
163 
164     /**
165      * Gets the last valid {@code Referenznummer11}.
166      *
167      * @return the last valid {@code Referenznummer11} or {@code null}.
168      */
169     public Referenznummer11 getReferenznummer11()
170     {
171         return (Referenznummer11) this.getValue();
172     }
173 
174     /**
175      * Gets the constant of the format used when formatting Referenznummer11 instances.
176      *
177      * @return the constant of the format used when formatting Referenznummer11 instances.
178      *
179      * @see Referenznummer11#ELECTRONIC_FORMAT
180      * @see Referenznummer11#LETTER_FORMAT
181      */
182     public int getFormat()
183     {
184         if ( this.format == null )
185         {
186             this.format = this.getDefaultFormat();
187         }
188 
189         return this.format.intValue();
190     }
191 
192     /**
193      * Sets the constant of the format to use when formatting Referenznummer11 instances.
194      *
195      * @param value the constant of the format to use when formatting Referenznummer11 instances.
196      *
197      * @throws IllegalArgumentException if {@code format} is neither {@code ELECTRONIC_FORMAT} nor
198      * {@code LETTER_FORMAT}.
199      *
200      * @see Referenznummer11#ELECTRONIC_FORMAT
201      * @see Referenznummer11#LETTER_FORMAT
202      */
203     public void setFormat( final int value )
204     {
205         if ( value != Referenznummer11.ELECTRONIC_FORMAT && value != Referenznummer11.LETTER_FORMAT )
206         {
207             throw new IllegalArgumentException( Integer.toString( value ) );
208         }
209 
210         this.format = new Integer( value );
211     }
212 
213     /**
214      * Gets the flag indicating if validation is performed.
215      *
216      * @return {@code true} if the field's value is validated; {@code false} if no validation of the field's value is
217      * performed.
218      */
219     public boolean isValidating()
220     {
221         if ( this.validating == null )
222         {
223             this.validating = this.isDefaultValidating();
224         }
225 
226         return this.validating.booleanValue();
227     }
228 
229     /**
230      * Sets the flag indicating if validation should be performed.
231      *
232      * @param value {@code true} to validate the field's value; {@code false} to not validate the field's value.
233      */
234     public void setValidating( boolean value )
235     {
236         this.validating = Boolean.valueOf( value );
237     }
238 
239     /**
240      * Checks configured properties.
241      *
242      * @throws PropertyException for invalid property values.
243      */
244     private void assertValidProperties()
245     {
246         if ( this.getFormat() != Referenznummer11.ELECTRONIC_FORMAT &&
247              this.getFormat() != Referenznummer11.LETTER_FORMAT )
248         {
249             throw new PropertyException( "format", Integer.toString( this.getFormat() ) );
250         }
251     }
252 
253     //--Properties--------------------------------------------------------------
254 
255 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
256     // This section is managed by jdtaus-container-mojo.
257 
258     /**
259      * Gets the value of property <code>defaultValidating</code>.
260      *
261      * @return Default value of the flag indicating if validation should be performed.
262      */
263     private java.lang.Boolean isDefaultValidating()
264     {
265         return (java.lang.Boolean) ContainerFactory.getContainer().
266             getProperty( this, "defaultValidating" );
267 
268     }
269 
270     /**
271      * Gets the value of property <code>defaultFormat</code>.
272      *
273      * @return Default value of the format to use when formatting Referenznummer11 instances (6001 = electronic format, 6002 letter format).
274      */
275     private java.lang.Integer getDefaultFormat()
276     {
277         return (java.lang.Integer) ContainerFactory.getContainer().
278             getProperty( this, "defaultFormat" );
279 
280     }
281 
282 // </editor-fold>//GEN-END:jdtausProperties
283 
284     //--------------------------------------------------------------Properties--
285 }