1 /*
   2  * Copyright (c) 2000, 2014, 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&trade;
  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     public DefaultFormatterFactory() {
 101     }
 102 
 103     /**
 104      * Creates a <code>DefaultFormatterFactory</code> with the specified
 105      * <code>JFormattedTextField.AbstractFormatter</code>.
 106      *
 107      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 108      *                      if a more specific
 109      *                      JFormattedTextField.AbstractFormatter can not be
 110      *                      found.
 111      */
 112     public DefaultFormatterFactory(JFormattedTextField.
 113                                        AbstractFormatter defaultFormat) {
 114         this(defaultFormat, null);
 115     }
 116 
 117     /**
 118      * Creates a <code>DefaultFormatterFactory</code> with the specified
 119      * <code>JFormattedTextField.AbstractFormatter</code>s.
 120      *
 121      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 122      *                      if a more specific
 123      *                      JFormattedTextField.AbstractFormatter can not be
 124      *                      found.
 125      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
 126      *                      when the JFormattedTextField does not have focus.
 127      */
 128     public DefaultFormatterFactory(
 129                      JFormattedTextField.AbstractFormatter defaultFormat,
 130                      JFormattedTextField.AbstractFormatter displayFormat) {
 131         this(defaultFormat, displayFormat, null);
 132     }
 133 
 134     /**
 135      * Creates a DefaultFormatterFactory with the specified
 136      * JFormattedTextField.AbstractFormatters.
 137      *
 138      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 139      *                      if a more specific
 140      *                      JFormattedTextField.AbstractFormatter can not be
 141      *                      found.
 142      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
 143      *                      when the JFormattedTextField does not have focus.
 144      * @param editFormat    JFormattedTextField.AbstractFormatter to be used
 145      *                      when the JFormattedTextField has focus.
 146      */
 147     public DefaultFormatterFactory(
 148                    JFormattedTextField.AbstractFormatter defaultFormat,
 149                    JFormattedTextField.AbstractFormatter displayFormat,
 150                    JFormattedTextField.AbstractFormatter editFormat) {
 151         this(defaultFormat, displayFormat, editFormat, null);
 152     }
 153 
 154     /**
 155      * Creates a DefaultFormatterFactory with the specified
 156      * JFormattedTextField.AbstractFormatters.
 157      *
 158      * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
 159      *                      if a more specific
 160      *                      JFormattedTextField.AbstractFormatter can not be
 161      *                      found.
 162      * @param displayFormat JFormattedTextField.AbstractFormatter to be used
 163      *                      when the JFormattedTextField does not have focus.
 164      * @param editFormat    JFormattedTextField.AbstractFormatter to be used
 165      *                      when the JFormattedTextField has focus.
 166      * @param nullFormat    JFormattedTextField.AbstractFormatter to be used
 167      *                      when the JFormattedTextField has a null value.
 168      */
 169     public DefaultFormatterFactory(
 170                   JFormattedTextField.AbstractFormatter defaultFormat,
 171                   JFormattedTextField.AbstractFormatter displayFormat,
 172                   JFormattedTextField.AbstractFormatter editFormat,
 173                   JFormattedTextField.AbstractFormatter nullFormat) {
 174         this.defaultFormat = defaultFormat;
 175         this.displayFormat = displayFormat;
 176         this.editFormat = editFormat;
 177         this.nullFormat = nullFormat;
 178     }
 179 
 180     /**
 181      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use as
 182      * a last resort, eg in case a display, edit or null
 183      * <code>JFormattedTextField.AbstractFormatter</code> has not been
 184      * specified.
 185      *
 186      * @param atf JFormattedTextField.AbstractFormatter used if a more
 187      *            specific is not specified
 188      */
 189     public void setDefaultFormatter(JFormattedTextField.AbstractFormatter atf){
 190         defaultFormat = atf;
 191     }
 192 
 193     /**
 194      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
 195      * as a last resort, eg in case a display, edit or null
 196      * <code>JFormattedTextField.AbstractFormatter</code>
 197      * has not been specified.
 198      *
 199      * @return JFormattedTextField.AbstractFormatter used if a more specific
 200      *         one is not specified.
 201      */
 202     public JFormattedTextField.AbstractFormatter getDefaultFormatter() {
 203         return defaultFormat;
 204     }
 205 
 206     /**
 207      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
 208      * the <code>JFormattedTextField</code> is not being edited and either
 209      * the value is not-null, or the value is null and a null formatter has
 210      * has not been specified.
 211      *
 212      * @param atf JFormattedTextField.AbstractFormatter to use when the
 213      *            JFormattedTextField does not have focus
 214      */
 215     public void setDisplayFormatter(JFormattedTextField.AbstractFormatter atf){
 216         displayFormat = atf;
 217     }
 218 
 219     /**
 220      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
 221      * if the <code>JFormattedTextField</code> is not being edited and either
 222      * the value is not-null, or the value is null and a null formatter has
 223      * has not been specified.
 224      *
 225      * @return JFormattedTextField.AbstractFormatter to use when the
 226      *         JFormattedTextField does not have focus
 227      */
 228     public JFormattedTextField.AbstractFormatter getDisplayFormatter() {
 229         return displayFormat;
 230     }
 231 
 232     /**
 233      * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
 234      * the <code>JFormattedTextField</code> is being edited and either
 235      * the value is not-null, or the value is null and a null formatter has
 236      * has not been specified.
 237      *
 238      * @param atf JFormattedTextField.AbstractFormatter to use when the
 239      *            component has focus
 240      */
 241     public void setEditFormatter(JFormattedTextField.AbstractFormatter atf) {
 242         editFormat = atf;
 243     }
 244 
 245     /**
 246      * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
 247      * if the <code>JFormattedTextField</code> is being edited and either
 248      * the value is not-null, or the value is null and a null formatter has
 249      * has not been specified.
 250      *
 251      * @return JFormattedTextField.AbstractFormatter to use when the
 252      *         component has focus
 253      */
 254     public JFormattedTextField.AbstractFormatter getEditFormatter() {
 255         return editFormat;
 256     }
 257 
 258     /**
 259      * Sets the formatter to use if the value of the JFormattedTextField is
 260      * null.
 261      *
 262      * @param atf JFormattedTextField.AbstractFormatter to use when
 263      * the value of the JFormattedTextField is null.
 264      */
 265     public void setNullFormatter(JFormattedTextField.AbstractFormatter atf) {
 266         nullFormat = atf;
 267     }
 268 
 269     /**
 270      * Returns the formatter to use if the value is null.
 271      *
 272      * @return JFormattedTextField.AbstractFormatter to use when the value is
 273      *         null
 274      */
 275     public JFormattedTextField.AbstractFormatter getNullFormatter() {
 276         return nullFormat;
 277     }
 278 
 279     /**
 280      * Returns either the default formatter, display formatter, editor
 281      * formatter or null formatter based on the state of the
 282      * JFormattedTextField.
 283      *
 284      * @param source JFormattedTextField requesting
 285      *               JFormattedTextField.AbstractFormatter
 286      * @return JFormattedTextField.AbstractFormatter to handle
 287      *         formatting duties.
 288      */
 289     public JFormattedTextField.AbstractFormatter getFormatter(
 290                      JFormattedTextField source) {
 291         JFormattedTextField.AbstractFormatter format = null;
 292 
 293         if (source == null) {
 294             return null;
 295         }
 296         Object value = source.getValue();
 297 
 298         if (value == null) {
 299             format = getNullFormatter();
 300         }
 301         if (format == null) {
 302             if (source.hasFocus()) {
 303                 format = getEditFormatter();
 304             }
 305             else {
 306                 format = getDisplayFormatter();
 307             }
 308             if (format == null) {
 309                 format = getDefaultFormatter();
 310             }
 311         }
 312         return format;
 313     }
 314 }