本文整理了Java中javax.swing.text.Element.getElementIndex()
方法的一些代码示例,展示了Element.getElementIndex()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.getElementIndex()
方法的具体详情如下:
包路径:javax.swing.text.Element
类名称:Element
方法名:getElementIndex
暂无
代码示例来源:origin: skylot/jadx
private boolean isCurrentLine(int rowStartOffset) {
int caretPosition = codeArea.getCaretPosition();
Element root = codeArea.getDocument().getDefaultRootElement();
return root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition);
}
代码示例来源:origin: SonarSource/sonarqube
private boolean isCurrentLine(int rowStartOffset) {
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
return root.getElementIndex(rowStartOffset) == root.getElementIndex(caretPosition);
}
代码示例来源:origin: SonarSource/sonarqube
protected String getTextLineNumber(int rowStartOffset) {
Element root = component.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
if (line.getStartOffset() == rowStartOffset)
return String.valueOf(index + 1);
else
return "";
}
代码示例来源:origin: skylot/jadx
@Nullable
protected String getTextLineNumber(int rowStartOffset) {
Element root = codeArea.getDocument().getDefaultRootElement();
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
if (line.getStartOffset() != rowStartOffset) {
return null;
}
int lineNumber = index + 1;
if (useSourceLines) {
Integer sourceLine = codeArea.getSourceLine(lineNumber);
if (sourceLine == null) {
return null;
}
return String.valueOf(sourceLine);
}
return String.valueOf(lineNumber);
}
代码示例来源:origin: SonarSource/sonarqube
@Override
public void caretUpdate(CaretEvent e) {
// Get the line the caret is positioned on
int caretPosition = component.getCaretPosition();
Element root = component.getDocument().getDefaultRootElement();
int currentLine = root.getElementIndex(caretPosition);
// Need to repaint so the correct line number can be highlighted
if (lastLine != currentLine) {
repaint();
lastLine = currentLine;
}
}
代码示例来源:origin: skylot/jadx
@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;
}
}
代码示例来源:origin: skylot/jadx
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
for (int i = 0; i < line.getElementCount(); i++) {
代码示例来源:origin: SonarSource/sonarqube
int index = root.getElementIndex(rowStartOffset);
Element line = root.getElement(index);
代码示例来源:origin: groovy/groovy-core
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// starting position in document
int start = textEditor.viewToModel(getViewport().getViewPosition());
// end position in document
int end = textEditor.viewToModel(new Point(10,
getViewport().getViewPosition().y +
(int) textEditor.getVisibleRect().getHeight())
);
// translate offsets to lines
Document doc = textEditor.getDocument();
int startline = doc.getDefaultRootElement().getElementIndex(start) + 1;
int endline = doc.getDefaultRootElement().getElementIndex(end) + 1;
Font f = textEditor.getFont();
int fontHeight = g.getFontMetrics(f).getHeight();
int fontDesc = g.getFontMetrics(f).getDescent();
int startingY = -1;
try {
startingY = textEditor.modelToView(start).y + fontHeight - fontDesc;
} catch (BadLocationException e1) {
System.err.println(e1.getMessage());
}
g.setFont(f);
for (int line = startline, y = startingY; line <= endline; y += fontHeight, line++) {
String lineNumber = StringGroovyMethods.padLeft(Integer.toString(line), 4, " ");
g.drawString(lineNumber, 0, y);
}
}
}
代码示例来源:origin: groovy/groovy-core
public int findTabLocation(int offset) {
// find first {
boolean cont = true;
while (offset > -1 && cont) {
Element el = doc.getCharacterElement(offset);
Object color =
el.getAttributes().getAttribute(StyleConstants.Foreground);
if (!COMMENT_COLOR.equals(color)) {
cont = segment.array[offset] != '{' &&
segment.array[offset] != '}';
}
offset -= cont ? 1 : 0;
}
if (offset > -1 && segment.array[offset] == '{') {
while (offset > -1 &&
!Character.isWhitespace(segment.array[offset--])) {
}
}
int index = offset < 0 || segment.array[offset] == '}' ? -4 : 0;
if (offset > -1) {
Element top = doc.getDefaultRootElement();
offset = top.getElement(top.getElementIndex(offset)).getStartOffset();
while (Character.isWhitespace(segment.array[offset++])) {
index++;
}
}
return index;
}
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
@Override
public int yForLineContaining(Rectangle alloc, int offs)
throws BadLocationException {
Element map = getElement();
int line = map.getElementIndex(offs);
return yForLine(alloc, line);
}
代码示例来源:origin: ron190/jsql-injection
/**
* Get index of line for current offset (generally cursor position).
* @param offset Position on the line
* @return Index of the line
* @throws BadLocationException
*/
public int getLineOfOffset(int offset) throws BadLocationException {
String errorMsg = "Can't translate offset to line";
Document doc = this.getDocument();
if (offset < 0) {
throw new BadLocationException(errorMsg, -1);
} else if (offset > doc.getLength()) {
throw new BadLocationException(errorMsg, doc.getLength() + 1);
} else {
Element map = doc.getDefaultRootElement();
return map.getElementIndex(offset);
}
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
/**
* Returns the token at the specified offset.
*
* @param doc The document.
* @param offset The offset of the token.
* @return The token, or <code>null</code> if the offset is not valid.
* @see #getTokenAtOffset(RSyntaxTextArea, int)
* @see #getTokenAtOffset(Token, int)
*/
public static Token getTokenAtOffset(RSyntaxDocument doc,
int offset) {
Element root = doc.getDefaultRootElement();
int lineIndex = root.getElementIndex(offset);
Token t = doc.getTokenListForLine(lineIndex);
return RSyntaxUtilities.getTokenAtOffset(t, offset);
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
private static Element getLineElem(Document d, int offs) {
Element map = d.getDefaultRootElement();
int index = map.getElementIndex(offs);
Element elem = map.getElement(index);
if ((offs>=elem.getStartOffset()) && (offs<elem.getEndOffset())) {
return elem;
}
return null;
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
@Override
protected int getWordStart(RTextArea textArea, int offs)
throws BadLocationException {
RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
Element root = doc.getDefaultRootElement();
int line = root.getElementIndex(offs);
Element elem = root.getElement(line);
return getWordStartImpl(doc, elem, offs);
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
@Override
public void insertUpdate(DocumentEvent e) {
// Adding text containing a newline to the visible line of a folded
// Fold causes that Fold to unfold. Check only start offset of
// insertion since that's the line that was "modified".
int startOffs = e.getOffset();
int endOffs = startOffs + e.getLength();
Document doc = e.getDocument();
Element root = doc.getDefaultRootElement();
int startLine = root.getElementIndex(startOffs);
int endLine = root.getElementIndex(endOffs);
if (startLine!=endLine) { // Inserted text covering > 1 line...
Fold fold = getFoldForLine(startLine);
if (fold!=null && fold.isCollapsed()) {
fold.toggleCollapsedState();
}
}
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
/**
* Returns the leading whitespace of a specific line in a document.
*
* @param doc The document.
* @param offs The offset whose line to get the leading whitespace for.
* @return The leading whitespace.
* @throws BadLocationException If <code>offs</code> is not a valid offset
* in the document.
* @see #getLeadingWhitespace(String)
*/
public static String getLeadingWhitespace(Document doc, int offs)
throws BadLocationException {
Element root = doc.getDefaultRootElement();
int line = root.getElementIndex(offs);
Element elem = root.getElement(line);
int startOffs = elem.getStartOffset();
int endOffs = elem.getEndOffset() - 1;
String text = doc.getText(startOffs, endOffs-startOffs);
return getLeadingWhitespace(text);
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
/**
* Returns whether the specified offset is "inside" the fold. This method
* returns <code>true</code> if the offset is greater than the fold start
* offset, and no further than the last offset of the last folded line.
*
* @param offs The offset to check.
* @return Whether the offset is "inside" the fold.
* @see #containsLine(int)
*/
public boolean containsOffset(int offs) {
boolean contained = false;
if (offs>getStartOffset()) {
// Use Elements to avoid BadLocationExceptions
Element root = textArea.getDocument().getDefaultRootElement();
int line = root.getElementIndex(offs);
contained = line<=getEndLine();
}
return contained;
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
@Override
protected int getWordEnd(RTextArea textArea, int offs)
throws BadLocationException {
RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
Element root = doc.getDefaultRootElement();
int line = root.getElementIndex(offs);
Element elem = root.getElement(line);
int end = elem.getEndOffset() - 1;
int wordEnd = offs;
while (wordEnd <= end) {
if (!isIdentifierChar(doc.charAt(wordEnd))) {
break;
}
wordEnd++;
}
return wordEnd;
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
try {
// We use the elements instead of calling getLineOfOffset(),
// etc. to speed things up just a tad (i.e. micro-optimize).
Document document = textArea.getDocument();
int caretPosition = textArea.getCaretPosition();
Element map = document.getDefaultRootElement();
int currentLineNum = map.getElementIndex(caretPosition);
Element currentLineElement = map.getElement(currentLineNum);
// Always take -1 as we don't want to remove the newline.
int currentLineEnd = currentLineElement.getEndOffset()-1;
if (caretPosition<currentLineEnd) {
document.remove(caretPosition,
currentLineEnd-caretPosition);
}
} catch (BadLocationException ble) {
ble.printStackTrace();
}
}
内容来源于网络,如有侵权,请联系作者删除!