Search in sources :

Example 1 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project languagetool by languagetool-org.

the class RetainLineBreakTransferHandler method extractText.

private String extractText(Reader reader) {
    StringBuilder result = new StringBuilder();
    HTMLEditorKit.ParserCallback parserCallback = new HTMLEditorKit.ParserCallback() {

        @Override
        public void handleText(char[] data, int pos) {
            result.append(data);
        }

        @Override
        public void handleStartTag(HTML.Tag tag, MutableAttributeSet attribute, int pos) {
        }

        @Override
        public void handleEndTag(HTML.Tag tag, int pos) {
        }

        @Override
        public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet a, int pos) {
            if (tag.equals(HTML.Tag.BR)) {
                result.append('\n');
            }
        }

        @Override
        public void handleComment(char[] data, int pos) {
        }

        @Override
        public void handleError(String errMsg, int pos) {
        }
    };
    try {
        new ParserDelegator().parse(reader, parserCallback, true);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return result.toString();
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) ParserDelegator(javax.swing.text.html.parser.ParserDelegator) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException)

Example 2 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project intellij-community by JetBrains.

the class PyPIPackageUtil method parsePackageVersionsFromArchives.

@NotNull
private static List<String> parsePackageVersionsFromArchives(@NotNull String archivesUrl) throws IOException {
    return HttpRequests.request(archivesUrl).userAgent(getUserAgent()).connect(request -> {
        final List<String> versions = new ArrayList<>();
        final Reader reader = request.getReader();
        new ParserDelegator().parse(reader, new HTMLEditorKit.ParserCallback() {

            HTML.Tag myTag;

            @Override
            public void handleStartTag(HTML.Tag tag, MutableAttributeSet set, int i) {
                myTag = tag;
            }

            @Override
            public void handleText(@NotNull char[] data, int pos) {
                if (myTag != null && "a".equals(myTag.toString())) {
                    String packageVersion = String.valueOf(data);
                    final String suffix = ".tar.gz";
                    if (!packageVersion.endsWith(suffix))
                        return;
                    packageVersion = StringUtil.trimEnd(packageVersion, suffix);
                    versions.add(splitNameVersion(packageVersion).second);
                }
            }
        }, true);
        versions.sort(PackageVersionComparator.VERSION_COMPARATOR.reversed());
        return versions;
    });
}
Also used : ParserDelegator(javax.swing.text.html.parser.ParserDelegator) Reader(java.io.Reader) FileReader(java.io.FileReader) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) HTML(javax.swing.text.html.HTML) MutableAttributeSet(javax.swing.text.MutableAttributeSet) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project intellij-community by JetBrains.

the class HyperlinkLabel method setHtmlText.

public void setHtmlText(String text) {
    HTMLEditorKit.Parser parse = new ParserDelegator();
    final HighlightedText highlightedText = new HighlightedText();
    try {
        parse.parse(new StringReader(text), new HTMLEditorKit.ParserCallback() {

            private TextAttributes currentAttributes = null;

            @Override
            public void handleText(char[] data, int pos) {
                highlightedText.appendText(data, currentAttributes);
            }

            @Override
            public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
                if (t == HTML.Tag.B) {
                    currentAttributes = BOLD_ATTRIBUTES;
                } else if (t == HTML.Tag.A) {
                    currentAttributes = myAnchorAttributes;
                }
            }

            @Override
            public void handleEndTag(HTML.Tag t, int pos) {
                currentAttributes = null;
            }
        }, false);
    } catch (IOException e) {
        LOG.error(e);
    }
    highlightedText.applyToComponent(this);
    final JComponent parent = (JComponent) getParent();
    parent.revalidate();
    parent.repaint();
    adjustSize();
}
Also used : ParserDelegator(javax.swing.text.html.parser.ParserDelegator) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) HTML(javax.swing.text.html.HTML) IOException(java.io.IOException) MutableAttributeSet(javax.swing.text.MutableAttributeSet) StringReader(java.io.StringReader) TextAttributes(com.intellij.openapi.editor.markup.TextAttributes)

Example 4 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project JMRI by JMRI.

the class JTextPaneAppender method createAttributes.

private void createAttributes() {
    String[] prio = new String[6];
    prio[0] = Level.FATAL.toString();
    prio[1] = Level.ERROR.toString();
    prio[2] = Level.WARN.toString();
    prio[3] = Level.INFO.toString();
    prio[4] = Level.DEBUG.toString();
    prio[5] = Level.TRACE.toString();
    myAttributeSet = new Hashtable<String, MutableAttributeSet>();
    for (int i = 0; i < prio.length; i++) {
        MutableAttributeSet att = new SimpleAttributeSet();
        myAttributeSet.put(prio[i], att);
        StyleConstants.setFontSize(att, 14);
    }
    StyleConstants.setForeground(myAttributeSet.get(Level.FATAL.toString()), Color.red);
    StyleConstants.setForeground(myAttributeSet.get(Level.ERROR.toString()), Color.red);
    StyleConstants.setForeground(myAttributeSet.get(Level.WARN.toString()), Color.orange);
    StyleConstants.setForeground(myAttributeSet.get(Level.INFO.toString()), Color.black);
    StyleConstants.setForeground(myAttributeSet.get(Level.DEBUG.toString()), Color.black);
    StyleConstants.setForeground(myAttributeSet.get(Level.TRACE.toString()), Color.black);
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) MutableAttributeSet(javax.swing.text.MutableAttributeSet)

Example 5 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project opennars by opennars.

the class SwingText method printColorBlock.

public void printColorBlock(final Color color, final String s) {
    // StyleContext sc = StyleContext.getDefaultStyleContext();
    MutableAttributeSet aset = getInputAttributes();
    StyleConstants.setBackground(aset, color);
    try {
        int l = doc.getLength();
        doc.insertString(l, s, aset);
    } catch (BadLocationException ex) {
        Logger.getLogger(SwingLogText.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

MutableAttributeSet (javax.swing.text.MutableAttributeSet)18 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)7 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)4 ParserDelegator (javax.swing.text.html.parser.ParserDelegator)4 IOException (java.io.IOException)3 BadLocationException (javax.swing.text.BadLocationException)3 HTML (javax.swing.text.html.HTML)3 Font (java.awt.Font)2 FileReader (java.io.FileReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 AttributeSet (javax.swing.text.AttributeSet)2 NotNull (org.jetbrains.annotations.NotNull)2 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 Point (java.awt.Point)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FocusAdapter (java.awt.event.FocusAdapter)1 FocusEvent (java.awt.event.FocusEvent)1 BufferedImage (java.awt.image.BufferedImage)1