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