Example usage for javax.swing.text MaskFormatter setValueContainsLiteralCharacters

List of usage examples for javax.swing.text MaskFormatter setValueContainsLiteralCharacters

Introduction

In this page you can find the example usage for javax.swing.text MaskFormatter setValueContainsLiteralCharacters.

Prototype

public void setValueContainsLiteralCharacters(boolean containsLiteralChars) 

Source Link

Document

If true, the returned value and set value will also contain the literal characters in mask.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {

    MaskFormatter mf = new MaskFormatter("A-AAAA-AAAA-A");
    mf.setValueContainsLiteralCharacters(false);
    System.out.println(mf.valueToString("123123123123"));
}

From source file:com.docdoku.core.util.Tools.java

public static String increaseId(String id, String mask) throws ParseException {
    LOGGER.info("#### Tools.increaseId id = " + id + " , mask = " + mask);
    MaskFormatter formatter = new MaskFormatter(mask);
    formatter.setValueContainsLiteralCharacters(false);
    String value = formatter.stringToValue(id).toString();
    StringBuilder newValue = new StringBuilder();
    boolean increase = true;
    for (int i = value.length() - 1; i >= 0; i--) {
        char c = value.charAt(i);
        switch (c) {
        case '9':
            newValue.append((increase) ? '0' : '9');
            break;

        case '8':
            newValue.append((increase) ? '9' : '8');
            increase = false;/*from  w  w w  . ja v  a2 s .co  m*/
            break;

        case '7':
            newValue.append((increase) ? '8' : '7');
            increase = false;
            break;

        case '6':
            newValue.append((increase) ? '7' : '6');
            increase = false;
            break;

        case '5':
            newValue.append((increase) ? '6' : '5');
            increase = false;
            break;

        case '4':
            newValue.append((increase) ? '5' : '4');
            increase = false;
            break;

        case '3':
            newValue.append((increase) ? '4' : '3');
            increase = false;
            break;

        case '2':
            newValue.append((increase) ? '3' : '2');
            increase = false;
            break;

        case '1':
            newValue.append((increase) ? '2' : '1');
            increase = false;
            break;

        case '0':
            newValue.append((increase) ? '1' : '0');
            increase = false;
            break;

        default:
            newValue.append(c);
            break;
        }
    }
    return formatter.valueToString(newValue.reverse().toString());
}