|
| 1 | +package org.fxmisc.richtext.demo; |
| 2 | + |
| 3 | +import static javafx.scene.input.KeyCode.*; |
| 4 | +import static javafx.scene.input.KeyCombination.*; |
| 5 | +import static org.fxmisc.wellbehaved.event.EventPattern.*; |
| 6 | + |
| 7 | +import javafx.application.Application; |
| 8 | +import javafx.event.Event; |
| 9 | +import javafx.event.EventHandler; |
| 10 | +import javafx.geometry.Insets; |
| 11 | +import javafx.scene.Scene; |
| 12 | +import javafx.scene.control.CheckBox; |
| 13 | +import javafx.scene.control.Label; |
| 14 | +import javafx.scene.input.KeyEvent; |
| 15 | +import javafx.scene.input.MouseEvent; |
| 16 | +import javafx.scene.layout.VBox; |
| 17 | +import javafx.stage.Stage; |
| 18 | + |
| 19 | +import org.fxmisc.richtext.InlineCssTextArea; |
| 20 | +import org.fxmisc.wellbehaved.event.InputMap; |
| 21 | +import org.fxmisc.wellbehaved.event.Nodes; |
| 22 | + |
| 23 | +/** |
| 24 | + * This demo shows how to override the default behavior of a StyledTextArea via an InputMap. |
| 25 | + * It also demonstrates the bugs that can arise if one adds a handler to one of the |
| 26 | + * {@code on[EventType]Property}. |
| 27 | + */ |
| 28 | +public class OverrideBehaviorDemo extends Application { |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + launch(args); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public void start(Stage primaryStage) { |
| 36 | + InlineCssTextArea area = new InlineCssTextArea(); |
| 37 | + |
| 38 | + InputMap<Event> preventSelectionOrRightArrowNavigation = InputMap.consume( |
| 39 | + anyOf( |
| 40 | + // prevent selection via (CTRL + ) SHIFT + [LEFT, UP, DOWN] |
| 41 | + keyPressed(LEFT, SHIFT_DOWN, SHORTCUT_ANY), |
| 42 | + keyPressed(KP_LEFT, SHIFT_DOWN, SHORTCUT_ANY), |
| 43 | + keyPressed(UP, SHIFT_DOWN, SHORTCUT_ANY), |
| 44 | + keyPressed(KP_UP, SHIFT_DOWN, SHORTCUT_ANY), |
| 45 | + keyPressed(DOWN, SHIFT_DOWN, SHORTCUT_ANY), |
| 46 | + keyPressed(KP_DOWN, SHIFT_DOWN, SHORTCUT_ANY), |
| 47 | + |
| 48 | + // prevent selection via mouse events |
| 49 | + eventType(MouseEvent.MOUSE_DRAGGED), |
| 50 | + eventType(MouseEvent.DRAG_DETECTED), |
| 51 | + mousePressed().unless(e -> e.getClickCount() == 1 && !e.isShiftDown()), |
| 52 | + |
| 53 | + // prevent any right arrow movement, regardless of modifiers |
| 54 | + keyPressed(RIGHT, SHORTCUT_ANY, SHIFT_ANY), |
| 55 | + keyPressed(KP_RIGHT, SHORTCUT_ANY, SHIFT_ANY) |
| 56 | + ) |
| 57 | + ); |
| 58 | + Nodes.addInputMap(area, preventSelectionOrRightArrowNavigation); |
| 59 | + |
| 60 | + area.replaceText(String.join("\n", |
| 61 | + "You can't move the caret to the right via the RIGHT arrow key in this area.", |
| 62 | + "Additionally, you cannot select anything either", |
| 63 | + "", |
| 64 | + ":-p" |
| 65 | + )); |
| 66 | + area.moveTo(0); |
| 67 | + |
| 68 | + CheckBox addExtraEnterHandlerCheckBox = new CheckBox("Temporarily add an EventHandler to area's `onKeyPressedProperty`?"); |
| 69 | + addExtraEnterHandlerCheckBox.setStyle("-fx-font-weight: bold;"); |
| 70 | + |
| 71 | + Label checkBoxExplanation = new Label(String.join("\n", |
| 72 | + "The added handler will insert a newline character at the caret's position when [Enter] is pressed.", |
| 73 | + "If checked, the default behavior and added handler will both occur: ", |
| 74 | + "\tthus, two newline characters should be inserted when user presses [Enter].", |
| 75 | + "When unchecked, the handler will be removed." |
| 76 | + )); |
| 77 | + checkBoxExplanation.setWrapText(true); |
| 78 | + |
| 79 | + EventHandler<KeyEvent> insertNewlineChar = e -> { |
| 80 | + if (e.getCode().equals(ENTER)) { |
| 81 | + area.insertText(area.getCaretPosition(), "\n"); |
| 82 | + e.consume(); |
| 83 | + } |
| 84 | + }; |
| 85 | + addExtraEnterHandlerCheckBox.selectedProperty().addListener( |
| 86 | + (obs, ov, isSelected) -> area.setOnKeyPressed( isSelected ? insertNewlineChar : null) |
| 87 | + ); |
| 88 | + |
| 89 | + VBox vbox = new VBox(area, addExtraEnterHandlerCheckBox, checkBoxExplanation); |
| 90 | + vbox.setSpacing(10); |
| 91 | + vbox.setPadding(new Insets(10)); |
| 92 | + |
| 93 | + primaryStage.setScene(new Scene(vbox, 700, 350)); |
| 94 | + primaryStage.show(); |
| 95 | + primaryStage.setTitle("An area whose behavior has been overridden permanently and temporarily!"); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments