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