Example usage for javax.swing.text SimpleAttributeSet addAttribute

List of usage examples for javax.swing.text SimpleAttributeSet addAttribute

Introduction

In this page you can find the example usage for javax.swing.text SimpleAttributeSet addAttribute.

Prototype

public void addAttribute(Object name, Object value) 

Source Link

Document

Adds an attribute to the list.

Usage

From source file:SimpleAttributeBoldItalic.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Simple Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);

    try {/*from w ww. jav  a  2  s.c om*/
        document.insertString(document.getLength(), "Bold, Italic", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Simple Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.LIGHT_GRAY);

    try {//  w  w  w  .j a v  a 2s .c  o  m
        document.insertString(document.getLength(), " Bold, Italic and light gray color", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:SimpleAttributeSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Simple Attributes");
    Container content = frame.getContentPane();

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.TRUE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.TRUE);

    // Insert content
    try {//from  w  w  w  .  j  a  v a2s . c o  m
        document.insertString(document.getLength(), "Hello Java", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.lightGray);

    // Insert content
    try {
        document.insertString(document.getLength(), " - Good-bye Visual Basic", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);
    content.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:SimpleAttributeBoldItalicColor.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Simple Attributes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.LIGHT_GRAY);

    try {//from  w  ww .j  a  v  a  2s  .co  m
        document.insertString(document.getLength(), " Bold, Italic and light gray color", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:com.hexidec.ekit.action.ListAutomationAction.java

private void revertList(Element element) {
    //log.debug("Reverting list " + element.toString());
    if (element == null) {
        return;/*w  w w.j a v  a2  s  .  c om*/
    }
    int pos = parentEkit.getCaretPosition();
    HTML.Tag tag = htmlUtilities.getHTMLTag(element);
    String source = parentEkit.getSourcePane().getText();
    boolean hit = false;
    String idString;
    int counter = 0;
    do {
        hit = false;
        idString = "revertomatictaggen" + counter;
        if (source.indexOf(idString) > -1) {
            counter++;
            hit = true;
            if (counter > 10000) {
                return;
            }
        }
    } while (hit);
    SimpleAttributeSet sa = new SimpleAttributeSet(element.getAttributes());
    sa.addAttribute("id", idString);
    parentEkit.getExtendedHtmlDoc().replaceAttributes(element, sa, tag);
    parentEkit.refreshOnUpdate();
    source = parentEkit.getSourcePane().getText();
    StringBuffer newHtmlString = new StringBuffer();
    int[] position = htmlUtilities.getPositions(element, source, true, idString);
    if (position == null) {
        return;
    }
    for (int i = 0; i < position.length; i++) {
        if (position[i] < 0) {
            return;
        }
    }
    int beginStartTag = position[0];
    int endStartTag = position[1];
    int beginEndTag = position[2];
    int endEndTag = position[3];
    newHtmlString.append(source.substring(0, beginStartTag));
    String listText = source.substring(endStartTag, beginEndTag);
    //log.debug("Affected text is :" + listText);
    if (parentEkit.getEnterKeyIsBreak()) {
        listText = listText.replaceAll("<li>", "");
        listText = listText.replaceAll("</li>", "<br/>");
        newHtmlString.append("<br/>" + listText);
    } else {
        listText = listText.replaceAll("<li>", "<p style=\"margin-top: 0\">");
        listText = listText.replaceAll("</li>", "</p>");
        newHtmlString.append(listText);
    }
    //log.debug("Updated text is :" + listText);
    newHtmlString.append(source.substring(endEndTag, source.length()));
    parentEkit.getTextPane().setText(newHtmlString.toString());
    parentEkit.refreshOnUpdate();
}

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

public SimpleAttributeSet removeAttributeByKey(SimpleAttributeSet sourceAS, String removeKey) {
    SimpleAttributeSet temp = new SimpleAttributeSet();
    temp.addAttribute(removeKey, "NULL");
    return removeAttribute(sourceAS, temp);
}

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

/** Diese Methode fgt durch String-Manipulation in jtpSource
  * ein neues Listenelement hinzu, content ist dabei der Text der in dem neuen
  * Element stehen soll/*from   w w  w  . ja va  2  s.  co  m*/
  */
public void insertListElement(String content) {
    int pos = parent.getCaretPosition();
    String source = parent.getSourcePane().getText();
    boolean hit = false;
    String idString;
    int counter = 0;
    do {
        hit = false;
        idString = "diesisteineidzumsuchenimsource" + counter;
        if (source.indexOf(idString) > -1) {
            counter++;
            hit = true;
            if (counter > 10000) {
                return;
            }
        }
    } while (hit);
    Element element = getListItemParent();
    if (element == null) {
        return;
    }
    SimpleAttributeSet sa = new SimpleAttributeSet(element.getAttributes());
    sa.addAttribute("id", idString);
    parent.getExtendedHtmlDoc().replaceAttributes(element, sa, HTML.Tag.LI);
    parent.refreshOnUpdate();
    source = parent.getSourcePane().getText();
    StringBuffer newHtmlString = new StringBuffer();
    int[] positions = getPositions(element, source, true, idString);
    newHtmlString.append(source.substring(0, positions[3]));
    newHtmlString.append("<li>");
    newHtmlString.append(content);
    newHtmlString.append("</li>");
    newHtmlString.append(source.substring(positions[3] + 1, source.length()));
    parent.getTextPane().setText(newHtmlString.toString());
    parent.refreshOnUpdate();
    parent.setCaretPosition(pos - 1);
    element = getListItemParent();
    if (element != null) {
        sa = new SimpleAttributeSet(element.getAttributes());
        sa = removeAttributeByKey(sa, "id");
        parent.getExtendedHtmlDoc().replaceAttributes(element, sa, HTML.Tag.LI);
    }
}

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

/** Diese Methode lscht durch Stringmanipulation in jtpSource das bergebene Element,
  * Alternative fr removeElement in ExtendedHTMLDocument, mit closingTag wird angegeben
  * ob es ein schlieenden Tag gibt/*from www .  j av a 2 s  . c o m*/
  */
public void removeTag(Element element, boolean closingTag) {
    if (element == null) {
        return;
    }
    int pos = parent.getCaretPosition();
    HTML.Tag tag = getHTMLTag(element);
    // Versieht den Tag mit einer einmaligen ID
    String source = parent.getSourcePane().getText();
    boolean hit = false;
    String idString;
    int counter = 0;
    do {
        hit = false;
        idString = "diesisteineidzumsuchenimsource" + counter;
        if (source.indexOf(idString) > -1) {
            counter++;
            hit = true;
            if (counter > 10000) {
                return;
            }
        }
    } while (hit);
    SimpleAttributeSet sa = new SimpleAttributeSet(element.getAttributes());
    sa.addAttribute("id", idString);
    parent.getExtendedHtmlDoc().replaceAttributes(element, sa, tag);
    parent.refreshOnUpdate();
    source = parent.getSourcePane().getText();
    StringBuffer newHtmlString = new StringBuffer();
    int[] position = getPositions(element, source, closingTag, idString);
    if (position == null) {
        return;
    }
    for (int i = 0; i < position.length; i++) {
        if (position[i] < 0) {
            return;
        }
    }
    int beginStartTag = position[0];
    int endStartTag = position[1];
    if (closingTag) {
        int beginEndTag = position[2];
        int endEndTag = position[3];
        newHtmlString.append(source.substring(0, beginStartTag));
        newHtmlString.append(source.substring(endStartTag, beginEndTag));
        newHtmlString.append(source.substring(endEndTag, source.length()));
    } else {
        newHtmlString.append(source.substring(0, beginStartTag));
        newHtmlString.append(source.substring(endStartTag, source.length()));
    }
    parent.getTextPane().setText(newHtmlString.toString());
    parent.refreshOnUpdate();
}

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

public SimpleAttributeSet removeAttribute(SimpleAttributeSet sourceAS, SimpleAttributeSet removeAS) {
    try {/*from w  ww .jav a  2s . co  m*/
        String[] sourceKeys = new String[sourceAS.getAttributeCount()];
        String[] sourceValues = new String[sourceAS.getAttributeCount()];
        Enumeration sourceEn = sourceAS.getAttributeNames();
        int i = 0;
        while (sourceEn.hasMoreElements()) {
            Object temp = new Object();
            temp = sourceEn.nextElement();
            sourceKeys[i] = (String) temp.toString();
            sourceValues[i] = new String();
            sourceValues[i] = (String) sourceAS.getAttribute(temp).toString();
            i++;
        }
        String[] removeKeys = new String[removeAS.getAttributeCount()];
        String[] removeValues = new String[removeAS.getAttributeCount()];
        Enumeration removeEn = removeAS.getAttributeNames();
        int j = 0;
        while (removeEn.hasMoreElements()) {
            removeKeys[j] = (String) removeEn.nextElement().toString();
            removeValues[j] = (String) removeAS.getAttribute(removeKeys[j]).toString();
            j++;
        }
        SimpleAttributeSet result = new SimpleAttributeSet();
        boolean hit = false;
        for (int countSource = 0; countSource < sourceKeys.length; countSource++) {
            hit = false;
            if (sourceKeys[countSource] == "name" | sourceKeys[countSource] == "resolver") {
                hit = true;
            } else {
                for (int countRemove = 0; countRemove < removeKeys.length; countRemove++) {
                    if (removeKeys[countRemove] != "NULL") {
                        if (sourceKeys[countSource].toString() == removeKeys[countRemove].toString()) {
                            if (removeValues[countRemove] != "NULL") {
                                if (sourceValues[countSource].toString() == removeValues[countRemove]
                                        .toString()) {
                                    hit = true;
                                }
                            } else if (removeValues[countRemove] == "NULL") {
                                hit = true;
                            }
                        }
                    } else if (removeKeys[countRemove] == "NULL") {
                        if (sourceValues[countSource].toString() == removeValues[countRemove].toString()) {
                            hit = true;
                        }
                    }
                }
            }
            if (!hit) {
                result.addAttribute(sourceKeys[countSource].toString(), sourceValues[countSource].toString());
            }
        }
        return result;
    } catch (ClassCastException cce) {
        return null;
    }
}

From source file:org.docx4all.xml.HyperlinkML.java

private RunPropertiesML createRunPropertiesML() {
    SimpleAttributeSet attrs = new SimpleAttributeSet();
    attrs.addAttribute(WordMLStyleConstants.RStyleAttribute, "Hyperlink");
    return ElementMLFactory.createRunPropertiesML(attrs);
}