use of javax.swing.text.StyledDocument in project vcell by virtualcell.
the class MultiPurposeTextPanel method highlightUpdate.
private void highlightUpdate() {
if (keywords.size() < 1) {
return;
}
if (textPane.getSelectedText() != null) {
return;
}
String content = textPane.getText();
if (content.trim().length() < 1) {
return;
}
int pos = textPane.getCaretPosition();
int textLength = textPane.getText().length();
if (pos == 0 || pos >= textLength) {
return;
}
char ch = content.charAt(pos - 1);
// Find where the word starts
int w1;
for (w1 = Math.min(pos - 1, content.length() - 1); w1 >= 0; w1--) {
char c = content.charAt(w1);
if (!isIdentifierPart(c)) {
break;
}
}
w1 = Math.max(0, w1 + 1);
int w2;
for (w2 = pos; w2 < content.length(); w2++) {
char c = content.charAt(w2);
if (!isIdentifierPart(c)) {
break;
}
}
if (w2 > w1) {
try {
String substr = content.substring(w1, w2);
((StyledDocument) getTextPane().getDocument()).setCharacterAttributes(w1, w2 - w1, keywords.contains(substr) ? getKeywordStyle() : getDefaultStyle(), true);
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
if (!isIdentifierPart(ch)) {
w2 = pos - 1;
for (w1 = pos - 2; w1 >= 0; w1--) {
char c = content.charAt(w1);
if (!isIdentifierPart(c)) {
break;
}
}
w1 = Math.max(0, w1 + 1);
if (w2 > w1) {
try {
String substr = content.substring(w1, w2);
((StyledDocument) getTextPane().getDocument()).setCharacterAttributes(w1, w2 - w1, keywords.contains(substr) ? getKeywordStyle() : getDefaultStyle(), true);
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
}
}
}
use of javax.swing.text.StyledDocument in project vcell by virtualcell.
the class MultiPurposeTextPanel method highlightComments.
private void highlightComments() {
String text = getTextPane().getText();
StyledDocument doc = (StyledDocument) getTextPane().getDocument();
MutableAttributeSet cstyle = getCommentStyle();
boolean inComment = false;
try (CountingLineReader reader = new CountingLineReader(new StringReader(text))) {
String line = reader.readLine();
while (line != null) {
if (!inComment) {
int cstart = line.indexOf(Commented.BEFORE_COMMENT);
if (cstart != NOT_THERE) {
inComment = parseAndMarkEndComment(doc, line, reader.lastStringPosition(), cstart);
}
int start = line.indexOf(Commented.AFTER_COMMENT);
if (start != NOT_THERE) {
int length = line.length() - start;
doc.setCharacterAttributes(reader.lastStringPosition() + start, length, cstyle, true);
}
} else {
// currently inside a /* */. comment
inComment = parseAndMarkEndComment(doc, line, reader.lastStringPosition(), 0);
}
line = reader.readLine();
}
} catch (IOException e) {
}
}
Aggregations