| 1 |
772 |
jeremybenn |
/* DefaultFormatterFactory.java -- FIXME: briefly describe file purpose
|
| 2 |
|
|
Copyright (C) 2005 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 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.
|
| 10 |
|
|
|
| 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.
|
| 15 |
|
|
|
| 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.
|
| 20 |
|
|
|
| 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.
|
| 25 |
|
|
|
| 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. */
|
| 37 |
|
|
|
| 38 |
|
|
|
| 39 |
|
|
package javax.swing.text;
|
| 40 |
|
|
|
| 41 |
|
|
import java.io.Serializable;
|
| 42 |
|
|
|
| 43 |
|
|
import javax.swing.JFormattedTextField;
|
| 44 |
|
|
import javax.swing.JFormattedTextField.AbstractFormatter;
|
| 45 |
|
|
import javax.swing.JFormattedTextField.AbstractFormatterFactory;
|
| 46 |
|
|
|
| 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.
|
| 52 |
|
|
*
|
| 53 |
|
|
* @author Anthony Balkissoon abalkiss at redhat dot com
|
| 54 |
|
|
* @since 1.4
|
| 55 |
|
|
*/
|
| 56 |
|
|
public class DefaultFormatterFactory extends AbstractFormatterFactory implements
|
| 57 |
|
|
Serializable
|
| 58 |
|
|
{
|
| 59 |
|
|
/**
|
| 60 |
|
|
* The default formatter.
|
| 61 |
|
|
**/
|
| 62 |
|
|
AbstractFormatter defaultFormatter;
|
| 63 |
|
|
|
| 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.
|
| 68 |
|
|
*/
|
| 69 |
|
|
AbstractFormatter editFormatter;
|
| 70 |
|
|
|
| 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.
|
| 75 |
|
|
*/
|
| 76 |
|
|
AbstractFormatter displayFormatter;
|
| 77 |
|
|
|
| 78 |
|
|
/**
|
| 79 |
|
|
* The formatter to use when the value of the JFormattedTextField is null.
|
| 80 |
|
|
*/
|
| 81 |
|
|
AbstractFormatter nullFormatter;
|
| 82 |
|
|
|
| 83 |
|
|
/**
|
| 84 |
|
|
* Creates a DefaultFormatterFactory with no formatters
|
| 85 |
|
|
*/
|
| 86 |
|
|
public DefaultFormatterFactory()
|
| 87 |
|
|
{
|
| 88 |
|
|
// Nothing to be done here.
|
| 89 |
|
|
}
|
| 90 |
|
|
|
| 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.
|
| 95 |
|
|
*/
|
| 96 |
|
|
public DefaultFormatterFactory(AbstractFormatter defaultFormat)
|
| 97 |
|
|
{
|
| 98 |
|
|
defaultFormatter = defaultFormat;
|
| 99 |
|
|
}
|
| 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 |
|
|
}
|