Search in sources :

Example 56 with Element

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

the class ResultAreaHelper method appendMain.

private void appendMain(String html) {
    HTMLDocument d = (HTMLDocument) statusPane.getDocument();
    Element e = d.getElement(MAIN);
    try {
        d.insertBeforeEnd(e, html);
    } catch (BadLocationException ex) {
        Tools.showError(ex);
    } catch (IOException ex) {
        Tools.showError(ex);
    }
}
Also used : HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) IOException(java.io.IOException) BadLocationException(javax.swing.text.BadLocationException)

Example 57 with Element

use of javax.swing.text.Element in project jadx by skylot.

the class LineNumbers method setPreferredWidth.

private void setPreferredWidth() {
    Element root = codeArea.getDocument().getDefaultRootElement();
    int lines = root.getElementCount();
    int digits = Math.max(String.valueOf(lines).length(), 3);
    if (lastDigits != digits) {
        lastDigits = digits;
        FontMetrics fontMetrics = getFontMetrics(getFont());
        int width = fontMetrics.charWidth('0') * digits;
        Insets insets = getInsets();
        int preferredWidth = insets.left + insets.right + width;
        Dimension d = getPreferredSize();
        if (d != null) {
            d.setSize(preferredWidth, HEIGHT);
            setPreferredSize(d);
            setSize(d);
        }
    }
}
Also used : Insets(java.awt.Insets) FontMetrics(java.awt.FontMetrics) Element(javax.swing.text.Element) Dimension(java.awt.Dimension) Point(java.awt.Point)

Example 58 with Element

use of javax.swing.text.Element in project jadx by skylot.

the class LineNumbers method getOffsetY.

private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
    Rectangle r = codeArea.modelToView(rowStartOffset);
    if (r == null) {
        throw new BadLocationException("Can't get Y offset", rowStartOffset);
    }
    int lineHeight = fontMetrics.getHeight();
    int y = r.y + r.height;
    int descent = 0;
    if (r.height == lineHeight) {
        descent = fontMetrics.getDescent();
    } else {
        if (fonts == null) {
            fonts = new HashMap<String, FontMetrics>();
        }
        Element root = codeArea.getDocument().getDefaultRootElement();
        int index = root.getElementIndex(rowStartOffset);
        Element line = root.getElement(index);
        for (int i = 0; i < line.getElementCount(); i++) {
            Element child = line.getElement(i);
            AttributeSet as = child.getAttributes();
            String fontFamily = (String) as.getAttribute(StyleConstants.FontFamily);
            Integer fontSize = (Integer) as.getAttribute(StyleConstants.FontSize);
            String key = fontFamily + fontSize;
            FontMetrics fm = fonts.get(key);
            if (fm == null) {
                Font font = new Font(fontFamily, Font.PLAIN, fontSize);
                fm = codeArea.getFontMetrics(font);
                fonts.put(key, fm);
            }
            descent = Math.max(descent, fm.getDescent());
        }
    }
    return y - descent;
}
Also used : AttributeSet(javax.swing.text.AttributeSet) FontMetrics(java.awt.FontMetrics) Element(javax.swing.text.Element) Rectangle(java.awt.Rectangle) BadLocationException(javax.swing.text.BadLocationException) Point(java.awt.Point) Font(java.awt.Font)

Example 59 with Element

use of javax.swing.text.Element in project jadx by skylot.

the class LineNumbers method caretUpdate.

@Override
public void caretUpdate(CaretEvent e) {
    int caretPosition = codeArea.getCaretPosition();
    Element root = codeArea.getDocument().getDefaultRootElement();
    int currentLine = root.getElementIndex(caretPosition);
    if (lastLine != currentLine) {
        repaint();
        lastLine = currentLine;
    }
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Example 60 with Element

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

the class SwingHelper method scrollToReference.

public static boolean scrollToReference(JEditorPane view, String reference) {
    reference = StringUtil.trimStart(reference, "#");
    List<String> toCheck = Arrays.asList("a", "h1", "h2", "h3", "h4");
    Document document = view.getDocument();
    if (document instanceof HTMLDocument) {
        List<Element> list = new ArrayList<>();
        for (Element root : document.getRootElements()) {
            getAllElements(root, list, toCheck);
        }
        for (Element element : list) {
            AttributeSet attributes = element.getAttributes();
            String nm = (String) attributes.getAttribute(HTML.Attribute.NAME);
            if (nm == null)
                nm = (String) attributes.getAttribute(HTML.Attribute.ID);
            if ((nm != null) && nm.equals(reference)) {
                try {
                    int pos = element.getStartOffset();
                    Rectangle r = view.modelToView(pos);
                    if (r != null) {
                        Rectangle vis = view.getVisibleRect();
                        r.y -= 5;
                        r.height = vis.height;
                        view.scrollRectToVisible(r);
                        return true;
                    }
                } catch (BadLocationException ex) {
                //ignore
                }
            }
        }
    }
    return false;
}
Also used : AttributeSet(javax.swing.text.AttributeSet) HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) HTMLDocument(javax.swing.text.html.HTMLDocument) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Aggregations

Element (javax.swing.text.Element)100 BadLocationException (javax.swing.text.BadLocationException)35 Point (java.awt.Point)19 AttributeSet (javax.swing.text.AttributeSet)15 Document (javax.swing.text.Document)15 HTMLDocument (javax.swing.text.html.HTMLDocument)11 FontMetrics (java.awt.FontMetrics)6 Dimension (java.awt.Dimension)5 Rectangle (java.awt.Rectangle)5 View (javax.swing.text.View)5 Font (java.awt.Font)4 Insets (java.awt.Insets)4 IOException (java.io.IOException)4 AbstractDocument (javax.swing.text.AbstractDocument)4 StyledDocument (javax.swing.text.StyledDocument)4 java.awt (java.awt)3 Objects (java.util.Objects)3 javax.swing (javax.swing)3 AbstractElement (javax.swing.text.AbstractDocument.AbstractElement)3 HTML (javax.swing.text.html.HTML)3