jdk/src/share/classes/javax/swing/text/DefaultFormatter.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 21278 ef8a3a2a72f2
child 22574 7f8ce0c8c20a
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6102
diff changeset
     2
 * Copyright (c) 2000, 2010, 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: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 14220
diff changeset
    27
import sun.reflect.misc.ReflectUtil;
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 14220
diff changeset
    28
import sun.swing.SwingUtilities2;
14220
ba920e7e0ec0 7195194: Better data validation for Swing
rupashka
parents: 7668
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.text.ParseException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
    37
 * <code>DefaultFormatter</code> formats arbitrary objects. Formatting is done
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * by invoking the <code>toString</code> method. In order to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * value back to a String, your class must provide a constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * takes a String argument. If no single argument constructor that takes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * String is found, the returned value will be the String passed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>stringToValue</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Instances of <code>DefaultFormatter</code> can not be used in multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * instances of <code>JFormattedTextField</code>. To obtain a copy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * an already configured <code>DefaultFormatter</code>, use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <code>clone</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * 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: 14220
diff changeset
    54
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see javax.swing.JFormattedTextField.AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class DefaultFormatter extends JFormattedTextField.AbstractFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    implements Cloneable, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** Indicates if the value being edited must match the mask. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean allowsInvalid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /** If true, editing mode is in overwrite (or strikethough). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private boolean overwriteMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** If true, any time a valid edit happens commitEdit is invoked. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private boolean commitOnEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /** Class used to create new instances. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    74
    private Class<?> valueClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /** NavigationFilter that forwards calls back to DefaultFormatter. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private NavigationFilter navigationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /** DocumentFilter that forwards calls back to DefaultFormatter. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private DocumentFilter documentFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** Used during replace to track the region to replace. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    transient ReplaceHolder replaceHolder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Creates a DefaultFormatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public DefaultFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        overwriteMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        allowsInvalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Installs the <code>DefaultFormatter</code> onto a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * This will invoke <code>valueToString</code> to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * current value from the <code>JFormattedTextField</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * a String. This will then install the <code>Action</code>s from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <code>getActions</code>, the <code>DocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * returned from <code>getDocumentFilter</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>NavigationFilter</code> returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>getNavigationFilter</code> onto the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Subclasses will typically only need to override this if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * wish to install additional listeners on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * If there is a <code>ParseException</code> in converting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * current value to a String, this will set the text to an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * String, and mark the <code>JFormattedTextField</code> as being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * in an invalid state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * While this is a public method, this is typically only useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * for subclassers of <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>JFormattedTextField</code> will invoke this method at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * the appropriate times when the value changes, or its internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * state changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param ftf JFormattedTextField to format for, may be null indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *            uninstall from current JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void install(JFormattedTextField ftf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        super.install(ftf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        positionCursorAtInitialLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Sets when edits are published back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <code>JFormattedTextField</code>. If true, <code>commitEdit</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * is invoked after every valid edit (any time the text is edited). On
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * the other hand, if this is false than the <code>DefaultFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * does not publish edits back to the <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * As such, the only time the value of the <code>JFormattedTextField</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * will change is when <code>commitEdit</code> is invoked on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <code>JFormattedTextField</code>, typically when enter is pressed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * or focus leaves the <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
   140
     * @param commit Used to indicate when edits are committed back to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *               JTextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void setCommitsOnValidEdit(boolean commit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        commitOnEdit = commit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Returns when edits are published back to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
   151
     * @return true if edits are committed after every valid edit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public boolean getCommitsOnValidEdit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return commitOnEdit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Configures the behavior when inserting characters. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <code>overwriteMode</code> is true (the default), new characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * overwrite existing characters in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param overwriteMode Indicates if overwrite or overstrike mode is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void setOverwriteMode(boolean overwriteMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this.overwriteMode = overwriteMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns the behavior when inserting characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return true if newly inserted characters overwrite existing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public boolean getOverwriteMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return overwriteMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Sets whether or not the value being edited is allowed to be invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * for a length of time (that is, <code>stringToValue</code> throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * a <code>ParseException</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * It is often convenient to allow the user to temporarily input an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * invalid value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param allowsInvalid Used to indicate if the edited value must always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *        be valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void setAllowsInvalid(boolean allowsInvalid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        this.allowsInvalid = allowsInvalid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns whether or not the value being edited is allowed to be invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * for a length of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return false if the edited value must always be valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public boolean getAllowsInvalid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return allowsInvalid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Sets that class that is used to create new Objects. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * passed in class does not have a single argument constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * takes a String, String values will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param valueClass Class used to construct return value from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *        stringToValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void setValueClass(Class<?> valueClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        this.valueClass = valueClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Returns that class that is used to create new Objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
   216
     * @return Class used to construct return value from stringToValue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public Class<?> getValueClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return valueClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Converts the passed in String into an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <code>getValueClass</code> by way of the constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * takes a String argument. If <code>getValueClass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * returns null, the Class of the current value in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <code>JFormattedTextField</code> will be used. If this is null, a
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
   228
     * String will be returned. If the constructor throws an exception, a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <code>ParseException</code> will be thrown. If there is no single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * argument String constructor, <code>string</code> will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param string String to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return Object representation of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public Object stringToValue(String string) throws ParseException {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   237
        Class<?> vc = getValueClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (vc == null && ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Object value = ftf.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                vc = value.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (vc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            Constructor cons;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            try {
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 14220
diff changeset
   251
                ReflectUtil.checkPackageAccess(vc);
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 14220
diff changeset
   252
                SwingUtilities2.checkAccess(vc.getModifiers());
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 14220
diff changeset
   253
                cons = vc.getConstructor(new Class[]{String.class});
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                cons = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (cons != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                try {
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 14220
diff changeset
   261
                    SwingUtilities2.checkAccess(cons.getModifiers());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    return cons.newInstance(new Object[] { string });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                } catch (Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    throw new ParseException("Error creating instance", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Converts the passed in Object into a String by way of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <code>toString</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param value Value to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return String representation of value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public String valueToString(Object value) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns the <code>DocumentFilter</code> used to restrict the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * that can be input into the <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return DocumentFilter to restrict edits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    protected DocumentFilter getDocumentFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (documentFilter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            documentFilter = new DefaultDocumentFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return documentFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Returns the <code>NavigationFilter</code> used to restrict where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * cursor can be placed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return NavigationFilter to restrict navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    protected NavigationFilter getNavigationFilter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (navigationFilter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            navigationFilter = new DefaultNavigationFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return navigationFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Creates a copy of the DefaultFormatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @return copy of the DefaultFormatter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        DefaultFormatter formatter = (DefaultFormatter)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        formatter.navigationFilter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        formatter.documentFilter = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        formatter.replaceHolder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return formatter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Positions the cursor at the initial location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    void positionCursorAtInitialLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            ftf.setCaretPosition(getInitialVisualPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Returns the initial location to position the cursor at. This forwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * the call to <code>getNextNavigatableChar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    int getInitialVisualPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return getNextNavigatableChar(0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Subclasses should override this if they want cursor navigation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * to skip certain characters. A return value of false indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * the character at <code>offset</code> should be skipped when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * navigating throught the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    boolean isNavigatable(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns true if the text in <code>text</code> can be inserted.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * does not mean the text will ultimately be inserted, it is used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * text can trivially reject certain characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    boolean isLegalInsertText(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Returns the next editable character starting at offset incrementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * the offset by <code>direction</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private int getNextNavigatableChar(int offset, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int max = getFormattedTextField().getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        while (offset >= 0 && offset < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (isNavigatable(offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            offset += direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * A convenience methods to return the result of deleting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <code>deleteLength</code> characters at <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * and inserting <code>replaceString</code> at <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * in the current text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    String getReplaceString(int offset, int deleteLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                            String replaceString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        String string = getFormattedTextField().getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        String result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        result = string.substring(0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (replaceString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            result += replaceString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (offset + deleteLength < string.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            result += string.substring(offset + deleteLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return result;
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
     * Returns true if the operation described by <code>rh</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * result in a legal edit.  This may set the <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * field of <code>rh</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    boolean isValidEdit(ReplaceHolder rh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (!getAllowsInvalid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            String newString = getReplaceString(rh.offset, rh.length, rh.text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                rh.value = stringToValue(newString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Invokes <code>commitEdit</code> on the JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    void commitEdit() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        JFormattedTextField ftf = getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            ftf.commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
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
     * Pushes the value to the JFormattedTextField if the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * is valid and invokes <code>setEditValid</code> based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * validity of the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    void updateValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        updateValue(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Pushes the <code>value</code> to the editor if we are to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * commit on edits. If <code>value</code> is null, the current value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * will be obtained from the text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    void updateValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                String string = getFormattedTextField().getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                value = stringToValue(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if (getCommitsOnValidEdit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                commitEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            setEditValid(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            setEditValid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Returns the next cursor position from offset by incrementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * <code>direction</code>. This uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <code>getNextNavigatableChar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * as well as constraining the location to the max position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    int getNextCursorPosition(int offset, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        int newOffset = getNextNavigatableChar(offset, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        int max = getFormattedTextField().getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (!getAllowsInvalid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (direction == -1 && offset == newOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                // Case where hit backspace and only characters before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                // offset are fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                newOffset = getNextNavigatableChar(newOffset, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                if (newOffset >= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    newOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            else if (direction == 1 && newOffset >= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                // Don't go beyond last editable character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                newOffset = getNextNavigatableChar(max - 1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                if (newOffset < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    newOffset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return newOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Resets the cursor by using getNextCursorPosition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    void repositionCursor(int offset, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        getFormattedTextField().getCaret().setDot(getNextCursorPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                                  (offset, direction));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
   503
     * Finds the next navigable character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    int getNextVisualPositionFrom(JTextComponent text, int pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                  Position.Bias bias, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                  Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                                           throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        int value = text.getUI().getNextVisualPositionFrom(text, pos, bias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                                           direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (value == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (!getAllowsInvalid() && (direction == SwingConstants.EAST ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                    direction == SwingConstants.WEST)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            int last = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            while (!isNavigatable(value) && value != last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                last = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                value = text.getUI().getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                              text, value, bias, direction,biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            int max = getFormattedTextField().getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            if (last == value || value == max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                if (value == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    value = getInitialVisualPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                if (value >= max && max > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    // Pending: should not assume forward!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    value = getNextNavigatableChar(max - 1, -1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Returns true if the edit described by <code>rh</code> will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * in a legal value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    boolean canReplace(ReplaceHolder rh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        return isValidEdit(rh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * DocumentFilter method, funnels into <code>replace</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    void replace(DocumentFilter.FilterBypass fb, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                     int length, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                     AttributeSet attrs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        ReplaceHolder rh = getReplaceHolder(fb, offset, length, text, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        replace(rh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * If the edit described by <code>rh</code> is legal, this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * return true, commit the edit (if necessary) and update the cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * position.  This forwards to <code>canReplace</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <code>isLegalInsertText</code> as necessary to determine if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * the edit is in fact legal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * All of the DocumentFilter methods funnel into here, you should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * generally only have to override this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    boolean replace(ReplaceHolder rh) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        boolean valid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        int direction = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (rh.length > 0 && (rh.text == null || rh.text.length() == 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
               (getFormattedTextField().getSelectionStart() != rh.offset ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                   rh.length > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            direction = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
6102
06e0e43b3f09 6462562: InternationalFormatter inserts text incorrectly
peterz
parents: 5506
diff changeset
   579
        if (getOverwriteMode() && rh.text != null &&
06e0e43b3f09 6462562: InternationalFormatter inserts text incorrectly
peterz
parents: 5506
diff changeset
   580
            getFormattedTextField().getSelectedText() == null)
06e0e43b3f09 6462562: InternationalFormatter inserts text incorrectly
peterz
parents: 5506
diff changeset
   581
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            rh.length = Math.min(Math.max(rh.length, rh.text.length()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                                 rh.fb.getDocument().getLength() - rh.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if ((rh.text != null && !isLegalInsertText(rh.text)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            !canReplace(rh) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            (rh.length == 0 && (rh.text == null || rh.text.length() == 0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            valid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            int cursor = rh.cursorPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            rh.fb.replace(rh.offset, rh.length, rh.text, rh.attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (cursor == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                cursor = rh.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                if (direction == 1 && rh.text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    cursor = rh.offset + rh.text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            updateValue(rh.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            repositionCursor(cursor, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            invalidEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * NavigationFilter method, subclasses that wish finer control should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * override this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    void setDot(NavigationFilter.FilterBypass fb, int dot, Position.Bias bias){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        fb.setDot(dot, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * NavigationFilter method, subclasses that wish finer control should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * override this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    void moveDot(NavigationFilter.FilterBypass fb, int dot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                 Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        fb.moveDot(dot, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Returns the ReplaceHolder to track the replace of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    ReplaceHolder getReplaceHolder(DocumentFilter.FilterBypass fb, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                   int length, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                   AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (replaceHolder == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            replaceHolder = new ReplaceHolder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        replaceHolder.reset(fb, offset, length, text, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        return replaceHolder;
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
     * ReplaceHolder is used to track where insert/remove/replace is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * going to happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    static class ReplaceHolder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        /** The FilterBypass that was passed to the DocumentFilter method. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        DocumentFilter.FilterBypass fb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        /** Offset where the remove/insert is going to occur. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        /** Length of text to remove. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        /** The text to insert, may be null. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        String text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        /** AttributeSet to attach to text, may be null. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        AttributeSet attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        /** The resulting value, this may never be set. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        Object value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        /** Position the cursor should be adjusted from.  If this is -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * the cursor position will be adjusted based on the direction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * the replace (-1: offset, 1: offset + text.length()), otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         * the cursor position is adusted from this position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int cursorPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        void reset(DocumentFilter.FilterBypass fb, int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                   String text, AttributeSet attrs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            this.fb = fb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            this.length = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            this.attrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            this.value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            cursorPosition = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * NavigationFilter implementation that calls back to methods with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * same name in DefaultFormatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    private class DefaultNavigationFilter extends NavigationFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                             implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        public void setDot(FilterBypass fb, int dot, Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            JTextComponent tc = DefaultFormatter.this.getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (tc.composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                // bypass the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                fb.setDot(dot, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                DefaultFormatter.this.setDot(fb, dot, bias);
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
        public void moveDot(FilterBypass fb, int dot, Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            JTextComponent tc = DefaultFormatter.this.getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (tc.composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                // bypass the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                fb.moveDot(dot, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                DefaultFormatter.this.moveDot(fb, dot, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        public int getNextVisualPositionFrom(JTextComponent text, int pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                             Position.Bias bias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                                             int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                                             Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                           throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (text.composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                // forward the call to the UI directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return text.getUI().getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                        text, pos, bias, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                return DefaultFormatter.this.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                        text, pos, bias, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * DocumentFilter implementation that calls back to the replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * method of DefaultFormatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    private class DefaultDocumentFilter extends DocumentFilter implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                             Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        public void remove(FilterBypass fb, int offset, int length) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                              BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            JTextComponent tc = DefaultFormatter.this.getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (tc.composedTextExists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                // bypass the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                fb.remove(offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                DefaultFormatter.this.replace(fb, offset, length, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        public void insertString(FilterBypass fb, int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                 String string, AttributeSet attr) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                              BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            JTextComponent tc = DefaultFormatter.this.getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (tc.composedTextExists() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                Utilities.isComposedTextAttributeDefined(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                // bypass the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                fb.insertString(offset, string, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                DefaultFormatter.this.replace(fb, offset, 0, string, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        public void replace(FilterBypass fb, int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                                 String text, AttributeSet attr) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                              BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            JTextComponent tc = DefaultFormatter.this.getFormattedTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            if (tc.composedTextExists() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                Utilities.isComposedTextAttributeDefined(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                // bypass the filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                fb.replace(offset, length, text, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                DefaultFormatter.this.replace(fb, offset, length, text, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
}