KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > DefaultFormatterFactory


1 /*
2  * @(#)DefaultFormatterFactory.java 1.8 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.io.Serializable JavaDoc;
10 import java.text.ParseException JavaDoc;
11 import javax.swing.JFormattedTextField JavaDoc;
12
13 /**
14  * An implementation of
15  * <code>JFormattedTextField.AbstractFormatterFactory</code>.
16  * <code>DefaultFormatterFactory</code> allows specifying a number of
17  * different <code>JFormattedTextField.AbstractFormatter</code>s that are to
18  * be used.
19  * The most important one is the default one
20  * (<code>setDefaultFormatter</code>). The default formatter will be used
21  * if a more specific formatter could not be found. The following process
22  * is used to determine the appropriate formatter to use.
23  * <ol>
24  * <li>Is the passed in value null? Use the null formatter.
25  * <li>Does the <code>JFormattedTextField</code> have focus? Use the edit
26  * formatter.
27  * <li>Otherwise, use the display formatter.
28  * <li>If a non-null <code>AbstractFormatter</code> has not been found, use
29  * the default formatter.
30  * </ol>
31  * <p>
32  * The following code shows how to configure a
33  * <code>JFormattedTextField</code> with two
34  * <code>JFormattedTextField.AbstractFormatter</code>s, one for display and
35  * one for editing.
36  * <pre>
37  * JFormattedTextField.AbstractFormatter editFormatter = ...;
38  * JFormattedTextField.AbstractFormatter displayFormatter = ...;
39  * DefaultFormatterFactory factory = new DefaultFormatterFactory(
40  * displayFormatter, displayFormatter, editFormatter);
41  * JFormattedTextField tf = new JFormattedTextField(factory);
42  * </pre>
43  * <p>
44  * <strong>Warning:</strong>
45  * Serialized objects of this class will not be compatible with
46  * future Swing releases. The current serialization support is
47  * appropriate for short term storage or RMI between applications running
48  * the same version of Swing. As of 1.4, support for long term storage
49  * of all JavaBeans<sup><font size="-2">TM</font></sup>
50  * has been added to the <code>java.beans</code> package.
51  * Please see {@link java.beans.XMLEncoder}.
52  *
53  * @see javax.swing.JFormattedTextField
54  *
55  * @version 1.8 12/19/03
56  * @since 1.4
57  */

58 public class DefaultFormatterFactory extends JFormattedTextField.AbstractFormatterFactory JavaDoc implements Serializable JavaDoc {
59     /**
60      * Default <code>AbstractFormatter</code> to use if a more specific one has
61      * not been specified.
62      */

63     private JFormattedTextField.AbstractFormatter JavaDoc defaultFormat;
64
65     /**
66      * <code>JFormattedTextField.AbstractFormatter</code> to use for display.
67      */

68     private JFormattedTextField.AbstractFormatter JavaDoc displayFormat;
69
70     /**
71      * <code>JFormattedTextField.AbstractFormatter</code> to use for editing.
72      */

73     private JFormattedTextField.AbstractFormatter JavaDoc editFormat;
74
75     /**
76      * <code>JFormattedTextField.AbstractFormatter</code> to use if the value
77      * is null.
78      */

79     private JFormattedTextField.AbstractFormatter JavaDoc nullFormat;
80
81
82     public DefaultFormatterFactory() {
83     }
84
85     /**
86      * Creates a <code>DefaultFormatterFactory</code> with the specified
87      * <code>JFormattedTextField.AbstractFormatter</code>.
88      *
89      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
90      * if a more specific
91      * JFormattedTextField.AbstractFormatter can not be
92      * found.
93      */

94     public DefaultFormatterFactory(JFormattedTextField.
95
JavaDoc                                       AbstractFormatter defaultFormat) {
96         this(defaultFormat, null);
97     }
98
99     /**
100      * Creates a <code>DefaultFormatterFactory</code> with the specified
101      * <code>JFormattedTextField.AbstractFormatter</code>s.
102      *
103      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
104      * if a more specific
105      * JFormattedTextField.AbstractFormatter can not be
106      * found.
107      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
108      * when the JFormattedTextField does not have focus.
109      */

110     public DefaultFormatterFactory(
111                      JFormattedTextField.AbstractFormatter JavaDoc defaultFormat,
112                      JFormattedTextField.AbstractFormatter JavaDoc displayFormat) {
113         this(defaultFormat, displayFormat, null);
114     }
115
116     /**
117      * Creates a DefaultFormatterFactory with the specified
118      * JFormattedTextField.AbstractFormatters.
119      *
120      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
121      * if a more specific
122      * JFormattedTextField.AbstractFormatter can not be
123      * found.
124      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
125      * when the JFormattedTextField does not have focus.
126      * @param editFormat JFormattedTextField.AbstractFormatter to be used
127      * when the JFormattedTextField has focus.
128      */

129     public DefaultFormatterFactory(
130                    JFormattedTextField.AbstractFormatter JavaDoc defaultFormat,
131                    JFormattedTextField.AbstractFormatter JavaDoc displayFormat,
132                    JFormattedTextField.AbstractFormatter JavaDoc editFormat) {
133         this(defaultFormat, displayFormat, editFormat, null);
134     }
135
136     /**
137      * Creates a DefaultFormatterFactory with the specified
138      * JFormattedTextField.AbstractFormatters.
139      *
140      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
141      * if a more specific
142      * JFormattedTextField.AbstractFormatter can not be
143      * found.
144      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
145      * when the JFormattedTextField does not have focus.
146      * @param editFormat JFormattedTextField.AbstractFormatter to be used
147      * when the JFormattedTextField has focus.
148      * @param nullFormat JFormattedTextField.AbstractFormatter to be used
149      * when the JFormattedTextField has a null value.
150      */

151     public DefaultFormatterFactory(
152                   JFormattedTextField.AbstractFormatter JavaDoc defaultFormat,
153                   JFormattedTextField.AbstractFormatter JavaDoc displayFormat,
154                   JFormattedTextField.AbstractFormatter JavaDoc editFormat,
155                   JFormattedTextField.AbstractFormatter JavaDoc nullFormat) {
156         this.defaultFormat = defaultFormat;
157         this.displayFormat = displayFormat;
158         this.editFormat = editFormat;
159         this.nullFormat = nullFormat;
160     }
161
162     /**
163      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use as
164      * a last resort, eg in case a display, edit or null
165      * <code>JFormattedTextField.AbstractFormatter</code> has not been
166      * specified.
167      *
168      * @param atf JFormattedTextField.AbstractFormatter used if a more
169      * specific is not specified
170      */

171     public void setDefaultFormatter(JFormattedTextField.AbstractFormatter JavaDoc atf){
172         defaultFormat = atf;
173     }
174
175     /**
176      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
177      * as a last resort, eg in case a display, edit or null
178      * <code>JFormattedTextField.AbstractFormatter</code>
179      * has not been specified.
180      *
181      * @return JFormattedTextField.AbstractFormatter used if a more specific
182      * one is not specified.
183      */

184     public JFormattedTextField.AbstractFormatter JavaDoc getDefaultFormatter() {
185         return defaultFormat;
186     }
187
188     /**
189      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
190      * the <code>JFormattedTextField</code> is not being edited and either
191      * the value is not-null, or the value is null and a null formatter has
192      * has not been specified.
193      *
194      * @param atf JFormattedTextField.AbstractFormatter to use when the
195      * JFormattedTextField does not have focus
196      */

197     public void setDisplayFormatter(JFormattedTextField.AbstractFormatter JavaDoc atf){
198         displayFormat = atf;
199     }
200
201     /**
202      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
203      * if the <code>JFormattedTextField</code> is not being edited and either
204      * the value is not-null, or the value is null and a null formatter has
205      * has not been specified.
206      *
207      * @return JFormattedTextField.AbstractFormatter to use when the
208      * JFormattedTextField does not have focus
209      */

210     public JFormattedTextField.AbstractFormatter JavaDoc getDisplayFormatter() {
211         return displayFormat;
212     }
213
214     /**
215      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
216      * the code>JFormattedTextField</code> is being edited and either
217      * the value is not-null, or the value is null and a null formatter has
218      * has not been specified.
219      *
220      * @param atf JFormattedTextField.AbstractFormatter to use when the
221      * component has focus
222      */

223     public void setEditFormatter(JFormattedTextField.AbstractFormatter JavaDoc atf) {
224         editFormat = atf;
225     }
226
227     /**
228      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
229      * if the <code>JFormattedTextField</code> is being edited and either
230      * the value is not-null, or the value is null and a null formatter has
231      * has not been specified.
232      *
233      * @return JFormattedTextField.AbstractFormatter to use when the
234      * component has focus
235      */

236     public JFormattedTextField.AbstractFormatter JavaDoc getEditFormatter() {
237         return editFormat;
238     }
239
240     /**
241      * Sets the formatter to use if the value of the JFormattedTextField is
242      * null.
243      *
244      * @param atf JFormattedTextField.AbstractFormatter to use when
245      * the value of the JFormattedTextField is null.
246      */

247     public void setNullFormatter(JFormattedTextField.AbstractFormatter JavaDoc atf) {
248         nullFormat = atf;
249     }
250
251     /**
252      * Returns the formatter to use if the value is null.
253      *
254      * @return JFormattedTextField.AbstractFormatter to use when the value is
255      * null
256      */

257     public JFormattedTextField.AbstractFormatter JavaDoc getNullFormatter() {
258         return nullFormat;
259     }
260
261     /**
262      * Returns either the default formatter, display formatter, editor
263      * formatter or null formatter based on the state of the
264      * JFormattedTextField.
265      *
266      * @param source JFormattedTextField requesting
267      * JFormattedTextField.AbstractFormatter
268      * @return JFormattedTextField.AbstractFormatter to handle
269      * formatting duties.
270      */

271     public JFormattedTextField.AbstractFormatter JavaDoc getFormatter(
272                      JFormattedTextField JavaDoc source) {
273         JFormattedTextField.AbstractFormatter JavaDoc format = null;
274
275         if (source == null) {
276             return null;
277         }
278         Object JavaDoc value = source.getValue();
279
280         if (value == null) {
281             format = getNullFormatter();
282         }
283         if (format == null) {
284             if (source.hasFocus()) {
285                 format = getEditFormatter();
286             }
287             else {
288                 format = getDisplayFormatter();
289             }
290             if (format == null) {
291                 format = getDefaultFormatter();
292             }
293         }
294         return format;
295     }
296 }
297
Popular Tags