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

org.fxmisc.richtext.demo.TextArea4SQL 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.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.time.Duration;

import javafx.beans.value.ChangeListener;
import javafx.concurrent.Task;

import org.fxmisc.richtext.LineNumberFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;

import com.sta.mlogger.MLogger;

/**
 * 

Name: TextArea4SQL

*

Description: . *

*

Comment: ... *

*

Copyright: Copyright (c) 2018, 2019

*

Company: >StA-Soft<

* @author StA * @version 1.0 */ public class TextArea4SQL extends TextAreaBase { /** * Das Problem: In SQL kann ein Schl?sselwort gro?, klein oder "gemischt" geschrieben sein. * Die regul?ren Ausdr?cke m?ssen entsprechend angepasst werden. * Aus "FROM" wird somit "(f|F)(r|R)(o|O)(m|M)". * @param s Schl?sselwort * @return regul?rer Ausdruck f?r Schl?sselwort */ private static String conv(String s) { return s; } private static final String[] KEYWORDS = new String[]{ // Keywords() conv("ABORT"), conv("ACCESS"), conv("ADD"), conv("ALTER"), conv("ARRAY"), conv("ARRAY_LEN"), conv("AS"), conv("ASC"), conv("ASSERT"), conv("ASSIGN"), conv("AT"), conv("AUDIT"), conv("AUTHORIZATION"), conv("AVG"), conv("BASE_TABLE"), conv("BEGIN"), conv("BODY"), conv("CASE"), conv("CHAR"), conv("CHAR_BASE"), conv("CHECK"), conv("CLOSE"), conv("CLUSTER"), conv("CLUSTERS"), conv("COLAUTH"), conv("COLUMN"), conv("COMMENT"), conv("COMMIT"), conv("COMPRESS"), conv("CONSTANT"), conv("CONSTRAINT"), conv("COUNT"), conv("CREATE"), conv("CURRENT"), conv("CURRVAL"), conv("CURSOR"), conv("DATABASE"), conv("DATA_BASE"), conv("DATE"), conv("DBA"), conv("DEBUGOFF"), conv("DEBUGON"), conv("DECLARE"), conv("DEFAULT"), conv("DEFINITION"), conv("DELAY"), conv("DELETE"), conv("DESC"), conv("DIGITS"), conv("DISPOSE"), conv("DISTINCT"), conv("DO"), conv("DROP"), conv("DUMP"), conv("ELSE"), conv("ELSIF"), conv("END"), conv("ENTRY"), conv("EXCEPTION"), conv("EXCEPTION_INIT"), conv("EXCLUSIVE"), conv("EXIT"), conv("FALSE"), conv("FETCH"), conv("FILE"), conv("FOR"), conv("FORM"), conv("FROM"), conv("FUNCTION"), conv("GENERIC"), conv("GOTO"), conv("GRANT"), conv("GREATEST"), conv("GROUP"), conv("HAVING"), conv("IDENTIFIED"), conv("IDENTITYCOL"), conv("IF"), conv("IMMEDIATE"), conv("INCREMENT"), conv("INDEX"), conv("INDEXES"), conv("INDICATOR"), conv("INITIAL"), conv("INSERT"), conv("INTERFACE"), conv("INTO"), conv("IS"), conv("LEAST"), conv("LEVEL"), conv("LIMITED"), conv("LOCK"), conv("LONG"), conv("LOOP"), conv("MAX"), conv("MAXEXTENTS"), conv("MIN"), conv("MINUS"), conv("MLSLABEL"), conv("MOD"), conv("MORE"), conv("NEW"), conv("NEXTVAL"), conv("NOAUDIT"), conv("NOCOMPRESS"), conv("NOWAIT"), conv("NULL"), conv("NUMBER_BASE"), conv("OF"), conv("OFFLINE"), conv("ON"), conv("OFF"), conv("ONLINE"), conv("OPEN"), conv("OPTION"), conv("ORDER"), conv("OTHERS"), conv("OUT"), conv("PACKAGE"), conv("PARTITION"), conv("PCTFREE"), conv("PRAGMA"), conv("PRIVATE"), conv("PRIVILEGES"), conv("PROCEDURE"), conv("PUBLIC"), conv("QUOTED_IDENTIFIER"), conv("RAISE"), conv("RANGE"), conv("RECORD"), conv("REF"), conv("RELEASE"), conv("REMR"), conv("RENAME"), conv("RESOURCE"), conv("RETURN"), conv("REVERSE"), conv("REVOKE"), conv("ROLLBACK"), conv("ROW"), conv("ROWLABEL"), conv("ROWNUM"), conv("ROWS"), conv("ROWTYPE"), conv("RUN"), conv("SAVEPOINT"), conv("SCHEMA"), conv("SELECT"), conv("SEPERATE"), conv("SESSION"), conv("SET"), conv("SHARE"), conv("SPACE"), conv("SQL"), conv("SQLCODE"), conv("SQLERRM"), conv("STATEMENT"), conv("STDDEV"), conv("SUBTYPE"), conv("SUCCESSFULL"), conv("SUM"), conv("SYNONYM"), conv("SYSDATE"), conv("TABAUTH"), conv("TABLE"), conv("TABLES"), conv("TASK"), conv("TERMINATE"), conv("THEN"), conv("TO"), conv("TRIGGER"), conv("TRUE"), conv("TYPE"), conv("UID"), conv("UNION"), conv("UNIQUE"), conv("UPDATE"), conv("UPDATETEXT"), conv("USE"), conv("USER"), conv("USING"), conv("VALIDATE"), conv("VALUES"), conv("VARIANCE"), conv("VIEW"), conv("VIEWS"), conv("WHEN"), conv("WHENEVER"), conv("WHERE"), conv("WHILE"), conv("WITH"), conv("WORK"), conv("WRITE"), conv("XOR"), conv("ABS"), conv("ACOS"), conv("ADD_MONTHS"), conv("ASCII"), conv("ASIN"), conv("ATAN"), conv("ATAN2"), conv("CEIL"), conv("CHARTOROWID"), conv("CHR"), conv("CONCAT"), conv("CONVERT"), conv("COS"), conv("COSH"), conv("DECODE"), conv("DEFINE"), conv("FLOOR"), conv("HEXTORAW"), conv("INITCAP"), conv("INSTR"), conv("INSTRB"), conv("LAST_DAY"), conv("LENGTH"), conv("LENGTHB"), conv("LN"), conv("LOG"), conv("LOWER"), conv("LPAD"), conv("LTRIM"), conv("MOD"), conv("MONTHS_BETWEEN"), conv("NEW_TIME"), conv("NEXT_DAY"), conv("NLSSORT"), conv("NSL_INITCAP"), conv("NLS_LOWER"), conv("NLS_UPPER"), conv("NVL"), conv("POWER"), conv("RAWTOHEX"), conv("REPLACE"), conv("ROUND"), conv("ROWIDTOCHAR"), conv("RPAD"), conv("RTRIM"), conv("SIGN"), conv("SOUNDEX"), conv("SIN"), conv("SINH"), conv("SQRT"), conv("SUBSTR"), conv("SUBSTRB"), conv("TAN"), conv("TANH"), conv("TO_CHAR"), conv("TO_DATE"), conv("TO_MULTIBYTE"), conv("TO_NUMBER"), conv("TO_SINGLE_BYTE"), conv("TRANSLATE"), conv("TRUNC"), conv("UPPER"), conv("VERIFY"), conv("SERVEROUTPUT"), conv("PAGESIZE"), conv("LINESIZE"), conv("ARRAYSIZE"), conv("DBMS_OUTPUT"), conv("PUT_LINE"), conv("ENABLE"), // DataTypes() conv("binary"), conv("bit"), conv("blob"), conv("boolean"), conv("char"), conv("character"), conv("DATE"), conv("datetime"), conv("DEC"), conv("decimal"), conv("DOUBLE PRECISION"), conv("float"), conv("image"), conv("int"), conv("integer"), conv("money"), conv("name"), conv("NATURAL"), conv("NATURALN"), conv("NUMBER"), conv("numeric"), conv("nchar"), conv("nvarchar"), conv("ntext"), conv("pls_integer"), conv("POSITIVE"), conv("POSITIVEN"), conv("RAW"), conv("real"), conv("ROWID"), conv("SIGNTYPE"), conv("smalldatetime"), conv("smallint"), conv("smallmoney"), conv("text"), conv("timestamp"), conv("tinyint"), conv("uniqueidentifier"), conv("UROWID"), conv("varbinary"), conv("varchar"), conv("varchar2"), // SystemFunctions() conv("SYSDATE"), // Operators() { conv("ALL"), conv("AND"), conv("ANY"), conv("BETWEEN"), conv("BY"), conv("CONNECT"), conv("EXISTS"), conv("IN"), conv("INTERSECT"), conv("LIKE"), conv("NOT"), conv("NULL"), conv("OR"), conv("START"), conv("UNION"), conv("WITH"), // SystemStoredProcedures conv("sp_add_agent_parameter"), // SystemTables conv("backupfile"), }; private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\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 STRING_PATTERN = "\"([^\"\\\\]|\\\\.)*\""; private static final String STRING_PATTERN = "\'([^\'\\\\]|\\\\.)*\'"; // private static final String NUMBER_PATTERN = "\\b(\\d+|0x[0-9a-fA-F]+)\\b"; private static final String NUMBER_PATTERN = "\\b(\\d+)\\b"; // private static final String COMMENT_PATTERN = "//[^\n]*" + "|" + "/\\*(.|\\R)*?\\*/"; private static final String COMMENT_PATTERN = "--[^\n]*"; private static final Pattern PATTERN = Pattern.compile( "(?" + KEYWORD_PATTERN + ")|" + "(?" + PAREN_PATTERN + ")|" + "(?" + BRACE_PATTERN + ")|" + "(?" + BRACKET_PATTERN + ")|" + "(?" + SEMICOLON_PATTERN + ")|" + "(?" + STRING_PATTERN + ")|" + "(?" + NUMBER_PATTERN + ")|" + "(?" + COMMENT_PATTERN + ")", Pattern.CASE_INSENSITIVE ); //=========================================================================== 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("NUMBER") != null ? "number" : 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(); } //=========================================================================== public TextArea4SQL(boolean sync) { super(); init(sync); } public TextArea4SQL(boolean sync, String text) { super(text); init(sync); } public TextArea4SQL(boolean sync, ChangeListener cl) { super(cl); init(sync); } //=========================================================================== private void init(boolean sync) { setParagraphGraphicFactory(LineNumberFactory.get(this)); if (sync) { initSync(); } else { initASync(); } getStylesheets().add(getClass().getResource("sql-keywords.css").toExternalForm()); } private void initSync() { richChanges() .filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX .subscribe(change -> { setStyleSpans(0, computeHighlighting(getText())); }); /* // Original f?r XML: textProperty().addListener((obs, oldText, newText) -> { setStyleSpans(0, computeHighlighting(newText)); }); */ } private void initASync() { richChanges() .filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX .successionEnds(Duration.ofMillis(500)) .supplyTask(this::computeHighlightingAsync) .awaitLatest(richChanges()) .filterMap(t -> { if (t.isSuccess()) { return Optional.of(t.get()); } else { MLogger.err("", t.getFailure()); return Optional.empty(); } }) .subscribe(this::applyHighlighting); } private Task>> computeHighlightingAsync() { String text = getText(); Task>> task = new Task>>() { @Override protected StyleSpans> call() throws Exception { return computeHighlighting(text); } }; ExecutorHelper.getExecutor().execute(task); return task; } private void applyHighlighting(StyleSpans> highlighting) { setStyleSpans(0, highlighting); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy