Hej alle sammen.
Det er sådan at jeg bruger RichTextFX mit problem er at den virkelig svær at finde ud af at bruge når den skal supporte PHP, og HTML altså highligthe begge koder.
Så jeg vil gerne have at vis jeg fx skriver
- <?php echo "Hej" ?> <p>Hej</p>
at den så kan style begge ting...
Man highlighter sådan her:
- textProperty().addListener((obs, oldText, newText) -> {
- setStyleSpans(0, PHP.computeHighlighting(newText)); //PHP er en static class.
- });
- setStyleSpans(0, PHP.computeHighlighting(getText()));
Her highligther den PHP.
- package application;
-
-
- import com.sun.javafx.tk.Toolkit;
- import javafx.concurrent.Task;
- import org.fxmisc.richtext.StyleSpans;
- import org.fxmisc.richtext.StyleSpansBuilder;
-
- import java.util.Collection;
- import java.util.Collections;
- import java.util.concurrent.ExecutorService;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- * Created by Mathias Enevoldsen on 07/03/15.
- * COPYRIGHT 2015 ALL RIGHTS RESERVED TO TripCode (C).
- * THIS SOFTWARE'S CODE CAN ONLY BE USED BY TRIPCODES TEAM.
- * THIS SOFTWARE'S CODE CAN NOT BE COPIED, MOVED, OR USED IN ANY OTHER WAY WITHOUT WRITTEN DOCUMENTATION FOR YOU CAN USE IT
- */
- public class PHP {
- private static final String[] KEYWORDS = new String[] {
- ""
- };
- private static final String[] CLASSES = new String[] {
- ""
- };
-
- private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
- private static final String CLASSES_PATTERN = "\\b(" + String.join("|", CLASSES) + ")\\b";
- private static final String PAREN_PATTERN = "\\(|\\)";
- private static final String BRACE_PATTERN = "\\{|\\}";
- private static final String BRACKET_PATTERN = "\\[|\\]";
- private static final String SEMICOLON_PATTERN = "\\;";
- private static final String HTTP_PATTERN = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
- private static final String STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\"";
- private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/";
- private static final String NUMBER_PATTERN = "\\d+";
- private static final String SPECIALWORDS_PATTERN = "\\=|\\-|\\/|\\+|\\%";
- private static final String SPECIALWORDS1_PATTERN = "\\$";
-
- private static final Pattern PATTERN = Pattern.compile(
- "(?<KEYWORD>" + KEYWORD_PATTERN + ")"
- + "|(?<PAREN>" + PAREN_PATTERN + ")"
- + "|(?<BRACE>" + BRACE_PATTERN + ")"
- + "|(?<BRACKET>" + BRACKET_PATTERN + ")"
- + "|(?<SEMICOLON>" + SEMICOLON_PATTERN + ")"
- + "|(?<STRING>" + STRING_PATTERN + ")"
- + "|(?<CLASSES>" + CLASSES_PATTERN + ")"
- + "|(?<COMMENT>" + COMMENT_PATTERN + ")"
- + "|(?<NUMBER>" + NUMBER_PATTERN + ")"
- + "|(?<HTTP>" + HTTP_PATTERN + ")"
- + "|(?<SPECIALWORD>" + SPECIALWORDS_PATTERN + ")"
- + "|(?<SPECIALWORD1>" + SPECIALWORDS1_PATTERN + ")"
- );
-
- public static StyleSpans<Collection<String>> computeHighlighting(String text) {
- Matcher matcher = PATTERN.matcher(text);
- int lastKwEnd = 0;
- StyleSpansBuilder<Collection<String>> spansBuilder
- = new StyleSpansBuilder<>();
- while(matcher.find()) {
- String styleClass =
- matcher.group("HTTP") != null ? "http" :
- matcher.group("SPECIALWORD") != null ? "specialword" :
- matcher.group("SPECIALWORD1") != null ? "specialword" :
- matcher.group("NUMBER") != null ? "number" :
- matcher.group("KEYWORD") != null ? "keyword" :
- matcher.group("COMMENT") != null ? "comment" :
- matcher.group("CLASSES") != null ? "classes" :
- 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" :
- 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();
- }
- }