Oli*_*Oli 7 javafx breakpoints line-numbers richtextfx
我正在使用RichTextFx的CodeArea来突出自定义迷你语言代码.
现在,在执行此代码时,我想在当前执行的行前面显示一个小箭头.我知道具体的行号,但行号标签不能发生任何事情.
由于github项目声称显示行号或断点切换作为一个功能,这可能不是很困难.但无法得到任何工作......
提前致谢
Tom*_*ula 13
要显示该行前面的任何图形,您需要设置CodeArea的"段落图形工厂".这个图形工厂只是一个功能int -> Node:给定行号,它返回一个Node将显示在行前面的.
这是一个图形工厂,生成一个指向该线的绿色三角形.只有当行等于给定的整数属性时才会显示它shownLine.
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(visible.conditionOnShowing(t??riangle));
return triangle;
}
}
Run Code Online (Sandbox Code Playgroud)
您创建的每个图形(即小绿色三角形)将观察给定shownLine属性以确定它是否应该可见.由于线条以及线条图形的来去,重要的是shownLine在不再使用图形时删除侦听器.visible.conditionOnShowing(t??riangle)是一个新属性,它将停止观察visible属性(并且shownLine由于ReactFX的惰性绑定语义而自动也是属性)并且false只要三角形不是显示窗口的一部分就重置为常量.因此,我们不会因未清理的侦听器而导致内存或CPU泄漏.
这是一个完整的可运行演示,它使用它ArrowFactory与LineNumberFactoryRichTextFX提供的结果显示行号和一个小三角形.此演示使用CodeArea的当前行作为shownLine属性.您将希望将其替换为包含当前执行行的属性.
import java.util.function.IntFunction;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.reactfx.value.Val;
public class CodeAreaWithLineIndicator 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);
primaryStage.setScene(new Scene(new StackPane(codeArea), 600, 400));
primaryStage.show();
}
}
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(visible.conditionOnShowing(t??riangle));
return triangle;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是结果:
