Skip to content

Commit f22bbd8

Browse files
authored
Merge pull request #364 from JordanMartinez/graphicFactoryDemo
Demonstrate via demo the usage of StyledTextArea#paragraphGraphicFactory
2 parents 169967a + 7b85791 commit f22bbd8

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.fxmisc.richtext.demo.lineindicator;
2+
3+
import javafx.beans.value.ObservableValue;
4+
import javafx.scene.Node;
5+
import javafx.scene.paint.Color;
6+
import javafx.scene.shape.Polygon;
7+
import org.reactfx.value.Val;
8+
9+
import java.util.function.IntFunction;
10+
11+
/**
12+
* Given the line number, return a node (graphic) to display to the left of a line.
13+
*/
14+
public class ArrowFactory implements IntFunction<Node> {
15+
private final ObservableValue<Integer> shownLine;
16+
17+
ArrowFactory(ObservableValue<Integer> shownLine) {
18+
this.shownLine = shownLine;
19+
}
20+
21+
@Override
22+
public Node apply(int lineNumber) {
23+
Polygon triangle = new Polygon(0.0, 0.0, 10.0, 5.0, 0.0, 10.0);
24+
triangle.setFill(Color.GREEN);
25+
26+
ObservableValue<Boolean> visible = Val.map(shownLine, sl -> sl == lineNumber);
27+
28+
triangle.visibleProperty().bind(
29+
Val.flatMap(triangle.sceneProperty(), scene -> {
30+
return scene != null ? visible : Val.constant(false);
31+
}));
32+
33+
return triangle;
34+
}
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Written as a StackOverflow answer (2015) by Tomas Mikula,
3+
* which Jordan Martinez then extracted into this demo.
4+
*
5+
* The author dedicates this file to the public domain.
6+
*/
7+
8+
package org.fxmisc.richtext.demo.lineindicator;
9+
10+
import javafx.application.Application;
11+
import javafx.geometry.Pos;
12+
import javafx.scene.Node;
13+
import javafx.scene.Scene;
14+
import javafx.scene.layout.HBox;
15+
import javafx.scene.layout.StackPane;
16+
import javafx.stage.Stage;
17+
import org.fxmisc.richtext.CodeArea;
18+
import org.fxmisc.richtext.LineNumberFactory;
19+
import org.fxmisc.richtext.StyledTextArea;
20+
21+
import java.util.function.IntFunction;
22+
23+
/**
24+
* Demonstrates the usage of {@link StyledTextArea#paragraphGraphicFactoryProperty()}.
25+
*/
26+
public class LineIndicatorDemo extends Application {
27+
28+
public static void main(String[] args) {
29+
launch(args);
30+
}
31+
32+
@Override
33+
public void start(Stage primaryStage) {
34+
CodeArea codeArea = new CodeArea();
35+
36+
IntFunction<Node> numberFactory = LineNumberFactory.get(codeArea);
37+
IntFunction<Node> arrowFactory = new ArrowFactory(codeArea.currentParagraphProperty());
38+
IntFunction<Node> graphicFactory = line -> {
39+
HBox hbox = new HBox(
40+
numberFactory.apply(line),
41+
arrowFactory.apply(line));
42+
hbox.setAlignment(Pos.CENTER_LEFT);
43+
return hbox;
44+
};
45+
codeArea.setParagraphGraphicFactory(graphicFactory);
46+
codeArea.replaceText("The green arrow will only be on the line where the caret appears.\n\nTry it.");
47+
codeArea.moveTo(0, 0);
48+
49+
primaryStage.setScene(new Scene(new StackPane(codeArea), 600, 400));
50+
primaryStage.show();
51+
}
52+
}

0 commit comments

Comments
 (0)