Search in sources :

Example 21 with StyledDocument

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();
            }
        }
    }
}
Also used : StyledDocument(javax.swing.text.StyledDocument) Point(java.awt.Point)

Example 22 with StyledDocument

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) {
    }
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) CountingLineReader(org.vcell.util.CountingLineReader) StyledDocument(javax.swing.text.StyledDocument) StringReader(java.io.StringReader) IOException(java.io.IOException) Point(java.awt.Point)

Aggregations

StyledDocument (javax.swing.text.StyledDocument)22 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)7 BadLocationException (javax.swing.text.BadLocationException)6 Point (java.awt.Point)5 Style (javax.swing.text.Style)4 PersistentArrayMap (clojure.lang.PersistentArrayMap)2 ArrayList (java.util.ArrayList)2 TaskCallbackStatus (cbit.vcell.mapping.TaskCallbackMessage.TaskCallbackStatus)1 ColorSet (com.android.tools.sherpa.drawing.ColorSet)1 ExtendedHTMLDocument (gmgen.gui.ExtendedHTMLDocument)1 BorderLayout (java.awt.BorderLayout)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 AttributedString (java.text.AttributedString)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1