Example usage for javafx.scene.control TextField positionCaret

List of usage examples for javafx.scene.control TextField positionCaret

Introduction

In this page you can find the example usage for javafx.scene.control TextField positionCaret.

Prototype

public void positionCaret(int pos) 

Source Link

Document

Positions the caret to the position indicated by pos .

Usage

From source file:com.playonlinux.javafx.mainwindow.console.ConsoleTab.java

public ConsoleTab(CommandLineInterpreterFactory commandLineInterpreterFactory) {
    final VBox content = new VBox();

    commandInterpreter = commandLineInterpreterFactory.createInstance();

    this.setText(translate("Console"));
    this.setContent(content);

    final TextField command = new TextField();
    command.getStyleClass().add("consoleCommandType");
    final TextFlow console = new TextFlow();
    final ScrollPane consolePane = new ScrollPane(console);
    content.getStyleClass().add("rightPane");

    consolePane.getStyleClass().add("console");
    consolePane.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
    content.getChildren().addAll(consolePane, command);

    command.requestFocus();//  www .j a  v a 2s  .  c o m

    command.setOnKeyPressed(event -> {
        if (event.getCode() == KeyCode.ENTER) {
            final String commandToSend = command.getText();
            final int cursorPosition = command.getCaretPosition();
            command.setDisable(true);
            commandHistory.add(new CommandHistory.Item(commandToSend, cursorPosition));
            Text commandText = new Text(nextSymbol + commandToSend + "\n");
            commandText.getStyleClass().add("commandText");
            console.getChildren().add(commandText);
            command.setText("");

            if (commandInterpreter.sendLine(commandToSend, message -> {
                Platform.runLater(() -> {
                    if (!StringUtils.isBlank(message)) {
                        Text resultText = new Text(message);
                        resultText.getStyleClass().add("resultText");
                        console.getChildren().add(resultText);
                    }
                    command.setDisable(false);
                    command.requestFocus();
                    consolePane.setVvalue(consolePane.getVmax());
                });
            })) {
                nextSymbol = NOT_INSIDE_BLOCK;
            } else {
                nextSymbol = INSIDE_BLOCK;
            }
        }
    });

    command.setOnKeyReleased(event -> {
        if (event.getCode() == KeyCode.UP) {
            CommandHistory.Item historyItem = commandHistory.up();
            command.setText(historyItem.getCommand());
            command.positionCaret(historyItem.getCursorPosition());
        } else if (event.getCode() == KeyCode.DOWN) {
            CommandHistory.Item historyItem = commandHistory.down();
            command.setText(historyItem.getCommand());
            command.positionCaret(historyItem.getCursorPosition());
        }
    });

    this.setOnCloseRequest(event -> commandInterpreter.close());
}

From source file:ambroafb.general.mapeditor.MapEditor.java

public MapEditor() {
    this.setEditable(true);
    itemsMap = new HashMap<>();
    delimiter = " : "; // default value of delimiter
    keyPattern = ""; // (?<![\\d-])\\d+
    valuePattern = ""; // [0-9]{1,13}(\\.[0-9]*)?
    keySpecChars = "";
    valueSpecChars = "";

    this.setCellFactory((ListView<MapEditorElement> param) -> new CustomCell());

    removeElement = (MapEditorElement elem) -> {
        if (itemsMap.containsKey(elem.getKey())) {
            itemsMap.remove(elem.getKey());
            if (getValue() != null && getValue().compare(elem) == 0) {
                getEditor().setText(delimiter);
            }/*w  ww  .  ja  v  a 2s. c  o m*/
            getItems().remove(elem);
        }
    };

    editElement = (MapEditorElement elem) -> {
        getSelectionModel().select(-1);
        getEditor().setText(elem.getKey() + delimiter + elem.getValue());
        itemsMap.remove(elem.getKey());
        getItems().remove(elem);
    };

    // Never hide comboBox items listView:
    this.setSkin(new ComboBoxListViewSkin(this) {
        @Override
        protected boolean isHideOnClickEnabled() {
            return false;
        }
    });

    // Control textField input.
    TextField editor = getEditor();
    editor.setText(delimiter);
    editor.textProperty()
            .addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
                if (newValue == null || newValue.isEmpty() || newValue.equals(delimiter)) {
                    editor.setText(delimiter);
                } else if (!newValue.contains(delimiter)) {
                    editor.setText(oldValue);
                } else {
                    String keyInput = StringUtils.substringBefore(newValue, delimiter).trim();
                    String valueInput = StringUtils.substringAfter(newValue, delimiter).trim();

                    if (!keyInput.isEmpty() && !Pattern.matches(keyPattern, keyInput)) {
                        keyInput = StringUtils.substringBefore(oldValue, delimiter).trim();
                    }
                    if (!valueInput.isEmpty() && !Pattern.matches(valuePattern, valueInput)) {
                        valueInput = StringUtils.substringAfter(oldValue, delimiter).trim();
                    }

                    editor.setText(keyInput + delimiter + valueInput);
                }
            });

    this.setConverter(new StringConverter<MapEditorElement>() {
        @Override
        public String toString(MapEditorElement object) {
            if (object == null) {
                return delimiter;
            }
            return object.getKey() + delimiter + object.getValue();
        }

        @Override
        public MapEditorElement fromString(String input) {
            MapEditorElement result = null;
            if (input != null && input.contains(delimiter)) {
                result = getNewInstance();
                if (result == null)
                    return null;
                String keyInput = StringUtils.substringBefore(input, delimiter).trim();
                String valueInput = StringUtils.substringAfter(input, delimiter).trim();
                if (!keyInput.isEmpty()) {
                    result.setKey(keyInput);
                }
                if (!valueInput.isEmpty()) {
                    result.setValue(valueInput);
                }
                boolean keyOutOfSpec = keySpecChars.isEmpty()
                        || !StringUtils.containsOnly(result.getKey(), keySpecChars);
                boolean valueOutOfSpec = valueSpecChars.isEmpty()
                        || !StringUtils.containsOnly(result.getValue(), valueSpecChars);
                if (!keyInput.isEmpty() && !valueInput.isEmpty() && !itemsMap.containsKey(keyInput)
                        && (keyOutOfSpec && valueOutOfSpec)) {
                    itemsMap.put(keyInput, result);
                    getItems().add(result);
                    return null;
                }
            }
            return result;
        }
    });

    // Control caret position in textField.
    editor.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent event) -> {
        int caretOldPos = editor.getCaretPosition();
        int delimiterIndex = editor.getText().indexOf(delimiter);
        if (event.getCode().equals(KeyCode.RIGHT)) {
            if (caretOldPos + 1 > delimiterIndex && caretOldPos + 1 <= delimiterIndex + delimiter.length()) {
                editor.positionCaret(delimiterIndex + delimiter.length());
                event.consume();
            }
        } else if (event.getCode().equals(KeyCode.LEFT)) {
            if (caretOldPos - 1 >= delimiterIndex && caretOldPos - 1 < delimiterIndex + delimiter.length()) {
                editor.positionCaret(delimiterIndex);
                event.consume();
            }
        }
    });
}