Menu

[r10]: / XMLTool / src / xmltool / UIBean.java  Maximize  Restore  History

Download this file

54 lines (42 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package xmltool;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.StyleSpansBuilder;
public class UIBean {
private static CodeArea curCodeArea;
public static CodeArea getCurCodeArea() {
return curCodeArea;
}
public static void setCurCodeArea(CodeArea newCodeArea) {
newCodeArea.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldText, String newText) {
//make text between <> brown
Pattern tags = Pattern.compile("\\<(.*?)\\>");
addSpanStyle(newCodeArea, newText, tags, "tag");
//make the "<" and ">" blue
Pattern arrows = Pattern.compile("\\<|\\>");
addSpanStyle(newCodeArea, newText, arrows, "arrow");
}
});
curCodeArea = newCodeArea;
}
private static void addSpanStyle(CodeArea codeArea, String newText, Pattern pattern, String spanClass) {
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>();
Matcher matcher = pattern.matcher(newText);
int lastKwEnd = 0;
while (matcher.find()) {
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(spanClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), newText.length() - lastKwEnd);
codeArea.setStyleSpans(0, spansBuilder.create());
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.