-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from JordanMartinez/graphicFactoryDemo
Demonstrate via demo the usage of StyledTextArea#paragraphGraphicFactory
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/lineindicator/ArrowFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.fxmisc.richtext.demo.lineindicator; | ||
|
||
import javafx.beans.value.ObservableValue; | ||
import javafx.scene.Node; | ||
import javafx.scene.paint.Color; | ||
import javafx.scene.shape.Polygon; | ||
import org.reactfx.value.Val; | ||
|
||
import java.util.function.IntFunction; | ||
|
||
/** | ||
* Given the line number, return a node (graphic) to display to the left of a line. | ||
*/ | ||
public class ArrowFactory implements IntFunction<Node> { | ||
private final ObservableValue<Integer> shownLine; | ||
|
||
ArrowFactory(ObservableValue<Integer> shownLine) { | ||
this.shownLine = shownLine; | ||
} | ||
|
||
@Override | ||
public Node apply(int lineNumber) { | ||
Polygon triangle = new Polygon(0.0, 0.0, 10.0, 5.0, 0.0, 10.0); | ||
triangle.setFill(Color.GREEN); | ||
|
||
ObservableValue<Boolean> visible = Val.map(shownLine, sl -> sl == lineNumber); | ||
|
||
triangle.visibleProperty().bind( | ||
Val.flatMap(triangle.sceneProperty(), scene -> { | ||
return scene != null ? visible : Val.constant(false); | ||
})); | ||
|
||
return triangle; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/lineindicator/LineIndicatorDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Written as a StackOverflow answer (2015) by Tomas Mikula, | ||
* which Jordan Martinez then extracted into this demo. | ||
* | ||
* The author dedicates this file to the public domain. | ||
*/ | ||
|
||
package org.fxmisc.richtext.demo.lineindicator; | ||
|
||
import javafx.application.Application; | ||
import javafx.geometry.Pos; | ||
import javafx.scene.Node; | ||
import javafx.scene.Scene; | ||
import javafx.scene.layout.HBox; | ||
import javafx.scene.layout.StackPane; | ||
import javafx.stage.Stage; | ||
import org.fxmisc.richtext.CodeArea; | ||
import org.fxmisc.richtext.LineNumberFactory; | ||
import org.fxmisc.richtext.StyledTextArea; | ||
|
||
import java.util.function.IntFunction; | ||
|
||
/** | ||
* Demonstrates the usage of {@link StyledTextArea#paragraphGraphicFactoryProperty()}. | ||
*/ | ||
public class LineIndicatorDemo extends Application { | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
|
||
@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(); | ||
} | ||
} |