Skip to content

Commit

Permalink
Merge pull request #84 from UzielSilva/NumberFormat
Browse files Browse the repository at this point in the history
Allow customizing line number format.
  • Loading branch information
TomasMikula committed Sep 4, 2014
2 parents 4b26240 + 624ecbb commit cae4710
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> 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));
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,11 +19,24 @@ public static IntFunction<Node> get(StyledTextArea<?> area) {
return new LineNumberFactory(area, STYLESHEET);
}

public static IntFunction<Node> get(StyledTextArea<?> area,IntFunction<String> format) {
return new LineNumberFactory(area,format,STYLESHEET);
}
public static IntFunction<Node> get(StyledTextArea<?> area,IntFunction<String> format,String customStylesheet) {
return new LineNumberFactory(area,format,customStylesheet);
}
private final EventStream<Integer> nParagraphs;
private final String stylesheet;
private final IntFunction<String> 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<String> format, String stylesheet) {
nParagraphs = EventStreams.sizeOf(area.getParagraphs());
this.format = format;
this.stylesheet = stylesheet;
}

Expand All @@ -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);
}
}

0 comments on commit cae4710

Please sign in to comment.