Search in sources :

Example 91 with Element

use of javax.swing.text.Element in project java-swing-tips by aterai.

the class EmptyIcon method scrollToId.

private static void scrollToId(JEditorPane editor, String id) {
    Document d = editor.getDocument();
    if (d instanceof HTMLDocument) {
        HTMLDocument doc = (HTMLDocument) d;
        Element element = doc.getElement(id);
        try {
            int pos = element.getStartOffset();
            // Java 9: Rectangle r = editor.modelToView2D(pos).getBounds();
            Rectangle r = editor.modelToView(pos);
            if (r != null) {
                Rectangle vis = editor.getVisibleRect();
                r.height = vis.height;
                editor.scrollRectToVisible(r);
                editor.setCaretPosition(pos);
            }
        } catch (BadLocationException ex) {
            UIManager.getLookAndFeel().provideErrorFeedback(editor);
        }
    }
}
Also used : 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 92 with Element

use of javax.swing.text.Element in project java-swing-tips by aterai.

the class SegmentCache method getWordEnd.

// @see javax.swing.text.Utilities.getWordEnd(...)
public static int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
    Element line = Optional.ofNullable(Utilities.getParagraphElement(c, offs)).orElseThrow(() -> new BadLocationException("No word at " + offs, offs));
    Document doc = c.getDocument();
    int lineStart = line.getStartOffset();
    int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
    int offs2 = offs;
    Segment seg = SegmentCache.getSharedSegment();
    doc.getText(lineStart, lineEnd - lineStart, seg);
    if (seg.count > 0) {
        BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
        words.setText(seg);
        int wordPosition = offs - lineStart + seg.offset;
        if (wordPosition >= words.last()) {
            wordPosition = words.last() - 1;
        }
        offs2 = lineStart + words.following(wordPosition) - seg.offset;
        for (int i = offs; i < offs2; i++) {
            char ch = seg.charAt(i - seg.offset);
            if (ch == '_' || ch == '-') {
                offs2 = i;
                break;
            }
        }
    }
    SegmentCache.releaseSharedSegment(seg);
    return offs2;
}
Also used : Element(javax.swing.text.Element) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException) Segment(javax.swing.text.Segment) BreakIterator(java.text.BreakIterator)

Example 93 with Element

use of javax.swing.text.Element in project java-swing-tips by aterai.

the class SimpleSyntaxDocument method processChangedLines.

private void processChangedLines(int offset, int length) throws BadLocationException {
    Element root = getDefaultRootElement();
    String content = getText(0, getLength());
    int startLine = root.getElementIndex(offset);
    int endLine = root.getElementIndex(offset + length);
    for (int i = startLine; i <= endLine; i++) {
        applyHighlighting(content, i);
    }
}
Also used : Element(javax.swing.text.Element)

Example 94 with Element

use of javax.swing.text.Element in project java-swing-tips by aterai.

the class LineNumberView method startScroll.

private void startScroll() {
    Document doc = textArea.getDocument();
    Element root = doc.getDefaultRootElement();
    // int ln = Math.max(1, Math.min(root.getElementCount(), model.getNumber().intValue()));
    int ln = model.getNumber().intValue();
    try {
        Element elem = root.getElement(ln - 1);
        Rectangle dest = textArea.modelToView(elem.getStartOffset());
        // Java 9: Rectangle dest = textArea.modelToView2D(elem.getStartOffset()).getBounds();
        Rectangle current = scroll.getViewport().getViewRect();
        new Timer(20, e -> {
            Timer animator = (Timer) e.getSource();
            if (dest.y < current.y && animator.isRunning()) {
                int d = Math.max(1, (current.y - dest.y) / 2);
                current.y = current.y - d;
                textArea.scrollRectToVisible(current);
            } else if (dest.y > current.y && animator.isRunning()) {
                int d = Math.max(1, (dest.y - current.y) / 2);
                current.y = current.y + d;
                textArea.scrollRectToVisible(current);
            } else {
                textArea.setCaretPosition(elem.getStartOffset());
                animator.stop();
            }
        }).start();
    } catch (BadLocationException ex) {
        UIManager.getLookAndFeel().provideErrorFeedback(textArea);
    }
}
Also used : java.awt(java.awt) ComponentAdapter(java.awt.event.ComponentAdapter) Objects(java.util.Objects) DocumentListener(javax.swing.event.DocumentListener) BadLocationException(javax.swing.text.BadLocationException) Document(javax.swing.text.Document) ComponentEvent(java.awt.event.ComponentEvent) DocumentEvent(javax.swing.event.DocumentEvent) Element(javax.swing.text.Element) Collections(java.util.Collections) javax.swing(javax.swing) Element(javax.swing.text.Element) Document(javax.swing.text.Document) BadLocationException(javax.swing.text.BadLocationException)

Example 95 with Element

use of javax.swing.text.Element in project freeplane by freeplane.

the class SplitNode method splitNode.

private String[] splitNode(final String text) {
    if (text.startsWith("<html>")) {
        String[] parts = null;
        final HTMLEditorKit kit = new HTMLEditorKit();
        final HTMLDocument doc = new HTMLDocument();
        final StringReader buf = new StringReader(text);
        try {
            kit.read(buf, doc, 0);
            final Element parent = getParentElement(doc);
            if (parent == null) {
                return null;
            }
            final int elementCount = parent.getElementCount();
            int notEmptyElementCount = 0;
            parts = new String[elementCount];
            for (int i = 0; i < elementCount; i++) {
                final Element current = parent.getElement(i);
                final int start = current.getStartOffset();
                final int end = current.getEndOffset();
                final String paragraphText = doc.getText(start, end - start).trim();
                if (paragraphText.length() > 0) {
                    final StringWriter out = new StringWriter();
                    new FixedHTMLWriter(out, doc, start, end - start).write();
                    final String string = out.toString();
                    if (!string.equals("")) {
                        parts[i] = string;
                        notEmptyElementCount++;
                    } else {
                        parts[i] = null;
                    }
                }
            }
            if (notEmptyElementCount <= 1) {
                return null;
            }
        } catch (final IOException e) {
            LogUtils.severe(e);
        } catch (final BadLocationException e) {
            LogUtils.severe(e);
        }
        return parts;
    }
    return text.split("\n");
}
Also used : StringWriter(java.io.StringWriter) HTMLDocument(javax.swing.text.html.HTMLDocument) Element(javax.swing.text.Element) StringReader(java.io.StringReader) FixedHTMLWriter(org.freeplane.core.util.FixedHTMLWriter) HTMLEditorKit(javax.swing.text.html.HTMLEditorKit) IOException(java.io.IOException) 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