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);
}
}
}
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;
}
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);
}
}
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);
}
}
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");
}
Aggregations