1 /*
   2  * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.text;
  26 
  27 import java.io.Serializable;
  28 import java.text.ParseException;
  29 import javax.swing.JFormattedTextField;
  30 
  31 /**
  32  * An implementation of
  33  * <code>JFormattedTextField.AbstractFormatterFactory</code>.
  34  * <code>DefaultFormatterFactory</code> allows specifying a number of
  35  * different <code>JFormattedTextField.AbstractFormatter</code>s that are to
  36  * be used.
  37  * The most important one is the default one
  38  * (<code>setDefaultFormatter</code>). The default formatter will be used
  39  * if a more specific formatter could not be found. The following process
  40  * is used to determine the appropriate formatter to use.
  41  * <ol>
  42  *   <li>Is the passed in value null? Use the null formatter.
  43  *   <li>Does the <code>JFormattedTextField</code> have focus? Use the edit
  44  *       formatter.
  45  *   <li>Otherwise, use the display formatter.
  46  *   <li>If a non-null <code>AbstractFormatter</code> has not been found, use
  47  *       the default formatter.
  48  * </ol>
  49  * <p>
  50  * The following code shows how to configure a
  51  * <code>JFormattedTextField</code> with two
  52  * <code>JFormattedTextField.AbstractFormatter</code>s, one for display and
  53  * one for editing.
  54  * <pre>
  55  * JFormattedTextField.AbstractFormatter editFormatter = ...;
  56  * JFormattedTextField.AbstractFormatter displayFormatter = ...;
  57  * DefaultFormatterFactory factory = new DefaultFormatterFactory(
  58  *                 displayFormatter, displayFormatter, editFormatter);
  59  * JFormattedTextField tf = new JFormattedTextField(factory);
  60  * </pre>
  61  * <p>
  62  * <strong>Warning:</strong>
  63  * Serialized objects of this class will not be compatible with
  64  * future Swing releases. The current serialization support is
  65  * appropriate for short term storage or RMI between applications running
  66  * the same version of Swing.  As of 1.4, support for long term storage
  67  * of all JavaBeans
  68  * has been added to the <code>java.beans</code> package.
  69  * Please see {@link java.beans.XMLEncoder}.
  70  *
  71  * @see javax.swing.JFormattedTextField
  72  *
  73  * @since 1.4
  74  */
  75 @SuppressWarnings("serial") // Same-version serialization only
  76 public class DefaultFormatterFactory extends JFormattedTextField.AbstractFormatterFactory implements Serializable {
  77     /**
  78      * Default <code>AbstractFormatter</code> to use if a more specific one has
  79      * not been specified.
  80      */
  81     private JFormattedTextField.AbstractFormatter defaultFormat;
  82 
  83     /**
  84      * <code>JFormattedTextField.AbstractFormatter</code> to use for display.
  85      */
  86     private JFormattedTextField.AbstractFormatter displayFormat;
  87 
  88     /**
  89      * <code>JFormattedTextField.AbstractFormatter</code> to use for editing.
  90      */
  91     private JFormattedTextField.AbstractFormatter editFormat;
  92 
  93     /**
  94      * <code>JFormattedTextField.AbstractFormatter</code> to use if the value
  95      * is null.
  96      */
  97     private JFormattedTextField.AbstractFormatter nullFormat;
  98 
  99 
 100     /**
 101      * Constructs a {@code DefaultFormatterFactory}.
 102      */
 103     public DefaultFormatterFactory() {
 104     }
 105 
 106     /**
 107      * Creates a <code>DefaultFormatterFactory</code> with the specified
 108      * <code>JFormattedTextField.AbstractFormatter</code>.
 109      *
 110      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 111      *                      if a more specific
 112      *                      JFormattedTextField.AbstractFormatter can not be
 113      *                      found.
 114      */
 115     public DefaultFormatterFactory(JFormattedTextField.
 116                                        AbstractFormatter defaultFormat) {
 117         this(defaultFormat, null);
 118     }
 119 
 120     /**
 121      * Creates a <code>DefaultFormatterFactory</code> with the specified
 122      * <code>JFormattedTextField.AbstractFormatter</code>s.
 123      *
 124      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 125      *                      if a more specific
 126      *                      JFormattedTextField.AbstractFormatter can not be
 127      *                      found.
 128      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
 129      *                      when the JFormattedTextField does not have focus.
 130      */
 131     public DefaultFormatterFactory(
 132                      JFormattedTextField.AbstractFormatter defaultFormat,
 133                      JFormattedTextField.AbstractFormatter displayFormat) {
 134         this(defaultFormat, displayFormat, null);
 135     }
 136 
 137     /**
 138      * Creates a DefaultFormatterFactory with the specified
 139      * JFormattedTextField.AbstractFormatters.
 140      *
 141      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 142      *                      if a more specific
 143      *                      JFormattedTextField.AbstractFormatter can not be
 144      *                      found.
 145      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
 146      *                      when the JFormattedTextField does not have focus.
 147      * @param editFormat    JFormattedTextField.AbstractFormatter to be used
 148      *                      when the JFormattedTextField has focus.
 149      */
 150     public DefaultFormatterFactory(
 151                    JFormattedTextField.AbstractFormatter defaultFormat,
 152                    JFormattedTextField.AbstractFormatter displayFormat,
 153                    JFormattedTextField.AbstractFormatter editFormat) {
 154         this(defaultFormat, displayFormat, editFormat, null);
 155     }
 156 
 157     /**
 158      * Creates a DefaultFormatterFactory with the specified
 159      * JFormattedTextField.AbstractFormatters.
 160      *
 161      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 162      *                      if a more specific
 163      *                      JFormattedTextField.AbstractFormatter can not be
 164      *                      found.
 165      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
 166      *                      when the JFormattedTextField does not have focus.
 167      * @param editFormat    JFormattedTextField.AbstractFormatter to be used
 168      *                      when the JFormattedTextField has focus.
 169      * @param nullFormat    JFormattedTextField.AbstractFormatter to be used
 170      *                      when the JFormattedTextField has a null value.
 171      */
 172     public DefaultFormatterFactory(
 173                   JFormattedTextField.AbstractFormatter defaultFormat,
 174                   JFormattedTextField.AbstractFormatter displayFormat,
 175                   JFormattedTextField.AbstractFormatter editFormat,
 176                   JFormattedTextField.AbstractFormatter nullFormat) {
 177         this.defaultFormat = defaultFormat;
 178         this.displayFormat = displayFormat;
 179         this.editFormat = editFormat;
 180         this.nullFormat = nullFormat;
 181     }
 182 
 183     /**
 184      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use as
 185      * a last resort, eg in case a display, edit or null
 186      * <code>JFormattedTextField.AbstractFormatter</code> has not been
 187      * specified.
 188      *
 189      * @param atf JFormattedTextField.AbstractFormatter used if a more
 190      *            specific is not specified
 191      */
 192     public void setDefaultFormatter(JFormattedTextField.AbstractFormatter atf){
 193         defaultFormat = atf;
 194     }
 195 
 196     /**
 197      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
 198      * as a last resort, eg in case a display, edit or null
 199      * <code>JFormattedTextField.AbstractFormatter</code>
 200      * has not been specified.
 201      *
 202      * @return JFormattedTextField.AbstractFormatter used if a more specific
 203      *         one is not specified.
 204      */
 205     public JFormattedTextField.AbstractFormatter getDefaultFormatter() {
 206         return defaultFormat;
 207     }
 208 
 209     /**
 210      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
 211      * the <code>JFormattedTextField</code> is not being edited and either
 212      * the value is not-null, or the value is null and a null formatter has
 213      * has not been specified.
 214      *
 215      * @param atf JFormattedTextField.AbstractFormatter to use when the
 216      *            JFormattedTextField does not have focus
 217      */
 218     public void setDisplayFormatter(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(JFormattedTextField.AbstractFormatter atf) {
 245         editFormat = atf;
 246     }
 247 
 248     /**
 249      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
 250      * if the <code>JFormattedTextField</code> is being edited and either
 251      * the value is not-null, or the value is null and a null formatter has
 252      * has not been specified.
 253      *
 254      * @return JFormattedTextField.AbstractFormatter to use when the
 255      *         component has focus
 256      */
 257     public JFormattedTextField.AbstractFormatter getEditFormatter() {
 258         return editFormat;
 259     }
 260 
 261     /**
 262      * Sets the formatter to use if the value of the JFormattedTextField is
 263      * null.
 264      *
 265      * @param atf JFormattedTextField.AbstractFormatter to use when
 266      * the value of the JFormattedTextField is null.
 267      */
 268     public void setNullFormatter(JFormattedTextField.AbstractFormatter atf) {
 269         nullFormat = atf;
 270     }
 271 
 272     /**
 273      * Returns the formatter to use if the value is null.
 274      *
 275      * @return JFormattedTextField.AbstractFormatter to use when the value is
 276      *         null
 277      */
 278     public JFormattedTextField.AbstractFormatter getNullFormatter() {
 279         return nullFormat;
 280     }
 281 
 282     /**
 283      * Returns either the default formatter, display formatter, editor
 284      * formatter or null formatter based on the state of the
 285      * JFormattedTextField.
 286      *
 287      * @param source JFormattedTextField requesting
 288      *               JFormattedTextField.AbstractFormatter
 289      * @return JFormattedTextField.AbstractFormatter to handle
 290      *         formatting duties.
 291      */
 292     public JFormattedTextField.AbstractFormatter getFormatter(
 293                      JFormattedTextField source) {
 294         JFormattedTextField.AbstractFormatter format = null;
 295 
 296         if (source == null) {
 297             return null;
 298         }
 299         Object value = source.getValue();
 300 
 301         if (value == null) {
 302             format = getNullFormatter();
 303         }
 304         if (format == null) {
 305             if (source.hasFocus()) {
 306                 format = getEditFormatter();
 307             }
 308             else {
 309                 format = getDisplayFormatter();
 310             }
 311             if (format == null) {
 312                 format = getDefaultFormatter();
 313             }
 314         }
 315         return format;
 316     }
 317 }