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