jdk/src/share/classes/javax/swing/text/NumberFormatter.java
author darcy
Wed, 11 Jun 2014 13:25:15 -0700
changeset 25193 187a455af8f8
parent 22574 7f8ce0c8c20a
permissions -rw-r--r--
8043549: Fix raw and unchecked lint warnings in javax.swing.text.* Reviewed-by: prr
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: 1299
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: 1299
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: 1299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 7668
diff changeset
    30
import sun.reflect.misc.ReflectUtil;
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 7668
diff changeset
    31
import sun.swing.SwingUtilities2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>NumberFormatter</code> subclasses <code>InternationalFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * adding special behavior for numbers. Among the specializations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * (these are only used if the <code>NumberFormatter</code> does not display
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
    37
 * invalid numbers, for example, <code>setAllowsInvalid(false)</code>):
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   <li>Pressing +/- (- is determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *       <code>DecimalFormatSymbols</code> associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *       <code>DecimalFormat</code>) in any field but the exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *       field will attempt to change the sign of the number to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *       positive/negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   <li>Pressing +/- (- is determined from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *       <code>DecimalFormatSymbols</code> associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *       <code>DecimalFormat</code>) in the exponent field will
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
    47
 *       attempt to change the sign of the exponent to positive/negative.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * If you are displaying scientific numbers, you may wish to turn on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * overwrite mode, <code>setOverwriteMode(true)</code>. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * DecimalFormat decimalFormat = new DecimalFormat("0.000E0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * NumberFormatter textFormatter = new NumberFormatter(decimalFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * textFormatter.setOverwriteMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * textFormatter.setAllowsInvalid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * If you are going to allow the user to enter decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * values, you should either force the DecimalFormat to contain at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * one decimal (<code>#.0###</code>), or allow the value to be invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>setAllowsInvalid(true)</code>. Otherwise users may not be able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * input decimal values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>NumberFormatter</code> provides slightly different behavior to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <code>stringToValue</code> than that of its superclass. If you have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * specified a Class for values, {@link #setValueClass}, that is one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * of <code>Integer</code>, <code>Long</code>, <code>Float</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <code>Double</code>, <code>Byte</code> or <code>Short</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * the Format's <code>parseObject</code> returns an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <code>Number</code>, the corresponding instance of the value class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * will be created using the constructor appropriate for the primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * type the value class represents. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <code>setValueClass(Integer.class)</code> will cause the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * value to be created via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <code>new Integer(((Number)formatter.parseObject(string)).intValue())</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * This is typically useful if you
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * wish to set a min/max value as the various <code>Number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * implementations are generally not comparable to each other. This is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * useful if for some reason you need a specific <code>Number</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * implementation for your values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * 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: 7668
diff changeset
    88
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21278
diff changeset
    94
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
public class NumberFormatter extends InternationalFormatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /** The special characters from the Format instance. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private String specialChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Creates a <code>NumberFormatter</code> with the a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <code>NumberFormat</code> instance obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <code>NumberFormat.getNumberInstance()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public NumberFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this(NumberFormat.getNumberInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Creates a NumberFormatter with the specified Format instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param format Format used to dictate legal values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public NumberFormatter(NumberFormat format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        super(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        setFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        setAllowsInvalid(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        setCommitsOnValidEdit(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        setOverwriteMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Sets the format that dictates the legal values that can be edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * and displayed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * If you have used the nullary constructor the value of this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * will be determined for the current locale by way of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <code>NumberFormat.getNumberInstance()</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param format NumberFormat instance used to dictate legal values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public void setFormat(Format format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        super.setFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        DecimalFormatSymbols dfs = getDecimalFormatSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (dfs != null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   137
            StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            sb.append(dfs.getCurrencySymbol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            sb.append(dfs.getDecimalSeparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            sb.append(dfs.getGroupingSeparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            sb.append(dfs.getInfinity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            sb.append(dfs.getInternationalCurrencySymbol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            sb.append(dfs.getMinusSign());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            sb.append(dfs.getMonetaryDecimalSeparator());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            sb.append(dfs.getNaN());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            sb.append(dfs.getPercent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            sb.append('+');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            specialChars = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            specialChars = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Invokes <code>parseObject</code> on <code>f</code>, returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * its value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    Object stringToValue(String text, Format f) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        Object value = f.parseObject(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return convertValueToValueClass(value, getValueClass());
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
     * Converts the passed in value to the passed in class. This only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * works if <code>valueClass</code> is one of <code>Integer</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <code>Long</code>, <code>Float</code>, <code>Double</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <code>Byte</code> or <code>Short</code> and <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * is an instanceof <code>Number</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   176
    private Object convertValueToValueClass(Object value,
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   177
                                            Class<?> valueClass) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (valueClass != null && (value instanceof Number)) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   179
            Number numberValue = (Number)value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (valueClass == Integer.class) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   181
                return Integer.valueOf(numberValue.intValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            else if (valueClass == Long.class) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   184
                return Long.valueOf(numberValue.longValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            else if (valueClass == Float.class) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   187
                return Float.valueOf(numberValue.floatValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            else if (valueClass == Double.class) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   190
                return Double.valueOf(numberValue.doubleValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            else if (valueClass == Byte.class) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   193
                return Byte.valueOf(numberValue.byteValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            else if (valueClass == Short.class) {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   196
                return Short.valueOf(numberValue.shortValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return value;
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
     * Returns the character that is used to toggle to positive values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private char getPositiveSign() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return '+';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Returns the character that is used to toggle to negative values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private char getMinusSign() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        DecimalFormatSymbols dfs = getDecimalFormatSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (dfs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return dfs.getMinusSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return '-';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns the character that is used to toggle to negative values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private char getDecimalSeparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        DecimalFormatSymbols dfs = getDecimalFormatSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (dfs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return dfs.getDecimalSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns the DecimalFormatSymbols from the Format instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private DecimalFormatSymbols getDecimalFormatSymbols() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        Format f = getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (f instanceof DecimalFormat) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return ((DecimalFormat)f).getDecimalFormatSymbols();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Subclassed to return false if <code>text</code> contains in an invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * character to insert, that is, it is not a digit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * (<code>Character.isDigit()</code>) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * not one of the characters defined by the DecimalFormatSymbols.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    boolean isLegalInsertText(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (getAllowsInvalid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        for (int counter = text.length() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            char aChar = text.charAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (!Character.isDigit(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                           specialChars.indexOf(aChar) == -1){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Subclassed to treat the decimal separator, grouping separator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * exponent symbol, percent, permille, currency and sign as literals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   270
    boolean isLiteral(Map<?, ?> attrs) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (!super.isLiteral(attrs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (attrs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            int size = attrs.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (attrs.get(NumberFormat.Field.GROUPING_SEPARATOR) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                if (attrs.get(NumberFormat.Field.INTEGER) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (attrs.get(NumberFormat.Field.EXPONENT_SYMBOL) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (attrs.get(NumberFormat.Field.PERCENT) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (attrs.get(NumberFormat.Field.PERMILLE) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (attrs.get(NumberFormat.Field.CURRENCY) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (attrs.get(NumberFormat.Field.SIGN) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   298
            return size == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
   304
     * Subclassed to make the decimal separator navigable, as well
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * as making the character between the integer field and the next
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20854
diff changeset
   306
     * field navigable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    boolean isNavigatable(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (!super.isNavigatable(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // Don't skip the decimal, it causes wierd behavior
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   311
            return getBufferedChar(index) == getDecimalSeparator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Returns the first <code>NumberFormat.Field</code> starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <code>index</code> incrementing by <code>direction</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private NumberFormat.Field getFieldFrom(int index, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (isValidMask()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            int max = getFormattedTextField().getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            AttributedCharacterIterator iterator = getIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (index >= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                index += direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            while (index >= 0 && index < max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                iterator.setIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   331
                Map<?,?> attrs = iterator.getAttributes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (attrs != null && attrs.size() > 0) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   334
                    for (Object key : attrs.keySet()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        if (key instanceof NumberFormat.Field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                            return (NumberFormat.Field)key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                index += direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return null;
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
     * Overriden to toggle the value if the positive/minus sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * is inserted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    void replace(DocumentFilter.FilterBypass fb, int offset, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                String string, AttributeSet attr) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (!getAllowsInvalid() && length == 0 && string != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            string.length() == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            toggleSignIfNecessary(fb, offset, string.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        super.replace(fb, offset, length, string, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Will change the sign of the integer or exponent field if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <code>aChar</code> is the positive or minus sign. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * true if a sign change was attempted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private boolean toggleSignIfNecessary(DocumentFilter.FilterBypass fb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                              int offset, char aChar) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                              BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (aChar == getMinusSign() || aChar == getPositiveSign()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            NumberFormat.Field field = getFieldFrom(offset, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            Object newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                if (field == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    (field != NumberFormat.Field.EXPONENT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                     field != NumberFormat.Field.EXPONENT_SYMBOL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                     field != NumberFormat.Field.EXPONENT_SIGN)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    newValue = toggleSign((aChar == getPositiveSign()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    // exponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    newValue = toggleExponentSign(offset, aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                if (newValue != null && isValidValue(newValue, false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    int lc = getLiteralCountTo(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    String string = valueToString(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    fb.remove(0, fb.getDocument().getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    fb.insertString(0, string, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    updateValue(newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    repositionCursor(getLiteralCountTo(offset) -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                     lc + offset, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                invalidEdit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return false;
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
     * Invoked to toggle the sign. For this to work the value class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * must have a single arg constructor that takes a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    private Object toggleSign(boolean positive) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        Object value = stringToValue(getFormattedTextField().getText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            // toString isn't localized, so that using +/- should work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            // correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            String string = value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            if (string != null && string.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (positive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    if (string.charAt(0) == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        string = string.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    if (string.charAt(0) == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                        string = string.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    if (string.length() > 0 && string.charAt(0) != '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        string = "-" + string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                if (string != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   428
                    Class<?> valueClass = getValueClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    if (valueClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        valueClass = value.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    try {
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 7668
diff changeset
   434
                        ReflectUtil.checkPackageAccess(valueClass);
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 7668
diff changeset
   435
                        SwingUtilities2.checkAccess(valueClass.getModifiers());
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   436
                        Constructor<?> cons = valueClass.getConstructor(
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 22574
diff changeset
   437
                                              new Class<?>[] { String.class });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        if (cons != null) {
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 7668
diff changeset
   439
                            SwingUtilities2.checkAccess(cons.getModifiers());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                            return cons.newInstance(new Object[]{string});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    } catch (Throwable ex) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Invoked to toggle the sign of the exponent (for scientific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * numbers).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    private Object toggleExponentSign(int offset, char aChar) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                             BadLocationException, ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        String string = getFormattedTextField().getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        int replaceLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        int loc = getAttributeStart(NumberFormat.Field.EXPONENT_SIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        if (loc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            replaceLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            offset = loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (aChar == getPositiveSign()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            string = getReplaceString(offset, replaceLength, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            string = getReplaceString(offset, replaceLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                                      new String(new char[] { aChar }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return stringToValue(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}