Search in sources :

Example 11 with MutableAttributeSet

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

the class PyPIPackageUtil method parsePyPIListFromWeb.

@NotNull
private static List<String> parsePyPIListFromWeb(@NotNull String url, boolean isSimpleIndex) throws IOException {
    LOG.debug("Fetching index of all packages available on " + url);
    return HttpRequests.request(url).userAgent(getUserAgent()).connect(request -> {
        final List<String> packages = new ArrayList<>();
        final Reader reader = request.getReader();
        new ParserDelegator().parse(reader, new HTMLEditorKit.ParserCallback() {

            boolean inTable = false;

            HTML.Tag myTag;

            @Override
            public void handleStartTag(@NotNull HTML.Tag tag, @NotNull MutableAttributeSet set, int i) {
                myTag = tag;
                if (!isSimpleIndex) {
                    if ("table".equals(tag.toString())) {
                        inTable = !inTable;
                    }
                    if (inTable && "a".equals(tag.toString())) {
                        packages.add(String.valueOf(set.getAttribute(HTML.Attribute.HREF)));
                    }
                }
            }

            @Override
            public void handleText(@NotNull char[] data, int pos) {
                if (isSimpleIndex) {
                    if (myTag != null && "a".equals(myTag.toString())) {
                        packages.add(String.valueOf(data));
                    }
                }
            }

            @Override
            public void handleEndTag(@NotNull HTML.Tag tag, int i) {
                if (!isSimpleIndex) {
                    if ("table".equals(tag.toString())) {
                        inTable = !inTable;
                    }
                }
            }
        }, true);
        return packages;
    });
}
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 12 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project org.alloytools.alloy by AlloyTools.

the class OurConsole method style.

/*
     * Helper method that construct a mutable style with the given font name, font
     * size, boldness, color, and left indentation.
     */
static MutableAttributeSet style(String fontName, int fontSize, boolean boldness, boolean italic, boolean strike, Color color, int leftIndent) {
    MutableAttributeSet s = new SimpleAttributeSet();
    StyleConstants.setFontFamily(s, fontName);
    StyleConstants.setFontSize(s, fontSize);
    StyleConstants.setLineSpacing(s, -0.2f);
    StyleConstants.setBold(s, boldness);
    StyleConstants.setItalic(s, italic);
    StyleConstants.setForeground(s, color);
    StyleConstants.setLeftIndent(s, leftIndent);
    StyleConstants.setStrikeThrough(s, strike);
    return s;
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) MutableAttributeSet(javax.swing.text.MutableAttributeSet)

Example 13 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project org.alloytools.alloy by AlloyTools.

the class OurSyntaxDocument method do_setFont.

/**
 * Changes the font and tabsize for the document.
 */
public final void do_setFont(String fontName, int fontSize, int tabSize) {
    if (tabSize < 1)
        tabSize = 1;
    else if (tabSize > 100)
        tabSize = 100;
    if (fontName.equals(this.font) && fontSize == this.fontSize && tabSize == this.tabSize)
        return;
    this.font = fontName;
    this.fontSize = fontSize;
    this.tabSize = tabSize;
    for (MutableAttributeSet s : all) {
        StyleConstants.setFontFamily(s, fontName);
        StyleConstants.setFontSize(s, fontSize);
    }
    do_reapplyAll();
    // this
    BufferedImage im = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    // is
    // used
    // to
    // derive
    // the
    // tab
    // width
    int gap = tabSize * im.createGraphics().getFontMetrics(new Font(fontName, Font.PLAIN, fontSize)).charWidth('X');
    TabStop[] pos = new TabStop[100];
    for (int i = 0; i < 100; i++) {
        pos[i] = new TabStop(i * gap + gap);
    }
    StyleConstants.setTabSet(tabset, new TabSet(pos));
    setParagraphAttributes(0, getLength(), tabset, false);
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) TabSet(javax.swing.text.TabSet) TabStop(javax.swing.text.TabStop) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font)

Example 14 with MutableAttributeSet

use of javax.swing.text.MutableAttributeSet in project vcell by virtualcell.

the class MultiPurposeTextPanel method highlightComments.

private void highlightComments() {
    String text = getTextPane().getText();
    StyledDocument doc = (StyledDocument) getTextPane().getDocument();
    MutableAttributeSet cstyle = getCommentStyle();
    boolean inComment = false;
    try (CountingLineReader reader = new CountingLineReader(new StringReader(text))) {
        String line = reader.readLine();
        while (line != null) {
            if (!inComment) {
                int cstart = line.indexOf(Commented.BEFORE_COMMENT);
                if (cstart != NOT_THERE) {
                    inComment = parseAndMarkEndComment(doc, line, reader.lastStringPosition(), cstart);
                }
                int start = line.indexOf(Commented.AFTER_COMMENT);
                if (start != NOT_THERE) {
                    int length = line.length() - start;
                    doc.setCharacterAttributes(reader.lastStringPosition() + start, length, cstyle, true);
                }
            } else {
                // currently inside a /* */. comment
                inComment = parseAndMarkEndComment(doc, line, reader.lastStringPosition(), 0);
            }
            line = reader.readLine();
        }
    } catch (IOException e) {
    }
}
Also used : MutableAttributeSet(javax.swing.text.MutableAttributeSet) CountingLineReader(org.vcell.util.CountingLineReader) StyledDocument(javax.swing.text.StyledDocument) StringReader(java.io.StringReader) IOException(java.io.IOException) Point(java.awt.Point)

Example 15 with MutableAttributeSet

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

the class FontFallbackMarker method getAttributes.

private AttributeSet getAttributes(Font font) {
    MutableAttributeSet attrs = new SimpleAttributeSet();
    StyleConstants.setFontFamily(attrs, font.getFamily());
    StyleConstants.setFontSize(attrs, editorFont.getSize());
    return attrs;
}
Also used : SimpleAttributeSet(javax.swing.text.SimpleAttributeSet) MutableAttributeSet(javax.swing.text.MutableAttributeSet)

Aggregations

MutableAttributeSet (javax.swing.text.MutableAttributeSet)19 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)7 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)5 ParserDelegator (javax.swing.text.html.parser.ParserDelegator)5 IOException (java.io.IOException)3 Reader (java.io.Reader)3 BadLocationException (javax.swing.text.BadLocationException)3 HTML (javax.swing.text.html.HTML)3 Font (java.awt.Font)2 FileReader (java.io.FileReader)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