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