Example usage for javax.swing.text AttributeSet copyAttributes

List of usage examples for javax.swing.text AttributeSet copyAttributes

Introduction

In this page you can find the example usage for javax.swing.text AttributeSet copyAttributes.

Prototype

public AttributeSet copyAttributes();

Source Link

Document

Returns an attribute set that is guaranteed not to change over time.

Usage

From source file:ShowHTMLViews.java

public static void displayAttributes(AttributeSet a, int indent, PrintStream out) {
    if (a == null)
        return;// w ww.j ava2  s  . co m

    a = a.copyAttributes();
    Enumeration x = a.getAttributeNames();
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }
    if (x != null) {
        out.println("ATTRIBUTES:");
        while (x.hasMoreElements()) {

            for (int i = 0; i < indent; i++) {
                out.print("  ");
            }
            Object attr = x.nextElement();
            out.println(
                    " (" + attr + ", " + a.getAttribute(attr) + ")" + " [" + getShortClassName(attr.getClass())
                            + "/" + getShortClassName(a.getAttribute(attr).getClass()) + "] ");
        }
    } else {
        out.println("No attributes");
    }
    if (a.getResolveParent() != null) {
        displayAttributes(a.getResolveParent().copyAttributes(), indent + 1, out);
    }
}

From source file:Main.java

private void changeStyle() {
    StyledDocument doc = (StyledDocument) textPane.getDocument();
    int selectionEnd = textPane.getSelectionEnd();
    int selectionStart = textPane.getSelectionStart();
    if (selectionStart == selectionEnd) {
        return;/* w  w  w. jav a2  s.c  o  m*/
    }
    Element element = doc.getCharacterElement(selectionStart);
    AttributeSet as = element.getAttributes();

    MutableAttributeSet asNew = new SimpleAttributeSet(as.copyAttributes());
    StyleConstants.setBold(asNew, !StyleConstants.isBold(as));
    doc.setCharacterAttributes(selectionStart, textPane.getSelectedText().length(), asNew, true);
    String text = (StyleConstants.isBold(as) ? "Cancel Bold" : "Bold");
    btnStyle.setText(text);
}

From source file:com.hexidec.ekit.component.ExtendedHTMLDocument.java

public void replaceAttributes(Element e, AttributeSet a, Tag tag) {
    if ((e != null) && (a != null)) {
        try {/*from  www  .  java 2  s.c  o  m*/
            writeLock();
            int start = e.getStartOffset();
            DefaultDocumentEvent changes = new DefaultDocumentEvent(start, e.getEndOffset() - start,
                    DocumentEvent.EventType.CHANGE);
            AttributeSet sCopy = a.copyAttributes();
            changes.addEdit(new AttributeUndoableEdit(e, sCopy, false));
            MutableAttributeSet attr = (MutableAttributeSet) e.getAttributes();
            Enumeration aNames = attr.getAttributeNames();
            Object value;
            Object aName;
            while (aNames.hasMoreElements()) {
                aName = aNames.nextElement();
                value = attr.getAttribute(aName);
                if (value != null && !value.toString().equalsIgnoreCase(tag.toString())) {
                    attr.removeAttribute(aName);
                }
            }
            attr.addAttributes(a);
            changes.end();
            fireChangedUpdate(changes);
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
        } finally {
            writeUnlock();
        }
    }
}

From source file:com.hexidec.ekit.component.ExtendedHTMLDocument.java

@Override
public void setParagraphAttributes(int offset, int length, AttributeSet s, boolean replace) {
    for (HTMLDocumentBehavior b : behaviors) {
        s = b.beforeSetParagraphAttributes(this, offset, length, s, replace);
    }//  ww w  .j a va2s . c  o  m

    try {
        writeLock();
        // Make sure we send out a change for the length of the paragraph.
        int end = Math.min(offset + length, getLength());
        Element e = getParagraphElement(offset);
        offset = e.getStartOffset();
        e = getParagraphElement(end);
        length = Math.max(0, e.getEndOffset() - offset);
        DefaultDocumentEvent changes = new DefaultDocumentEvent(offset, length, DocumentEvent.EventType.CHANGE);
        AttributeSet sCopy = s.copyAttributes();
        int lastEnd = Integer.MAX_VALUE;
        for (int pos = offset; pos <= end; pos = lastEnd) {
            Element paragraph = getParagraphElement(pos);
            if (lastEnd == paragraph.getEndOffset()) {
                lastEnd++;
            } else {
                lastEnd = paragraph.getEndOffset();
            }
            MutableAttributeSet attr = (MutableAttributeSet) paragraph.getAttributes();
            changes.addEdit(new AttributeUndoableEdit(paragraph, sCopy, replace));
            if (replace) {
                attr.removeAttributes(attr);
            }

            DocumentUtil.copyAllAttributesRemovingMarked(attr, s);
        }
        changes.end();
        fireChangedUpdate(changes);
        fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
    } finally {
        writeUnlock();
    }
}