Java Code Examples for javafx.scene.control.TextFormatter#Change

The following examples show how to use javafx.scene.control.TextFormatter#Change . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MacProtocolView.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Add Mac input field validations
 */
@Override
protected void addInputValidation() {
    final UnaryOperator<TextFormatter.Change> ipAddressFilter = Util.getTextChangeFormatter(validateAddressRegex());
    srcAddress.setTextFormatter(new TextFormatter<>(ipAddressFilter));
    dstAddress.setTextFormatter(new TextFormatter<>(ipAddressFilter));

    // add format for step and count
    srcCount.setTextFormatter(Util.getNumberFilter(4));
    dstCount.setTextFormatter(Util.getNumberFilter(4));
    srcStep.setTextFormatter(Util.getNumberFilter(3));
    dstStep.setTextFormatter(Util.getNumberFilter(3));
}
 
Example 2
Source File: ImportedPacketPropertiesView.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Add input formatter instructions
 */
private void addInputValidation() {
    UnaryOperator<TextFormatter.Change> unitFormatter = Util.getTextChangeFormatter(Util.getUnitRegex(false));
    srcCountTF.setTextFormatter(new TextFormatter<>(unitFormatter));
    dstCountTF.setTextFormatter(new TextFormatter<>(unitFormatter));

    countTF.setTextFormatter(Util.getNumberFilter(5));

    UnaryOperator<TextFormatter.Change> digitsFormatter = Util.getTextChangeFormatter(digitsRegex());
    speedupTF.setTextFormatter(new TextFormatter<>(digitsFormatter));
    ipgTF.setTextFormatter(new TextFormatter<>(digitsFormatter));

}
 
Example 3
Source File: Util.java    From trex-stateless-gui with Apache License 2.0 5 votes vote down vote up
/**
 * Return textChange formatter
 *
 * @param regex
 * @return
 */
public static UnaryOperator<TextFormatter.Change> getTextChangeFormatter(String regex) {
    return c -> {
        String text = c.getControlNewText();
        if (text.matches(regex)) {
            return c;
        } else {
            return null;
        }
    };
}
 
Example 4
Source File: Formatters.java    From pikatimer with GNU General Public License v3.0 5 votes vote down vote up
public static TextFormatter<Integer> integerFormatter(Boolean negOK) {
    
    UnaryOperator<TextFormatter.Change> filter = c -> {
        String newText = c.getControlNewText() ;
        if (newText.isEmpty()) { // always allow deleting all characters
            return c ;
        } else if (negOK && ! newText.matches("-?\\d*")) { 
            return null;
        } else if (! newText.matches("\\d+")) {// otherwise, must have all digits:
            return null ;
        }
        return c;
    };
    
    // Custom string converter to deal with blanks and such
    StringConverter<Integer> sc = new StringConverter<Integer>() {

        @Override
        public String toString(Integer i) {
            if (i == null) return "";
            return i.toString() ;
        }

        @Override
        public Integer fromString(String s) {
            if (s != null && s.matches("-?\\d+")) {
                return Integer.valueOf(s);
            } else {
                // fall back to what the value used to be 
                // (in case they blanked it out)
                return 0 ;
            }
        }
    };
    
    return new TextFormatter<>(sc, 0, filter) ;
}
 
Example 5
Source File: DateTimeRangeInputPane.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * Build the hour/minute/second part of the datetime picker.
 *
 * @param label
 * @param min
 * @param max
 * @param value
 * @param changed
 * @return
 */
private Pane createSpinner(final String label, final int min, final int max, final ChangeListener<String> changed) {
    final int NUMBER_SPINNER_WIDTH = 55;
    final String small = "-fx-font-size: 75%;";

    final Spinner<Integer> spinner = new Spinner<>(min, max, 1);
    spinner.setPrefWidth(NUMBER_SPINNER_WIDTH);

    // Create a filter to limit text entry to just numerical digits
    final NumberFormat format = NumberFormat.getIntegerInstance();
    final UnaryOperator<TextFormatter.Change> filter = c -> {
        if (c.isContentChange()) {
            final ParsePosition parsePosition = new ParsePosition(0);
            // NumberFormat evaluates the beginning of the text
            format.parse(c.getControlNewText(), parsePosition);
            if (parsePosition.getIndex() == 0 || c.getControlNewText().length() > 2
                    || parsePosition.getIndex() < c.getControlNewText().length()) {
                // reject parsing the complete text failed
                return null;
            }
        }
        return c;
    };

    // Ensure spinner is set to editable, meaning user can directly edit text, then hook in
    // a text formatter which in turn will trigger flitering of input text.
    spinner.setEditable(true);
    final TextFormatter<Integer> timeFormatter = new TextFormatter<>(new IntegerStringConverter(), 0, filter);
    spinner.getEditor().setTextFormatter(timeFormatter);

    final Label spinnerLabel = new Label(label);
    spinnerLabel.setLabelFor(spinner);
    spinnerLabel.setStyle(small);

    final VBox vbox = new VBox();
    vbox.getChildren().addAll(spinnerLabel, spinner);

    spinner.valueProperty().addListener((ObservableValue<? extends Number> observable, Number oldValue, Number newValue) -> {
        changed.changed(null, null, null);
    });

    timeSpinners.add(spinner);

    return vbox;
}
 
Example 6
Source File: TextFieldValidator.java    From AudioBookConverter with GNU General Public License v2.0 4 votes vote down vote up
private TextFormatter.Change validateChange(TextFormatter.Change c) {
    if (validate(c.getControlNewText())) {
        return c;
    }
    return null;
}
 
Example 7
Source File: PayloadView.java    From trex-stateless-gui with Apache License 2.0 4 votes vote down vote up
/**
 * Add input validation for pattern
 */
@Override
protected void addInputValidation() {
    final UnaryOperator<TextFormatter.Change> filter = Util.getTextChangeFormatter(validatePayloadPattern());
    pattern.setTextFormatter(new TextFormatter<>(filter));
}
 
Example 8
Source File: IntegerEditingCell.java    From pikatimer with GNU General Public License v3.0 4 votes vote down vote up
public IntegerEditingCell() {
    
    
    UnaryOperator<TextFormatter.Change> filter = c -> {
        String newText = c.getControlNewText() ;
        if (newText.isEmpty()) { // always allow deleting all characters
            return c ;
        } else if (negOK && ! newText.matches("-?\\d*")) { 
            return null;
        } else if (! newText.matches("\\d+")) {// otherwise, must have all digits:
            return null ;
        }
        return c;
    };
    
    // Custom string converter to deal with blanks and such
    StringConverter<Integer> sc = new StringConverter<Integer>() {

        @Override
        public String toString(Integer i) {
            if (i == null) return "";
            return i.toString() ;
        }

        @Override
        public Integer fromString(String s) {
            if (s != null && s.matches("-?\\d+")) {
                return Integer.valueOf(s);
            } else {
                // fall back to what the value used to be 
                // (in case they blanked it out)
                return getItem() ;
            }
        }
    };
    
    textFormatter = new TextFormatter<>(sc, 0, filter) ;
    textField.setTextFormatter(textFormatter);

    textField.addEventFilter(KeyEvent.KEY_RELEASED, e -> {
        if (e.getCode() == KeyCode.ESCAPE) {
            cancelEdit();
        }
    });

    
    textField.setOnAction(e -> commitEdit(sc.fromString(textField.getText())));
    
    // This may work. I hope... 
    textField.focusedProperty().addListener((e, o, n) -> {if (!n) commitEdit(sc.fromString(textField.getText()));});

    textProperty().bind(Bindings
            .when(emptyProperty())
            .then("")
            .otherwise(itemProperty().asString()));

    setGraphic(textField);
    setContentDisplay(ContentDisplay.TEXT_ONLY);
}
 
Example 9
Source File: Util.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 * Return hex filter
 *
 * @param numOfChar
 * @return
 */
public static TextFormatter getHexFilter(int numOfChar) {
    UnaryOperator<TextFormatter.Change> filter = Util.getTextChangeFormatter(hexRegex(numOfChar));
    return new TextFormatter<>(filter);
}
 
Example 10
Source File: Util.java    From trex-stateless-gui with Apache License 2.0 2 votes vote down vote up
/**
 * Return hex filter
 *
 * @param numOfChar
 * @return
 */
public static TextFormatter getNumberFilter(int numOfChar) {
    UnaryOperator<TextFormatter.Change> filter = Util.getTextChangeFormatter(numberRegex(numOfChar));
    return new TextFormatter<>(filter);
}