|
| 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