use of javax.swing.text.Element in project binnavi by google.
the class ConsoleHelpers method getCurrentLine.
/**
* Get the text of the element containing the given position.
*
* @param pos the global document position at which to find the element containing it.
* @return the text contained within the element
*/
public String getCurrentLine(final int pos) {
final Element element = document.getParagraphElement(pos);
String line = "";
try {
line = document.getText(element.getStartOffset(), element.getEndOffset() - element.getStartOffset());
} catch (final BadLocationException e) {
System.out.println("Bad location!");
e.printStackTrace();
}
return line;
}
use of javax.swing.text.Element in project binnavi by google.
the class ConsoleCodeDocument method setCurrentLine.
/**
* Insert a line in the text element containing the passed position skipping a number of character
* from the beginning of the element. The function of the skipping of characters is to "jump" over
* the prompt in the beginning of a line.
*
* @param pos the position from which to retrieve the element that contains it
* @param skip how many characters are skipped from the beginning of the element when inserting
* the string
* @param line the line to insert
*/
public void setCurrentLine(final int pos, final int skip, final String line) {
final Element element = getParagraphElement(pos);
final int start = element.getStartOffset();
final int end = element.getEndOffset();
try {
remove(start + skip, end - (start + skip + 1));
super.insertString(start + skip, line, normal);
} catch (final BadLocationException e) {
System.out.println("Bad location!");
e.printStackTrace();
}
}
use of javax.swing.text.Element in project binnavi by google.
the class TextHelpers method getLineAtCaret.
public static int getLineAtCaret(final JTextComponent component) {
final int caretPosition = component.getCaretPosition();
final Element root = component.getDocument().getDefaultRootElement();
return root.getElementIndex(caretPosition) + 1;
}
use of javax.swing.text.Element in project jadx by skylot.
the class LineNumbers method paintComponent.
@SuppressWarnings("deprecation")
@Override
public void paintComponent(Graphics g) {
visibleRect = g.getClipBounds(visibleRect);
if (visibleRect == null) {
visibleRect = getVisibleRect();
}
if (visibleRect == null) {
return;
}
applyRenderHints(g);
Font baseFont = codeArea.getFont();
Font font = baseFont.deriveFont(baseFont.getSize2D() - 1.0f);
g.setFont(font);
Dimension size = getSize();
g.setColor(codeArea.getBackground());
g.fillRect(0, visibleRect.y, size.width, visibleRect.height);
FontMetrics fontMetrics = codeArea.getFontMetrics(font);
Insets insets = getInsets();
int availableWidth = size.width - insets.right;
textAreaInsets = codeArea.getInsets(textAreaInsets);
if (visibleRect.y < textAreaInsets.top) {
visibleRect.height -= (textAreaInsets.top - visibleRect.y);
visibleRect.y = textAreaInsets.top;
}
boolean lineWrap = codeArea.getLineWrap();
int cellHeight = codeArea.getLineHeight();
int ascent = codeArea.getMaxAscent();
int currentLine = codeArea.getCaretLineNumber();
int y;
int topLine;
int linesCount;
View parentView = null;
Rectangle editorRect = null;
if (lineWrap) {
Element root = codeArea.getDocument().getDefaultRootElement();
parentView = codeArea.getUI().getRootView(codeArea).getView(0);
int topPosition = codeArea.viewToModel(new Point(visibleRect.x, visibleRect.y));
topLine = root.getElementIndex(topPosition);
linesCount = root.getElementCount();
editorRect = getEditorBoundingRect();
Rectangle topLineBounds = getLineBounds(parentView, topLine, editorRect);
if (topLineBounds == null) {
return;
}
y = ascent + topLineBounds.y;
} else {
linesCount = codeArea.getLineCount();
topLine = (visibleRect.y - textAreaInsets.top) / cellHeight;
y = ascent + topLine * cellHeight + textAreaInsets.top;
}
int endY = visibleRect.y + visibleRect.height + ascent;
int lineNum = topLine;
boolean isCurLine = updateColor(g, false, true);
while (y < endY && lineNum < linesCount) {
try {
String lineStr = getTextLineNumber(lineNum + 1);
if (lineStr != null) {
isCurLine = updateColor(g, lineNum == currentLine, isCurLine);
int x = availableWidth - fontMetrics.stringWidth(lineStr);
g.drawString(lineStr, x, y);
}
if (lineWrap) {
Rectangle lineBounds = getLineBounds(parentView, lineNum, editorRect);
if (lineBounds == null) {
return;
}
y += lineBounds.height;
} else {
y += cellHeight;
}
lineNum++;
} catch (Exception e) {
LOG.debug("Line numbers draw error", e);
break;
}
}
}
use of javax.swing.text.Element in project chatty by chatty.
the class LinkController method getElement.
public static Element getElement(MouseEvent e) {
JTextPane text = (JTextPane) e.getSource();
Point mouseLocation = new Point(e.getX(), e.getY());
int pos = text.viewToModel(mouseLocation);
if (pos >= 0) {
/**
* Check if the found element is actually located where the mouse is
* pointing, and if not try the previous element.
*
* This is a fix to make detection of emotes more reliable. The
* viewToModel() method apparently searches for the closest element
* to the given position, disregarding the size of the elements,
* which means on the right side of an emote the next (non-emote)
* element is nearer.
*
* See also:
* https://stackoverflow.com/questions/24036650/detecting-image-on-current-mouse-position-only-works-on-part-of-image
*/
try {
Rectangle rect = text.modelToView(pos);
if (e.getX() < rect.x && e.getY() < rect.y + rect.height && pos > 0) {
pos--;
}
} catch (BadLocationException ex) {
}
StyledDocument doc = text.getStyledDocument();
Element element = doc.getCharacterElement(pos);
return element;
}
return null;
}
Aggregations