All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fxmisc.richtext.demo.JavaKeywords Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version

package org.fxmisc.richtext.demo;

import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javafx.application.Application;
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;

/**
 * 

Name: JavaKeywords

*

Description: . *

*

Comment: ... *

*

Copyright: Copyright (c) 2016-2019

*

Company: >StA-Soft<

* @author StA * @version 1.0 */ public class JavaKeywords 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.err(\"Warning: empty string as argument\");", " }", " }", "", "}" }); /** * Main-Methode. * @param args Kommandozeilenparameter */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { CodeArea codeArea = new CodeArea(); codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea)); codeArea.richChanges().filter(ch -> !ch.getInserted().equals(ch.getRemoved())).subscribe(change -> { codeArea.setStyleSpans(0, computeHighlighting(codeArea.getText())); }); 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 Demo"); primaryStage.show(); } /** * 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(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy