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