Search in sources :

Example 31 with AttributeSet

use of javax.swing.text.AttributeSet in project pcgen by PCGen.

the class NotesView method colorButtonActionPerformed.

//GEN-LAST:event_leftJustifyButtonActionPerformed
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);
    }
    editor.repaint();
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) AttributeSet(javax.swing.text.AttributeSet) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) Color(java.awt.Color)

Example 32 with AttributeSet

use of javax.swing.text.AttributeSet in project freeplane by freeplane.

the class HtmlUtils method getURLOfExistingLink.

/**
 * Gets the string URL of an existing link, or null if none.
 */
public static String getURLOfExistingLink(HTMLDocument doc, int pos) {
    // setIgnoreActions(true);
    final Element linkElement = HtmlUtils.getCurrentLinkElement(doc, pos);
    final boolean foundLink = (linkElement != null);
    if (!foundLink) {
        return null;
    }
    final AttributeSet elemAttrs = linkElement.getAttributes();
    final Object linkAttr = elemAttrs.getAttribute(HTML.Tag.A);
    final Object href = ((AttributeSet) linkAttr).getAttribute(HTML.Attribute.HREF);
    return href != null ? href.toString() : null;
}
Also used : AttributeSet(javax.swing.text.AttributeSet) Element(javax.swing.text.Element)

Example 33 with AttributeSet

use of javax.swing.text.AttributeSet in project freeplane by freeplane.

the class XHTMLWriter method isEndtag.

private boolean isEndtag(final Element elem) {
    final AttributeSet attributes = elem.getAttributes();
    final Object endTag = attributes.getAttribute(HTML.Attribute.ENDTAG);
    final boolean isEndtag = (endTag instanceof String) && ((String) endTag).equals("true");
    return isEndtag;
}
Also used : AttributeSet(javax.swing.text.AttributeSet)

Example 34 with AttributeSet

use of javax.swing.text.AttributeSet in project Zong by Xenoage.

the class FormattedTextConverter method fromStyledDocument.

/**
 * Creates a {@link FormattedText} from the given {@link StyledDocument}.
 */
public static FormattedText fromStyledDocument(StyledDocument styledDoc) {
    CList<FormattedTextParagraph> paragraphs = clist();
    int endPos = styledDoc.getLength();
    AttributeSet attribute = null;
    String textelement = "";
    String letter = "";
    CList<FormattedTextElement> elements = clist();
    Alignment alignment = null;
    // paragraph, when '\n' is found
    for (int i = 0; i < endPos; i++) {
        try {
            letter = styledDoc.getText(i, 1);
        } catch (BadLocationException e) {
        }
        // line break: create new paragraph
        if (letter.equals("\n")) {
            if (!textelement.equals("")) {
                elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
                textelement = "";
            }
            paragraphs.add(new FormattedTextParagraph(elements, alignment));
            elements = clist();
            alignment = null;
        } else if (attribute != styledDoc.getCharacterElement(i).getAttributes()) {
            // set alignment, if not already done
            if (alignment == null) {
                alignment = fromAttributeSet(styledDoc.getParagraphElement(i).getAttributes());
            }
            // style has changed, so save old element and create a new one
            if (!textelement.equals("")) {
                elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
            }
            attribute = styledDoc.getCharacterElement(i).getAttributes();
            textelement = letter;
        } else {
            // style stayed the same, so just append
            textelement += letter;
        }
    }
    if (!textelement.equals("")) {
        // save the last string
        elements.add(new FormattedTextString(textelement, getStyleFromAttributeSet(attribute)));
    }
    if (elements.size() > 0) {
        // add (non-empty) paragraph
        paragraphs.add(new FormattedTextParagraph(elements, alignment));
    }
    return new FormattedText(paragraphs);
}
Also used : Alignment(com.xenoage.zong.core.text.Alignment) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) AlignmentUtils.applyAlignmentToAttributeSet(com.xenoage.zong.desktop.utils.text.AlignmentUtils.applyAlignmentToAttributeSet) AlignmentUtils.fromAttributeSet(com.xenoage.zong.desktop.utils.text.AlignmentUtils.fromAttributeSet) AttributeSet(javax.swing.text.AttributeSet) FormattedTextParagraph(com.xenoage.zong.core.text.FormattedTextParagraph) FormattedTextString(com.xenoage.zong.core.text.FormattedTextString) FormattedText(com.xenoage.zong.core.text.FormattedText) FormattedTextElement(com.xenoage.zong.core.text.FormattedTextElement) BadLocationException(javax.swing.text.BadLocationException)

Example 35 with AttributeSet

use of javax.swing.text.AttributeSet in project energy3d by concord-consortium.

the class MyEditorPane method setText.

public void setText(final String text) {
    editorPane.setText(text);
    if (editorPane.getDocument() instanceof HTMLDocument) {
        final HTMLDocument doc = (HTMLDocument) editorPane.getDocument();
        final ElementIterator it = new ElementIterator(doc);
        Element element;
        while ((element = it.next()) != null) {
            final AttributeSet as = element.getAttributes();
            final Enumeration<?> en = as.getAttributeNames();
            DefaultButtonModel buttonModel = null;
            PlainDocument document = null;
            String action = null;
            String question = null;
            String choice = null;
            String key = null;
            String dataName = null;
            while (en.hasMoreElements()) {
                final Object n = en.nextElement();
                final Object v = as.getAttribute(n);
                if (v instanceof DefaultButtonModel) {
                    buttonModel = (DefaultButtonModel) v;
                } else if (v instanceof PlainDocument) {
                    document = (PlainDocument) v;
                } else if (n.toString().equals("action")) {
                    action = v.toString();
                } else if (n.toString().equals("question")) {
                    question = v.toString();
                } else if (n.toString().equals("choice")) {
                    choice = v.toString();
                } else if (n.toString().equals("key")) {
                    key = v.toString();
                } else if (n.toString().equals("data")) {
                    dataName = v.toString();
                }
            }
            if (action != null) {
                final String a = action;
                if (document != null) {
                    final String n = dataName;
                    final PlainDocument d = document;
                    document.addDocumentListener(new DocumentListener() {

                        @Override
                        public void removeUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }

                        @Override
                        public void insertUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }

                        @Override
                        public void changedUpdate(final DocumentEvent e) {
                            textFieldUpdated(a, n, d);
                        }
                    });
                }
                if (buttonModel != null) {
                    final QuestionnaireModel qm;
                    if (question != null && choice != null) {
                        boolean isKey = false;
                        if ("yes".equalsIgnoreCase(key) || "true".equalsIgnoreCase(key)) {
                            isKey = true;
                        }
                        qm = new QuestionnaireModel(question, choice, isKey);
                    } else {
                        qm = null;
                    }
                    final DefaultButtonModel bm = buttonModel;
                    if (buttonModel instanceof JToggleButton.ToggleButtonModel) {
                        buttonModel.addItemListener(new ItemListener() {

                            @Override
                            public void itemStateChanged(final ItemEvent e) {
                                if (qm != null) {
                                    // fire only one for questionnaires
                                    if (e.getStateChange() == ItemEvent.SELECTED) {
                                        buttonActionPerformed(a, qm, bm);
                                    }
                                } else {
                                    buttonActionPerformed(a, qm, bm);
                                }
                            }
                        });
                    } else {
                        buttonModel.addActionListener(new ActionListener() {

                            @Override
                            public void actionPerformed(final ActionEvent e) {
                                buttonActionPerformed(a, qm, bm);
                            }
                        });
                    }
                }
            }
        }
    }
}
Also used : ElementIterator(javax.swing.text.ElementIterator) DocumentListener(javax.swing.event.DocumentListener) DefaultButtonModel(javax.swing.DefaultButtonModel) ItemEvent(java.awt.event.ItemEvent) HTMLDocument(javax.swing.text.html.HTMLDocument) ActionEvent(java.awt.event.ActionEvent) Element(javax.swing.text.Element) EventString(org.concord.energy3d.agents.EventString) PlainDocument(javax.swing.text.PlainDocument) DocumentEvent(javax.swing.event.DocumentEvent) ActionListener(java.awt.event.ActionListener) AttributeSet(javax.swing.text.AttributeSet) ItemListener(java.awt.event.ItemListener) QuestionnaireModel(org.concord.energy3d.agents.QuestionnaireModel)

Aggregations

AttributeSet (javax.swing.text.AttributeSet)44 Element (javax.swing.text.Element)15 MutableAttributeSet (javax.swing.text.MutableAttributeSet)10 BadLocationException (javax.swing.text.BadLocationException)9 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)9 HTMLDocument (javax.swing.text.html.HTMLDocument)7 AbstractDocument (javax.swing.text.AbstractDocument)6 Document (javax.swing.text.Document)5 DocumentFilter (javax.swing.text.DocumentFilter)5 Font (java.awt.Font)3 Point (java.awt.Point)3 URL (java.net.URL)3 HTML (javax.swing.text.html.HTML)3 java.awt (java.awt)2 Color (java.awt.Color)2 Dimension (java.awt.Dimension)2 FontMetrics (java.awt.FontMetrics)2 Rectangle (java.awt.Rectangle)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2