Search in sources :

Example 1 with InlineCssTextArea

use of org.fxmisc.richtext.InlineCssTextArea in project RichTextFX by FXMisc.

the class MultipleCaretSelectionTests method attempting_to_add_caret_associated_with_different_area_fails.

@Test
public void attempting_to_add_caret_associated_with_different_area_fails() {
    InlineCssTextArea area2 = new InlineCssTextArea();
    CaretNode caret = new CaretNode("test caret", area2);
    interact(() -> {
        try {
            area.addCaret(caret);
            fail();
        } catch (IllegalArgumentException e) {
        // cannot add a caret associated with a different area
        }
    });
}
Also used : CaretNode(org.fxmisc.richtext.CaretNode) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) InlineCssTextAreaAppTest(org.fxmisc.richtext.InlineCssTextAreaAppTest) Test(org.junit.Test)

Example 2 with InlineCssTextArea

use of org.fxmisc.richtext.InlineCssTextArea in project RichTextFX by FXMisc.

the class MultipleCaretSelectionTests method attempting_to_add_selection_associated_with_different_area_fails.

@Test
public void attempting_to_add_selection_associated_with_different_area_fails() {
    InlineCssTextArea area2 = new InlineCssTextArea();
    Selection<String, String, String> selection = new SelectionImpl<>("test selection", area2);
    interact(() -> {
        try {
            area.addSelection(selection);
            fail();
        } catch (IllegalArgumentException e) {
        // cannot add a selection associated with a different area
        }
    });
}
Also used : SelectionImpl(org.fxmisc.richtext.SelectionImpl) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) InlineCssTextAreaAppTest(org.fxmisc.richtext.InlineCssTextAreaAppTest) Test(org.junit.Test)

Example 3 with InlineCssTextArea

use of org.fxmisc.richtext.InlineCssTextArea in project RichTextFX by FXMisc.

the class CloneDemo method start.

@Override
public void start(Stage primaryStage) {
    String selectedText = "selection";
    String text = "Edit the top area (original)\nand watch the (clone) bottom area's displayed text change and its " + "selected text [" + selectedText + "] update itself accordingly.";
    InlineCssTextArea area = new InlineCssTextArea(text);
    InlineCssTextArea clone = new InlineCssTextArea(area.getContent());
    VBox vbox = new VBox(area, clone);
    vbox.setSpacing(10);
    // set up labels displaying caret position
    String caret = "Caret: ";
    Label areaCaret = new Label(caret);
    area.caretPositionProperty().addListener((observable, oldValue, newValue) -> areaCaret.setText(caret + String.valueOf(newValue)));
    Label cloneCaret = new Label(caret);
    clone.caretPositionProperty().addListener((observable, oldValue, newValue) -> cloneCaret.setText(caret + String.valueOf(newValue)));
    // set up label's displaying selection position
    String selection = "Selected Text: ";
    Label areaSelection = new Label(selection);
    area.selectedTextProperty().addListener(((observable, oldValue, newValue) -> areaSelection.setText(selection + newValue)));
    Label cloneSelection = new Label(selection);
    clone.selectedTextProperty().addListener(((observable, oldValue, newValue) -> cloneSelection.setText(selection + newValue)));
    // now that listeners are set up update the selection for clone
    int selectionStart = text.indexOf(selectedText);
    int selectionEnd = selectionStart + selectedText.length();
    clone.selectRange(selectionStart, selectionEnd);
    // set up Label's distinguishing which labels belong to which area.
    Label areaLabel = new Label("Original Area: ");
    Label cloneLabel = new Label("Cloned Area: ");
    // set up Buttons that programmatically change area but not clone
    Button deleteLastThreeChars = new Button("Click to Delete the previous 3 chars.");
    deleteLastThreeChars.setOnAction((ae) -> {
        for (int i = 0; i <= 2; i++) {
            area.deletePreviousChar();
        }
    });
    // finish GUI
    GridPane grid = new GridPane();
    // add area content to first row
    grid.add(areaLabel, 0, 0);
    grid.add(areaCaret, 1, 0);
    grid.add(areaSelection, 2, 0);
    // add clone content to second row
    grid.add(cloneLabel, 0, 1);
    grid.add(cloneCaret, 1, 1);
    grid.add(cloneSelection, 2, 1);
    grid.setHgap(10);
    grid.setVgap(4);
    BorderPane pane = new BorderPane();
    pane.setCenter(vbox);
    pane.setTop(grid);
    pane.setBottom(deleteLastThreeChars);
    Scene scene = new Scene(pane, 800, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
}
Also used : Application(javafx.application.Application) Button(javafx.scene.control.Button) Scene(javafx.scene.Scene) Label(javafx.scene.control.Label) Stage(javafx.stage.Stage) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) BorderPane(javafx.scene.layout.BorderPane) VBox(javafx.scene.layout.VBox) GridPane(javafx.scene.layout.GridPane) BorderPane(javafx.scene.layout.BorderPane) GridPane(javafx.scene.layout.GridPane) Button(javafx.scene.control.Button) Label(javafx.scene.control.Label) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Scene(javafx.scene.Scene) VBox(javafx.scene.layout.VBox)

Example 4 with InlineCssTextArea

use of org.fxmisc.richtext.InlineCssTextArea in project RichTextFX by FXMisc.

the class OverrideBehaviorDemo method start.

@Override
public void start(Stage primaryStage) {
    InlineCssTextArea area = new InlineCssTextArea();
    InputMap<Event> preventSelectionOrRightArrowNavigation = InputMap.consume(anyOf(// prevent selection via (CTRL + ) SHIFT + [LEFT, UP, DOWN]
    keyPressed(LEFT, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(KP_LEFT, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(UP, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(KP_UP, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(DOWN, SHIFT_DOWN, SHORTCUT_ANY), keyPressed(KP_DOWN, SHIFT_DOWN, SHORTCUT_ANY), // prevent selection via mouse events
    eventType(MouseEvent.MOUSE_DRAGGED), eventType(MouseEvent.DRAG_DETECTED), mousePressed().unless(e -> e.getClickCount() == 1 && !e.isShiftDown()), // prevent any right arrow movement, regardless of modifiers
    keyPressed(RIGHT, SHORTCUT_ANY, SHIFT_ANY), keyPressed(KP_RIGHT, SHORTCUT_ANY, 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<KeyEvent> insertNewlineChar = e -> {
        if (e.getCode().equals(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!");
}
Also used : KeyEvent(javafx.scene.input.KeyEvent) EventHandler(javafx.event.EventHandler) KeyCode(javafx.scene.input.KeyCode) Scene(javafx.scene.Scene) Label(javafx.scene.control.Label) InputMap(org.fxmisc.wellbehaved.event.InputMap) MouseEvent(javafx.scene.input.MouseEvent) Event(javafx.event.Event) CheckBox(javafx.scene.control.CheckBox) Nodes(org.fxmisc.wellbehaved.event.Nodes) KeyEvent(javafx.scene.input.KeyEvent) VBox(javafx.scene.layout.VBox) KeyCombination(javafx.scene.input.KeyCombination) Application(javafx.application.Application) Insets(javafx.geometry.Insets) Stage(javafx.stage.Stage) EventPattern(org.fxmisc.wellbehaved.event.EventPattern) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Insets(javafx.geometry.Insets) CheckBox(javafx.scene.control.CheckBox) Label(javafx.scene.control.Label) MouseEvent(javafx.scene.input.MouseEvent) Event(javafx.event.Event) KeyEvent(javafx.scene.input.KeyEvent) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Scene(javafx.scene.Scene) VBox(javafx.scene.layout.VBox)

Example 5 with InlineCssTextArea

use of org.fxmisc.richtext.InlineCssTextArea in project RichTextFX by FXMisc.

the class MultiCaretAndSelectionDemo method start.

@Override
public void start(Stage primaryStage) {
    // initialize area with some lines of text
    String alphabet = "abcdefghijklmnopqrstuvwxyz";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10; i++) {
        sb.append(i).append(" :").append(alphabet).append("\n");
    }
    area = new InlineCssTextArea(sb.toString());
    setupRTFXSpecificCSSShapes();
    addExtraCaret();
    addExtraSelection();
    // select some other range with the regular caret/selection before showing area
    area.selectRange(2, 0, 2, 4);
    primaryStage.setScene(new Scene(area, 400, 400));
    primaryStage.show();
    // request focus so carets blink
    area.requestFocus();
}
Also used : InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Scene(javafx.scene.Scene)

Aggregations

InlineCssTextArea (org.fxmisc.richtext.InlineCssTextArea)7 Scene (javafx.scene.Scene)5 VBox (javafx.scene.layout.VBox)4 Application (javafx.application.Application)3 BorderPane (javafx.scene.layout.BorderPane)3 Stage (javafx.stage.Stage)3 Button (javafx.scene.control.Button)2 Label (javafx.scene.control.Label)2 VirtualizedScrollPane (org.fxmisc.flowless.VirtualizedScrollPane)2 InlineCssTextAreaAppTest (org.fxmisc.richtext.InlineCssTextAreaAppTest)2 Test (org.junit.Test)2 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 ActionEvent (javafx.event.ActionEvent)1 Event (javafx.event.Event)1 EventHandler (javafx.event.EventHandler)1 Bounds (javafx.geometry.Bounds)1 Insets (javafx.geometry.Insets)1 Pos (javafx.geometry.Pos)1