Example usage for javax.swing.text Element getAttributes

List of usage examples for javax.swing.text Element getAttributes

Introduction

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

Prototype

public AttributeSet getAttributes();

Source Link

Document

Fetches the collection of attributes this element contains.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame();
    JEditorPane edPane = new JEditorPane();
    edPane.setContentType("text/html");

    HTMLEditorKit hek = new HTMLEditorKit();

    edPane.setEditorKit(hek);//from   ww  w .ja v  a2  s .c  om

    HTMLDocument doc = (HTMLDocument) edPane.getDocument();

    doc.insertString(0, "Test testing", null);

    Element[] roots = doc.getRootElements();
    Element body = null;
    for (int i = 0; i < roots[0].getElementCount(); i++) {
        Element element = roots[0].getElement(i);
        if (element.getAttributes().getAttribute(StyleConstants.NameAttribute) == HTML.Tag.BODY) {
            body = element;
            break;
        }
    }

    doc.insertAfterEnd(body, "<img src=" + ClassLoader.getSystemResource("thumbnail.png").toString() + ">");
    frame.add(edPane);

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.java2s.com");
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);/*  w w  w.  jav  a 2  s .  c o  m*/

    ElementIterator iterator = new ElementIterator(htmlDoc);
    Element element;
    while ((element = iterator.next()) != null) {
        AttributeSet attributes = element.getAttributes();
        Object name = attributes.getAttribute(StyleConstants.NameAttribute);
        if ((name instanceof HTML.Tag) && (name == HTML.Tag.H1)) {
            StringBuffer text = new StringBuffer();
            int count = element.getElementCount();
            for (int i = 0; i < count; i++) {
                Element child = element.getElement(i);
                AttributeSet childAttributes = child.getAttributes();
                if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) {
                    int startOffset = child.getStartOffset();
                    int endOffset = child.getEndOffset();
                    int length = endOffset - startOffset;
                    text.append(htmlDoc.getText(startOffset, length));
                }
            }
            System.out.println(name + ": " + text.toString());
        }
    }
}

From source file:ElementIteratorExample.java

public static void main(String args[]) throws Exception {

    if (args.length != 1) {
        System.err.println("Usage: java ElementIteratorExample input-URL");
    }/*from w  w  w .  jav a 2s  . co  m*/

    // Load HTML file synchronously
    URL url = new URL(args[0]);
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);

    // Parse
    ElementIterator iterator = new ElementIterator(htmlDoc);
    Element element;
    while ((element = iterator.next()) != null) {
        AttributeSet attributes = element.getAttributes();
        Object name = attributes.getAttribute(StyleConstants.NameAttribute);
        if ((name instanceof HTML.Tag)
                && ((name == HTML.Tag.H1) || (name == HTML.Tag.H2) || (name == HTML.Tag.H3))) {
            // Build up content text as it may be within multiple elements
            StringBuffer text = new StringBuffer();
            int count = element.getElementCount();
            for (int i = 0; i < count; i++) {
                Element child = element.getElement(i);
                AttributeSet childAttributes = child.getAttributes();
                if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) {
                    int startOffset = child.getStartOffset();
                    int endOffset = child.getEndOffset();
                    int length = endOffset - startOffset;
                    text.append(htmlDoc.getText(startOffset, length));
                }
            }
            System.out.println(name + ": " + text.toString());
        }
    }
    System.exit(0);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    URLConnection connection = url.openConnection();
    InputStream is = connection.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    HTMLEditorKit htmlKit = new HTMLEditorKit();
    HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
    HTMLEditorKit.Parser parser = new ParserDelegator();
    HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
    parser.parse(br, callback, true);//from   ww  w  .j av  a  2 s . co  m

    Element element;
    ElementIterator iterator = new ElementIterator(htmlDoc);
    while ((element = iterator.next()) != null) {
        AttributeSet attributes = element.getAttributes();
        Object name = attributes.getAttribute(StyleConstants.NameAttribute);

        if ((name instanceof HTML.Tag) && (name == HTML.Tag.H1 || name == HTML.Tag.H2 || name == HTML.Tag.P)) {
            // Build up content text as it may be within multiple elements
            int count = element.getElementCount();
            for (int i = 0; i < count; i++) {
                Element child = element.getElement(i);
                AttributeSet childAttributes = child.getAttributes();
                if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.CONTENT) {
                    int startOffset = child.getStartOffset();
                    int endOffset = child.getEndOffset();
                    int length = endOffset - startOffset;
                    System.out.println(htmlDoc.getText(startOffset, length));
                }
            }
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws BadLocationException {
    JTextPane textPane1 = new JTextPane();

    MutableAttributeSet black = new SimpleAttributeSet();
    MutableAttributeSet red = new SimpleAttributeSet();

    StyleConstants.setForeground(black, Color.black);
    StyleConstants.setForeground(red, Color.red);
    textPane1.setEditorKit(new StyledEditorKit());
    doc1 = textPane1.getDocument();//from  www. j  ava2 s  .  c om

    append1("This is a Test!\n");

    attribute = red;
    append1("Hello world! Hello Stackoverflow\n");

    attribute = black;
    append1("the text is black again\n");

    StyledDocument styledDocument = textPane1.getStyledDocument();
    Element element;

    JTextPane textPane2 = new JTextPane();
    textPane2.setEditorKit(new StyledEditorKit());

    doc2 = textPane2.getDocument();
    for (int i = 0; i < styledDocument.getLength(); i++) {
        element = styledDocument.getCharacterElement(i);
        AttributeSet attributeNew = element.getAttributes();
        System.out.println(i);
        append2(styledDocument.getText(i, 1), attributeNew);
    }

    JFrame frame1 = new JFrame();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(400, 300);
    frame1.getContentPane().add(new JScrollPane(textPane1), BorderLayout.CENTER);
    frame1.setVisible(true);

    JFrame frame2 = new JFrame();
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(400, 300);
    frame2.setLocation(300, 0);
    frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER);
    frame2.setVisible(true);
}

From source file:Main.java

public static HTML.Tag nameOf(Element element) {
    return (HTML.Tag) element.getAttributes().getAttribute(AttributeSet.NameAttribute);
}

From source file:Main.java

private static AttributeSet getAttributes(MouseEvent e) {
    JTextPane text = (JTextPane) e.getSource();
    Point mouseLocation = new Point(e.getX(), e.getY());
    int pos = text.viewToModel(mouseLocation);

    if (pos >= 0) {
        try {/*w  ww .  j  a  v a  2  s . c  o  m*/
            Rectangle rect = text.modelToView(pos);
            int lowerCorner = rect.y + rect.height;
            if (e.getX() < rect.x && e.getY() < lowerCorner && pos > 0) {
                pos--;
            }
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
        StyledDocument doc = text.getStyledDocument();
        Element element = doc.getCharacterElement(pos);
        return element.getAttributes();
    }
    return null;
}

From source file:Main.java

public static boolean containsAttribute(Element element, Object name, Object value) {
    if (element == null) {
        return false;
    }//from  w  w  w .  ja v a  2s  .  c  o  m

    Object attribute = element.getAttributes().getAttribute(name);

    if (attribute == null) {
        return containsAttribute(element.getParentElement(), name, value);
    }
    return value.equals(attribute.toString());
}

From source file:ShowHTMLViews.java

public static void displayElement(Document doc, Element e, int indent, PrintStream out) {
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }/*from ww  w . ja v  a 2 s  .  c  o m*/
    out.println("===== Element Class: " + getShortClassName(e.getClass()));
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }
    int startOffset = e.getStartOffset();
    int endOffset = e.getEndOffset();
    out.println("Offsets [" + startOffset + ", " + endOffset + "]");
    AttributeSet a = e.getAttributes();
    Enumeration x = a.getAttributeNames();
    for (int i = 0; i < indent; i++) {
        out.print("  ");
    }
    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()) + "] ");
    }

    // Display the text for a leaf element
    if (e.isLeaf()) {
        try {
            String str = doc.getText(startOffset, endOffset - startOffset);
            if (str.length() > 40) {
                str = str.substring(0, 40);
            }
            if (str.length() > 0) {
                for (int i = 0; i < indent; i++) {
                    out.print("  ");
                }
                out.println("[" + str + "]");
            }
        } catch (BadLocationException ex) {
        }
    }

    // Display child elements
    int count = e.getElementCount();
    for (int i = 0; i < count; i++) {
        displayElement(doc, e.getElement(i), 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;/*from  www  .  j  a  v a  2s .com*/
    }
    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);
}