本文整理汇总了Java中javax.swing.text.Element.getElement方法的典型用法代码示例。如果您正苦于以下问题:Java Element.getElement方法的具体用法?Java Element.getElement怎么用?Java Element.getElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Element
的用法示例。
在下文中一共展示了Element.getElement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: lineHeight
import javax.swing.text.Element; //导入方法依赖的package包/类
private int lineHeight() {
if (lineHeight < 0) {
Element root = editor.getDocument().getDefaultRootElement();
if (root.getElementCount()>0) {
Element elem = root.getElement(0);
try {
int y1 = editor.modelToView(elem.getStartOffset()).y;
int y2 = editor.modelToView(elem.getEndOffset()).y;
lineHeight = y2-y1;
} catch (BadLocationException blex) {
Logger.getLogger(CustomCodeView.class.getName()).log(Level.INFO, blex.getMessage(), blex);
}
}
if (lineHeight <= 0) {
// fallback
lineHeight = editor.getFontMetrics(editor.getFont()).getHeight();
}
}
return lineHeight;
}
示例2: getSelectionStart
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* Returns the offset where the selection starts on the specified line.
*/
public int getSelectionStart(int line) {
if (line == selectionStartLine) {
return selectionStart;
} else if (rectSelect) {
Element map = document.getDefaultRootElement();
int start = selectionStart - map.getElement(selectionStartLine).getStartOffset();
Element lineElement = map.getElement(line);
int lineStart = lineElement.getStartOffset();
int lineEnd = lineElement.getEndOffset() - 1;
return Math.min(lineEnd, lineStart + start);
} else {
return getLineStartOffset(line);
}
}
示例3: getWordEnd
import javax.swing.text.Element; //导入方法依赖的package包/类
@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;
}
示例4: appendOffset
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* Get string representation of an offset for debugging purposes
* in form "offset[line:column]". Both lines and columns start counting from 1
* like in the editor's status bar. Tabs are expanded when counting the column.
*
* @param sb valid string builder to which text will be appended or null in which case
* the method itself will create a string builder and it will return it.
* @param doc non-null document in which the offset is located.
* @param offset offset in the document.
* @return non-null string builder to which the description was added.
* @since 1.27
*/
public static StringBuilder appendOffset(StringBuilder sb, Document doc, int offset) {
if (sb == null) {
sb = new StringBuilder(50);
}
sb.append(offset).append('[');
if (offset < 0) { // Offset too low
sb.append("<0");
} else if (offset > doc.getLength() + 1) { // +1 for AbstractDocument-based docs
sb.append(">").append(doc.getLength());
} else { // Valid offset
Element paragraphRoot = getParagraphRootElement(doc);
int lineIndex = paragraphRoot.getElementIndex(offset);
Element lineElem = paragraphRoot.getElement(lineIndex);
sb.append(lineIndex + 1).append(':'); // Line
sb.append(visualColumn(doc, lineElem.getStartOffset(), offset) + 1); // Column
}
sb.append(']');
return sb;
}
示例5: setSharedSegment
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* 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);
}
}
示例6: invoke
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* Invokes this code template. The changes are made to the given text
* area.
*
* @param textArea The text area to operate on.
* @throws BadLocationException If something bad happens.
*/
@Override
public void invoke(RSyntaxTextArea textArea) throws BadLocationException {
Caret c = textArea.getCaret();
int dot = c.getDot();
int mark = c.getMark();
int p0 = Math.min(dot, mark);
int p1 = Math.max(dot, mark);
RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
Element map = doc.getDefaultRootElement();
int lineNum = map.getElementIndex(dot);
Element line = map.getElement(lineNum);
int start = line.getStartOffset();
int end = line.getEndOffset()-1; // Why always "-1"?
String s = textArea.getText(start,end-start);
int len = s.length();
// endWS is the end of the leading whitespace
// of the current line.
int endWS = 0;
while (endWS<len && RSyntaxUtilities.isWhitespace(s.charAt(endWS))) {
endWS++;
}
s = s.substring(0, endWS);
p0 -= getID().length();
String beforeText = getBeforeTextIndented(s);
String afterText = getAfterTextIndented(s);
doc.replace(p0,p1-p0, beforeText+afterText, null);
textArea.setCaretPosition(p0+beforeText.length());
}
示例7: getPathForIndex
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* Returns a TreePath to the element at <code>position</code>.
*/
protected TreePath getPathForIndex(int position, Object root,
Element rootElement) {
TreePath path = new TreePath(root);
Element child = rootElement.getElement(rootElement.getElementIndex(
position));
path = path.pathByAddingChild(rootElement);
path = path.pathByAddingChild(child);
while (!child.isLeaf()) {
child = child.getElement(child.getElementIndex(position));
path = path.pathByAddingChild(child);
}
return path;
}
示例8: doError
import javax.swing.text.Element; //导入方法依赖的package包/类
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);
}
示例9: getTextLineNumber
import javax.swing.text.Element; //导入方法依赖的package包/类
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 "";
}
示例10: getWordStart
import javax.swing.text.Element; //导入方法依赖的package包/类
@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);
}
示例11: loadChildren
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* Loads all of the children to initialize the view.
* This is called by the <code>setParent</code> method.
* Subclasses can re-implement this to initialize their
* child views in a different manner. The default
* implementation creates a child view for each
* child element.
*
* @param f the view factory
*/
@Override
protected void loadChildren(ViewFactory f) {
Element e = getElement();
int n = e.getElementCount();
if (n > 0) {
View[] added = new View[n];
for (int i = 0; i < n; i++) {
added[i] = new WrappedLine(e.getElement(i));
}
replace(0, 0, added);
}
}
示例12: noticeContainsPosition
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* 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();
}
示例13: getRangeForLine
import javax.swing.text.Element; //导入方法依赖的package包/类
static int[] getRangeForLine(final Accessible a, final int lineIndex) {
Accessible sa = CAccessible.getSwingAccessible(a);
if (!(sa instanceof JTextComponent)) return null;
final JTextComponent jc = (JTextComponent) sa;
final Element root = jc.getDocument().getDefaultRootElement();
final Element line = root.getElement(lineIndex);
if (line == null) return null;
return new int[] { line.getStartOffset(), line.getEndOffset() };
}
示例14: getTokenListForLine
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* Returns a token list for the specified segment of text representing
* the specified line number. This method is basically a wrapper for
* <code>tokenMaker.getTokenList</code> that takes into account the last
* token on the previous line to assure token accuracy.
*
* @param line The line number of <code>text</code> in the document,
* >= 0.
* @return A token list representing the specified line.
*/
public final Token getTokenListForLine(int line) {
tokenRetrievalCount++;
if (line==lastLine && cachedTokenList!=null) {
if (DEBUG_TOKEN_CACHING) {
useCacheCount++;
System.err.println("--- Using cached line; ratio now: " +
useCacheCount + "/" + tokenRetrievalCount);
}
return cachedTokenList;
}
lastLine = line;
Element map = getDefaultRootElement();
Element elem = map.getElement(line);
int startOffset = elem.getStartOffset();
//int endOffset = (line==map.getElementCount()-1 ? elem.getEndOffset() - 1:
// elem.getEndOffset() - 1);
int endOffset = elem.getEndOffset() - 1; // Why always "-1"?
try {
getText(startOffset,endOffset-startOffset, s);
} catch (BadLocationException ble) {
ble.printStackTrace();
return null;
}
int initialTokenType = line==0 ? Token.NULL :
getLastTokenTypeOnLine(line-1);
//return tokenMaker.getTokenList(s, initialTokenType, startOffset);
cachedTokenList = tokenMaker.getTokenList(s, initialTokenType, startOffset);
return cachedTokenList;
}
示例15: getParagraphElement
import javax.swing.text.Element; //导入方法依赖的package包/类
/**
* Get the paragraph element for the given document.
*
* @param doc non-null document instance.
* @param offset offset in the document >=0
* @return paragraph element containing the given offset.
*/
public static Element getParagraphElement(Document doc, int offset) {
Element paragraph;
if (doc instanceof StyledDocument) {
paragraph = ((StyledDocument)doc).getParagraphElement(offset);
} else {
Element rootElem = doc.getDefaultRootElement();
int index = rootElem.getElementIndex(offset);
paragraph = rootElem.getElement(index);
if ((offset < paragraph.getStartOffset()) || (offset >= paragraph.getEndOffset())) {
paragraph = null;
}
}
return paragraph;
}