Search in sources :

Example 31 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 32 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 33 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 34 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)

Example 35 with Element

use of javax.swing.text.Element in project jdk8u_jdk by JetBrains.

the class ElementTreePanel method caretUpdate.

// CaretListener
/**
     * Messaged when the selection in the editor has changed. Will update
     * the selection in the tree.
     */
public void caretUpdate(CaretEvent e) {
    if (!updatingSelection) {
        int selBegin = Math.min(e.getDot(), e.getMark());
        int end = Math.max(e.getDot(), e.getMark());
        List<TreePath> paths = new ArrayList<TreePath>();
        TreeModel model = getTreeModel();
        Object root = model.getRoot();
        int rootCount = model.getChildCount(root);
        // in the selection.
        for (int counter = 0; counter < rootCount; counter++) {
            int start = selBegin;
            while (start <= end) {
                TreePath path = getPathForIndex(start, root, (Element) model.getChild(root, counter));
                Element charElement = (Element) path.getLastPathComponent();
                paths.add(path);
                if (start >= charElement.getEndOffset()) {
                    start++;
                } else {
                    start = charElement.getEndOffset();
                }
            }
        }
        // If a path was found, select it (them).
        int numPaths = paths.size();
        if (numPaths > 0) {
            TreePath[] pathArray = new TreePath[numPaths];
            paths.toArray(pathArray);
            updatingSelection = true;
            try {
                getTree().setSelectionPaths(pathArray);
                getTree().scrollPathToVisible(pathArray[0]);
            } finally {
                updatingSelection = false;
            }
        }
    }
}
Also used : TreeModel(javax.swing.tree.TreeModel) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) TreePath(javax.swing.tree.TreePath) Element(javax.swing.text.Element)

Aggregations

Element (javax.swing.text.Element)40 BadLocationException (javax.swing.text.BadLocationException)15 Point (java.awt.Point)13 AttributeSet (javax.swing.text.AttributeSet)6 HTMLDocument (javax.swing.text.html.HTMLDocument)6 FontMetrics (java.awt.FontMetrics)4 Dimension (java.awt.Dimension)3 IOException (java.io.IOException)3 Document (javax.swing.text.Document)3 TreePath (javax.swing.tree.TreePath)3 ExtendedHTMLDocument (gmgen.gui.ExtendedHTMLDocument)2 Font (java.awt.Font)2 Insets (java.awt.Insets)2 Rectangle (java.awt.Rectangle)2 Accessible (javax.accessibility.Accessible)2 JTextComponent (javax.swing.text.JTextComponent)2 SimpleAttributeSet (javax.swing.text.SimpleAttributeSet)2 HTMLEditorKit (javax.swing.text.html.HTMLEditorKit)2 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1