Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate via demo the usage of StyledTextArea#paragraphGraphicFactory #364

Merged
merged 1 commit into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}