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
}
});
}
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
}
});
}
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();
}
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!");
}
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();
}
Aggregations