1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.jsql.view.swing.sql.lexer;
19
20 import com.jsql.util.LogLevelUtil;
21 import com.jsql.view.swing.sql.lexer.syntax.JavaScriptLexer;
22 import com.jsql.view.swing.sql.lexer.syntax.Lexer;
23 import com.jsql.view.swing.sql.lexer.syntax.SQLLexer;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26
27 import javax.swing.text.AttributeSet;
28 import javax.swing.text.BadLocationException;
29 import javax.swing.text.DefaultStyledDocument;
30 import java.io.Reader;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.InvocationTargetException;
33
34
35
36
37
38 public class HighlightedDocument extends DefaultStyledDocument {
39
40
41
42
43 private static final Logger LOGGER = LogManager.getRootLogger();
44
45 public static final Object SQL_STYLE = SQLLexer.class;
46 public static final Object JAVASCRIPT_STYLE = JavaScriptLexer.class;
47
48 public static final Object GRAYED_OUT_STYLE = new Object();
49
50
51
52
53
54 private final transient DocumentReader documentReader;
55
56
57 private transient AttributeSet globalStyle = null;
58
59
60
61
62 private transient Lexer syntaxLexer;
63
64
65
66
67 private final transient Colorer colorer;
68
69
70
71
72
73 private final transient Object docLock = new Object();
74
75
76
77
78 public HighlightedDocument(Object l) {
79
80
81 this.colorer = new Colorer(this);
82 this.colorer.start();
83
84
85 this.documentReader = new DocumentReader(this);
86
87 if (l == SQL_STYLE) {
88 this.syntaxLexer = new SQLLexer(this.documentReader);
89 } else {
90 this.syntaxLexer = new JavaScriptLexer(this.documentReader);
91 }
92 }
93
94
95
96
97 public void colorAll() {
98 this.color(0, this.getLength());
99 }
100
101
102
103
104
105
106
107
108
109
110 public void color(int position, int adjustment) {
111 this.colorer.color(position, adjustment);
112 }
113
114 public void setGlobalStyle(AttributeSet value) {
115
116 this.globalStyle = value;
117 this.colorAll();
118 }
119
120 public void setHighlightStyle(Object valueSource) {
121
122 Object value = valueSource;
123
124 if (value == HighlightedDocument.GRAYED_OUT_STYLE) {
125
126 this.setGlobalStyle(TokenStyles.getStyle("grayedOut"));
127 return;
128 }
129
130 if (!(value instanceof Class)) {
131 value = HighlightedDocument.SQL_STYLE;
132 }
133
134 Class<?> source = (Class<?>) value;
135 Class<?>[] parms = { Reader.class };
136 Object[] args = { this.documentReader };
137
138 try {
139 Constructor<?> cons = source.getConstructor(parms);
140 this.syntaxLexer = (Lexer) cons.newInstance(args);
141 this.globalStyle = null;
142 this.colorAll();
143 } catch (
144 SecurityException
145 | NoSuchMethodException
146 | InstantiationException
147 | IllegalAccessException
148 | IllegalArgumentException
149 | InvocationTargetException e
150 ) {
151 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
152 }
153 }
154
155
156
157
158 @Override
159 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
160
161 synchronized (this.docLock) {
162
163 super.insertString(offs, str, a);
164 this.color(offs, str.length());
165 this.documentReader.update(offs, str.length());
166 }
167 }
168
169 @Override
170 public void remove(int offs, int len) throws BadLocationException {
171
172 synchronized (this.docLock) {
173
174 super.remove(offs, len);
175 this.color(offs, -len);
176 this.documentReader.update(offs, -len);
177 }
178 }
179
180
181 DocumentReader getDocumentReader() { return this.documentReader; }
182 Object getDocumentLock() { return this.docLock; }
183 Lexer getSyntaxLexer() { return this.syntaxLexer; }
184 AttributeSet getGlobalStyle() { return this.globalStyle; }
185
186
187
188
189 public void stopColorer() {
190 this.colorer.stopThread();
191 }
192 }