From 624ecbb64bc53c59f728c048107af88688576b4f Mon Sep 17 00:00:00 2001 From: Uziel Silva Date: Wed, 3 Sep 2014 12:22:22 -0500 Subject: [PATCH] Change format management for line numbers. --- .../org/fxmisc/richtext/demo/JavaKeywords.java | 4 +++- .../org/fxmisc/richtext/LineNumberFactory.java | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 3 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 70dde42b1..d6e0e58c4 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 @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Collections; +import java.util.function.IntFunction; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -85,8 +86,9 @@ public static void main(String[] args) { public void start(Stage primaryStage) { CodeArea codeArea = new CodeArea(); String stylesheet = JavaKeywords.class.getResource("java-keywords.css").toExternalForm(); + IntFunction format = (digits -> " %" + digits + "d "); - codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea, stylesheet)); + codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea, format, stylesheet)); codeArea.textProperty().addListener((obs, oldText, newText) -> { codeArea.setStyleSpans(0, computeHighlighting(newText)); }); diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java b/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java index f2493afa1..4f44c0b71 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/LineNumberFactory.java @@ -1,7 +1,6 @@ package org.fxmisc.richtext; import java.util.function.IntFunction; -import java.util.function.Supplier; import javafx.scene.Node; import javafx.scene.control.Label; @@ -20,11 +19,24 @@ public static IntFunction get(StyledTextArea area) { return new LineNumberFactory(area, STYLESHEET); } + public static IntFunction get(StyledTextArea area,IntFunction format) { + return new LineNumberFactory(area,format,STYLESHEET); + } + public static IntFunction get(StyledTextArea area,IntFunction format,String customStylesheet) { + return new LineNumberFactory(area,format,customStylesheet); + } private final EventStream nParagraphs; private final String stylesheet; + private final IntFunction format; private LineNumberFactory(StyledTextArea area, String stylesheet) { nParagraphs = EventStreams.sizeOf(area.getParagraphs()); + this.format = (digits -> "%0" + digits + "d"); + this.stylesheet = stylesheet; + } + private LineNumberFactory(StyledTextArea area, IntFunction format, String stylesheet) { + nParagraphs = EventStreams.sizeOf(area.getParagraphs()); + this.format = format; this.stylesheet = stylesheet; } @@ -47,6 +59,6 @@ public Node apply(int idx) { private String format(int x, int max) { int digits = (int) Math.floor(Math.log10(max)) + 1; - return String.format("%0" + digits + "d", x); + return String.format(format.apply(digits), x); } }