jdk/src/java.desktop/share/classes/javax/swing/text/MaskFormatter.java
author ddehaven
Tue, 19 Aug 2014 10:32:16 -0700
changeset 26037 508779ce6619
parent 26001 jdk/src/share/classes/javax/swing/text/MaskFormatter.java@991e1be0b235
parent 25859 jdk/src/share/classes/javax/swing/text/MaskFormatter.java@3317bb8137f4
child 30462 507bcb03c954
permissions -rw-r--r--
Merge
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: 20458
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
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>MaskFormatter</code> is used to format and edit strings. The behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of a <code>MaskFormatter</code> is controlled by way of a String mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * that specifies the valid characters that can be contained at a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * location in the <code>Document</code> model. The following characters can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * be specified:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <table border=1 summary="Valid characters and their descriptions">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *    <th>Character&nbsp;</th>
20451
4cedf4e1560a 8025409: Fix javadoc comments errors and warning reported by doclint report
cl
parents: 7668
diff changeset
    43
 *    <th><p style="text-align:left">Description</p></th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *    <td>#</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *    <td>Any valid number, uses <code>Character.isDigit</code>.</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *    <td>'</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *    <td>Escape character, used to escape any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *       special formatting characters.</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    <td>U</td><td>Any character (<code>Character.isLetter</code>). All
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *        lowercase letters are mapped to upper case.</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tr><td>L</td><td>Any character (<code>Character.isLetter</code>). All
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *        upper case letters are mapped to lower case.</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <tr><td>A</td><td>Any character or number (<code>Character.isLetter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *       or <code>Character.isDigit</code>)</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <tr><td>?</td><td>Any character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *        (<code>Character.isLetter</code>).</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <tr><td>*</td><td>Anything.</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <tr><td>H</td><td>Any hex character (0-9, a-f or A-F).</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Typically characters correspond to one char, but in certain languages this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * is not the case. The mask is on a per character basis, and will thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * adjust to fit as many chars as are needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * You can further restrict the characters that can be input by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <code>setInvalidCharacters</code> and <code>setValidCharacters</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * methods. <code>setInvalidCharacters</code> allows you to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * which characters are not legal. <code>setValidCharacters</code> allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * you to specify which characters are valid. For example, the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * code block is equivalent to a mask of '0xHHH' with no invalid/valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * characters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * MaskFormatter formatter = new MaskFormatter("0x***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * formatter.setValidCharacters("0123456789abcdefABCDEF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * When initially formatting a value if the length of the string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * less than the length of the mask, two things can happen. Either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * the placeholder string will be used, or the placeholder character will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * be used. Precedence is given to the placeholder string. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   MaskFormatter formatter = new MaskFormatter("###-####");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   formatter.setPlaceholderCharacter('_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   formatter.getDisplayValue(tf, "123");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * Would result in the string '123-____'. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <code>setPlaceholder("555-1212")</code> was invoked '123-1212' would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * result. The placeholder String is only used on the initial format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * on subsequent formats only the placeholder character will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * If a <code>MaskFormatter</code> is configured to only allow valid characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * (<code>setAllowsInvalid(false)</code>) literal characters will be skipped as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * necessary when editing. Consider a <code>MaskFormatter</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * the mask "###-####" and current value "555-1212". Using the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * arrow key to navigate through the field will result in (| indicates the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * position of the caret):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *   |555-1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   5|55-1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *   55|5-1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *   555-|1212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *   555-1|212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * The '-' is a literal (non-editable) character, and is skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Similar behavior will result when editing. Consider inserting the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * '123-45' and '12345' into the <code>MaskFormatter</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * previous example. Both inserts will result in the same String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * '123-45__'. When <code>MaskFormatter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * is processing the insert at character position 3 (the '-'), two things can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * happen:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *   <li>If the inserted character is '-', it is accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *   <li>If the inserted character matches the mask for the next non-literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *       character, it is accepted at the new location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *   <li>Anything else results in an invalid edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * By default <code>MaskFormatter</code> will not allow invalid edits, you can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * change this with the <code>setAllowsInvalid</code> method, and will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * commit edits on valid edits (use the <code>setCommitsOnValidEdit</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * change this).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * By default, <code>MaskFormatter</code> is in overwrite mode. That is as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * characters are typed a new character is not inserted, rather the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * at the current location is replaced with the newly typed character. You
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * can change this behavior by way of the method <code>setOverwriteMode</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * 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: 20451
diff changeset
   146
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
   152
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
public class MaskFormatter extends DefaultFormatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // Potential values in mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final char DIGIT_KEY = '#';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final char LITERAL_KEY = '\'';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final char UPPERCASE_KEY = 'U';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final char LOWERCASE_KEY = 'L';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final char ALPHA_NUMERIC_KEY = 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static final char CHARACTER_KEY = '?';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static final char ANYTHING_KEY = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static final char HEX_KEY = 'H';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private static final MaskCharacter[] EmptyMaskChars = new MaskCharacter[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /** The user specified mask. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private String mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    private transient MaskCharacter[] maskChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /** List of valid characters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private String validCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /** List of invalid characters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private String invalidCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /** String used for the passed in value if it does not completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * fill the mask. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private String placeholderString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /** String used to represent characters not present. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private char placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /** Indicates if the value contains the literal characters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private boolean containsLiteralChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Creates a MaskFormatter with no mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public MaskFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        setAllowsInvalid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        containsLiteralChars = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        maskChars = EmptyMaskChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        placeholder = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Creates a <code>MaskFormatter</code> with the specified mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * A <code>ParseException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * will be thrown if <code>mask</code> is an invalid mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @throws ParseException if mask does not contain valid mask characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public MaskFormatter(String mask) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        setMask(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Sets the mask dictating the legal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * This will throw a <code>ParseException</code> if <code>mask</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @throws ParseException if mask does not contain valid mask characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public void setMask(String mask) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        this.mask = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        updateInternalMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Returns the formatting mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return Mask dictating legal character values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public String getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Allows for further restricting of the characters that can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Only characters specified in the mask, not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <code>invalidCharacters</code>, and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <code>validCharacters</code> will be allowed to be input. Passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * in null (the default) implies the valid characters are only bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * by the mask and the invalid characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param validCharacters If non-null, specifies legal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public void setValidCharacters(String validCharacters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        this.validCharacters = validCharacters;
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
     * Returns the valid characters that can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return Legal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public String getValidCharacters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return validCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Allows for further restricting of the characters that can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Only characters specified in the mask, not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <code>invalidCharacters</code>, and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <code>validCharacters</code> will be allowed to be input. Passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * in null (the default) implies the valid characters are only bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * by the mask and the valid characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param invalidCharacters If non-null, specifies illegal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public void setInvalidCharacters(String invalidCharacters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        this.invalidCharacters = invalidCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Returns the characters that are not valid for input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return illegal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public String getInvalidCharacters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return invalidCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Sets the string to use if the value does not completely fill in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * the mask. A null value implies the placeholder char should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param placeholder String used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public void setPlaceholder(String placeholder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        this.placeholderString = placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Returns the String to use if the value does not completely fill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return String used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public String getPlaceholder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return placeholderString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Sets the character to use in place of characters that are not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * in the value, ie the user must fill them in. The default value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * a space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * This is only applicable if the placeholder string has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * specified, or does not completely fill in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param placeholder Character used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public void setPlaceholderCharacter(char placeholder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        this.placeholder = placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Returns the character to use in place of characters that are not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * in the value, ie the user must fill them in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return Character used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public char getPlaceholderCharacter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * If true, the returned value and set value will also contain the literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * characters in mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * For example, if the mask is <code>'(###) ###-####'</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * current value is <code>'(415) 555-1212'</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <code>valueContainsLiteralCharacters</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * true <code>stringToValue</code> will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <code>'(415) 555-1212'</code>. On the other hand, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <code>valueContainsLiteralCharacters</code> is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <code>stringToValue</code> will return <code>'4155551212'</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param containsLiteralChars Used to indicate if literal characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *        mask should be returned in stringToValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void setValueContainsLiteralCharacters(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                        boolean containsLiteralChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        this.containsLiteralChars = containsLiteralChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns true if <code>stringToValue</code> should return literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * characters in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @return True if literal characters in mask should be returned in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *         stringToValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public boolean getValueContainsLiteralCharacters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return containsLiteralChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Parses the text, returning the appropriate Object representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * the String <code>value</code>. This strips the literal characters as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * necessary and invokes supers <code>stringToValue</code>, so that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * you have specified a value class (<code>setValueClass</code>) an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * instance of it will be created. This will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <code>ParseException</code> if the value does not match the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * mask.  Refer to {@link #setValueContainsLiteralCharacters} for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * on how literals are treated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param value String to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @see #setValueContainsLiteralCharacters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return Object representation of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public Object stringToValue(String value) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        return stringToValue(value, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Returns a String representation of the Object <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * based on the mask.  Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * {@link #setValueContainsLiteralCharacters} for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * on how literals are treated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param value Value to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see #setValueContainsLiteralCharacters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return String representation of value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public String valueToString(Object value) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        String sValue = (value == null) ? "" : value.toString();
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   388
        StringBuilder result = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        String placeholder = getPlaceholder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int[] valueCounter = { 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        append(result, sValue, valueCounter, placeholder, maskChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Installs the <code>DefaultFormatter</code> onto a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * This will invoke <code>valueToString</code> to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * current value from the <code>JFormattedTextField</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * a String. This will then install the <code>Action</code>s from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <code>getActions</code>, the <code>DocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * returned from <code>getDocumentFilter</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <code>NavigationFilter</code> returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <code>getNavigationFilter</code> onto the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Subclasses will typically only need to override this if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * wish to install additional listeners on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * If there is a <code>ParseException</code> in converting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * current value to a String, this will set the text to an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * String, and mark the <code>JFormattedTextField</code> as being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * in an invalid state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * While this is a public method, this is typically only useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * for subclassers of <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <code>JFormattedTextField</code> will invoke this method at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * the appropriate times when the value changes, or its internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * state changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param ftf JFormattedTextField to format for, may be null indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *            uninstall from current JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public void install(JFormattedTextField ftf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        super.install(ftf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // valueToString doesn't throw, but stringToValue does, need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // update the editValid state appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            Object value = ftf.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                stringToValue(valueToString(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                setEditValid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Actual <code>stringToValue</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * If <code>completeMatch</code> is true, the value must exactly match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * the mask, on the other hand if <code>completeMatch</code> is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * the string must match the mask or the placeholder string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private Object stringToValue(String value, boolean completeMatch) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                         ParseException {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   449
        int errorOffset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if ((errorOffset = getInvalidOffset(value, completeMatch)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (!getValueContainsLiteralCharacters()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                value = stripLiteralChars(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return super.stringToValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        throw new ParseException("stringToValue passed invalid value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                 errorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Returns -1 if the passed in string is valid, otherwise the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * the first bogus character is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    private int getInvalidOffset(String string, boolean completeMatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        int iLength = string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (iLength != getMaxLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            // trivially false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return iLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        for (int counter = 0, max = string.length(); counter < max; counter++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            char aChar = string.charAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (!isValidCharacter(counter, aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                (completeMatch || !isPlaceholder(counter, aChar))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                return counter;
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 -1;
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
     * Invokes <code>append</code> on the mask characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * <code>mask</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   487
    private void append(StringBuilder result, String value, int[] index,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                        String placeholder, MaskCharacter[] mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                          throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        for (int counter = 0, maxCounter = mask.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            mask[counter].append(result, value, index, placeholder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * Updates the internal representation of the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    private void updateInternalMask() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        String mask = getMask();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   501
        ArrayList<MaskCharacter> fixed = new ArrayList<MaskCharacter>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   502
        ArrayList<MaskCharacter> temp = fixed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if (mask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            for (int counter = 0, maxCounter = mask.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                 counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                char maskChar = mask.charAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                switch (maskChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                case DIGIT_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    temp.add(new DigitMaskCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                case LITERAL_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    if (++counter < maxCounter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        maskChar = mask.charAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        temp.add(new LiteralCharacter(maskChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    // else: Could actually throw if else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                case UPPERCASE_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    temp.add(new UpperCaseCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                case LOWERCASE_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    temp.add(new LowerCaseCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                case ALPHA_NUMERIC_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    temp.add(new AlphaNumericCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                case CHARACTER_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    temp.add(new CharCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                case ANYTHING_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    temp.add(new MaskCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                case HEX_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    temp.add(new HexCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    temp.add(new LiteralCharacter(maskChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (fixed.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            maskChars = EmptyMaskChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            maskChars = new MaskCharacter[fixed.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            fixed.toArray(maskChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Returns the MaskCharacter at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    private MaskCharacter getMaskCharacter(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        if (index >= maskChars.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return maskChars[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * Returns true if the placeholder character matches aChar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    private boolean isPlaceholder(int index, char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return (getPlaceholderCharacter() == aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Returns true if the passed in character matches the mask at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    private boolean isValidCharacter(int index, char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        return getMaskCharacter(index).isValidCharacter(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Returns true if the character at the specified location is a literal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * that is it can not be edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private boolean isLiteral(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return getMaskCharacter(index).isLiteral();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Returns the maximum length the text can be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    private int getMaxLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return maskChars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * Returns the literal character at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    private char getLiteral(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return getMaskCharacter(index).getChar((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Returns the character to insert at the specified location based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * the passed in character.  This provides a way to map certain sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * of characters to alternative values (lowercase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * uppercase...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    private char getCharacter(int index, char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return getMaskCharacter(index).getChar(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Removes the literal characters from the passed in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private String stripLiteralChars(String string) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   614
        StringBuilder sb = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        for (int counter = 0, max = string.length(); counter < max; counter++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (isLiteral(counter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                if (sb == null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   620
                    sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    if (counter > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                        sb.append(string.substring(0, counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                else if (last != counter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    sb.append(string.substring(last, counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            // Assume the mask isn't all literals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        else if (last != string.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                return string.substring(last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            sb.append(string.substring(last));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Subclassed to update the internal representation of the mask after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * the default read operation has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        throws IOException, ClassNotFoundException {
26001
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   652
        ObjectInputStream.GetField f = s.readFields();
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   653
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   654
        validCharacters = (String) f.get("validCharacters", null);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   655
        invalidCharacters = (String) f.get("invalidCharacters", null);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   656
        placeholderString = (String) f.get("placeholderString", null);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   657
        placeholder = f.get("placeholder", '\0');
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   658
        containsLiteralChars = f.get("containsLiteralChars", false);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   659
        mask = (String) f.get("mask", null);
991e1be0b235 8038937: Validate fields on Swing classes deserialization
alexsch
parents: 22574
diff changeset
   660
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            updateInternalMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            // assert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * Returns true if the MaskFormatter allows invalid, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * the offset is less than the max length and the character at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <code>offset</code> is a literal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    boolean isNavigatable(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if (!getAllowsInvalid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            return (offset < getMaxLength() && !isLiteral(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Returns true if the operation described by <code>rh</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * result in a legal edit.  This may set the <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * field of <code>rh</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * This is overriden to return true for a partial match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    boolean isValidEdit(ReplaceHolder rh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (!getAllowsInvalid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            String newString = getReplaceString(rh.offset, rh.length, rh.text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                rh.value = stringToValue(newString, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * This method does the following (assuming !getAllowsInvalid()):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * iterate over the max of the deleted region or the text length, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * each character:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * <li>If it is valid (matches the mask at the particular position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *     matches the literal character at the position), allow it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <li>Else if the position identifies a literal character, add it. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *     allows for the user to paste in text that may/may not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *     the literals.  For example, in pasing in 5551212 into ###-####
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *     when the 1 is evaluated it is illegal (by the first test), but there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *     is a literal at this position (-), so it is used.  NOTE: This has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *     a problem that you can't tell (without looking ahead) if you should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *     eat literals in the text. For example, if you paste '555' into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *     #5##, should it result in '5555' or '555 '? The current code will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *     result in the latter, which feels a little better as selecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *     text than pasting will always result in the same thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <li>Else if at the end of the inserted text, the replace the item with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *     the placeholder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * <li>Otherwise the insert is bogus and false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    boolean canReplace(ReplaceHolder rh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        // This method is rather long, but much of the burden is in
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   726
        // maintaining a String and swapping to a StringBuilder only if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // absolutely necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (!getAllowsInvalid()) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   729
            StringBuilder replace = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            String text = rh.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            int tl = (text != null) ? text.length() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            if (tl == 0 && rh.length == 1 && getFormattedTextField().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                              getSelectionStart() != rh.offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                // Backspace, adjust to actually delete next non-literal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                while (rh.offset > 0 && isLiteral(rh.offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    rh.offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            int max = Math.min(getMaxLength() - rh.offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                               Math.max(tl, rh.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            for (int counter = 0, textIndex = 0; counter < max; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                if (textIndex < tl && isValidCharacter(rh.offset + counter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                                   text.charAt(textIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    char aChar = text.charAt(textIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    if (aChar != getCharacter(rh.offset + counter, aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                        if (replace == null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   748
                            replace = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                            if (textIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                                replace.append(text.substring(0, textIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    if (replace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                        replace.append(getCharacter(rh.offset + counter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                                    aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    textIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                else if (isLiteral(rh.offset + counter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    if (replace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        replace.append(getLiteral(rh.offset + counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                        if (textIndex < tl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                            max = Math.min(max + 1, getMaxLength() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                                           rh.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    else if (textIndex > 0) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   769
                        replace = new StringBuilder(max);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        replace.append(text.substring(0, textIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                        replace.append(getLiteral(rh.offset + counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                        if (textIndex < tl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                            // Evaluate the character in text again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                            max = Math.min(max + 1, getMaxLength() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                           rh.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        else if (rh.cursorPosition == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                            rh.cursorPosition = rh.offset + counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                        rh.offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        rh.length--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        counter--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        max--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                else if (textIndex >= tl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    // placeholder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    if (replace == null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   791
                        replace = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                            replace.append(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    replace.append(getPlaceholderCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    if (tl > 0 && rh.cursorPosition == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                        rh.cursorPosition = rh.offset + counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    // Bogus character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (replace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                rh.text = replace.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            else if (text != null && rh.offset + tl > getMaxLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                rh.text = text.substring(0, getMaxLength() - rh.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (getOverwriteMode() && rh.text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                rh.length = rh.text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        return super.canReplace(rh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    // Interal classes used to represent the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    private class MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * Subclasses should override this returning true if the instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * represents a literal character. The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        public boolean isLiteral() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         * Returns true if <code>aChar</code> is a valid reprensentation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         * the receiver. The default implementation returns true if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
         * receiver represents a literal character and <code>getChar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * == aChar. Otherwise, this will return true is <code>aChar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * is contained in the valid characters and not contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         * in the invalid characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (isLiteral()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                return (getChar(aChar) == aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            aChar = getChar(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            String filter = getValidCharacters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            if (filter != null && filter.indexOf(aChar) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            filter = getInvalidCharacters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            if (filter != null && filter.indexOf(aChar) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         * Returns the character to insert for <code>aChar</code>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * default implementation returns <code>aChar</code>. Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         * that wish to do some sort of mapping, perhaps lower case to upper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
         * case should override this and do the necessary mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            return aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * Appends the necessary character in <code>formatting</code> at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         * <code>index</code> to <code>buff</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   874
        public void append(StringBuilder buff, String formatting, int[] index,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                           String placeholder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                          throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            boolean inString = index[0] < formatting.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            char aChar = inString ? formatting.charAt(index[0]) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            if (isLiteral()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                buff.append(getChar(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                if (getValueContainsLiteralCharacters()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    if (inString && aChar != getChar(aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                        throw new ParseException("Invalid character: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                                                 aChar, index[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    index[0] = index[0] + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            else if (index[0] >= formatting.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                if (placeholder != null && index[0] < placeholder.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    buff.append(placeholder.charAt(index[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    buff.append(getPlaceholderCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                index[0] = index[0] + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            else if (isValidCharacter(aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                buff.append(getChar(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                index[0] = index[0] + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                throw new ParseException("Invalid character: " + aChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                                         index[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * Used to represent a fixed character in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    private class LiteralCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        private char fixedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        public LiteralCharacter(char fixedChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            this.fixedChar = fixedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        public boolean isLiteral() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            return fixedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Represents a number, uses <code>Character.isDigit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    private class DigitMaskCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            return (Character.isDigit(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * Represents a character, lower case letters are mapped to upper case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * using <code>Character.toUpperCase</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    private class UpperCaseCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            return (Character.isLetter(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                     super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            return Character.toUpperCase(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * Represents a character, upper case letters are mapped to lower case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * using <code>Character.toLowerCase</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    private class LowerCaseCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            return (Character.isLetter(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                     super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            return Character.toLowerCase(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * Represents either a character or digit, uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * <code>Character.isLetterOrDigit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    private class AlphaNumericCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            return (Character.isLetterOrDigit(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                     super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * Represents a letter, uses <code>Character.isLetter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    private class CharCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            return (Character.isLetter(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                     super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Represents a hex character, 0-9a-fA-F. a-f is mapped to A-F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    private class HexCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return ((aChar == '0' || aChar == '1' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                     aChar == '2' || aChar == '3' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                     aChar == '4' || aChar == '5' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                     aChar == '6' || aChar == '7' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                     aChar == '8' || aChar == '9' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                     aChar == 'a' || aChar == 'A' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                     aChar == 'b' || aChar == 'B' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                     aChar == 'c' || aChar == 'C' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                     aChar == 'd' || aChar == 'D' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                     aChar == 'e' || aChar == 'E' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                     aChar == 'f' || aChar == 'F') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if (Character.isDigit(aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                return aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            return Character.toUpperCase(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
}