Skip to content

Commit

Permalink
feat: line number highlight demo (#1200)
Browse files Browse the repository at this point in the history
* feat: line number highlight demo
* Package and class name fix up
* Removed unused numberFactory
* Only add currentParagraphProperty listener once
* Replace complex list usage with SimpleIntegerProperty
* Make LineNumberHighLightFactory independent of outer class
* Replaced double Label and setVisible with setStyle
* Removed unneeded CodeArea field and StackPane
* Removed unnecessary index increment
* Replaced IntegerProperty with direct currentParagraphProperty use
* Removed unneeded graphicFactory wrapper
* Added implementation comment
* Implemented more efficient style change mechanism
* Fixed indent
* Renamed variable
* Moved to lineindicator folder
---------
Co-authored-by: Jurgen <[email protected]>
  • Loading branch information
impactCn authored Jun 25, 2024
1 parent 5954a8c commit d78bab9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.fxmisc.richtext.demo.lineindicator;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;

import java.util.function.IntFunction;

public class LineNumberHighLightDemo extends Application {

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
CodeArea codeArea = new CodeArea();
codeArea.replaceText(0,0,"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
codeArea.setPrefHeight(600);

IntFunction<Node> labelFactory = new LineNumberHighLightFactory( codeArea );
codeArea.setParagraphGraphicFactory(labelFactory);

VBox vbox = new VBox();
vbox.getChildren().addAll(codeArea);

Scene scene = new Scene(vbox, 600, 600);
primaryStage.setTitle("Line number highLight Demo");
primaryStage.setScene(scene);
primaryStage.show();
}


class LineNumberHighLightFactory implements IntFunction<Node> {

private final ObservableValue<Integer> shownLines;

public LineNumberHighLightFactory( CodeArea codeArea ) {
shownLines = codeArea.currentParagraphProperty();
shownLines.addListener( (ob,oldValue,newValue) ->
{
codeArea.getParagraphGraphic( oldValue ).setStyle("-fx-text-fill: blue");
codeArea.getParagraphGraphic( newValue ).setStyle("-fx-text-fill: red");
});
}

@Override
public Node apply(int lineIndex) {

Label after = new Label();
after.setText(String.valueOf(lineIndex+1));

if ( lineIndex != shownLines.getValue() ) {
after.setStyle("-fx-text-fill: blue");
}
else after.setStyle("-fx-text-fill: red");

return after;
}
}
}

0 comments on commit d78bab9

Please sign in to comment.