001 /* DefaultFormatterFactory.java -- FIXME: briefly describe file purpose
002 Copyright (C) 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing.text;
040
041 import java.io.Serializable;
042
043 import javax.swing.JFormattedTextField;
044 import javax.swing.JFormattedTextField.AbstractFormatter;
045 import javax.swing.JFormattedTextField.AbstractFormatterFactory;
046
047 /**
048 * This class is Swing's only concrete implementation of
049 * JFormattedTextField.AbstractFormatterFactory. It holds several
050 * formatters and determines the best one to be used based on the
051 * passed-in value from the text field.
052 *
053 * @author Anthony Balkissoon abalkiss at redhat dot com
054 * @since 1.4
055 */
056 public class DefaultFormatterFactory extends AbstractFormatterFactory implements
057 Serializable
058 {
059 /**
060 * The default formatter.
061 **/
062 AbstractFormatter defaultFormatter;
063
064 /**
065 * The formatter to use when the JFormattedTextField has focus and either the
066 * value isn't null or the value is null but no <code>nullFormatter</code>
067 * has been specified.
068 */
069 AbstractFormatter editFormatter;
070
071 /**
072 * The formatter to use when the JFormattedTextField doesn't havefocus and
073 * either the value isn't null or the value is null but no
074 * <code>nullFormatter</code> has been specified.
075 */
076 AbstractFormatter displayFormatter;
077
078 /**
079 * The formatter to use when the value of the JFormattedTextField is null.
080 */
081 AbstractFormatter nullFormatter;
082
083 /**
084 * Creates a DefaultFormatterFactory with no formatters
085 */
086 public DefaultFormatterFactory()
087 {
088 // Nothing to be done here.
089 }
090
091 /**
092 * Creates a new DefaultFormatterFactory with the specified formatters.
093 * @param defaultFormat the formatter to use if no other appropriate non-null
094 * formatted can be found.
095 */
096 public DefaultFormatterFactory(AbstractFormatter defaultFormat)
097 {
098 defaultFormatter = defaultFormat;
099 }
100
101 /**
102 * Creates a new DefaultFormatterFactory with the specified formatters.
103 * @param defaultFormat the formatter to use if no other appropriate non-null
104 * formatted can be found.
105 * @param displayFormat the formatter to use if the JFormattedTextField
106 * doesn't have focus and either the value is not null or the value is null
107 * but no <code>nullFormatter</code> has been specified.
108 */
109 public DefaultFormatterFactory(AbstractFormatter defaultFormat,
110 AbstractFormatter displayFormat)
111 {
112 defaultFormatter = defaultFormat;
113 displayFormatter = displayFormat;
114 }
115
116 /**
117 * Creates a new DefaultFormatterFactory with the specified formatters.
118 * @param defaultFormat the formatter to use if no other appropriate non-null
119 * formatted can be found.
120 * @param displayFormat the formatter to use if the JFormattedTextField
121 * doesn't have focus and either the value is not null or the value is null
122 * but no <code>nullFormatter</code> has been specified.
123 * @param editFormat the formatter to use if the JFormattedTextField has
124 * focus and either the value is not null or the value is null but not
125 * <code>nullFormatter</code> has been specified.
126 */
127 public DefaultFormatterFactory(AbstractFormatter defaultFormat,
128 AbstractFormatter displayFormat,
129 AbstractFormatter editFormat)
130 {
131 defaultFormatter = defaultFormat;
132 displayFormatter = displayFormat;
133 editFormatter = editFormat;
134 }
135
136 /**
137 * Creates a new DefaultFormatterFactory with the specified formatters.
138 * @param defaultFormat the formatter to use if no other appropriate non-null
139 * formatted can be found.
140 * @param displayFormat the formatter to use if the JFormattedTextField
141 * doesn't have focus and either the value is not null or the value is null
142 * but no <code>nullFormatter</code> has been specified.
143 * @param editFormat the formatter to use if the JFormattedTextField has
144 * focus and either the value is not null or the value is null but not
145 * <code>nullFormatter</code> has been specified.
146 * @param nullFormat the formatter to use when the value of the
147 * JFormattedTextField is null.
148 */
149 public DefaultFormatterFactory(AbstractFormatter defaultFormat,
150 AbstractFormatter displayFormat,
151 AbstractFormatter editFormat,
152 AbstractFormatter nullFormat)
153 {
154 defaultFormatter = defaultFormat;
155 displayFormatter = displayFormat;
156 editFormatter = editFormat;
157 nullFormatter = nullFormat;
158 }
159
160 /**
161 * Returns the formatted to be used if no other appropriate non-null
162 * formatter can be found.
163 * @return the formatted to be used if no other appropriate non-null
164 * formatter can be found.
165 */
166 public AbstractFormatter getDefaultFormatter()
167 {
168 return defaultFormatter;
169 }
170
171 /**
172 * Sets the formatted to be used if no other appropriate non-null formatter
173 * can be found.
174 * @param defaultFormatter the formatted to be used if no other appropriate
175 * non-null formatter can be found.
176 */
177 public void setDefaultFormatter(AbstractFormatter defaultFormatter)
178 {
179 this.defaultFormatter = defaultFormatter;
180 }
181
182 /**
183 * Gets the <code>displayFormatter</code>. This is the formatter to use if
184 * the JFormattedTextField is not being edited and either the value is not
185 * null or the value is null and no <code>nullFormatter<code> has been
186 * specified.
187 * @return the formatter to use if
188 * the JFormattedTextField is not being edited and either the value is not
189 * null or the value is null and no <code>nullFormatter<code> has been
190 * specified.
191 */
192 public AbstractFormatter getDisplayFormatter()
193 {
194 return displayFormatter;
195 }
196
197 /**
198 * Sets the <code>displayFormatter</code>. This is the formatter to use if
199 * the JFormattedTextField is not being edited and either the value is not
200 * null or the value is null and no <code>nullFormatter<code> has been
201 * specified.
202 * @param displayFormatter the formatter to use.
203 */
204 public void setDisplayFormatter(AbstractFormatter displayFormatter)
205 {
206 this.displayFormatter = displayFormatter;
207 }
208
209 /**
210 * Gets the <code>editFormatter</code>. This is the formatter to use if the
211 * JFormattedTextField is being edited and either the value is not null or
212 * the value is null and no <code>nullFormatter<code> has been specified.
213 * @return the formatter to use if the JFormattedTextField is being edited
214 * and the value is not null or the value is null but no nullFormatted has
215 * been specified.
216 */
217 public AbstractFormatter getEditFormatter()
218 {
219 return editFormatter;
220 }
221
222 /**
223 * Sets the <code>editFormatter</code>. This is the formatter to use if the
224 * JFormattedTextField is being edited and either the value is not null or
225 * the value is null and no <code>nullFormatter<code> has been specified.
226 * @param editFormatter the formatter to use.
227 */
228 public void setEditFormatter(AbstractFormatter editFormatter)
229 {
230 this.editFormatter = editFormatter;
231 }
232
233 /**
234 * Gets the formatter to use if the value of the JFormattedTextField is null.
235 * @return the formatter to use for null values.
236 */
237 public AbstractFormatter getNullFormatter()
238 {
239 return nullFormatter;
240 }
241
242 /**
243 * Sets the <code>nullFormatter</code>. This is the formatter to use if the
244 * value of the JFormattedTextField is null.
245 * @param nullFormatter the formatter to use for null values.
246 */
247 public void setNullFormatter(AbstractFormatter nullFormatter)
248 {
249 this.nullFormatter = nullFormatter;
250 }
251
252 /**
253 * Returns the appropriate formatter based on the state of
254 * <code>tf</code>. If <code>tf<code> is null we return null, otherwise
255 * we return one of the following:
256 * 1. Returns <code>nullFormatter</code> if <code>tf.getValue()</code> is
257 * null and <code>nullFormatter</code> is not.
258 * 2. Returns <code>editFormatter</code> if <code>tf.hasFocus()</code> is
259 * true and <code>editFormatter</code> is not null.
260 * 3. Returns <code>displayFormatter</code> if <code>tf.hasFocus()</code> is
261 * false and <code>displayFormatter</code> is not null.
262 * 4. Otherwise returns <code>defaultFormatter</code>.
263 */
264 public AbstractFormatter getFormatter(JFormattedTextField tf)
265 {
266 if (tf == null)
267 return null;
268
269 if (tf.getValue() == null && nullFormatter != null)
270 return nullFormatter;
271
272 if (tf.hasFocus() && editFormatter != null)
273 return editFormatter;
274
275 if (!tf.hasFocus() && displayFormatter != null)
276 return displayFormatter;
277
278 return defaultFormatter;
279 }
280 }