From aa31d64e0d2f7622de7cad55e77deb8920c0af71 Mon Sep 17 00:00:00 2001 From: Uziel Silva Date: Tue, 2 Sep 2014 17:33:37 -0500 Subject: [PATCH] Change stylesheet management for line numbers. --- .../org/fxmisc/richtext/demo/JavaKeywords.java | 6 ++++-- .../org/fxmisc/richtext/demo/java-keywords.css | 15 ++++++++++++++- .../org/fxmisc/richtext/LineNumberFactory.java | 13 ++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywords.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywords.java index 0a993a17c..9516b7c42 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywords.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywords.java @@ -84,14 +84,16 @@ public static void main(String[] args) { @Override public void start(Stage primaryStage) { CodeArea codeArea = new CodeArea(); - codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea)); + String stylesheet = JavaKeywords.class.getResource("java-keywords.css").toExternalForm(); + + codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea,stylesheet)); codeArea.textProperty().addListener((obs, oldText, newText) -> { codeArea.setStyleSpans(0, computeHighlighting(newText)); }); codeArea.replaceText(0, 0, sampleCode); Scene scene = new Scene(new StackPane(codeArea), 600, 400); - scene.getStylesheets().add(JavaKeywords.class.getResource("java-keywords.css").toExternalForm()); + scene.getStylesheets().add(stylesheet); primaryStage.setScene(scene); primaryStage.setTitle("Java Keywords Demo"); primaryStage.show(); diff --git a/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/java-keywords.css b/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/java-keywords.css index 49121edb1..bcd10f841 100644 --- a/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/java-keywords.css +++ b/richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/java-keywords.css @@ -1,4 +1,17 @@ .keyword { - -fx-fill: brown; + -fx-fill: #8a6; -fx-font-weight: bold; +} +.default { + -fx-fill: #666; +} +.lineno { + -fx-font-family: monospace; + -fx-background-color: #eee; + -fx-text-fill: #666; + -fx-padding: 0 5px; +} +.code-area { + -fx-font-family: monospace; + -fx-background-color: #eee; } \ No newline at end of file diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java b/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java index 0f0fe4dba..188357cad 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java @@ -1,6 +1,7 @@ package org.fxmisc.richtext; import java.util.function.IntFunction; +import java.util.function.Supplier; import javafx.scene.Node; import javafx.scene.control.Label; @@ -12,21 +13,27 @@ public class LineNumberFactory implements IntFunction { private static final String STYLESHEET = LineNumberFactory.class.getResource("lineno.css").toExternalForm(); + public static IntFunction get(StyledTextArea area,String customStylesheet) { + return new LineNumberFactory(area,customStylesheet); + } public static IntFunction get(StyledTextArea area) { - return new LineNumberFactory(area); + return new LineNumberFactory(area,STYLESHEET); } private final EventStream nParagraphs; - private LineNumberFactory(StyledTextArea area) { + private LineNumberFactory(StyledTextArea area,String Stylesheet) { nParagraphs = EventStreams.sizeOf(area.getParagraphs()); + this.Stylesheet = Stylesheet; } + private final String Stylesheet; + @Override public Node apply(int idx) { Label lineNo = new Label(); lineNo.getStyleClass().add("lineno"); - lineNo.getStylesheets().add(STYLESHEET); + lineNo.getStylesheets().add(Stylesheet); // When removed from the scene, stay subscribed to never(), which is // a fake subscription that consumes no resources, instead of staying