1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/* * @(#)DefaultFormatterFactory.java 1.10 06/04/07 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.text; import java.io.Serializable; import java.text.ParseException; import javax.swing.JFormattedTextField; /** * An implementation of * <code>JFormattedTextField.AbstractFormatterFactory</code>. * <code>DefaultFormatterFactory</code> allows specifying a number of * different <code>JFormattedTextField.AbstractFormatter</code>s that are to * be used. * The most important one is the default one * (<code>setDefaultFormatter</code>). The default formatter will be used * if a more specific formatter could not be found. The following process * is used to determine the appropriate formatter to use. * <ol> * <li>Is the passed in value null? Use the null formatter. * <li>Does the <code>JFormattedTextField</code> have focus? Use the edit * formatter. * <li>Otherwise, use the display formatter. * <li>If a non-null <code>AbstractFormatter</code> has not been found, use * the default formatter. * </ol> * <p> * The following code shows how to configure a * <code>JFormattedTextField</code> with two * <code>JFormattedTextField.AbstractFormatter</code>s, one for display and * one for editing. * <pre> * JFormattedTextField.AbstractFormatter editFormatter = ...; * JFormattedTextField.AbstractFormatter displayFormatter = ...; * DefaultFormatterFactory factory = new DefaultFormatterFactory( * displayFormatter, displayFormatter, editFormatter); * JFormattedTextField tf = new JFormattedTextField(factory); * </pre> * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see javax.swing.JFormattedTextField * * @version 1.10 04/07/06 * @since 1.4 */ public class DefaultFormatterFactory extends JFormattedTextField.AbstractFormatterFactory implements Serializable { /** * Default <code>AbstractFormatter</code> to use if a more specific one has * not been specified. */ private JFormattedTextField.AbstractFormatter defaultFormat; /** * <code>JFormattedTextField.AbstractFormatter</code> to use for display. */ private JFormattedTextField.AbstractFormatter displayFormat; /** * <code>JFormattedTextField.AbstractFormatter</code> to use for editing. */ private JFormattedTextField.AbstractFormatter editFormat; /** * <code>JFormattedTextField.AbstractFormatter</code> to use if the value * is null. */ private JFormattedTextField.AbstractFormatter nullFormat; public DefaultFormatterFactory() { } /** * Creates a <code>DefaultFormatterFactory</code> with the specified * <code>JFormattedTextField.AbstractFormatter</code>. * * @param defaultFormat JFormattedTextField.AbstractFormatter to be used * if a more specific * JFormattedTextField.AbstractFormatter can not be * found. */ public DefaultFormatterFactory(JFormattedTextField. AbstractFormatter defaultFormat) { this(defaultFormat, null); } /** * Creates a <code>DefaultFormatterFactory</code> with the specified * <code>JFormattedTextField.AbstractFormatter</code>s. * * @param defaultFormat JFormattedTextField.AbstractFormatter to be used * if a more specific * JFormattedTextField.AbstractFormatter can not be * found. * @param displayFormat JFormattedTextField.AbstractFormatter to be used * when the JFormattedTextField does not have focus. */ public DefaultFormatterFactory( JFormattedTextField.AbstractFormatter defaultFormat, JFormattedTextField.AbstractFormatter displayFormat) { this(defaultFormat, displayFormat, null); } /** * Creates a DefaultFormatterFactory with the specified * JFormattedTextField.AbstractFormatters. * * @param defaultFormat JFormattedTextField.AbstractFormatter to be used * if a more specific * JFormattedTextField.AbstractFormatter can not be * found. * @param displayFormat JFormattedTextField.AbstractFormatter to be used * when the JFormattedTextField does not have focus. * @param editFormat JFormattedTextField.AbstractFormatter to be used * when the JFormattedTextField has focus. */ public DefaultFormatterFactory( JFormattedTextField.AbstractFormatter defaultFormat, JFormattedTextField.AbstractFormatter displayFormat, JFormattedTextField.AbstractFormatter editFormat) { this(defaultFormat, displayFormat, editFormat, null); } /** * Creates a DefaultFormatterFactory with the specified * JFormattedTextField.AbstractFormatters. * * @param defaultFormat JFormattedTextField.AbstractFormatter to be used * if a more specific * JFormattedTextField.AbstractFormatter can not be * found. * @param displayFormat JFormattedTextField.AbstractFormatter to be used * when the JFormattedTextField does not have focus. * @param editFormat JFormattedTextField.AbstractFormatter to be used * when the JFormattedTextField has focus. * @param nullFormat JFormattedTextField.AbstractFormatter to be used * when the JFormattedTextField has a null value. */ public DefaultFormatterFactory( JFormattedTextField.AbstractFormatter defaultFormat, JFormattedTextField.AbstractFormatter displayFormat, JFormattedTextField.AbstractFormatter editFormat, JFormattedTextField.AbstractFormatter nullFormat) { this.defaultFormat = defaultFormat; this.displayFormat = displayFormat; this.editFormat = editFormat; this.nullFormat = nullFormat; } /** * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use as * a last resort, eg in case a display, edit or null * <code>JFormattedTextField.AbstractFormatter</code> has not been * specified. * * @param atf JFormattedTextField.AbstractFormatter used if a more * specific is not specified */ public void setDefaultFormatter(JFormattedTextField.AbstractFormatter atf){ defaultFormat = atf; } /** * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use * as a last resort, eg in case a display, edit or null * <code>JFormattedTextField.AbstractFormatter</code> * has not been specified. * * @return JFormattedTextField.AbstractFormatter used if a more specific * one is not specified. */ public JFormattedTextField.AbstractFormatter getDefaultFormatter() { return defaultFormat; } /** * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if * the <code>JFormattedTextField</code> is not being edited and either * the value is not-null, or the value is null and a null formatter has * has not been specified. * * @param atf JFormattedTextField.AbstractFormatter to use when the * JFormattedTextField does not have focus */ public void setDisplayFormatter(JFormattedTextField.AbstractFormatter atf){ displayFormat = atf; } /** * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use * if the <code>JFormattedTextField</code> is not being edited and either * the value is not-null, or the value is null and a null formatter has * has not been specified. * * @return JFormattedTextField.AbstractFormatter to use when the * JFormattedTextField does not have focus */ public JFormattedTextField.AbstractFormatter getDisplayFormatter() { return displayFormat; } /** * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if * the <code>JFormattedTextField</code> is being edited and either * the value is not-null, or the value is null and a null formatter has * has not been specified. * * @param atf JFormattedTextField.AbstractFormatter to use when the * component has focus */ public void setEditFormatter(JFormattedTextField.AbstractFormatter atf) { editFormat = atf; } /** * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use * if the <code>JFormattedTextField</code> is being edited and either * the value is not-null, or the value is null and a null formatter has * has not been specified. * * @return JFormattedTextField.AbstractFormatter to use when the * component has focus */ public JFormattedTextField.AbstractFormatter getEditFormatter() { return editFormat; } /** * Sets the formatter to use if the value of the JFormattedTextField is * null. * * @param atf JFormattedTextField.AbstractFormatter to use when * the value of the JFormattedTextField is null. */ public void setNullFormatter(JFormattedTextField.AbstractFormatter atf) { nullFormat = atf; } /** * Returns the formatter to use if the value is null. * * @return JFormattedTextField.AbstractFormatter to use when the value is * null */ public JFormattedTextField.AbstractFormatter getNullFormatter() { return nullFormat; } /** * Returns either the default formatter, display formatter, editor * formatter or null formatter based on the state of the * JFormattedTextField. * * @param source JFormattedTextField requesting * JFormattedTextField.AbstractFormatter * @return JFormattedTextField.AbstractFormatter to handle * formatting duties. */ public JFormattedTextField.AbstractFormatter getFormatter( JFormattedTextField source) { JFormattedTextField.AbstractFormatter format = null; if (source == null) { return null; } Object value = source.getValue(); if (value == null) { format = getNullFormatter(); } if (format == null) { if (source.hasFocus()) { format = getEditFormatter(); } else { format = getDisplayFormatter(); } if (format == null) { format = getDefaultFormatter(); } } return format; } }