001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.text;
026
027 import java.io.Serializable;
028 import java.text.ParseException;
029 import javax.swing.JFormattedTextField;
030
031 /**
032 * An implementation of
033 * <code>JFormattedTextField.AbstractFormatterFactory</code>.
034 * <code>DefaultFormatterFactory</code> allows specifying a number of
035 * different <code>JFormattedTextField.AbstractFormatter</code>s that are to
036 * be used.
037 * The most important one is the default one
038 * (<code>setDefaultFormatter</code>). The default formatter will be used
039 * if a more specific formatter could not be found. The following process
040 * is used to determine the appropriate formatter to use.
041 * <ol>
042 * <li>Is the passed in value null? Use the null formatter.
043 * <li>Does the <code>JFormattedTextField</code> have focus? Use the edit
044 * formatter.
045 * <li>Otherwise, use the display formatter.
046 * <li>If a non-null <code>AbstractFormatter</code> has not been found, use
047 * the default formatter.
048 * </ol>
049 * <p>
050 * The following code shows how to configure a
051 * <code>JFormattedTextField</code> with two
052 * <code>JFormattedTextField.AbstractFormatter</code>s, one for display and
053 * one for editing.
054 * <pre>
055 * JFormattedTextField.AbstractFormatter editFormatter = ...;
056 * JFormattedTextField.AbstractFormatter displayFormatter = ...;
057 * DefaultFormatterFactory factory = new DefaultFormatterFactory(
058 * displayFormatter, displayFormatter, editFormatter);
059 * JFormattedTextField tf = new JFormattedTextField(factory);
060 * </pre>
061 * <p>
062 * <strong>Warning:</strong>
063 * Serialized objects of this class will not be compatible with
064 * future Swing releases. The current serialization support is
065 * appropriate for short term storage or RMI between applications running
066 * the same version of Swing. As of 1.4, support for long term storage
067 * of all JavaBeans<sup><font size="-2">TM</font></sup>
068 * has been added to the <code>java.beans</code> package.
069 * Please see {@link java.beans.XMLEncoder}.
070 *
071 * @see javax.swing.JFormattedTextField
072 *
073 * @version 1.16 05/05/07
074 * @since 1.4
075 */
076 public class DefaultFormatterFactory extends
077 JFormattedTextField.AbstractFormatterFactory implements
078 Serializable {
079 /**
080 * Default <code>AbstractFormatter</code> to use if a more specific one has
081 * not been specified.
082 */
083 private JFormattedTextField.AbstractFormatter defaultFormat;
084
085 /**
086 * <code>JFormattedTextField.AbstractFormatter</code> to use for display.
087 */
088 private JFormattedTextField.AbstractFormatter displayFormat;
089
090 /**
091 * <code>JFormattedTextField.AbstractFormatter</code> to use for editing.
092 */
093 private JFormattedTextField.AbstractFormatter editFormat;
094
095 /**
096 * <code>JFormattedTextField.AbstractFormatter</code> to use if the value
097 * is null.
098 */
099 private JFormattedTextField.AbstractFormatter nullFormat;
100
101 public DefaultFormatterFactory() {
102 }
103
104 /**
105 * Creates a <code>DefaultFormatterFactory</code> with the specified
106 * <code>JFormattedTextField.AbstractFormatter</code>.
107 *
108 * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
109 * if a more specific
110 * JFormattedTextField.AbstractFormatter can not be
111 * found.
112 */
113 public DefaultFormatterFactory(
114 JFormattedTextField.AbstractFormatter defaultFormat) {
115 this (defaultFormat, null);
116 }
117
118 /**
119 * Creates a <code>DefaultFormatterFactory</code> with the specified
120 * <code>JFormattedTextField.AbstractFormatter</code>s.
121 *
122 * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
123 * if a more specific
124 * JFormattedTextField.AbstractFormatter can not be
125 * found.
126 * @param displayFormat JFormattedTextField.AbstractFormatter to be used
127 * when the JFormattedTextField does not have focus.
128 */
129 public DefaultFormatterFactory(
130 JFormattedTextField.AbstractFormatter defaultFormat,
131 JFormattedTextField.AbstractFormatter displayFormat) {
132 this (defaultFormat, displayFormat, null);
133 }
134
135 /**
136 * Creates a DefaultFormatterFactory with the specified
137 * JFormattedTextField.AbstractFormatters.
138 *
139 * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
140 * if a more specific
141 * JFormattedTextField.AbstractFormatter can not be
142 * found.
143 * @param displayFormat JFormattedTextField.AbstractFormatter to be used
144 * when the JFormattedTextField does not have focus.
145 * @param editFormat JFormattedTextField.AbstractFormatter to be used
146 * when the JFormattedTextField has focus.
147 */
148 public DefaultFormatterFactory(
149 JFormattedTextField.AbstractFormatter defaultFormat,
150 JFormattedTextField.AbstractFormatter displayFormat,
151 JFormattedTextField.AbstractFormatter editFormat) {
152 this (defaultFormat, displayFormat, editFormat, null);
153 }
154
155 /**
156 * Creates a DefaultFormatterFactory with the specified
157 * JFormattedTextField.AbstractFormatters.
158 *
159 * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
160 * if a more specific
161 * JFormattedTextField.AbstractFormatter can not be
162 * found.
163 * @param displayFormat JFormattedTextField.AbstractFormatter to be used
164 * when the JFormattedTextField does not have focus.
165 * @param editFormat JFormattedTextField.AbstractFormatter to be used
166 * when the JFormattedTextField has focus.
167 * @param nullFormat JFormattedTextField.AbstractFormatter to be used
168 * when the JFormattedTextField has a null value.
169 */
170 public DefaultFormatterFactory(
171 JFormattedTextField.AbstractFormatter defaultFormat,
172 JFormattedTextField.AbstractFormatter displayFormat,
173 JFormattedTextField.AbstractFormatter editFormat,
174 JFormattedTextField.AbstractFormatter nullFormat) {
175 this .defaultFormat = defaultFormat;
176 this .displayFormat = displayFormat;
177 this .editFormat = editFormat;
178 this .nullFormat = nullFormat;
179 }
180
181 /**
182 * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use as
183 * a last resort, eg in case a display, edit or null
184 * <code>JFormattedTextField.AbstractFormatter</code> has not been
185 * specified.
186 *
187 * @param atf JFormattedTextField.AbstractFormatter used if a more
188 * specific is not specified
189 */
190 public void setDefaultFormatter(
191 JFormattedTextField.AbstractFormatter atf) {
192 defaultFormat = atf;
193 }
194
195 /**
196 * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
197 * as a last resort, eg in case a display, edit or null
198 * <code>JFormattedTextField.AbstractFormatter</code>
199 * has not been specified.
200 *
201 * @return JFormattedTextField.AbstractFormatter used if a more specific
202 * one is not specified.
203 */
204 public JFormattedTextField.AbstractFormatter getDefaultFormatter() {
205 return defaultFormat;
206 }
207
208 /**
209 * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
210 * the <code>JFormattedTextField</code> is not being edited and either
211 * the value is not-null, or the value is null and a null formatter has
212 * has not been specified.
213 *
214 * @param atf JFormattedTextField.AbstractFormatter to use when the
215 * JFormattedTextField does not have focus
216 */
217 public void setDisplayFormatter(
218 JFormattedTextField.AbstractFormatter atf) {
219 displayFormat = atf;
220 }
221
222 /**
223 * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
224 * if the <code>JFormattedTextField</code> is not being edited and either
225 * the value is not-null, or the value is null and a null formatter has
226 * has not been specified.
227 *
228 * @return JFormattedTextField.AbstractFormatter to use when the
229 * JFormattedTextField does not have focus
230 */
231 public JFormattedTextField.AbstractFormatter getDisplayFormatter() {
232 return displayFormat;
233 }
234
235 /**
236 * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
237 * the <code>JFormattedTextField</code> is being edited and either
238 * the value is not-null, or the value is null and a null formatter has
239 * has not been specified.
240 *
241 * @param atf JFormattedTextField.AbstractFormatter to use when the
242 * component has focus
243 */
244 public void setEditFormatter(
245 JFormattedTextField.AbstractFormatter atf) {
246 editFormat = atf;
247 }
248
249 /**
250 * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
251 * if the <code>JFormattedTextField</code> is being edited and either
252 * the value is not-null, or the value is null and a null formatter has
253 * has not been specified.
254 *
255 * @return JFormattedTextField.AbstractFormatter to use when the
256 * component has focus
257 */
258 public JFormattedTextField.AbstractFormatter getEditFormatter() {
259 return editFormat;
260 }
261
262 /**
263 * Sets the formatter to use if the value of the JFormattedTextField is
264 * null.
265 *
266 * @param atf JFormattedTextField.AbstractFormatter to use when
267 * the value of the JFormattedTextField is null.
268 */
269 public void setNullFormatter(
270 JFormattedTextField.AbstractFormatter atf) {
271 nullFormat = atf;
272 }
273
274 /**
275 * Returns the formatter to use if the value is null.
276 *
277 * @return JFormattedTextField.AbstractFormatter to use when the value is
278 * null
279 */
280 public JFormattedTextField.AbstractFormatter getNullFormatter() {
281 return nullFormat;
282 }
283
284 /**
285 * Returns either the default formatter, display formatter, editor
286 * formatter or null formatter based on the state of the
287 * JFormattedTextField.
288 *
289 * @param source JFormattedTextField requesting
290 * JFormattedTextField.AbstractFormatter
291 * @return JFormattedTextField.AbstractFormatter to handle
292 * formatting duties.
293 */
294 public JFormattedTextField.AbstractFormatter getFormatter(
295 JFormattedTextField source) {
296 JFormattedTextField.AbstractFormatter format = null;
297
298 if (source == null) {
299 return null;
300 }
301 Object value = source.getValue();
302
303 if (value == null) {
304 format = getNullFormatter();
305 }
306 if (format == null) {
307 if (source.hasFocus()) {
308 format = getEditFormatter();
309 } else {
310 format = getDisplayFormatter();
311 }
312 if (format == null) {
313 format = getDefaultFormatter();
314 }
315 }
316 return format;
317 }
318 }
|