jdk/src/share/classes/javax/swing/text/MaskFormatter.java
author rupashka
Tue, 02 Nov 2010 13:32:00 +0300
changeset 7014 eb4fcf73ee99
parent 5506 202f599c92aa
child 7668 d4a77089c587
permissions -rw-r--r--
6432566: Replace usage of StringBuffer with StringBuilder in Swing Reviewed-by: malenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2000, 2008, 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>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *    <th><p align="left">Description</p></th>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
public class MaskFormatter extends DefaultFormatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // Potential values in mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private static final char DIGIT_KEY = '#';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static final char LITERAL_KEY = '\'';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private static final char UPPERCASE_KEY = 'U';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final char LOWERCASE_KEY = 'L';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static final char ALPHA_NUMERIC_KEY = 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final char CHARACTER_KEY = '?';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static final char ANYTHING_KEY = '*';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static final char HEX_KEY = 'H';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static final MaskCharacter[] EmptyMaskChars = new MaskCharacter[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /** The user specified mask. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private String mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private transient MaskCharacter[] maskChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /** List of valid characters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private String validCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /** List of invalid characters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private String invalidCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /** String used for the passed in value if it does not completely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * fill the mask. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private String placeholderString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /** String used to represent characters not present. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private char placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /** Indicates if the value contains the literal characters. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private boolean containsLiteralChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Creates a MaskFormatter with no mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public MaskFormatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        setAllowsInvalid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        containsLiteralChars = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        maskChars = EmptyMaskChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        placeholder = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Creates a <code>MaskFormatter</code> with the specified mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * A <code>ParseException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * will be thrown if <code>mask</code> is an invalid mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @throws ParseException if mask does not contain valid mask characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public MaskFormatter(String mask) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        setMask(mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Sets the mask dictating the legal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * This will throw a <code>ParseException</code> if <code>mask</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @throws ParseException if mask does not contain valid mask characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void setMask(String mask) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        this.mask = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        updateInternalMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Returns the formatting mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return Mask dictating legal character values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public String getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Allows for further restricting of the characters that can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Only characters specified in the mask, not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <code>invalidCharacters</code>, and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <code>validCharacters</code> will be allowed to be input. Passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * in null (the default) implies the valid characters are only bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * by the mask and the invalid characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param validCharacters If non-null, specifies legal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public void setValidCharacters(String validCharacters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        this.validCharacters = validCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns the valid characters that can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return Legal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public String getValidCharacters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return validCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Allows for further restricting of the characters that can be input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Only characters specified in the mask, not in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <code>invalidCharacters</code>, and in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <code>validCharacters</code> will be allowed to be input. Passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * in null (the default) implies the valid characters are only bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * by the mask and the valid characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param invalidCharacters If non-null, specifies illegal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public void setInvalidCharacters(String invalidCharacters) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        this.invalidCharacters = invalidCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Returns the characters that are not valid for input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return illegal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public String getInvalidCharacters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return invalidCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Sets the string to use if the value does not completely fill in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * the mask. A null value implies the placeholder char should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param placeholder String used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public void setPlaceholder(String placeholder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        this.placeholderString = placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Returns the String to use if the value does not completely fill
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return String used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public String getPlaceholder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return placeholderString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Sets the character to use in place of characters that are not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * in the value, ie the user must fill them in. The default value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * a space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * This is only applicable if the placeholder string has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * specified, or does not completely fill in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param placeholder Character used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void setPlaceholderCharacter(char placeholder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        this.placeholder = placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Returns the character to use in place of characters that are not present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * in the value, ie the user must fill them in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @return Character used when formatting if the value does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *        completely fill the mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    public char getPlaceholderCharacter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return placeholder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * If true, the returned value and set value will also contain the literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * characters in mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * For example, if the mask is <code>'(###) ###-####'</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * current value is <code>'(415) 555-1212'</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <code>valueContainsLiteralCharacters</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * true <code>stringToValue</code> will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * <code>'(415) 555-1212'</code>. On the other hand, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <code>valueContainsLiteralCharacters</code> is false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <code>stringToValue</code> will return <code>'4155551212'</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param containsLiteralChars Used to indicate if literal characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *        mask should be returned in stringToValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void setValueContainsLiteralCharacters(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                        boolean containsLiteralChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        this.containsLiteralChars = containsLiteralChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Returns true if <code>stringToValue</code> should return literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * characters in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return True if literal characters in mask should be returned in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *         stringToValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public boolean getValueContainsLiteralCharacters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return containsLiteralChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Parses the text, returning the appropriate Object representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * the String <code>value</code>. This strips the literal characters as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * necessary and invokes supers <code>stringToValue</code>, so that if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * you have specified a value class (<code>setValueClass</code>) an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * instance of it will be created. This will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>ParseException</code> if the value does not match the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * mask.  Refer to {@link #setValueContainsLiteralCharacters} for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * on how literals are treated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param value String to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see #setValueContainsLiteralCharacters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @return Object representation of text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public Object stringToValue(String value) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return stringToValue(value, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Returns a String representation of the Object <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * based on the mask.  Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * {@link #setValueContainsLiteralCharacters} for details
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * on how literals are treated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @throws ParseException if there is an error in the conversion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param value Value to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see #setValueContainsLiteralCharacters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @return String representation of value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public String valueToString(Object value) throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        String sValue = (value == null) ? "" : value.toString();
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   387
        StringBuilder result = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        String placeholder = getPlaceholder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        int[] valueCounter = { 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        append(result, sValue, valueCounter, placeholder, maskChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return result.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Installs the <code>DefaultFormatter</code> onto a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * This will invoke <code>valueToString</code> to convert the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * current value from the <code>JFormattedTextField</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * a String. This will then install the <code>Action</code>s from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <code>getActions</code>, the <code>DocumentFilter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * returned from <code>getDocumentFilter</code> and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <code>NavigationFilter</code> returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <code>getNavigationFilter</code> onto the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Subclasses will typically only need to override this if they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * wish to install additional listeners on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * If there is a <code>ParseException</code> in converting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * current value to a String, this will set the text to an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * String, and mark the <code>JFormattedTextField</code> as being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * in an invalid state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * While this is a public method, this is typically only useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * for subclassers of <code>JFormattedTextField</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <code>JFormattedTextField</code> will invoke this method at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * the appropriate times when the value changes, or its internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * state changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param ftf JFormattedTextField to format for, may be null indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *            uninstall from current JFormattedTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public void install(JFormattedTextField ftf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        super.install(ftf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // valueToString doesn't throw, but stringToValue does, need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // update the editValid state appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (ftf != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            Object value = ftf.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                stringToValue(valueToString(value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                setEditValid(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
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
     * Actual <code>stringToValue</code> implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * If <code>completeMatch</code> is true, the value must exactly match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * the mask, on the other hand if <code>completeMatch</code> is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * the string must match the mask or the placeholder string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    private Object stringToValue(String value, boolean completeMatch) throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                         ParseException {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   448
        int errorOffset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if ((errorOffset = getInvalidOffset(value, completeMatch)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            if (!getValueContainsLiteralCharacters()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                value = stripLiteralChars(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return super.stringToValue(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        throw new ParseException("stringToValue passed invalid value",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                                 errorOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Returns -1 if the passed in string is valid, otherwise the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * the first bogus character is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private int getInvalidOffset(String string, boolean completeMatch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        int iLength = string.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (iLength != getMaxLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            // trivially false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return iLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        for (int counter = 0, max = string.length(); counter < max; counter++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            char aChar = string.charAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (!isValidCharacter(counter, aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                (completeMatch || !isPlaceholder(counter, aChar))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Invokes <code>append</code> on the mask characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <code>mask</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   486
    private void append(StringBuilder result, String value, int[] index,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        String placeholder, MaskCharacter[] mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                          throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        for (int counter = 0, maxCounter = mask.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
             counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            mask[counter].append(result, value, index, placeholder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Updates the internal representation of the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private void updateInternalMask() throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        String mask = getMask();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   500
        ArrayList<MaskCharacter> fixed = new ArrayList<MaskCharacter>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   501
        ArrayList<MaskCharacter> temp = fixed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (mask != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            for (int counter = 0, maxCounter = mask.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                 counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                char maskChar = mask.charAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                switch (maskChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                case DIGIT_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    temp.add(new DigitMaskCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                case LITERAL_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    if (++counter < maxCounter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        maskChar = mask.charAt(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        temp.add(new LiteralCharacter(maskChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    // else: Could actually throw if else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                case UPPERCASE_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    temp.add(new UpperCaseCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                case LOWERCASE_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    temp.add(new LowerCaseCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                case ALPHA_NUMERIC_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                    temp.add(new AlphaNumericCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                case CHARACTER_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    temp.add(new CharCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                case ANYTHING_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    temp.add(new MaskCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                case HEX_KEY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    temp.add(new HexCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    temp.add(new LiteralCharacter(maskChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (fixed.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            maskChars = EmptyMaskChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            maskChars = new MaskCharacter[fixed.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            fixed.toArray(maskChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        }
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
     * Returns the MaskCharacter at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private MaskCharacter getMaskCharacter(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (index >= maskChars.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return maskChars[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Returns true if the placeholder character matches aChar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    private boolean isPlaceholder(int index, char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return (getPlaceholderCharacter() == aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * Returns true if the passed in character matches the mask at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private boolean isValidCharacter(int index, char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return getMaskCharacter(index).isValidCharacter(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Returns true if the character at the specified location is a literal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * that is it can not be edited.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    private boolean isLiteral(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return getMaskCharacter(index).isLiteral();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Returns the maximum length the text can be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    private int getMaxLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        return maskChars.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Returns the literal character at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    private char getLiteral(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return getMaskCharacter(index).getChar((char)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Returns the character to insert at the specified location based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * the passed in character.  This provides a way to map certain sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * of characters to alternative values (lowercase to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * uppercase...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    private char getCharacter(int index, char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return getMaskCharacter(index).getChar(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Removes the literal characters from the passed in string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    private String stripLiteralChars(String string) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   613
        StringBuilder sb = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        for (int counter = 0, max = string.length(); counter < max; counter++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            if (isLiteral(counter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                if (sb == null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   619
                    sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    if (counter > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                        sb.append(string.substring(0, counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                else if (last != counter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    sb.append(string.substring(last, counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                last = counter + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            // Assume the mask isn't all literals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        else if (last != string.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if (sb == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                return string.substring(last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            sb.append(string.substring(last));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * Subclassed to update the internal representation of the mask after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * the default read operation has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            updateInternalMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            // assert();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Returns true if the MaskFormatter allows invalid, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * the offset is less than the max length and the character at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * <code>offset</code> is a literal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    boolean isNavigatable(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if (!getAllowsInvalid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            return (offset < getMaxLength() && !isLiteral(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * Returns true if the operation described by <code>rh</code> will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * result in a legal edit.  This may set the <code>value</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * field of <code>rh</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * This is overriden to return true for a partial match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    boolean isValidEdit(ReplaceHolder rh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        if (!getAllowsInvalid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            String newString = getReplaceString(rh.offset, rh.length, rh.text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                rh.value = stringToValue(newString, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            } catch (ParseException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * This method does the following (assuming !getAllowsInvalid()):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * iterate over the max of the deleted region or the text length, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * each character:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * <li>If it is valid (matches the mask at the particular position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *     matches the literal character at the position), allow it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * <li>Else if the position identifies a literal character, add it. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *     allows for the user to paste in text that may/may not contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *     the literals.  For example, in pasing in 5551212 into ###-####
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *     when the 1 is evaluated it is illegal (by the first test), but there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *     is a literal at this position (-), so it is used.  NOTE: This has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *     a problem that you can't tell (without looking ahead) if you should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *     eat literals in the text. For example, if you paste '555' into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *     #5##, should it result in '5555' or '555 '? The current code will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *     result in the latter, which feels a little better as selecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *     text than pasting will always result in the same thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <li>Else if at the end of the inserted text, the replace the item with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *     the placeholder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * <li>Otherwise the insert is bogus and false is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    boolean canReplace(ReplaceHolder rh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        // 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
   717
        // maintaining a String and swapping to a StringBuilder only if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        // absolutely necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (!getAllowsInvalid()) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   720
            StringBuilder replace = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            String text = rh.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            int tl = (text != null) ? text.length() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            if (tl == 0 && rh.length == 1 && getFormattedTextField().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                              getSelectionStart() != rh.offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                // Backspace, adjust to actually delete next non-literal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                while (rh.offset > 0 && isLiteral(rh.offset)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    rh.offset--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            int max = Math.min(getMaxLength() - rh.offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                               Math.max(tl, rh.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            for (int counter = 0, textIndex = 0; counter < max; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                if (textIndex < tl && isValidCharacter(rh.offset + counter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                                                   text.charAt(textIndex))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    char aChar = text.charAt(textIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    if (aChar != getCharacter(rh.offset + counter, aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                        if (replace == null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   739
                            replace = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                            if (textIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                                replace.append(text.substring(0, textIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    if (replace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        replace.append(getCharacter(rh.offset + counter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                                                    aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    textIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                else if (isLiteral(rh.offset + counter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                    if (replace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                        replace.append(getLiteral(rh.offset + counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                        if (textIndex < tl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            max = Math.min(max + 1, getMaxLength() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                           rh.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    else if (textIndex > 0) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   760
                        replace = new StringBuilder(max);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                        replace.append(text.substring(0, textIndex));
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
                            // Evaluate the character in text again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                            max = Math.min(max + 1, getMaxLength() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                           rh.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                        else if (rh.cursorPosition == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                            rh.cursorPosition = rh.offset + counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        rh.offset++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                        rh.length--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        counter--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                        max--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                else if (textIndex >= tl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                    // placeholder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    if (replace == null) {
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   782
                        replace = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                            replace.append(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    replace.append(getPlaceholderCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    if (tl > 0 && rh.cursorPosition == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                        rh.cursorPosition = rh.offset + counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    // Bogus character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            if (replace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                rh.text = replace.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            else if (text != null && rh.offset + tl > getMaxLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                rh.text = text.substring(0, getMaxLength() - rh.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (getOverwriteMode() && rh.text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                rh.length = rh.text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        return super.canReplace(rh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    // Interal classes used to represent the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    private class MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * Subclasses should override this returning true if the instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         * represents a literal character. The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
         * returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        public boolean isLiteral() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * Returns true if <code>aChar</code> is a valid reprensentation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * the receiver. The default implementation returns true if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * receiver represents a literal character and <code>getChar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         * == aChar. Otherwise, this will return true is <code>aChar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         * is contained in the valid characters and not contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         * in the invalid characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if (isLiteral()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                return (getChar(aChar) == aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            aChar = getChar(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            String filter = getValidCharacters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            if (filter != null && filter.indexOf(aChar) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            filter = getInvalidCharacters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            if (filter != null && filter.indexOf(aChar) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * Returns the character to insert for <code>aChar</code>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         * default implementation returns <code>aChar</code>. Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * that wish to do some sort of mapping, perhaps lower case to upper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * case should override this and do the necessary mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            return aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
         * Appends the necessary character in <code>formatting</code> at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
         * <code>index</code> to <code>buff</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
         */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   865
        public void append(StringBuilder buff, String formatting, int[] index,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                           String placeholder)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                          throws ParseException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            boolean inString = index[0] < formatting.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            char aChar = inString ? formatting.charAt(index[0]) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            if (isLiteral()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                buff.append(getChar(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                if (getValueContainsLiteralCharacters()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                    if (inString && aChar != getChar(aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                        throw new ParseException("Invalid character: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                                                 aChar, index[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    index[0] = index[0] + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            else if (index[0] >= formatting.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                if (placeholder != null && index[0] < placeholder.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    buff.append(placeholder.charAt(index[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    buff.append(getPlaceholderCharacter());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                index[0] = index[0] + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            else if (isValidCharacter(aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                buff.append(getChar(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                index[0] = index[0] + 1;
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
                throw new ParseException("Invalid character: " + aChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                                         index[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * Used to represent a fixed character in the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    private class LiteralCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        private char fixedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        public LiteralCharacter(char fixedChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            this.fixedChar = fixedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        public boolean isLiteral() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return fixedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * Represents a number, uses <code>Character.isDigit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    private class DigitMaskCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            return (Character.isDigit(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    super.isValidCharacter(aChar));
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Represents a character, lower case letters are mapped to upper case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * using <code>Character.toUpperCase</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    private class UpperCaseCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            return (Character.isLetter(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                     super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            return Character.toUpperCase(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * Represents a character, upper case letters are mapped to lower case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * using <code>Character.toLowerCase</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    private class LowerCaseCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            return (Character.isLetter(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                     super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            return Character.toLowerCase(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Represents either a character or digit, uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * <code>Character.isLetterOrDigit</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    private class AlphaNumericCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            return (Character.isLetterOrDigit(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                     super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * Represents a letter, uses <code>Character.isLetter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    private class CharCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            return (Character.isLetter(aChar) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                     super.isValidCharacter(aChar));
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * Represents a hex character, 0-9a-fA-F. a-f is mapped to A-F
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    private class HexCharacter extends MaskCharacter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        public boolean isValidCharacter(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            return ((aChar == '0' || aChar == '1' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                     aChar == '2' || aChar == '3' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                     aChar == '4' || aChar == '5' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                     aChar == '6' || aChar == '7' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                     aChar == '8' || aChar == '9' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                     aChar == 'a' || aChar == 'A' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                     aChar == 'b' || aChar == 'B' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                     aChar == 'c' || aChar == 'C' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                     aChar == 'd' || aChar == 'D' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                     aChar == 'e' || aChar == 'E' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                     aChar == 'f' || aChar == 'F') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                    super.isValidCharacter(aChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        public char getChar(char aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if (Character.isDigit(aChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                return aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            return Character.toUpperCase(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
}