Example usage for javax.swing.text SimpleAttributeSet SimpleAttributeSet

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

Introduction

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

Prototype

public SimpleAttributeSet(AttributeSet source) 

Source Link

Document

Creates a new attribute set based on a supplied set of attributes.

Usage

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  .j  a  v a  2 s  .co 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:Main.java

private void initComponents() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane textPane = new JTextPane();
    ((AbstractDocument) textPane.getDocument()).addDocumentListener(new DocumentListener() {
        @Override//from  ww  w. ja va 2s .c om
        public void insertUpdate(final DocumentEvent de) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                        StyledDocument doc = (StyledDocument) de.getDocument();
                        int start = Utilities.getRowStart(textPane, Math.max(0, de.getOffset() - 1));
                        int end = Utilities.getWordStart(textPane, de.getOffset() + de.getLength());

                        String text = doc.getText(start, end - start);

                        for (String emoticon : imageTokens) {
                            int i = text.indexOf(emoticon);
                            while (i >= 0) {
                                final SimpleAttributeSet attrs = new SimpleAttributeSet(
                                        doc.getCharacterElement(start + i).getAttributes());
                                if (StyleConstants.getIcon(attrs) == null) {
                                    switch (emoticon) {
                                    case imageToken:
                                        StyleConstants.setIcon(attrs, anImage);
                                        break;
                                    }
                                    doc.remove(start + i, emoticon.length());
                                    doc.insertString(start + i, emoticon, attrs);
                                }
                                i = text.indexOf(emoticon, i + emoticon.length());
                            }
                        }
                    } catch (BadLocationException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
        }
    });

    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(300, 300));
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

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/*w w  w .  j a  va  2 s  . com*/
  */
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   w w w.  ja v a 2s  .  c  om
  */
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.action.ListAutomationAction.java

private void revertList(Element element) {
    //log.debug("Reverting list " + element.toString());
    if (element == null) {
        return;//from w  ww  . j av  a2  s . c o  m
    }
    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:plugin.notes.gui.NotesView.java

private void colorButtonActionPerformed() {
    //GEN-FIRST:event_colorButtonActionPerformed
    AttributeSet as = editor.getCharacterAttributes();
    SimpleAttributeSet sas = new SimpleAttributeSet(as);
    Color newColor = JColorChooser.showDialog(GMGenSystem.inst, "Choose Text Color",
            editor.getStyledDocument().getForeground(as));

    if (newColor != null) {
        StyleConstants.setForeground(sas, newColor);
        editor.setCharacterAttributes(sas, true);
    }/*from  w  w w  .  j  av  a 2s.c o m*/

    editor.repaint();
}