Java Code Examples for org.fxmisc.richtext.CodeArea#setParagraphGraphicFactory()

The following examples show how to use org.fxmisc.richtext.CodeArea#setParagraphGraphicFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LineIndicatorDemo.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void start(Stage primaryStage) {
    CodeArea codeArea = new CodeArea();

    IntFunction<Node> numberFactory = LineNumberFactory.get(codeArea);
    IntFunction<Node> arrowFactory = new ArrowFactory(codeArea.currentParagraphProperty());
    IntFunction<Node> graphicFactory = line -> {
        HBox hbox = new HBox(
                numberFactory.apply(line),
                arrowFactory.apply(line));
        hbox.setAlignment(Pos.CENTER_LEFT);
        return hbox;
    };
    codeArea.setParagraphGraphicFactory(graphicFactory);
    codeArea.replaceText("The green arrow will only be on the line where the caret appears.\n\nTry it.");
    codeArea.moveTo(0, 0);

    primaryStage.setScene(new Scene(new StackPane(codeArea), 600, 400));
    primaryStage.show();
}
 
Example 2
Source File: XMLEditorDemo.java    From RichTextFX with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
  public void start(Stage primaryStage) {
      CodeArea codeArea = new CodeArea();
      codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));

      codeArea.textProperty().addListener((obs, oldText, newText) -> {
          codeArea.setStyleSpans(0, computeHighlighting(newText));
      });
      codeArea.replaceText(0, 0, sampleCode);

Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);
      scene.getStylesheets().add(XMLEditorDemo.class.getResource("xml-highlighting.css").toExternalForm());
      primaryStage.setScene(scene);
      primaryStage.setTitle("XML Editor Demo");
      primaryStage.show();
  }
 
Example 3
Source File: CodeAreaConfigurator.java    From kafka-message-tool with MIT License 5 votes vote down vote up
public static void configureCodeArea(CodeArea codeArea,
                                     Pattern pattern,
                                     Function<Matcher, String> getStyleClassFunction,
                                     Executor executor
) {
    codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
    codeArea.richChanges()
        .filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX

        .successionEnds(Duration.ofMillis(WAIT_PERIOD_TO_START_HIGHLIGHTING_AFTER_LAST_KEY_PRESSED))
        .supplyTask(() -> CodeAreaConfigurator.computeHighlightingAsync(codeArea,
                                                                        pattern,
                                                                        getStyleClassFunction,
                                                                        executor))
        .awaitLatest(codeArea.richChanges())
        .filterMap(t -> {
            if (t.isSuccess()) {
                return Optional.of(t.get());
            } else {
                t.getFailure().printStackTrace();
                return Optional.empty();
            }
        })

        .subscribe(highlighting -> codeArea.setStyleSpans(0, highlighting));

}
 
Example 4
Source File: JavaKeywordsAsyncDemo.java    From RichTextFX with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@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(JavaKeywordsAsyncDemo.class.getResource("java-keywords.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.setTitle("Java Keywords Async Demo");
    primaryStage.show();
}
 
Example 5
Source File: JavaKeywordsDemo.java    From RichTextFX with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@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

            // plain changes = ignore style changes that are emitted when syntax highlighting is reapplied
            // multi plain changes = save computation by not rerunning the code multiple times
            //   when making multiple changes (e.g. renaming a method at multiple parts in file)
            .multiPlainChanges()

            // do not emit an event until 500 ms have passed since the last emission of previous stream
            .successionEnds(Duration.ofMillis(500))

            // run the following code block when previous stream emits an event
            .subscribe(ignore -> codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText())));

    // when no longer need syntax highlighting and wish to clean up memory leaks
    // run: `cleanupWhenNoLongerNeedIt.unsubscribe();`


    // auto-indent: insert previous line's indents on enter
    final Pattern whiteSpace = Pattern.compile( "^\\s+" );
    codeArea.addEventHandler( KeyEvent.KEY_PRESSED, KE ->
    {
        if ( KE.getCode() == KeyCode.ENTER ) {
        	int caretPosition = codeArea.getCaretPosition();
        	int currentParagraph = codeArea.getCurrentParagraph();
            Matcher m0 = whiteSpace.matcher( codeArea.getParagraph( currentParagraph-1 ).getSegments().get( 0 ) );
            if ( m0.find() ) Platform.runLater( () -> codeArea.insertText( caretPosition, m0.group() ) );
        }
    });

    
    codeArea.replaceText(0, 0, sampleCode);

    Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);
    scene.getStylesheets().add(JavaKeywordsAsyncDemo.class.getResource("java-keywords.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.setTitle("Java Keywords Demo");
    primaryStage.show();
}