jdk/src/share/classes/javax/swing/JFormattedTextField.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 22574 7f8ce0c8c20a
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 21278
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.im.InputContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.UIManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.UIResource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>JFormattedTextField</code> extends <code>JTextField</code> adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * support for formatting arbitrary values, as well as retrieving a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * object once the user has edited the text. The following illustrates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * configuring a <code>JFormattedTextField</code> to edit dates:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   JFormattedTextField ftf = new JFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   ftf.setValue(new Date());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Once a <code>JFormattedTextField</code> has been created, you can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * listen for editing changes by way of adding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * a <code>PropertyChangeListener</code> and listening for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <code>PropertyChangeEvent</code>s with the property name <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>JFormattedTextField</code> allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * configuring what action should be taken when focus is lost. The possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * configurations are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <table summary="Possible JFormattedTextField configurations and their descriptions">
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    57
 * <tr><th><p style="text-align:left">Value</p></th><th><p style="text-align:left">Description</p></th></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tr><td>JFormattedTextField.REVERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *            <td>Revert the display to match that of <code>getValue</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *                possibly losing the current edit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *        <tr><td>JFormattedTextField.COMMIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *            <td>Commits the current value. If the value being edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *                isn't considered a legal value by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *                <code>AbstractFormatter</code> that is, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *                <code>ParseException</code> is thrown, then the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *                will not change, and then edited value will persist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *        <tr><td>JFormattedTextField.COMMIT_OR_REVERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *            <td>Similar to <code>COMMIT</code>, but if the value isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *                legal, behave like <code>REVERT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *        <tr><td>JFormattedTextField.PERSIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *            <td>Do nothing, don't obtain a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *                <code>AbstractFormatter</code>, and don't update the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The default is <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * refer to {@link #setFocusLostBehavior} for more information on this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <code>JFormattedTextField</code> allows the focus to leave, even if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * the currently edited value is invalid. To lock the focus down while the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <code>JFormattedTextField</code> is an invalid edit state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * you can attach an <code>InputVerifier</code>. The following code snippet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * shows a potential implementation of such an <code>InputVerifier</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * public class FormattedTextFieldVerifier extends InputVerifier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     public boolean verify(JComponent input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         if (input instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *             JFormattedTextField ftf = (JFormattedTextField)input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *             AbstractFormatter formatter = ftf.getFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *             if (formatter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *                 String text = ftf.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *                 try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *                      formatter.stringToValue(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *                      return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *                  } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *                      return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *          return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      public boolean shouldYieldFocus(JComponent input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *          return verify(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * Alternatively, you could invoke <code>commitEdit</code>, which would also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * commit the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <code>JFormattedTextField</code> does not do the formatting it self,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * rather formatting is done through an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <code>JFormattedTextField.AbstractFormatter</code> which is obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * an instance of <code>JFormattedTextField.AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Instances of <code>JFormattedTextField.AbstractFormatter</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * notified when they become active by way of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <code>install</code> method, at which point the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <code>JFormattedTextField.AbstractFormatter</code> can install whatever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * it needs to, typically a <code>DocumentFilter</code>. Similarly when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <code>JFormattedTextField</code> no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * needs the <code>AbstractFormatter</code>, it will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <code>uninstall</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <code>JFormattedTextField</code> typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * queries the <code>AbstractFormatterFactory</code> for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <code>AbstractFormat</code> when it gains or loses focus. Although this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * can change based on the focus lost policy. If the focus lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * policy is <code>JFormattedTextField.PERSIST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * and the <code>JFormattedTextField</code> has been edited, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <code>AbstractFormatterFactory</code> will not be queried until the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   129
 * value has been committed. Similarly if the focus lost policy is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <code>JFormattedTextField.COMMIT</code> and an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * is thrown from <code>stringToValue</code>, the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   132
 * <code>AbstractFormatterFactory</code> will not be queried when focus is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * lost or gained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <code>JFormattedTextField.AbstractFormatter</code>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   136
 * is also responsible for determining when values are committed to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * the <code>JFormattedTextField</code>. Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <code>JFormattedTextField.AbstractFormatter</code>s will make new values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * available on every edit, and others will never commit the value. You can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * force the current value to be obtained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * from the current <code>JFormattedTextField.AbstractFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * by way of invoking <code>commitEdit</code>. <code>commitEdit</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * be invoked whenever return is pressed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * If an <code>AbstractFormatterFactory</code> has not been explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * set, one will be set based on the <code>Class</code> of the value type after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <code>setValue</code> has been invoked (assuming value is non-null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * For example, in the following code an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <code>AbstractFormatterFactory</code> and <code>AbstractFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * will be created to handle formatting of numbers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *   JFormattedTextField tf = new JFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *   tf.setValue(new Number(100));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <strong>Warning:</strong> As the <code>AbstractFormatter</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * typically install a <code>DocumentFilter</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <code>Document</code>, and a <code>NavigationFilter</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <code>JFormattedTextField</code> you should not install your own. If you do,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * you are likely to see odd behavior in that the editing policy of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <code>AbstractFormatter</code> will not be enforced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * 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
   174
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 22567
diff changeset
   180
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
public class JFormattedTextField extends JTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static final String uiClassID = "FormattedTextFieldUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static final Action[] defaultActions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            { new CommitAction(), new CancelAction() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Constant identifying that when focus is lost,
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   188
     * <code>commitEdit</code> should be invoked. If in committing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * new value a <code>ParseException</code> is thrown, the invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * value will remain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static final int COMMIT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Constant identifying that when focus is lost,
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   198
     * <code>commitEdit</code> should be invoked. If in committing the new
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * value a <code>ParseException</code> is thrown, the value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * reverted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public static final int COMMIT_OR_REVERT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Constant identifying that when focus is lost, editing value should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * be reverted to current value set on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static final int REVERT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Constant identifying that when focus is lost, the edited value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * should be left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static final int PERSIST = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Factory used to obtain an instance of AbstractFormatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private AbstractFormatterFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Object responsible for formatting the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private AbstractFormatter format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Last valid value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    private Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * True while the value being edited is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private boolean editValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Behavior when focus is lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private int focusLostBehavior;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Indicates the current value has been edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private boolean edited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Used to set the dirty state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private DocumentListener documentListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Masked used to set the AbstractFormatterFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private Object mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * ActionMap that the TextFormatter Actions are added to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private ActionMap textFormatterActionMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Indicates the input method composed text is in the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private boolean composedTextExists = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * A handler for FOCUS_LOST event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private FocusLostHandler focusLostHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Creates a <code>JFormattedTextField</code> with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <code>AbstractFormatterFactory</code>. Use <code>setMask</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <code>setFormatterFactory</code> to configure the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <code>JFormattedTextField</code> to edit a particular type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public JFormattedTextField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        enableEvents(AWTEvent.FOCUS_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        setFocusLostBehavior(COMMIT_OR_REVERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Creates a JFormattedTextField with the specified value. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * create an <code>AbstractFormatterFactory</code> based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * type of <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param value Initial value for the JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public JFormattedTextField(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Creates a <code>JFormattedTextField</code>. <code>format</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * wrapped in an appropriate <code>AbstractFormatter</code> which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * then wrapped in an <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param format Format used to look up an AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public JFormattedTextField(java.text.Format format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        setFormatterFactory(getDefaultFormatterFactory(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Creates a <code>JFormattedTextField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <code>AbstractFormatter</code>. The <code>AbstractFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * is placed in an <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param formatter AbstractFormatter to use for formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public JFormattedTextField(AbstractFormatter formatter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        this(new DefaultFormatterFactory(formatter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Creates a <code>JFormattedTextField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param factory AbstractFormatterFactory used for formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public JFormattedTextField(AbstractFormatterFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        setFormatterFactory(factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Creates a <code>JFormattedTextField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <code>AbstractFormatterFactory</code> and initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @param factory <code>AbstractFormatterFactory</code> used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *        formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param currentValue Initial value to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public JFormattedTextField(AbstractFormatterFactory factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                               Object currentValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        this(currentValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        setFormatterFactory(factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Sets the behavior when focus is lost. This will be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <code>JFormattedTextField.REVERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <code>JFormattedTextField.COMMIT</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <code>JFormattedTextField.PERSIST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Note that some <code>AbstractFormatter</code>s may push changes as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * they occur, so that the value of this will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * This will throw an <code>IllegalArgumentException</code> if the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * passed in is not one of the afore mentioned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * The default value of this property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <code>JFormattedTextField.COMMIT_OR_REVERT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @param behavior Identifies behavior when focus is lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws IllegalArgumentException if behavior is not one of the known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *         values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *  enum: COMMIT         JFormattedTextField.COMMIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *        COMMIT_OR_REVERT JFormattedTextField.COMMIT_OR_REVERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *        REVERT         JFormattedTextField.REVERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *        PERSIST        JFormattedTextField.PERSIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *  description: Behavior when component loses focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public void setFocusLostBehavior(int behavior) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (behavior != COMMIT && behavior != COMMIT_OR_REVERT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            behavior != PERSIST && behavior != REVERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new IllegalArgumentException("setFocusLostBehavior must be one of: JFormattedTextField.COMMIT, JFormattedTextField.COMMIT_OR_REVERT, JFormattedTextField.PERSIST or JFormattedTextField.REVERT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        focusLostBehavior = behavior;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Returns the behavior when focus is lost. This will be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <code>COMMIT_OR_REVERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <code>COMMIT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <code>REVERT</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <code>PERSIST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Note that some <code>AbstractFormatter</code>s may push changes as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * they occur, so that the value of this will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return returns behavior when focus is lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public int getFocusLostBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return focusLostBehavior;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Sets the <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <code>AbstractFormatterFactory</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * able to return an instance of <code>AbstractFormatter</code> that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * used to format a value for display, as well an enforcing an editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * If you have not explicitly set an <code>AbstractFormatterFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * by way of this method (or a constructor) an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <code>AbstractFormatterFactory</code> and consequently an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <code>AbstractFormatter</code> will be used based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <code>Class</code> of the value. <code>NumberFormatter</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * be used for <code>Number</code>s, <code>DateFormatter</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * be used for <code>Dates</code>, otherwise <code>DefaultFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param tf <code>AbstractFormatterFactory</code> used to lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *          instances of <code>AbstractFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * description: AbstractFormatterFactory, responsible for returning an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *              AbstractFormatter that can format the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public void setFormatterFactory(AbstractFormatterFactory tf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        AbstractFormatterFactory oldFactory = factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        factory = tf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        firePropertyChange("formatterFactory", oldFactory, tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        setValue(getValue(), true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Returns the current <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @see #setFormatterFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @return <code>AbstractFormatterFactory</code> used to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *         <code>AbstractFormatter</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    public AbstractFormatterFactory getFormatterFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * Sets the current <code>AbstractFormatter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * You should not normally invoke this, instead set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * <code>AbstractFormatterFactory</code> or set the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <code>JFormattedTextField</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * invoke this as the state of the <code>JFormattedTextField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * changes and requires the value to be reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <code>JFormattedTextField</code> passes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <code>AbstractFormatter</code> obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @see #setFormatterFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param format AbstractFormatter to use for formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * description: TextFormatter, responsible for formatting the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    protected void setFormatter(AbstractFormatter format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        AbstractFormatter oldFormat = this.format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        if (oldFormat != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            oldFormat.uninstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        setEditValid(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (format != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            format.install(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        setEdited(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        firePropertyChange("textFormatter", oldFormat, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Returns the <code>AbstractFormatter</code> that is used to format and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * parse the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @return AbstractFormatter used for formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public AbstractFormatter getFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Sets the value that will be formatted by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <code>AbstractFormatter</code> obtained from the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <code>AbstractFormatterFactory</code>. If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * <code>AbstractFormatterFactory</code> has been specified, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * attempt to create one based on the type of <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * The default value of this property is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param value Current value to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * description: The value to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (value != null && getFormatterFactory() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            setFormatterFactory(getDefaultFormatterFactory(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        setValue(value, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Returns the last valid value. Based on the editing policy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * the <code>AbstractFormatter</code> this may not return the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * value. The currently edited value can be obtained by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <code>commitEdit</code> followed by <code>getValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return Last valid value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Forces the current value to be taken from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * <code>AbstractFormatter</code> and set as the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * This has no effect if there is no current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * <code>AbstractFormatter</code> installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @throws ParseException if the <code>AbstractFormatter</code> is not able
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *         to format the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public void commitEdit() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        AbstractFormatter format = getFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (format != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            setValue(format.stringToValue(getText()), false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Sets the validity of the edit on the receiver. You should not normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * invoke this. This will be invoked by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <code>AbstractFormatter</code> as the user edits the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Not all formatters will allow the component to get into an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * state, and thus this may never be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Based on the look and feel this may visually change the state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param isValid boolean indicating if the currently edited value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *        valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * description: True indicates the edited value is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    private void setEditValid(boolean isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (isValid != editValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            editValid = isValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            firePropertyChange("editValid", Boolean.valueOf(!isValid),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                               Boolean.valueOf(isValid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * Returns true if the current value being edited is valid. The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * this is managed by the current <code>AbstractFormatter</code>, as such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * there is no public setter for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @return true if the current value being edited is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public boolean isEditValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        return editValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * Invoked when the user inputs an invalid value. This gives the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * component a chance to provide feedback. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * implementation beeps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    protected void invalidEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        UIManager.getLookAndFeel().provideErrorFeedback(JFormattedTextField.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Processes any input method events, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <code>InputMethodEvent.INPUT_METHOD_TEXT_CHANGED</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <code>InputMethodEvent.CARET_POSITION_CHANGED</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param e the <code>InputMethodEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @see InputMethodEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    protected void processInputMethodEvent(InputMethodEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        AttributedCharacterIterator text = e.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        int commitCount = e.getCommittedCharacterCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // Keep track of the composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            int begin = text.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            int end = text.getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            composedTextExists = ((end - begin) > commitCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            composedTextExists = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        super.processInputMethodEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Processes any focus events, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * <code>FocusEvent.FOCUS_GAINED</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * <code>FocusEvent.FOCUS_LOST</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param e the <code>FocusEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @see FocusEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    protected void processFocusEvent(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        super.processFocusEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        // ignore temporary focus event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (e.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (isEdited() && e.getID() == FocusEvent.FOCUS_LOST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            InputContext ic = getInputContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (focusLostHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                focusLostHandler = new FocusLostHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            // if there is a composed text, process it first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if ((ic != null) && composedTextExists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                ic.endComposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                EventQueue.invokeLater(focusLostHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                focusLostHandler.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        else if (!isEdited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            // reformat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            setValue(getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * FOCUS_LOST behavior implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    private class FocusLostHandler implements Runnable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            int fb = JFormattedTextField.this.getFocusLostBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            if (fb == JFormattedTextField.COMMIT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                fb == JFormattedTextField.COMMIT_OR_REVERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    JFormattedTextField.this.commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    // Give it a chance to reformat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    JFormattedTextField.this.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        JFormattedTextField.this.getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                } catch (ParseException pe) {
22567
5816a47fa4dd 8032047: Fix static lint warnings in client libraries
darcy
parents: 21278
diff changeset
   655
                    if (fb == JFormattedTextField.COMMIT_OR_REVERT) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        JFormattedTextField.this.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                            JFormattedTextField.this.getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            else if (fb == JFormattedTextField.REVERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                JFormattedTextField.this.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    JFormattedTextField.this.getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * Fetches the command list for the editor.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * the list of commands supported by the plugged-in UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * augmented by the collection of commands that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * editor itself supports.  These are useful for binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * to events, such as in a keymap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @return the command list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    public Action[] getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return TextAction.augmentList(super.getActions(), defaultActions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Gets the class ID for a UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return the string "FormattedTextFieldUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Associates the editor with a text document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * The currently registered factory is used to build a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * the document, which gets displayed by the editor after revalidation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * A PropertyChange event ("document") is propagated to each listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @param doc  the document to display/edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @see #getDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *  description: the text document model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public void setDocument(Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (documentListener != null && getDocument() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            getDocument().removeDocumentListener(documentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        super.setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        if (documentListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            documentListener = new DocumentHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        doc.addDocumentListener(documentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * See readObject and writeObject in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @param s Stream to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Resets the Actions that come from the TextFormatter to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <code>actions</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    private void setFormatterActions(Action[] actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            if (textFormatterActionMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                textFormatterActionMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            if (textFormatterActionMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                ActionMap map = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                textFormatterActionMap = new ActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                while (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    ActionMap parent = map.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                    if (parent instanceof UIResource || parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                        map.setParent(textFormatterActionMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        textFormatterActionMap.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    map = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            for (int counter = actions.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                 counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                Object key = actions[counter].getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    textFormatterActionMap.put(key, actions[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Does the setting of the value. If <code>createFormat</code> is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * this will also obtain a new <code>AbstractFormatter</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * current factory. The property change event will be fired if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * <code>firePC</code> is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    private void setValue(Object value, boolean createFormat, boolean firePC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        Object oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        if (createFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            AbstractFormatterFactory factory = getFormatterFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            AbstractFormatter atf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                atf = factory.getFormatter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                atf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            setFormatter(atf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            // Assumed to be valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            setEditValid(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        setEdited(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        if (firePC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            firePropertyChange("value", oldValue, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Sets the edited state of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    private void setEdited(boolean edited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        this.edited = edited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * Returns true if the receiver has been edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    private boolean isEdited() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        return edited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Returns an AbstractFormatterFactory suitable for the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Object type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    private AbstractFormatterFactory getDefaultFormatterFactory(Object type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (type instanceof DateFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            return new DefaultFormatterFactory(new DateFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                                               ((DateFormat)type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (type instanceof NumberFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            return new DefaultFormatterFactory(new NumberFormatter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                               (NumberFormat)type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (type instanceof Format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            return new DefaultFormatterFactory(new InternationalFormatter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                                               (Format)type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        if (type instanceof Date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            return new DefaultFormatterFactory(new DateFormatter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (type instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            AbstractFormatter displayFormatter = new NumberFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            ((NumberFormatter)displayFormatter).setValueClass(type.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            AbstractFormatter editFormatter = new NumberFormatter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                                  new DecimalFormat("#.#"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            ((NumberFormatter)editFormatter).setValueClass(type.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            return new DefaultFormatterFactory(displayFormatter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                                               displayFormatter,editFormatter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        return new DefaultFormatterFactory(new DefaultFormatter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Instances of <code>AbstractFormatterFactory</code> are used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <code>JFormattedTextField</code> to obtain instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * <code>AbstractFormatter</code> which in turn are used to format values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <code>AbstractFormatterFactory</code> can return different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <code>AbstractFormatter</code>s based on the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * <code>JFormattedTextField</code>, perhaps returning different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * <code>AbstractFormatter</code>s when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * <code>JFormattedTextField</code> has focus vs when it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * doesn't have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    public static abstract class AbstractFormatterFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
         * Returns an <code>AbstractFormatter</code> that can handle formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
         * of the passed in <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         * @param tf JFormattedTextField requesting AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         * @return AbstractFormatter to handle formatting duties, a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         *         return value implies the JFormattedTextField should behave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         *         like a normal JTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        public abstract AbstractFormatter getFormatter(JFormattedTextField tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * Instances of <code>AbstractFormatter</code> are used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <code>JFormattedTextField</code> to handle the conversion both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * from an Object to a String, and back from a String to an Object.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   882
     * <code>AbstractFormatter</code>s can also enforce editing policies,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * or navigation policies, or manipulate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * <code>JFormattedTextField</code> in any way it sees fit to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * enforce the desired policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * An <code>AbstractFormatter</code> can only be active in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * one <code>JFormattedTextField</code> at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * <code>JFormattedTextField</code> invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * <code>install</code> when it is ready to use it followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * by <code>uninstall</code> when done. Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * that wish to install additional state should override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * <code>install</code> and message super appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Subclasses must override the conversion methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * <code>stringToValue</code> and <code>valueToString</code>. Optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * they can override <code>getActions</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * <code>getNavigationFilter</code> and <code>getDocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * to restrict the <code>JFormattedTextField</code> in a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * Subclasses that allow the <code>JFormattedTextField</code> to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * a temporarily invalid state should invoke <code>setEditValid</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * at the appropriate times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    public static abstract class AbstractFormatter implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        private JFormattedTextField ftf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
         * Installs the <code>AbstractFormatter</code> onto a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
         * This will invoke <code>valueToString</code> to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         * current value from the <code>JFormattedTextField</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
         * a String. This will then install the <code>Action</code>s from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
         * <code>getActions</code>, the <code>DocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
         * returned from <code>getDocumentFilter</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         * <code>NavigationFilter</code> returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
         * <code>getNavigationFilter</code> onto the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
         * Subclasses will typically only need to override this if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
         * wish to install additional listeners on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         * If there is a <code>ParseException</code> in converting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * current value to a String, this will set the text to an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * String, and mark the <code>JFormattedTextField</code> as being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         * in an invalid state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * While this is a public method, this is typically only useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         * for subclassers of <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         * <code>JFormattedTextField</code> will invoke this method at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         * the appropriate times when the value changes, or its internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         * state changes.  You will only need to invoke this yourself if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         * you are subclassing <code>JFormattedTextField</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         * installing/uninstalling <code>AbstractFormatter</code> at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         * different time than <code>JFormattedTextField</code> does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         * @param ftf JFormattedTextField to format for, may be null indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         *            uninstall from current JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        public void install(JFormattedTextField ftf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            if (this.ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                uninstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            this.ftf = ftf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    ftf.setText(valueToString(ftf.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    ftf.setText("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    setEditValid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                installDocumentFilter(getDocumentFilter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                ftf.setNavigationFilter(getNavigationFilter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                ftf.setFormatterActions(getActions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * Uninstalls any state the <code>AbstractFormatter</code> may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * installed on the <code>JFormattedTextField</code>. This resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         * <code>DocumentFilter</code>, <code>NavigationFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         * and additional <code>Action</code>s installed on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        public void uninstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            if (this.ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                installDocumentFilter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                this.ftf.setNavigationFilter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                this.ftf.setFormatterActions(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * Parses <code>text</code> returning an arbitrary Object. Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         * formatters may return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
         * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         * @param text String to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         * @return Object representation of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        public abstract Object stringToValue(String text) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                                     ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * Returns the string value to display for <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * @param value Value to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         * @return String representation of value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        public abstract String valueToString(Object value) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                        ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         * Returns the current <code>JFormattedTextField</code> the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         * <code>AbstractFormatter</code> is installed on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         * @return JFormattedTextField formatting for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        protected JFormattedTextField getFormattedTextField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            return ftf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
         * This should be invoked when the user types an invalid character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
         * This forwards the call to the current JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        protected void invalidEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                ftf.invalidEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
         * Invoke this to update the <code>editValid</code> property of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
         * <code>JFormattedTextField</code>. If you an enforce a policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         * such that the <code>JFormattedTextField</code> is always in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         * valid state, you will never need to invoke this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         * @param valid Valid state of the JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        protected void setEditValid(boolean valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                ftf.setEditValid(valid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         * Subclass and override if you wish to provide a custom set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         * <code>Action</code>s. <code>install</code> will install these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         * on the <code>JFormattedTextField</code>'s <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * @return Array of Actions to install on JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        protected Action[] getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
         * Subclass and override if you wish to provide a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
         * <code>DocumentFilter</code> to restrict what can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
         * <code>install</code> will install the returned value onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
         * the <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
         * @return DocumentFilter to restrict edits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        protected DocumentFilter getDocumentFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
         * Subclass and override if you wish to provide a filter to restrict
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
         * where the user can navigate to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
         * <code>install</code> will install the returned value onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
         * the <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
         * @return NavigationFilter to restrict navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        protected NavigationFilter getNavigationFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
         * Clones the <code>AbstractFormatter</code>. The returned instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
         * is not associated with a <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         * @return Copy of the AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        protected Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            AbstractFormatter formatter = (AbstractFormatter)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            formatter.ftf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            return formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
         * Installs the <code>DocumentFilter</code> <code>filter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
         * onto the current <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         * @param filter DocumentFilter to install on the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        private void installDocumentFilter(DocumentFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                Document doc = ftf.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    ((AbstractDocument)doc).setDocumentFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                doc.putProperty(DocumentFilter.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * Used to commit the edit. This extends JTextField.NotifyAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * so that <code>isEnabled</code> is true while a JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * has focus, and extends <code>actionPerformed</code> to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * commitEdit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    static class CommitAction extends JTextField.NotifyAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                // Attempt to commit the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    ((JFormattedTextField)target).commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    ((JFormattedTextField)target).invalidEdit();
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1120
                    // value not committed, don't notify ActionListeners
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            // Super behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            super.actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                JFormattedTextField ftf = (JFormattedTextField)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                if (!ftf.isEdited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            return super.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * CancelAction will reset the value in the JFormattedTextField when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * <code>actionPerformed</code> is invoked. It will only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * enabled if the focused component is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    private static class CancelAction extends TextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        public CancelAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            super("reset-field-edit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                JFormattedTextField ftf = (JFormattedTextField)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                ftf.setValue(ftf.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                JFormattedTextField ftf = (JFormattedTextField)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                if (!ftf.isEdited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            return super.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * Sets the dirty state as the document changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    private class DocumentHandler implements DocumentListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        public void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            setEdited(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        public void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            setEdited(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        public void changedUpdate(DocumentEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
}