All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jidefx.scene.control.field.MaskTextField Maven / Gradle / Ivy

The newest version!
/*
 * @(#)MaskTextField.java 5/19/2013
 *
 * Copyright 2002 - 2013 JIDE Software Inc. All rights reserved.
 */

package jidefx.scene.control.field;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.*;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.IndexRange;
import javafx.scene.control.TextField;
import javafx.util.Callback;
import jidefx.scene.control.decoration.DecorationUtils;
import jidefx.scene.control.decoration.Decorator;
import jidefx.scene.control.decoration.PredefinedDecorators;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;

/**
 * {@code MaskTextField} is a {@code TextField} that can restrict the user input by applying a mask.
 * 

* Position-based Mask *

* Position-base mask works for all the editing cases when the input string has a fixed length and each character can be * restricted based on its position. To address this case, we allow you define the masks on a MaskTextField. *

* InputMask *

* MaskTextField has setInputMask(String mask) to set the mask we mentioned above. The pre-defined mask characters are: *

    *
  • A INPUT_MASK_LETTER ASCII alphabetic character required. A-Z, a-z. *
  • N INPUT_MASK_DIGIT_OR_LETTER ASCII alphanumeric character required. A-Z, a-z, 0-9.
  • *
  • X INPUT_MASK_ANY_NON_SPACE Any character required except spaces. *
  • H INPUT_MASK_HAX Hexadecimal character required. A-F, a-f, 0-9. *
  • D INPUT_MASK_DIGIT_NON_ZERO ASCII digit required. 1-9. *
  • 9 INPUT_MASK_DIGIT ASCII digit required. 0-9. *
  • 8 INPUT_MASK_DIGIT_0_TO_8 ASCII digit required. 0-8. *
  • 7, 6, 5, 4, 3, 2 and so on which means only allows from 0 to that number *
  • 2 INPUT_MASK_DIGIT_0_TO_2 ASCII digit required. 0-2. *
  • 1 INPUT_MASK_DIGIT_0_TO_1 ASCII digit required. 0-1, for example, a binary number *
  • 0 INPUT_MASK_DIGIT_ZERO 0 required *
  • G INPUT_MASK_DIGIT_GROUP Indicates a Group. This is the special one, we will discuss it later *
* If the setInitialText is not called, the InputMask will be used to create the initial text for the field. We create * it by removing all masks, replace them with spaces or the specified placeholder character, and leave the non-mask * characters where they are. The placeholder character can be set using setPlaceholderCharacter(char). See below. In * both cases, the InputMask is 999-99-9999. *

* ConversionMask *

* To make the grammar easy to understand, we strictly enforced that there is only one mask character per position for * the InputMask. But clearly, one character is not enough in some cases. So we also allow you to define several other * masks for other purposes. In addition to the Input Mask, we also have a separate Conversion Mask which will * automatically convert the entered character to another character. This mask is optional. It can be set using * setConversionMask(String mask). If not set, there will be no conversion. If you ever set the mask, please make sure * they have the exact same length as the InputMask, and have a valid conversion mask character at the exact position * where there is an InputMask character. *

    *
  • U CONVERSION_MASK_UPPER_CASE Uppercase required. If user enters a lowercase letter, it will be automatically * converted to the corresponding uppercase letter. *
  • L CONVERSION_MASK_LOWER_CASE Lowercase required. If user enters an uppercase letter, it will be automatically * converted to the corresponding lowercase letter. *
  • Any other undefined chars CONVERSION_MASK_IGNORE No conversion *
*

* {@code RequiredMask} *

* The RequiredMask is to indicate whether the character on a particular position is required. It is again optional. It * can be set using setRequiredMask(String mask). If not set, a valid non-space character is required on all the * positions. If you ever set the mask, please make sure they have the same length as the InputMask, and have a valid * required mask character at the exact position where there is an InputMask character. *

    *
  • R REQUIRED_MASK_REQUIRED Required. Users must enter a valid character that matches with the mask on this * position. *
  • Any other undefined chars REQUIRED_MASK_NOT_REQUIRED Not required. User can enter a space at this position. *
*/ @SuppressWarnings({"Convert2Lambda", "SpellCheckingInspection"}) public class MaskTextField extends TextField /*implements DecorationSupport */ { private static final String STYLE_CLASS_DEFAULT = "mask-combo-box"; //NON-NLS public static final char INPUT_MASK_LETTER = 'A'; public static final char INPUT_MASK_DIGIT_OR_LETTER = 'N'; public static final char INPUT_MASK_ANY_NON_SPACE = 'X'; public static final char INPUT_MASK_HAX = 'H'; public static final char INPUT_MASK_DIGIT_NON_ZERO = 'D'; public static final char INPUT_MASK_DIGIT = '9'; public static final char INPUT_MASK_DIGIT_0_TO_8 = '8'; public static final char INPUT_MASK_DIGIT_0_TO_7 = '7'; public static final char INPUT_MASK_DIGIT_0_TO_6 = '6'; public static final char INPUT_MASK_DIGIT_0_TO_5 = '5'; public static final char INPUT_MASK_DIGIT_0_TO_4 = '4'; public static final char INPUT_MASK_DIGIT_0_TO_3 = '3'; public static final char INPUT_MASK_DIGIT_0_TO_2 = '2'; public static final char INPUT_MASK_DIGIT_0_TO_1 = '1'; public static final char INPUT_MASK_DIGIT_ZERO = '0'; public static final char CONVERSION_MASK_UPPER_CASE = 'U'; public static final char CONVERSION_MASK_LOWER_CASE = 'L'; public static final char CONVERSION_MASK_IGNORE = '_'; public static final char REQUIRED_MASK_REQUIRED = 'R'; public static final char REQUIRED_MASK_NOT_REQUIRED = '_'; private StringProperty _inputMaskProperty; private StringProperty _requiredMaskProperty; private StringProperty _conversionMaskProperty; private StringProperty _validCharactersProperty; private StringProperty _invalidCharactersProperty; private ObjectProperty _placeholderCharacterProperty; private StringProperty _initialTextProperty; private BooleanProperty _autoAdvanceProperty; private BooleanProperty _clearbuttonVisibleProperty; private Decorator




© 2015 - 2025 Weber Informatics LLC | Privacy Policy