Java Code Examples for javax.swing.text.DefaultStyledDocument#putProperty()

The following examples show how to use javax.swing.text.DefaultStyledDocument#putProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HighlightingTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Document createDocument(String text) {
    try {
        DefaultStyledDocument doc = new DefaultStyledDocument();
        doc.putProperty(Language.class, JavaTokenId.language());
        doc.insertString(0, text, SimpleAttributeSet.EMPTY);
        return doc;
    } catch (BadLocationException e) {
        fail(e.getMessage());
    }
    return null;
}
 
Example 2
Source File: SyntaxHighlightingTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Document createDocument(Language lang, String text) {
    try {
        DefaultStyledDocument doc = new DefaultStyledDocument();
        doc.putProperty(Language.class, lang);
        doc.insertString(0, text, SimpleAttributeSet.EMPTY);
        return doc;
    } catch (BadLocationException e) {
        fail(e.getMessage());
        return null;
    }
}
 
Example 3
Source File: JavaSourceDocument.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public JavaSourceDocument(String title, Reader in, SourceFile theSource) throws IOException {
    doc = new DefaultStyledDocument();
    this.title = title;
    this.sourceFile = theSource;
    Debug.println("Created JavaSourceDocument for " + title);
    try {
        dek.read(in, doc, 0);
    } catch (BadLocationException e) {
        throw new RuntimeException(e);
    }
    in.close();
    doc.putProperty(Document.TitleProperty, title);
    //        root = doc.getDefaultRootElement();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    FontMetrics fontMetrics = toolkit.getFontMetrics(sourceFont);
    TabStop[] tabs = new TabStop[50];
    float width = fontMetrics.stringWidth(" ");

    int tabSize = GUISaveState.getInstance().getTabSize();
    for (int i = 0; i < tabs.length; i++) {
        tabs[i] = new TabStop(width * (tabSize + tabSize * i));
    }
    TAB_SET = new TabSet(tabs);
    StyleConstants.setTabSet(commentAttributes, TAB_SET);
    StyleConstants.setTabSet(javadocAttributes, TAB_SET);

    StyleConstants.setTabSet(quotesAttributes, TAB_SET);

    StyleConstants.setTabSet(keywordsAttributes, TAB_SET);

    StyleConstants.setTabSet(commentAttributes, TAB_SET);

    StyleConstants.setTabSet(whiteAttributes, TAB_SET);
    StyleConstants.setFontFamily(whiteAttributes, sourceFont.getFamily());
    StyleConstants.setFontSize(whiteAttributes, sourceFont.getSize());
    StyleConstants.setLeftIndent(whiteAttributes, NumberedParagraphView.NUMBERS_WIDTH);

    doc.setParagraphAttributes(0, doc.getLength(), whiteAttributes, true);
    JavaScanner parser = new JavaScanner(new DocumentCharacterIterator(doc));
    while (parser.next() != JavaScanner.EOF) {
        int kind = parser.getKind();
        switch (kind) {
        case JavaScanner.COMMENT:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), commentAttributes, true);
            break;

        case JavaScanner.KEYWORD:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), keywordsAttributes, true);
            break;

        case JavaScanner.JAVADOC:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), javadocAttributes, true);
            break;

        case JavaScanner.QUOTE:
            doc.setCharacterAttributes(parser.getStartPosition(), parser.getLength(), quotesAttributes, true);
            break;

        default:
            break;
        }

    }

}