jdk/src/share/classes/javax/swing/JFormattedTextField.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 20458 f2423fb3fd19
child 22567 5816a47fa4dd
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2006, 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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
public class JFormattedTextField extends JTextField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private static final String uiClassID = "FormattedTextFieldUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static final Action[] defaultActions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            { new CommitAction(), new CancelAction() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Constant identifying that when focus is lost,
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   187
     * <code>commitEdit</code> should be invoked. If in committing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * new value a <code>ParseException</code> is thrown, the invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * value will remain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static final int COMMIT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Constant identifying that when focus is lost,
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
   197
     * <code>commitEdit</code> should be invoked. If in committing the new
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * value a <code>ParseException</code> is thrown, the value will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * reverted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static final int COMMIT_OR_REVERT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Constant identifying that when focus is lost, editing value should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * be reverted to current value set on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public static final int REVERT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Constant identifying that when focus is lost, the edited value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * should be left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see #setFocusLostBehavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public static final int PERSIST = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Factory used to obtain an instance of AbstractFormatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private AbstractFormatterFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Object responsible for formatting the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private AbstractFormatter format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Last valid value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    private Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * True while the value being edited is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private boolean editValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Behavior when focus is lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private int focusLostBehavior;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Indicates the current value has been edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private boolean edited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Used to set the dirty state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private DocumentListener documentListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Masked used to set the AbstractFormatterFactory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private Object mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * ActionMap that the TextFormatter Actions are added to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private ActionMap textFormatterActionMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Indicates the input method composed text is in the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private boolean composedTextExists = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * A handler for FOCUS_LOST event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private FocusLostHandler focusLostHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Creates a <code>JFormattedTextField</code> with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <code>AbstractFormatterFactory</code>. Use <code>setMask</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <code>setFormatterFactory</code> to configure the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <code>JFormattedTextField</code> to edit a particular type of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public JFormattedTextField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        enableEvents(AWTEvent.FOCUS_EVENT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        setFocusLostBehavior(COMMIT_OR_REVERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Creates a JFormattedTextField with the specified value. This will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * create an <code>AbstractFormatterFactory</code> based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * type of <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param value Initial value for the JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public JFormattedTextField(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        setValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Creates a <code>JFormattedTextField</code>. <code>format</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * wrapped in an appropriate <code>AbstractFormatter</code> which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * then wrapped in an <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param format Format used to look up an AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public JFormattedTextField(java.text.Format format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        setFormatterFactory(getDefaultFormatterFactory(format));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Creates a <code>JFormattedTextField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <code>AbstractFormatter</code>. The <code>AbstractFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * is placed in an <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param formatter AbstractFormatter to use for formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public JFormattedTextField(AbstractFormatter formatter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        this(new DefaultFormatterFactory(formatter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Creates a <code>JFormattedTextField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param factory AbstractFormatterFactory used for formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public JFormattedTextField(AbstractFormatterFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        setFormatterFactory(factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Creates a <code>JFormattedTextField</code> with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <code>AbstractFormatterFactory</code> and initial value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param factory <code>AbstractFormatterFactory</code> used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *        formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param currentValue Initial value to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public JFormattedTextField(AbstractFormatterFactory factory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                               Object currentValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        this(currentValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        setFormatterFactory(factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Sets the behavior when focus is lost. This will be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <code>JFormattedTextField.COMMIT_OR_REVERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <code>JFormattedTextField.REVERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * <code>JFormattedTextField.COMMIT</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <code>JFormattedTextField.PERSIST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Note that some <code>AbstractFormatter</code>s may push changes as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * they occur, so that the value of this will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * This will throw an <code>IllegalArgumentException</code> if the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * passed in is not one of the afore mentioned values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * The default value of this property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <code>JFormattedTextField.COMMIT_OR_REVERT</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param behavior Identifies behavior when focus is lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @throws IllegalArgumentException if behavior is not one of the known
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *         values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *  enum: COMMIT         JFormattedTextField.COMMIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *        COMMIT_OR_REVERT JFormattedTextField.COMMIT_OR_REVERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *        REVERT         JFormattedTextField.REVERT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *        PERSIST        JFormattedTextField.PERSIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *  description: Behavior when component loses focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public void setFocusLostBehavior(int behavior) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (behavior != COMMIT && behavior != COMMIT_OR_REVERT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            behavior != PERSIST && behavior != REVERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            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
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        focusLostBehavior = behavior;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Returns the behavior when focus is lost. This will be one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <code>COMMIT_OR_REVERT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <code>COMMIT</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <code>REVERT</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <code>PERSIST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Note that some <code>AbstractFormatter</code>s may push changes as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * they occur, so that the value of this will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return returns behavior when focus is lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public int getFocusLostBehavior() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return focusLostBehavior;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Sets the <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <code>AbstractFormatterFactory</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * able to return an instance of <code>AbstractFormatter</code> that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * used to format a value for display, as well an enforcing an editing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * If you have not explicitly set an <code>AbstractFormatterFactory</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * by way of this method (or a constructor) an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <code>AbstractFormatterFactory</code> and consequently an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <code>AbstractFormatter</code> will be used based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <code>Class</code> of the value. <code>NumberFormatter</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * be used for <code>Number</code>s, <code>DateFormatter</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * be used for <code>Dates</code>, otherwise <code>DefaultFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param tf <code>AbstractFormatterFactory</code> used to lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          instances of <code>AbstractFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * description: AbstractFormatterFactory, responsible for returning an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *              AbstractFormatter that can format the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public void setFormatterFactory(AbstractFormatterFactory tf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        AbstractFormatterFactory oldFactory = factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        factory = tf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        firePropertyChange("formatterFactory", oldFactory, tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        setValue(getValue(), true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Returns the current <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @see #setFormatterFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @return <code>AbstractFormatterFactory</code> used to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *         <code>AbstractFormatter</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public AbstractFormatterFactory getFormatterFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Sets the current <code>AbstractFormatter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * You should not normally invoke this, instead set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <code>AbstractFormatterFactory</code> or set the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * <code>JFormattedTextField</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * invoke this as the state of the <code>JFormattedTextField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * changes and requires the value to be reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * <code>JFormattedTextField</code> passes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * <code>AbstractFormatter</code> obtained from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * <code>AbstractFormatterFactory</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @see #setFormatterFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param format AbstractFormatter to use for formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * description: TextFormatter, responsible for formatting the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    protected void setFormatter(AbstractFormatter format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        AbstractFormatter oldFormat = this.format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (oldFormat != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            oldFormat.uninstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        setEditValid(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (format != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            format.install(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        setEdited(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        firePropertyChange("textFormatter", oldFormat, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Returns the <code>AbstractFormatter</code> that is used to format and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * parse the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @return AbstractFormatter used for formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public AbstractFormatter getFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Sets the value that will be formatted by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <code>AbstractFormatter</code> obtained from the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <code>AbstractFormatterFactory</code>. If no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <code>AbstractFormatterFactory</code> has been specified, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * attempt to create one based on the type of <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * The default value of this property is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param value Current value to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * description: The value to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void setValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (value != null && getFormatterFactory() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            setFormatterFactory(getDefaultFormatterFactory(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        setValue(value, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Returns the last valid value. Based on the editing policy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * the <code>AbstractFormatter</code> this may not return the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * value. The currently edited value can be obtained by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * <code>commitEdit</code> followed by <code>getValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return Last valid value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public Object getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Forces the current value to be taken from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <code>AbstractFormatter</code> and set as the current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * This has no effect if there is no current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <code>AbstractFormatter</code> installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @throws ParseException if the <code>AbstractFormatter</code> is not able
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *         to format the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public void commitEdit() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        AbstractFormatter format = getFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (format != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            setValue(format.stringToValue(getText()), false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
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
     * Sets the validity of the edit on the receiver. You should not normally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * invoke this. This will be invoked by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <code>AbstractFormatter</code> as the user edits the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Not all formatters will allow the component to get into an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * state, and thus this may never be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Based on the look and feel this may visually change the state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @param isValid boolean indicating if the currently edited value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *        valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *       bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *   attribute: visualUpdate true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * description: True indicates the edited value is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    private void setEditValid(boolean isValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        if (isValid != editValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            editValid = isValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            firePropertyChange("editValid", Boolean.valueOf(!isValid),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                               Boolean.valueOf(isValid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
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
     * Returns true if the current value being edited is valid. The value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * this is managed by the current <code>AbstractFormatter</code>, as such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * there is no public setter for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @return true if the current value being edited is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public boolean isEditValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return editValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Invoked when the user inputs an invalid value. This gives the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * component a chance to provide feedback. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * implementation beeps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    protected void invalidEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        UIManager.getLookAndFeel().provideErrorFeedback(JFormattedTextField.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Processes any input method events, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <code>InputMethodEvent.INPUT_METHOD_TEXT_CHANGED</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <code>InputMethodEvent.CARET_POSITION_CHANGED</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @param e the <code>InputMethodEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @see InputMethodEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    protected void processInputMethodEvent(InputMethodEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        AttributedCharacterIterator text = e.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        int commitCount = e.getCommittedCharacterCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        // Keep track of the composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            int begin = text.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            int end = text.getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            composedTextExists = ((end - begin) > commitCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            composedTextExists = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        super.processInputMethodEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Processes any focus events, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * <code>FocusEvent.FOCUS_GAINED</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * <code>FocusEvent.FOCUS_LOST</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param e the <code>FocusEvent</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @see FocusEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    protected void processFocusEvent(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        super.processFocusEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // ignore temporary focus event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (e.isTemporary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        if (isEdited() && e.getID() == FocusEvent.FOCUS_LOST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            InputContext ic = getInputContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if (focusLostHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                focusLostHandler = new FocusLostHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            // if there is a composed text, process it first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if ((ic != null) && composedTextExists) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                ic.endComposition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                EventQueue.invokeLater(focusLostHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                focusLostHandler.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        else if (!isEdited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            // reformat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            setValue(getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
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
     * FOCUS_LOST behavior implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    private class FocusLostHandler implements Runnable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            int fb = JFormattedTextField.this.getFocusLostBehavior();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (fb == JFormattedTextField.COMMIT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                fb == JFormattedTextField.COMMIT_OR_REVERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    JFormattedTextField.this.commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    // Give it a chance to reformat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    JFormattedTextField.this.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        JFormattedTextField.this.getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    if (fb == JFormattedTextField.this.COMMIT_OR_REVERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                        JFormattedTextField.this.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                            JFormattedTextField.this.getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            else if (fb == JFormattedTextField.REVERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                JFormattedTextField.this.setValue(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    JFormattedTextField.this.getValue(), true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            }
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
     * Fetches the command list for the editor.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * the list of commands supported by the plugged-in UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * augmented by the collection of commands that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * editor itself supports.  These are useful for binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * to events, such as in a keymap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @return the command list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public Action[] getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return TextAction.augmentList(super.getActions(), defaultActions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Gets the class ID for a UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * @return the string "FormattedTextFieldUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * Associates the editor with a text document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * The currently registered factory is used to build a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * the document, which gets displayed by the editor after revalidation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * A PropertyChange event ("document") is propagated to each listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @param doc  the document to display/edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @see #getDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @beaninfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *  description: the text document model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *        bound: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *       expert: true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    public void setDocument(Document doc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        if (documentListener != null && getDocument() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            getDocument().removeDocumentListener(documentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        super.setDocument(doc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        if (documentListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            documentListener = new DocumentHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        doc.addDocumentListener(documentListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * See readObject and writeObject in JComponent for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param s Stream to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            }
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
     * Resets the Actions that come from the TextFormatter to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <code>actions</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    private void setFormatterActions(Action[] actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (actions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (textFormatterActionMap != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                textFormatterActionMap.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if (textFormatterActionMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                ActionMap map = getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                textFormatterActionMap = new ActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                while (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    ActionMap parent = map.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    if (parent instanceof UIResource || parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        map.setParent(textFormatterActionMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                        textFormatterActionMap.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    map = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            for (int counter = actions.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                 counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                Object key = actions[counter].getValue(Action.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    textFormatterActionMap.put(key, actions[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                }
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
     * Does the setting of the value. If <code>createFormat</code> is true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * this will also obtain a new <code>AbstractFormatter</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * current factory. The property change event will be fired if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <code>firePC</code> is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    private void setValue(Object value, boolean createFormat, boolean firePC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        Object oldValue = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (createFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            AbstractFormatterFactory factory = getFormatterFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            AbstractFormatter atf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                atf = factory.getFormatter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                atf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            setFormatter(atf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            // Assumed to be valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            setEditValid(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        setEdited(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if (firePC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            firePropertyChange("value", oldValue, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
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
     * Sets the edited state of the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    private void setEdited(boolean edited) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        this.edited = edited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * Returns true if the receiver has been edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    private boolean isEdited() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        return edited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Returns an AbstractFormatterFactory suitable for the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * Object type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    private AbstractFormatterFactory getDefaultFormatterFactory(Object type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (type instanceof DateFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return new DefaultFormatterFactory(new DateFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                                               ((DateFormat)type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (type instanceof NumberFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            return new DefaultFormatterFactory(new NumberFormatter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                                               (NumberFormat)type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (type instanceof Format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return new DefaultFormatterFactory(new InternationalFormatter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                                               (Format)type));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (type instanceof Date) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            return new DefaultFormatterFactory(new DateFormatter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        if (type instanceof Number) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            AbstractFormatter displayFormatter = new NumberFormatter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            ((NumberFormatter)displayFormatter).setValueClass(type.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            AbstractFormatter editFormatter = new NumberFormatter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                                  new DecimalFormat("#.#"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            ((NumberFormatter)editFormatter).setValueClass(type.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            return new DefaultFormatterFactory(displayFormatter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                               displayFormatter,editFormatter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return new DefaultFormatterFactory(new DefaultFormatter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
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
     * Instances of <code>AbstractFormatterFactory</code> are used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <code>JFormattedTextField</code> to obtain instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <code>AbstractFormatter</code> which in turn are used to format values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * <code>AbstractFormatterFactory</code> can return different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <code>AbstractFormatter</code>s based on the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <code>JFormattedTextField</code>, perhaps returning different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * <code>AbstractFormatter</code>s when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * <code>JFormattedTextField</code> has focus vs when it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * doesn't have focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    public static abstract class AbstractFormatterFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
         * Returns an <code>AbstractFormatter</code> that can handle formatting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
         * of the passed in <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         * @param tf JFormattedTextField requesting AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         * @return AbstractFormatter to handle formatting duties, a null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         *         return value implies the JFormattedTextField should behave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         *         like a normal JTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        public abstract AbstractFormatter getFormatter(JFormattedTextField tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    }
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
     * Instances of <code>AbstractFormatter</code> are used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * <code>JFormattedTextField</code> to handle the conversion both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * 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
   881
     * <code>AbstractFormatter</code>s can also enforce editing policies,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * or navigation policies, or manipulate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <code>JFormattedTextField</code> in any way it sees fit to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * enforce the desired policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * An <code>AbstractFormatter</code> can only be active in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * one <code>JFormattedTextField</code> at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <code>JFormattedTextField</code> invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * <code>install</code> when it is ready to use it followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * by <code>uninstall</code> when done. Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * that wish to install additional state should override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * <code>install</code> and message super appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * Subclasses must override the conversion methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * <code>stringToValue</code> and <code>valueToString</code>. Optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * they can override <code>getActions</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * <code>getNavigationFilter</code> and <code>getDocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * to restrict the <code>JFormattedTextField</code> in a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * Subclasses that allow the <code>JFormattedTextField</code> to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * a temporarily invalid state should invoke <code>setEditValid</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * at the appropriate times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    public static abstract class AbstractFormatter implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        private JFormattedTextField ftf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
         * Installs the <code>AbstractFormatter</code> onto a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
         * This will invoke <code>valueToString</code> to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
         * current value from the <code>JFormattedTextField</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         * a String. This will then install the <code>Action</code>s from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
         * <code>getActions</code>, the <code>DocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
         * returned from <code>getDocumentFilter</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
         * <code>NavigationFilter</code> returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
         * <code>getNavigationFilter</code> onto the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
         * Subclasses will typically only need to override this if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
         * wish to install additional listeners on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         * If there is a <code>ParseException</code> in converting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         * current value to a String, this will set the text to an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * String, and mark the <code>JFormattedTextField</code> as being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * in an invalid state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         * While this is a public method, this is typically only useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * for subclassers of <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         * <code>JFormattedTextField</code> will invoke this method at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         * the appropriate times when the value changes, or its internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         * state changes.  You will only need to invoke this yourself if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         * you are subclassing <code>JFormattedTextField</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         * installing/uninstalling <code>AbstractFormatter</code> at a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         * different time than <code>JFormattedTextField</code> does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         * @param ftf JFormattedTextField to format for, may be null indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         *            uninstall from current JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        public void install(JFormattedTextField ftf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (this.ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                uninstall();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            this.ftf = ftf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    ftf.setText(valueToString(ftf.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                    ftf.setText("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                    setEditValid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                installDocumentFilter(getDocumentFilter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                ftf.setNavigationFilter(getNavigationFilter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                ftf.setFormatterActions(getActions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            }
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
         * Uninstalls any state the <code>AbstractFormatter</code> may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * installed on the <code>JFormattedTextField</code>. This resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * <code>DocumentFilter</code>, <code>NavigationFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         * and additional <code>Action</code>s installed on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        public void uninstall() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            if (this.ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                installDocumentFilter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                this.ftf.setNavigationFilter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                this.ftf.setFormatterActions(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            }
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
         * Parses <code>text</code> returning an arbitrary Object. Some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * formatters may return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
         * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
         * @param text String to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         * @return Object representation of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        public abstract Object stringToValue(String text) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                     ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * Returns the string value to display for <code>value</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         * @param value Value to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * @return String representation of value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        public abstract String valueToString(Object value) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                        ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
         * Returns the current <code>JFormattedTextField</code> the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         * <code>AbstractFormatter</code> is installed on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         * @return JFormattedTextField formatting for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        protected JFormattedTextField getFormattedTextField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            return ftf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
         * This should be invoked when the user types an invalid character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
         * This forwards the call to the current JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        protected void invalidEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                ftf.invalidEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            }
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
         * Invoke this to update the <code>editValid</code> property of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
         * <code>JFormattedTextField</code>. If you an enforce a policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
         * such that the <code>JFormattedTextField</code> is always in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         * valid state, you will never need to invoke this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
         * @param valid Valid state of the JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        protected void setEditValid(boolean valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                ftf.setEditValid(valid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
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
         * Subclass and override if you wish to provide a custom set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
         * <code>Action</code>s. <code>install</code> will install these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
         * on the <code>JFormattedTextField</code>'s <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         * @return Array of Actions to install on JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        protected Action[] getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
         * Subclass and override if you wish to provide a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
         * <code>DocumentFilter</code> to restrict what can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
         * <code>install</code> will install the returned value onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
         * the <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
         * @return DocumentFilter to restrict edits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        protected DocumentFilter getDocumentFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
         * Subclass and override if you wish to provide a filter to restrict
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
         * where the user can navigate to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
         * <code>install</code> will install the returned value onto
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
         * the <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
         * @return NavigationFilter to restrict navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        protected NavigationFilter getNavigationFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
         * Clones the <code>AbstractFormatter</code>. The returned instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
         * is not associated with a <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         * @return Copy of the AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        protected Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            AbstractFormatter formatter = (AbstractFormatter)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            formatter.ftf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            return formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
         * Installs the <code>DocumentFilter</code> <code>filter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
         * onto the current <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
         * @param filter DocumentFilter to install on the Document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        private void installDocumentFilter(DocumentFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                Document doc = ftf.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    ((AbstractDocument)doc).setDocumentFilter(filter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                doc.putProperty(DocumentFilter.class, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            }
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
     * Used to commit the edit. This extends JTextField.NotifyAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * so that <code>isEnabled</code> is true while a JFormattedTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * has focus, and extends <code>actionPerformed</code> to invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * commitEdit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    static class CommitAction extends JTextField.NotifyAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                // Attempt to commit the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    ((JFormattedTextField)target).commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                    ((JFormattedTextField)target).invalidEdit();
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20458
diff changeset
  1119
                    // value not committed, don't notify ActionListeners
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            // Super behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            super.actionPerformed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                JFormattedTextField ftf = (JFormattedTextField)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                if (!ftf.isEdited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            return super.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        }
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
     * CancelAction will reset the value in the JFormattedTextField when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * <code>actionPerformed</code> is invoked. It will only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * enabled if the focused component is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    private static class CancelAction extends TextAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        public CancelAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            super("reset-field-edit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                JFormattedTextField ftf = (JFormattedTextField)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                ftf.setValue(ftf.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            JTextComponent target = getFocusedComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            if (target instanceof JFormattedTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                JFormattedTextField ftf = (JFormattedTextField)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                if (!ftf.isEdited()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            return super.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        }
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
     * Sets the dirty state as the document changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    private class DocumentHandler implements DocumentListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        public void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            setEdited(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        public void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            setEdited(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        public void changedUpdate(DocumentEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
}