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