jdk/src/share/classes/javax/swing/text/DefaultFormatterFactory.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 22574 7f8ce0c8c20a
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    67
 * of all JavaBeans&trade;
2
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
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    75
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public class DefaultFormatterFactory extends JFormattedTextField.AbstractFormatterFactory implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Default <code>AbstractFormatter</code> to use if a more specific one has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * not been specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private JFormattedTextField.AbstractFormatter defaultFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>JFormattedTextField.AbstractFormatter</code> to use for display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private JFormattedTextField.AbstractFormatter displayFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <code>JFormattedTextField.AbstractFormatter</code> to use for editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private JFormattedTextField.AbstractFormatter editFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <code>JFormattedTextField.AbstractFormatter</code> to use if the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private JFormattedTextField.AbstractFormatter nullFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public DefaultFormatterFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates a <code>DefaultFormatterFactory</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <code>JFormattedTextField.AbstractFormatter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *                      if a more specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *                      JFormattedTextField.AbstractFormatter can not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *                      found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public DefaultFormatterFactory(JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                       AbstractFormatter defaultFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        this(defaultFormat, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Creates a <code>DefaultFormatterFactory</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>JFormattedTextField.AbstractFormatter</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *                      if a more specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *                      JFormattedTextField.AbstractFormatter can not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *                      found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param displayFormat JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *                      when the JFormattedTextField does not have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public DefaultFormatterFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                     JFormattedTextField.AbstractFormatter defaultFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                     JFormattedTextField.AbstractFormatter displayFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this(defaultFormat, displayFormat, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Creates a DefaultFormatterFactory with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * JFormattedTextField.AbstractFormatters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *                      if a more specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *                      JFormattedTextField.AbstractFormatter can not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *                      found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param displayFormat JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *                      when the JFormattedTextField does not have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param editFormat    JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *                      when the JFormattedTextField has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public DefaultFormatterFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                   JFormattedTextField.AbstractFormatter defaultFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                   JFormattedTextField.AbstractFormatter displayFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                   JFormattedTextField.AbstractFormatter editFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this(defaultFormat, displayFormat, editFormat, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Creates a DefaultFormatterFactory with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * JFormattedTextField.AbstractFormatters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param defaultFormat JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *                      if a more specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *                      JFormattedTextField.AbstractFormatter can not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                      found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param displayFormat JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *                      when the JFormattedTextField does not have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param editFormat    JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *                      when the JFormattedTextField has focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param nullFormat    JFormattedTextField.AbstractFormatter to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *                      when the JFormattedTextField has a null value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public DefaultFormatterFactory(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                  JFormattedTextField.AbstractFormatter defaultFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                  JFormattedTextField.AbstractFormatter displayFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                  JFormattedTextField.AbstractFormatter editFormat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                  JFormattedTextField.AbstractFormatter nullFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        this.defaultFormat = defaultFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this.displayFormat = displayFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        this.editFormat = editFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.nullFormat = nullFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * a last resort, eg in case a display, edit or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <code>JFormattedTextField.AbstractFormatter</code> has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param atf JFormattedTextField.AbstractFormatter used if a more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *            specific is not specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public void setDefaultFormatter(JFormattedTextField.AbstractFormatter atf){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        defaultFormat = atf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * as a last resort, eg in case a display, edit or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <code>JFormattedTextField.AbstractFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * has not been specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return JFormattedTextField.AbstractFormatter used if a more specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *         one is not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public JFormattedTextField.AbstractFormatter getDefaultFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return defaultFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * the <code>JFormattedTextField</code> is not being edited and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the value is not-null, or the value is null and a null formatter has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * has not been specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param atf JFormattedTextField.AbstractFormatter to use when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *            JFormattedTextField does not have focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public void setDisplayFormatter(JFormattedTextField.AbstractFormatter atf){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        displayFormat = atf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * if the <code>JFormattedTextField</code> is not being edited and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * the value is not-null, or the value is null and a null formatter has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * has not been specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return JFormattedTextField.AbstractFormatter to use when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *         JFormattedTextField does not have focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public JFormattedTextField.AbstractFormatter getDisplayFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return displayFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Sets the <code>JFormattedTextField.AbstractFormatter</code> to use if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * the <code>JFormattedTextField</code> is being edited and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * the value is not-null, or the value is null and a null formatter has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * has not been specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param atf JFormattedTextField.AbstractFormatter to use when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *            component has focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public void setEditFormatter(JFormattedTextField.AbstractFormatter atf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        editFormat = atf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Returns the <code>JFormattedTextField.AbstractFormatter</code> to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * if the <code>JFormattedTextField</code> is being edited and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * the value is not-null, or the value is null and a null formatter has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * has not been specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @return JFormattedTextField.AbstractFormatter to use when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         component has focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public JFormattedTextField.AbstractFormatter getEditFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return editFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Sets the formatter to use if the value of the JFormattedTextField is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param atf JFormattedTextField.AbstractFormatter to use when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * the value of the JFormattedTextField is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public void setNullFormatter(JFormattedTextField.AbstractFormatter atf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        nullFormat = atf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns the formatter to use if the value is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return JFormattedTextField.AbstractFormatter to use when the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *         null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public JFormattedTextField.AbstractFormatter getNullFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return nullFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Returns either the default formatter, display formatter, editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * formatter or null formatter based on the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param source JFormattedTextField requesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *               JFormattedTextField.AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return JFormattedTextField.AbstractFormatter to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         formatting duties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public JFormattedTextField.AbstractFormatter getFormatter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                     JFormattedTextField source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        JFormattedTextField.AbstractFormatter format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (source == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        Object value = source.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            format = getNullFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (format == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (source.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                format = getEditFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                format = getDisplayFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (format == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                format = getDefaultFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}