Skip to content

Commit

Permalink
Merge pull request #364 from JordanMartinez/graphicFactoryDemo
Browse files Browse the repository at this point in the history
Demonstrate via demo the usage of StyledTextArea#paragraphGraphicFactory
  • Loading branch information
TomasMikula authored Sep 20, 2016
2 parents 169967a + 7b85791 commit f22bbd8
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
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;
}
}
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();
}
}

0 comments on commit f22bbd8

Please sign in to comment.