Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / javax / swing / text / DefaultFormatterFactory.java
blob84a1676d2dc3ebf9fc4d7bc5399ce7abfe182ea6
1 /* DefaultFormatterFactory.java -- FIXME: briefly describe file purpose
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text;
41 import java.io.Serializable;
43 import javax.swing.JFormattedTextField;
44 import javax.swing.JFormattedTextField.AbstractFormatter;
45 import javax.swing.JFormattedTextField.AbstractFormatterFactory;
47 /**
48 * This class is Swing's only concrete implementation of
49 * JFormattedTextField.AbstractFormatterFactory. It holds several
50 * formatters and determines the best one to be used based on the
51 * passed-in value from the text field.
53 * @author Anthony Balkissoon abalkiss at redhat dot com
54 * @since 1.4
56 public class DefaultFormatterFactory extends AbstractFormatterFactory implements
57 Serializable
59 /**
60 * The default formatter.
61 **/
62 AbstractFormatter defaultFormatter;
64 /**
65 * The formatter to use when the JFormattedTextField has focus and either the
66 * value isn't null or the value is null but no <code>nullFormatter</code>
67 * has been specified.
69 AbstractFormatter editFormatter;
71 /**
72 * The formatter to use when the JFormattedTextField doesn't havefocus and
73 * either the value isn't null or the value is null but no
74 * <code>nullFormatter</code> has been specified.
76 AbstractFormatter displayFormatter;
78 /**
79 * The formatter to use when the value of the JFormattedTextField is null.
81 AbstractFormatter nullFormatter;
83 /**
84 * Creates a DefaultFormatterFactory with no formatters
86 public DefaultFormatterFactory()
88 // Nothing to be done here.
91 /**
92 * Creates a new DefaultFormatterFactory with the specified formatters.
93 * @param defaultFormat the formatter to use if no other appropriate non-null
94 * formatted can be found.
96 public DefaultFormatterFactory(AbstractFormatter defaultFormat)
98 defaultFormatter = defaultFormat;
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.
109 public DefaultFormatterFactory(AbstractFormatter defaultFormat,
110 AbstractFormatter displayFormat)
112 defaultFormatter = defaultFormat;
113 displayFormatter = displayFormat;
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.
127 public DefaultFormatterFactory(AbstractFormatter defaultFormat,
128 AbstractFormatter displayFormat,
129 AbstractFormatter editFormat)
131 defaultFormatter = defaultFormat;
132 displayFormatter = displayFormat;
133 editFormatter = editFormat;
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.
149 public DefaultFormatterFactory(AbstractFormatter defaultFormat,
150 AbstractFormatter displayFormat,
151 AbstractFormatter editFormat,
152 AbstractFormatter nullFormat)
154 defaultFormatter = defaultFormat;
155 displayFormatter = displayFormat;
156 editFormatter = editFormat;
157 nullFormatter = nullFormat;
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.
166 public AbstractFormatter getDefaultFormatter()
168 return defaultFormatter;
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.
177 public void setDefaultFormatter(AbstractFormatter defaultFormatter)
179 this.defaultFormatter = defaultFormatter;
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.
192 public AbstractFormatter getDisplayFormatter()
194 return displayFormatter;
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.
204 public void setDisplayFormatter(AbstractFormatter displayFormatter)
206 this.displayFormatter = displayFormatter;
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.
217 public AbstractFormatter getEditFormatter()
219 return editFormatter;
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.
228 public void setEditFormatter(AbstractFormatter editFormatter)
230 this.editFormatter = editFormatter;
234 * Gets the formatter to use if the value of the JFormattedTextField is null.
235 * @return the formatter to use for null values.
237 public AbstractFormatter getNullFormatter()
239 return nullFormatter;
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.
247 public void setNullFormatter(AbstractFormatter nullFormatter)
249 this.nullFormatter = nullFormatter;
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>.
264 public AbstractFormatter getFormatter(JFormattedTextField tf)
266 if (tf == null)
267 return null;
269 if (tf.getValue() == null && nullFormatter != null)
270 return nullFormatter;
272 if (tf.hasFocus() && editFormatter != null)
273 return editFormatter;
275 if (!tf.hasFocus() && displayFormatter != null)
276 return displayFormatter;
278 return defaultFormatter;