Search in sources :

Example 6 with VirtualizedScrollPane

use of org.fxmisc.flowless.VirtualizedScrollPane in project RichTextFX by FXMisc.

the class PopupDemo method start.

@Override
public void start(Stage primaryStage) {
    stage = primaryStage;
    StringBuilder sb = new StringBuilder();
    for (int i = 2; i < 100; i++) {
        sb.append(String.valueOf(i)).append("        END\n");
    }
    InlineCssTextArea area = new InlineCssTextArea("Hello popup!\n" + sb.toString());
    area.setWrapText(true);
    VirtualizedScrollPane<InlineCssTextArea> vsPane = new VirtualizedScrollPane<>(area);
    BoundsPopup caretPopup = new BoundsPopup("I am the caret popup button!");
    BoundsPopup selectionPopup = new BoundsPopup("I am the selection popup button!");
    VBox caretOptions = createPopupOptions(caretPopup, "Show/Hide caret-based popup", "Show/Hide popup even when caret is out of viewport");
    VBox selectionOptions = createPopupOptions(selectionPopup, "Show/Hide selection-based popup", "Show/Hide popup even when selection is out of viewport");
    BorderPane borderPane = new BorderPane();
    borderPane.setTop(caretOptions);
    borderPane.setCenter(vsPane);
    borderPane.setBottom(selectionOptions);
    primaryStage.setScene(new Scene(borderPane, 400, 500));
    primaryStage.setTitle("Popup Demo");
    primaryStage.show();
    // ### Set up EventStreams
    // update labels depending on whether item is within viewport
    EventStream<Optional<Bounds>> caretBounds = nonNullValuesOf(area.caretBoundsProperty());
    Subscription cBoundsSub = feedVisibilityToLabelText(caretBounds, caretPopup, "Caret");
    EventStream<Optional<Bounds>> selectionBounds = nonNullValuesOf(area.selectionBoundsProperty());
    Subscription sBoundsSub = feedVisibilityToLabelText(selectionBounds, selectionPopup, "Selection");
    // set up event streams to update popups every time bounds change
    double caretXOffset = 0;
    double caretYOffset = 0;
    double selectionXOffset = 30;
    double selectionYOffset = 30;
    Subscription caretPopupSub = EventStreams.combine(caretBounds, caretPopup.outsideViewportValues()).subscribe(tuple3 -> {
        Optional<Bounds> opt = tuple3._1;
        boolean showPopupWhenCaretOutside = tuple3._2;
        if (opt.isPresent()) {
            Bounds b = opt.get();
            caretPopup.setX(b.getMaxX() + caretXOffset);
            caretPopup.setY(b.getMaxY() + caretYOffset);
            if (caretPopup.isHiddenTemporarily()) {
                caretPopup.show(stage);
                caretPopup.setHideTemporarily(false);
            }
        } else {
            if (!showPopupWhenCaretOutside) {
                caretPopup.hide();
                caretPopup.setHideTemporarily(true);
            }
        }
    });
    Subscription selectionPopupSub = EventStreams.combine(selectionBounds, selectionPopup.outsideViewportValues()).subscribe(tuple3 -> {
        Optional<Bounds> opt = tuple3._1;
        boolean showPopupWhenSelectionOutside = tuple3._2;
        if (opt.isPresent()) {
            Bounds b = opt.get();
            selectionPopup.setX(b.getMinX() + selectionXOffset + caretPopup.getWidth());
            selectionPopup.setY(b.getMinY() + selectionYOffset);
            if (selectionPopup.isHiddenTemporarily()) {
                selectionPopup.show(stage);
                selectionPopup.setHideTemporarily(false);
            }
        } else {
            if (!showPopupWhenSelectionOutside) {
                selectionPopup.hide();
                selectionPopup.setHideTemporarily(true);
            }
        }
    });
    Subscription caretSubs = caretPopupSub.and(cBoundsSub);
    Subscription selectionSubs = selectionPopupSub.and(sBoundsSub);
    caretPopup.show(primaryStage);
    selectionPopup.show(primaryStage);
    area.moveTo(0);
    area.requestFollowCaret();
}
Also used : BorderPane(javafx.scene.layout.BorderPane) Optional(java.util.Optional) Bounds(javafx.geometry.Bounds) Scene(javafx.scene.Scene) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Subscription(org.reactfx.Subscription) VBox(javafx.scene.layout.VBox) VirtualizedScrollPane(org.fxmisc.flowless.VirtualizedScrollPane)

Example 7 with VirtualizedScrollPane

use of org.fxmisc.flowless.VirtualizedScrollPane in project RichTextFX by FXMisc.

the class JavaKeywords method start.

@Override
public void start(Stage primaryStage) {
    CodeArea codeArea = new CodeArea();
    // add line numbers to the left of area
    codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
    // recompute the syntax highlighting 500 ms after user stops editing area
    Subscription cleanupWhenNoLongerNeedIt = codeArea.multiPlainChanges().successionEnds(Duration.ofMillis(500)).subscribe(ignore -> codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText())));
    // when no longer need syntax highlighting and wish to clean up memory leaks
    // run: `cleanupWhenNoLongerNeedIt.unsubscribe();`
    codeArea.replaceText(0, 0, sampleCode);
    Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);
    scene.getStylesheets().add(JavaKeywordsAsync.class.getResource("java-keywords.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.setTitle("Java Keywords Demo");
    primaryStage.show();
}
Also used : Subscription(org.reactfx.Subscription) Scene(javafx.scene.Scene) StackPane(javafx.scene.layout.StackPane) VirtualizedScrollPane(org.fxmisc.flowless.VirtualizedScrollPane) CodeArea(org.fxmisc.richtext.CodeArea)

Example 8 with VirtualizedScrollPane

use of org.fxmisc.flowless.VirtualizedScrollPane in project RichTextFX by FXMisc.

the class JavaKeywordsAsync method start.

@Override
public void start(Stage primaryStage) {
    executor = Executors.newSingleThreadExecutor();
    codeArea = new CodeArea();
    codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
    Subscription cleanupWhenDone = codeArea.multiPlainChanges().successionEnds(Duration.ofMillis(500)).supplyTask(this::computeHighlightingAsync).awaitLatest(codeArea.multiPlainChanges()).filterMap(t -> {
        if (t.isSuccess()) {
            return Optional.of(t.get());
        } else {
            t.getFailure().printStackTrace();
            return Optional.empty();
        }
    }).subscribe(this::applyHighlighting);
    // call when no longer need it: `cleanupWhenFinished.unsubscribe();`
    codeArea.replaceText(0, 0, sampleCode);
    Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);
    scene.getStylesheets().add(JavaKeywordsAsync.class.getResource("java-keywords.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.setTitle("Java Keywords Async Demo");
    primaryStage.show();
}
Also used : Scene(javafx.scene.Scene) Collection(java.util.Collection) StyleSpans(org.fxmisc.richtext.model.StyleSpans) StackPane(javafx.scene.layout.StackPane) CodeArea(org.fxmisc.richtext.CodeArea) Executors(java.util.concurrent.Executors) LineNumberFactory(org.fxmisc.richtext.LineNumberFactory) StyleSpansBuilder(org.fxmisc.richtext.model.StyleSpansBuilder) Application(javafx.application.Application) Task(javafx.concurrent.Task) Matcher(java.util.regex.Matcher) Subscription(org.reactfx.Subscription) Stage(javafx.stage.Stage) VirtualizedScrollPane(org.fxmisc.flowless.VirtualizedScrollPane) Duration(java.time.Duration) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ExecutorService(java.util.concurrent.ExecutorService) Subscription(org.reactfx.Subscription) Scene(javafx.scene.Scene) StackPane(javafx.scene.layout.StackPane) VirtualizedScrollPane(org.fxmisc.flowless.VirtualizedScrollPane) CodeArea(org.fxmisc.richtext.CodeArea)

Example 9 with VirtualizedScrollPane

use of org.fxmisc.flowless.VirtualizedScrollPane in project RichTextFX by FXMisc.

the class SpellChecking method start.

@Override
public void start(Stage primaryStage) {
    StyleClassedTextArea textArea = new StyleClassedTextArea();
    textArea.setWrapText(true);
    Subscription cleanupWhenFinished = textArea.multiPlainChanges().successionEnds(Duration.ofMillis(500)).subscribe(change -> {
        textArea.setStyleSpans(0, computeHighlighting(textArea.getText()));
    });
    // load the dictionary
    try (InputStream input = getClass().getResourceAsStream("spellchecking.dict");
        BufferedReader br = new BufferedReader(new InputStreamReader(input))) {
        String line;
        while ((line = br.readLine()) != null) {
            dictionary.add(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // load the sample document
    InputStream input2 = getClass().getResourceAsStream("spellchecking.txt");
    try (java.util.Scanner s = new java.util.Scanner(input2)) {
        String document = s.useDelimiter("\\A").hasNext() ? s.next() : "";
        textArea.replaceText(0, 0, document);
    }
    Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(textArea)), 600, 400);
    scene.getStylesheets().add(getClass().getResource("spellchecking.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.setTitle("Spell Checking Demo");
    primaryStage.show();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) Scene(javafx.scene.Scene) StyleClassedTextArea(org.fxmisc.richtext.StyleClassedTextArea) BufferedReader(java.io.BufferedReader) Subscription(org.reactfx.Subscription) StackPane(javafx.scene.layout.StackPane) VirtualizedScrollPane(org.fxmisc.flowless.VirtualizedScrollPane)

Example 10 with VirtualizedScrollPane

use of org.fxmisc.flowless.VirtualizedScrollPane in project RichTextFX by FXMisc.

the class ShowLineDemo method start.

@Override
public void start(Stage primaryStage) throws Exception {
    StringBuilder sb = new StringBuilder();
    int max = 100;
    for (int i = 0; i < max; i++) {
        sb.append("Line Index: ").append(i).append("\n");
    }
    sb.append("Line Index: ").append(max);
    InlineCssTextArea area = new InlineCssTextArea(sb.toString());
    VirtualizedScrollPane<InlineCssTextArea> vsPane = new VirtualizedScrollPane<>(area);
    Function<Integer, Integer> clamp = i -> Math.max(0, Math.min(i, area.getLength() - 1));
    Button showInViewportButton = createButton("Show line somewhere in Viewport", ae -> {
        area.showParagraphInViewport(clamp.apply(field.getTextAsInt()));
    });
    Button showAtViewportTopButton = createButton("Show line at top of viewport", ae -> {
        area.showParagraphAtTop(clamp.apply(field.getTextAsInt()));
    });
    Button showAtViewportBottomButton = createButton("Show line at bottom of viewport", ae -> {
        area.showParagraphAtBottom(clamp.apply(field.getTextAsInt()));
    });
    VBox vbox = new VBox(field, showInViewportButton, showAtViewportTopButton, showAtViewportBottomButton);
    vbox.setAlignment(Pos.CENTER);
    BorderPane root = new BorderPane();
    root.setCenter(vsPane);
    root.setBottom(vbox);
    Scene scene = new Scene(root, 700, 500);
    primaryStage.setScene(scene);
    primaryStage.show();
}
Also used : Button(javafx.scene.control.Button) Pos(javafx.geometry.Pos) Scene(javafx.scene.Scene) TextField(javafx.scene.control.TextField) VBox(javafx.scene.layout.VBox) Function(java.util.function.Function) Application(javafx.application.Application) Consumer(java.util.function.Consumer) ActionEvent(javafx.event.ActionEvent) Stage(javafx.stage.Stage) VirtualizedScrollPane(org.fxmisc.flowless.VirtualizedScrollPane) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) BorderPane(javafx.scene.layout.BorderPane) BorderPane(javafx.scene.layout.BorderPane) Button(javafx.scene.control.Button) InlineCssTextArea(org.fxmisc.richtext.InlineCssTextArea) Scene(javafx.scene.Scene) VBox(javafx.scene.layout.VBox) VirtualizedScrollPane(org.fxmisc.flowless.VirtualizedScrollPane)

Aggregations

Scene (javafx.scene.Scene)10 VirtualizedScrollPane (org.fxmisc.flowless.VirtualizedScrollPane)10 VBox (javafx.scene.layout.VBox)5 Application (javafx.application.Application)4 Button (javafx.scene.control.Button)4 StackPane (javafx.scene.layout.StackPane)4 Stage (javafx.stage.Stage)4 IOException (java.io.IOException)3 Optional (java.util.Optional)3 StyleClassedTextArea (org.fxmisc.richtext.StyleClassedTextArea)3 Subscription (org.reactfx.Subscription)3 Consumer (java.util.function.Consumer)2 Function (java.util.function.Function)2 BorderPane (javafx.scene.layout.BorderPane)2 HBox (javafx.scene.layout.HBox)2 CodeArea (org.fxmisc.richtext.CodeArea)2 java.awt (java.awt)1 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1