org.fxmisc.richtext.demo.JavaKeywordsAsync Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
FX-Text-Area for formatted text and other special effects.
package org.fxmisc.richtext.demo;
import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import com.sta.mlogger.MLogger;
/**
* Name: JavaKeywordsAsync
* Description: .
*
* Comment: ...
*
* Copyright: Copyright (c) 2016-2019
* Company: >StA-Soft<
* @author StA
* @version 1.0
*/
public class JavaKeywordsAsync extends Application
{
/**
* Keywords.
*/
private static final String[] KEYWORDS = new String[]{
"abstract", "assert", "boolean", "break", "byte",
"case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else",
"enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public",
"return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws",
"transient", "try", "void", "volatile", "while"
};
/**
* Keyword pattern.
*/
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
/**
* Paren pattern.
*/
private static final String PAREN_PATTERN = "\\(|\\)";
/**
* Brace pattern.
*/
private static final String BRACE_PATTERN = "\\{|\\}";
/**
* Bracket pattern.
*/
private static final String BRACKET_PATTERN = "\\[|\\]";
/**
* Semicolon pattern.
*/
private static final String SEMICOLON_PATTERN = "\\;";
/**
* String pattern.
*/
private static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
/**
* Comment pattern.
*/
private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
/**
* Pattern.
*/
private static final Pattern PATTERN = Pattern.compile(
"(?" + KEYWORD_PATTERN + ")" +
"|(?" + PAREN_PATTERN + ")" +
"|(?" + BRACE_PATTERN + ")" +
"|(?" + BRACKET_PATTERN + ")" +
"|(?" + SEMICOLON_PATTERN + ")" +
"|(?" + STRING_PATTERN + ")" +
"|(?" + COMMENT_PATTERN + ")"
);
/**
* Beispiel-Text.
*/
private static final String SAMPLE_CODE = String.join("\n", new String[]{
"package com.example;",
"",
"import java.util.*;",
"",
"public class Foo extends Bar implements Baz {",
"",
" /*",
" * multi-line comment",
" */",
" public static void main(String[] args) {",
" // single-line comment",
" for(String arg: args) {",
" if(arg.length() != 0)",
" MLogger.inf(arg);",
" else",
" MLogger.wrn(\"Warning: empty string as argument\");",
" }",
" }",
"",
"}"
});
/**
* Main-Methode.
* @param args Kommandozeilenparameter
*/
public static void main(String[] args)
{
launch(args);
}
/**
* Code-Area.
*/
private CodeArea codeArea;
/**
* Executor-Service.
*/
private ExecutorService executor;
@Override
public void start(Stage primaryStage)
{
executor = Executors.newSingleThreadExecutor();
codeArea = new CodeArea();
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
codeArea.richChanges()
.filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX
.successionEnds(Duration.ofMillis(500))
.supplyTask(this::computeHighlightingAsync)
.awaitLatest(codeArea.richChanges())
.filterMap(t ->
{
if (t.isSuccess())
{
return Optional.of(t.get());
}
else
{
MLogger.err("", t.getFailure());
return Optional.empty();
}
})
.subscribe(this::applyHighlighting);
codeArea.replaceText(0, 0, SAMPLE_CODE);
Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(codeArea)), 600, 400);
scene.getStylesheets().add(JavaKeywordsAsync.class.getResource("java-keywords.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("Java Keywords Async Demo");
primaryStage.show();
}
@Override
public void stop()
{
executor.shutdown();
}
/**
* Highlighting.
* @return was auch immer
*/
private Task>> computeHighlightingAsync()
{
String text = codeArea.getText();
Task>> task = new Task>>()
{
@Override
protected StyleSpans> call() throws Exception
{
return computeHighlighting(text);
}
};
executor.execute(task);
return task;
}
/**
* Highlighting.
* @param highlighting Highlighting
*/
private void applyHighlighting(StyleSpans> highlighting)
{
codeArea.setStyleSpans(0, highlighting);
}
/**
* Highlighting.
* @param text Text
* @return was auch immer
*/
private static StyleSpans> computeHighlighting(String text)
{
Matcher matcher = PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder> spansBuilder = new StyleSpansBuilder<>();
while (matcher.find())
{
String styleClass =
matcher.group("KEYWORD") != null ? "keyword" :
matcher.group("PAREN") != null ? "paren" :
matcher.group("BRACE") != null ? "brace" :
matcher.group("BRACKET") != null ? "bracket" :
matcher.group("SEMICOLON") != null ? "semicolon" :
matcher.group("STRING") != null ? "string" :
matcher.group("COMMENT") != null ? "comment" :
null; /* never happens */
assert styleClass != null;
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}
}