本文整理了Java中javax.swing.text.Element.getElement()
方法的一些代码示例,展示了Element.getElement()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.getElement()
方法的具体详情如下:
包路径:javax.swing.text.Element
类名称:Element
方法名:getElement
暂无
代码示例来源: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: 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
private boolean getDoAdd(Document doc, Element map, int startLine,
int endLine, String[] startEnd)
throws BadLocationException {
boolean doAdd = false;
for (int i=startLine; i<=endLine; i++) {
Element elem = map.getElement(i);
int start = elem.getStartOffset();
String t = doc.getText(start, elem.getEndOffset()-start-1);
if (!t.startsWith(startEnd[0]) ||
(startEnd[1]!=null && !t.endsWith(startEnd[1]))) {
doAdd = true;
break;
}
}
return doAdd;
}
代码示例来源:origin: ron190/jsql-injection
/**
* Get position of the beginning of the line.
* @param line Index of the line
* @return Offset of line
* @throws BadLocationException
*/
public int getLineStartOffset(int line) throws BadLocationException {
Element map = this.getDocument().getDefaultRootElement();
if (line < 0) {
throw new BadLocationException("Negative line", -1);
} else if (line >= map.getElementCount()) {
throw new BadLocationException("No such line", this.getDocument().getLength() + 1);
} else {
Element lineElem = map.getElement(line);
return lineElem.getStartOffset();
}
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
@Override
public int yForLine(Rectangle alloc, int line) throws BadLocationException {
return yForLineContaining(alloc,
getElement().getElement(line).getStartOffset());
//return alloc.y + getOffset(Y_AXIS, line);
}
代码示例来源: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: SKCraft/Launcher
private void removeFromStart(Document document, Element root, int excess) {
Element line = root.getElement(excess - 1);
int end = line.getEndOffset();
try {
document.remove(0, end);
} catch (BadLocationException ble) {
System.out.println(ble);
}
}
代码示例来源: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
/**
* Makes our private <code>Segment s</code> point to the text in our
* document referenced by the specified element. Note that
* <code>line</code> MUST be a valid line number in the document.
*
* @param line The line number you want to get.
*/
private void setSharedSegment(int line) {
Element map = getDefaultRootElement();
//int numLines = map.getElementCount();
Element element = map.getElement(line);
if (element==null) {
throw new InternalError("Invalid line number: " + line);
}
int startOffset = element.getStartOffset();
//int endOffset = (line==numLines-1 ?
// element.getEndOffset()-1 : element.getEndOffset() - 1);
int endOffset = element.getEndOffset()-1; // Why always "-1"?
try {
getText(startOffset, endOffset-startOffset, s);
} catch (BadLocationException ble) {
throw new InternalError("Text range not in document: " +
startOffset + "-" + endOffset);
}
}
代码示例来源: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: org.gosu-lang.gosu/gosu-lab
public int getLineOffset( int iLine )
{
Element root = getDocument().getRootElements()[0];
iLine = root.getElementCount() < iLine ? root.getElementCount() : iLine;
Element elemAtLine = root.getElement( iLine );
return elemAtLine == null ? 0 : elemAtLine.getStartOffset();
}
代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce
private int getLineStart(int i) throws BadLocationException {
Element line = editor.getDocument().getDefaultRootElement().getElement(i);
Rectangle result = editor.modelToView(line.getStartOffset());
if (result != null) {
return result.y;
}
return -1;
}
代码示例来源: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();
}
}
代码示例来源:origin: SKCraft/SKMCLauncher
private void removeFromStart(Document document, Element root) {
Element line = root.getElement(0);
int end = line.getEndOffset();
try {
document.remove(0, end);
} catch (BadLocationException ble) {
System.out.println(ble);
}
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
private void doError(SAXParseException e, ParserNotice.Level level) {
int line = e.getLineNumber() - 1;
Element root = doc.getDefaultRootElement();
Element elem = root.getElement(line);
int offs = elem.getStartOffset();
int len = elem.getEndOffset() - offs;
if (line==root.getElementCount()-1) {
len++;
}
DefaultParserNotice pn = new DefaultParserNotice(XmlParser.this,
e.getMessage(), line, offs, len);
pn.setLevel(level);
result.addNotice(pn);
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
/**
* Returns whether a parser notice contains the specified offset.
*
* @param notice The notice.
* @param offs The offset.
* @return Whether the notice contains the offset.
*/
private boolean noticeContainsPosition(ParserNotice notice, int offs){
if (notice.getKnowsOffsetAndLength()) {
return notice.containsPosition(offs);
}
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
int line = notice.getLine();
if (line<0) { // Defensive against possible bad user-defined notices.
return false;
}
Element elem = root.getElement(line);
return elem != null && offs>=elem.getStartOffset() && offs<elem.getEndOffset();
}
代码示例来源: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: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
/** Return start offset of the line
* @param lineIndex line index starting from 0
* @return start position of the line or -1 if lineIndex was invalid
*/
public static int getRowStartFromLineOffset(BaseDocument doc, int lineIndex) {
Element lineRoot = doc.getParagraphElement(0).getParentElement();
if (lineIndex < 0 || lineIndex >= lineRoot.getElementCount()) {
return -1; // invalid line number
} else {
return lineRoot.getElement(lineIndex).getStartOffset();
}
}
代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce
private int getLineStart(int i) throws BadLocationException {
Element line = editor.getDocument().getDefaultRootElement().getElement(i);
Rectangle result = editor.modelToView(line.getStartOffset());
if (result != null) {
return result.y;
}
return -1;
}
代码示例来源:origin: bobbylight/RSyntaxTextArea
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
int offs = textArea.getCaretPosition();
int endOffs = 0;
try {
if (textArea.getLineWrap()) {
// Must check per character, since one logical line may be
// many physical lines.
// FIXME: Replace Utilities call with custom version to
// cut down on all of the modelToViews, as each call causes
// a getTokenList => expensive!
endOffs = Utilities.getRowEnd(textArea, offs);
}
else {
Element root = textArea.getDocument().getDefaultRootElement();
int line = root.getElementIndex(offs);
endOffs = root.getElement(line).getEndOffset() - 1;
}
if (select) {
textArea.moveCaretPosition(endOffs);
}
else {
textArea.setCaretPosition(endOffs);
}
} catch (Exception ex) {
UIManager.getLookAndFeel().provideErrorFeedback(textArea);
}
}
内容来源于网络,如有侵权,请联系作者删除!