org.fxmisc.richtext.demo.OverrideBehaviorDemo Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
FX-Text-Area for formatted text and other special effects.
package org.fxmisc.richtext.demo;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCombination;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.fxmisc.wellbehaved.event.EventPattern;
import org.fxmisc.richtext.InlineCssTextArea;
import org.fxmisc.wellbehaved.event.InputMap;
import org.fxmisc.wellbehaved.event.Nodes;
/**
* Name: OverrideBehaviorDemo
* Description: This demo shows how to override the default behavior of a StyledTextArea via an InputMap.
* It also demonstrates the bugs that can arise if one adds a handler to one of the {@code on[EventType]Property}.
*
* Comment: ...
*
* Copyright: Copyright (c) 2016-2019
* Company: >StA-Soft<
* @author StA
* @version 1.0
*/
public class OverrideBehaviorDemo extends Application
{
/**
* Main-Methode.
* @param args Kommandozeilenparameter
*/
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
InlineCssTextArea area = new InlineCssTextArea();
InputMap preventSelectionOrRightArrowNavigation = InputMap.consume(
EventPattern.anyOf(
// prevent selection via (CTRL + ) SHIFT + [LEFT, UP, DOWN]
EventPattern.keyPressed(KeyCode.LEFT, KeyCombination.SHIFT_DOWN, KeyCombination.SHORTCUT_ANY),
EventPattern.keyPressed(KeyCode.KP_LEFT, KeyCombination.SHIFT_DOWN, KeyCombination.SHORTCUT_ANY),
EventPattern.keyPressed(KeyCode.UP, KeyCombination.SHIFT_DOWN, KeyCombination.SHORTCUT_ANY),
EventPattern.keyPressed(KeyCode.KP_UP, KeyCombination.SHIFT_DOWN, KeyCombination.SHORTCUT_ANY),
EventPattern.keyPressed(KeyCode.DOWN, KeyCombination.SHIFT_DOWN, KeyCombination.SHORTCUT_ANY),
EventPattern.keyPressed(KeyCode.KP_DOWN, KeyCombination.SHIFT_DOWN, KeyCombination.SHORTCUT_ANY),
// prevent selection via mouse events
EventPattern.eventType(MouseEvent.MOUSE_DRAGGED),
EventPattern.eventType(MouseEvent.DRAG_DETECTED),
EventPattern.mousePressed().unless(e -> e.getClickCount() == 1 && !e.isShiftDown()),
// prevent any right arrow movement, regardless of modifiers
EventPattern.keyPressed(KeyCode.RIGHT, KeyCombination.SHORTCUT_ANY, KeyCombination.SHIFT_ANY),
EventPattern.keyPressed(KeyCode.KP_RIGHT, KeyCombination.SHORTCUT_ANY, KeyCombination.SHIFT_ANY)
)
);
Nodes.addInputMap(area, preventSelectionOrRightArrowNavigation);
area.replaceText(String.join("\n",
"You can't move the caret to the right via the RIGHT arrow key in this area.",
"Additionally, you cannot select anything either",
"",
":-p"
));
area.moveTo(0);
CheckBox addExtraEnterHandlerCheckBox = new CheckBox("Temporarily add an EventHandler to area's `onKeyPressedProperty`?");
addExtraEnterHandlerCheckBox.setStyle("-fx-font-weight: bold;");
Label checkBoxExplanation = new Label(String.join("\n",
"The added handler will insert a newline character at the caret's position when [Enter] is pressed.",
"If checked, the default behavior and added handler will both occur: ",
"\tthus, two newline characters should be inserted when user presses [Enter].",
"When unchecked, the handler will be removed."
));
checkBoxExplanation.setWrapText(true);
EventHandler insertNewlineChar = e ->
{
if (e.getCode().equals(KeyCode.ENTER))
{
area.insertText(area.getCaretPosition(), "\n");
e.consume();
}
};
addExtraEnterHandlerCheckBox.selectedProperty().addListener(
(obs, ov, isSelected) -> area.setOnKeyPressed(isSelected ? insertNewlineChar : null)
);
VBox vbox = new VBox(area, addExtraEnterHandlerCheckBox, checkBoxExplanation);
vbox.setSpacing(10);
vbox.setPadding(new Insets(10));
primaryStage.setScene(new Scene(vbox, 700, 350));
primaryStage.show();
primaryStage.setTitle("An area whose behavior has been overridden permanently and temporarily!");
}
}