Search in sources :

Example 1 with Element

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

the class ResultAreaHelper method setHeader.

private void setHeader(String txt) {
    HTMLDocument d = (HTMLDocument) statusPane.getDocument();
    Element e = d.getElement(HEADER);
    try {
        d.setInnerHTML(e, "<p class=\"grayed\">" + txt + "</p>");
    } 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 2 with Element

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

the class ResultAreaHelper method setMain.

private void setMain(String html) {
    HTMLDocument d = (HTMLDocument) statusPane.getDocument();
    Element e = d.getElement(MAIN);
    try {
        d.setInnerHTML(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 3 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method getOffsetY.

/*
   * Determine the Y offset for the current row
   */
private int getOffsetY(int rowStartOffset, FontMetrics fontMetrics) throws BadLocationException {
    // Get the bounding rectangle of the row
    Rectangle r = component.modelToView(rowStartOffset);
    int lineHeight = fontMetrics.getHeight();
    int y = r.y + r.height;
    int descent = 0;
    if (// default font is being used
    r.height == lineHeight) {
        descent = fontMetrics.getDescent();
    } else // We need to check all the attributes for font changes
    {
        if (fonts == null)
            fonts = new HashMap<>();
        Element root = component.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 = component.getFontMetrics(font);
                fonts.put(key, fm);
            }
            descent = Math.max(descent, fm.getDescent());
        }
    }
    return y - descent;
}
Also used : HashMap(java.util.HashMap) AttributeSet(javax.swing.text.AttributeSet) FontMetrics(java.awt.FontMetrics) Element(javax.swing.text.Element) Rectangle(java.awt.Rectangle) Point(java.awt.Point) Font(java.awt.Font)

Example 4 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method isCurrentLine.

/*
   * We need to know if the caret is currently positioned on the line we
   * are about to paint so the line number can be highlighted.
   */
private boolean isCurrentLine(int rowStartOffset) {
    int caretPosition = component.getCaretPosition();
    Element root = component.getDocument().getDefaultRootElement();
    return root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition);
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Example 5 with Element

use of javax.swing.text.Element in project sonarqube by SonarSource.

the class TextLineNumber method getTextLineNumber.

/*
   * Get the line number to be drawn. The empty string will be returned
   * when a line of text has wrapped.
   */
protected String getTextLineNumber(int rowStartOffset) {
    Element root = component.getDocument().getDefaultRootElement();
    int index = root.getElementIndex(rowStartOffset);
    Element line = root.getElement(index);
    if (line.getStartOffset() == rowStartOffset)
        return String.valueOf(index + 1);
    else
        return "";
}
Also used : Element(javax.swing.text.Element) Point(java.awt.Point)

Aggregations

Element (javax.swing.text.Element)90 BadLocationException (javax.swing.text.BadLocationException)33 Point (java.awt.Point)16 AttributeSet (javax.swing.text.AttributeSet)14 Document (javax.swing.text.Document)14 HTMLDocument (javax.swing.text.html.HTMLDocument)11 View (javax.swing.text.View)5 FontMetrics (java.awt.FontMetrics)4 Rectangle (java.awt.Rectangle)4 IOException (java.io.IOException)4 AbstractDocument (javax.swing.text.AbstractDocument)4 java.awt (java.awt)3 Dimension (java.awt.Dimension)3 Font (java.awt.Font)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 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)3 TreePath (javax.swing.tree.TreePath)3