Example usage for javax.swing JTextPane getStyledDocument

List of usage examples for javax.swing JTextPane getStyledDocument

Introduction

In this page you can find the example usage for javax.swing JTextPane getStyledDocument.

Prototype

public StyledDocument getStyledDocument() 

Source Link

Document

Fetches the model associated with the editor.

Usage

From source file:plugin.notes.gui.NotesView.java

/**
 *  Updates Editing buttons based on the location of the cursor
 *
 *@param  textPane  text pane to update buttons base on
 *@param  pos       current text position
 *///from w w  w  . j av a2s .c o  m
private void updateButtons(JTextPane textPane, int pos) {
    StyledDocument doc = textPane.getStyledDocument();
    AttributeSet set = doc.getCharacterElement(pos - 1).getAttributes();
    AttributeSet set1 = doc.getCharacterElement(pos).getAttributes();

    if (StyleConstants.isBold(set) && StyleConstants.isBold(set1)) {
        highlightButton(boldButton);
    } else {
        stdButton(boldButton);
    }

    if (StyleConstants.isItalic(set) && StyleConstants.isItalic(set1)) {
        highlightButton(italicButton);
    } else {
        stdButton(italicButton);
    }

    if (StyleConstants.isUnderline(set) && StyleConstants.isUnderline(set1)) {
        highlightButton(underlineButton);
    } else {
        stdButton(underlineButton);
    }

    int align = StyleConstants.getAlignment(set);
    stdButton(leftJustifyButton);
    stdButton(rightJustifyButton);
    stdButton(centerJustifyButton);

    if (align == StyleConstants.ALIGN_LEFT) {
        highlightButton(leftJustifyButton);
    } else if (align == StyleConstants.ALIGN_RIGHT) {
        highlightButton(rightJustifyButton);
    } else if (align == StyleConstants.ALIGN_CENTER) {
        highlightButton(centerJustifyButton);
    }

    int fontSize = StyleConstants.getFontSize(set);

    for (int i = 0; i < sizeCB.getItemCount(); i++) {
        String value = (String) sizeCB.getItemAt(i);

        if (value.equals(Integer.toString(fontSize))) {
            sizeCB.setSelectedItem(value);

            break;
        }
    }
}