Example usage for javax.swing.text Style getAttributeNames

List of usage examples for javax.swing.text Style getAttributeNames

Introduction

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

Prototype

public Enumeration<?> getAttributeNames();

Source Link

Document

Returns an enumeration over the names of the attributes that are defined locally in the set.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextPane textPane = new JTextPane();

    DefaultStyledDocument doc = (DefaultStyledDocument) textPane.getDocument();
    Enumeration e1 = doc.getStyleNames();
    while (e1.hasMoreElements()) {
        String styleName = (String) e1.nextElement();
        System.out.println(styleName);
        Style style = doc.getStyle(styleName);
        int count = style.getAttributeCount();
        System.out.println(count);

        Enumeration e = style.getAttributeNames();
        while (e.hasMoreElements()) {
            Object o = e.nextElement();
            if (o instanceof String) {
                String attrName = (String) o;
                Object attrValue = style.getAttribute(attrName);
                System.out.println(attrValue);
            } else if (o == StyleConstants.NameAttribute) {
                styleName = (String) style.getAttribute(o);
                System.out.println(styleName);
            } else if (o == StyleConstants.ResolveAttribute) {
                Style parent = (Style) style.getAttribute(o);
                System.out.println(parent.getName());
            } else {
                String attrName = o.toString();
                System.out.println(attrName);
                Object attrValue = style.getAttribute(o);
                System.out.println(attrValue);
            }//from w ww .ja v  a 2s.  c  om
        }
    }
}

From source file:pl.otros.logview.gui.message.html.ExportToHtml.java

protected Map<String, String> styleToCssMap(MessageFragmentStyle mfs) {
    Style style = mfs.getStyle();
    HashMap<String, Object> attributes = new HashMap<String, Object>();
    Enumeration<?> attributeNames = style.getAttributeNames();
    while (attributeNames.hasMoreElements()) {
        Object nextElement = attributeNames.nextElement();
        attributes.put(nextElement.toString(), style.getAttribute(nextElement));
    }//from ww w.  j ava2s.c  om

    Map<String, String> cssMap = new HashMap<String, String>();

    if (attributes.get("family") != null) {
        cssMap.put("font-family", String.format("'%s'", StyleConstants.getFontFamily(style)));
    }

    if (attributes.get("size") != null) {
        cssMap.put("font-size", Integer.toString(StyleConstants.getFontSize(style)));
    }

    if (attributes.get("foreground") != null) {
        cssMap.put("color", colorToHex(StyleConstants.getForeground(style)));
    }

    if (attributes.get("background") != null) {
        cssMap.put("background-color", colorToHex(StyleConstants.getBackground(style)));
    }

    if (attributes.get("bold") != null) {
        cssMap.put("font-weight", StyleConstants.isBold(style) ? "bold" : "normal");
    }

    if (attributes.get("italic") != null) {
        cssMap.put("font-style", StyleConstants.isItalic(style) ? "italic" : "normal");
    }

    if (attributes.get("underline") != null) {
        cssMap.put("text-decoration", StyleConstants.isItalic(style) ? "underline" : "none");
    }

    return cssMap;
}