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