This code highlights all instances of keywords in a JTextPane. I don't think it is very CPU efficient (it has three loops per LINE) It splits all the lines, creates a Matcher for each keyword, and iterates through those, which consequently means slow execution with larger files. The thread is called every time a key is released. This is the code:
private final Thread highlighter = new Thread() {
@Override
public void run() {
final String text = editor.getText();
final StyledDocument doc = editor.getStyledDocument();
final String[] lines = text.split("\n");
int offset = 0;
doc.setCharacterAttributes(0, text.length(),
Constants.getDefaultStyle(), true);
for (final String line : lines) {
for (final String keyword : Tools.KEYWORDS) {
final int length = keyword.length();
final Matcher m = Pattern.compile(keyword).matcher(line);
while (m.find()) {
doc.setCharacterAttributes(offset + m.start(), length,
Constants.getKeywordsStyle(), true);
}
}
offset += line.length();
}
}
};
I was thinking of getting the line only that the caret is on, and do the above code only for when the user pastes text. Would that be a more efficient way?
P.S: When the user types a keyword, presses the delete (look below), the default style is the same as the keyword style, and I can't seem to stop this.
keyword |
**Backspace**
keyword|
**Starts typing again**
keyword non|
until the user presses space, then the statement of
doc.setCharacterAttributes(0, text.length(),
Constants.getDefaultStyle(), true);
clears it.