本文整理匯總了Java中javax.swing.text.Element.getStartOffset方法的典型用法代碼示例。如果您正苦於以下問題:Java Element.getStartOffset方法的具體用法?Java Element.getStartOffset怎麽用?Java Element.getStartOffset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.text.Element
的用法示例。
在下文中一共展示了Element.getStartOffset方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getRowFirstNonWhite
import javax.swing.text.Element; //導入方法依賴的package包/類
/**
* Gets index of first not space/tab element in line where caret is or caret
* position if non found before its location
*
* @param doc edited document
* @param caretOffset current caret position
* @return Integer index of first space or offset passed in if none before
* it
* @throws BadLocationException
*/
static int getRowFirstNonWhite(StyledDocument doc, int caretOffset)
throws BadLocationException {
Element lineElement = doc.getParagraphElement(caretOffset);//line start&stop offsets
int start = lineElement.getStartOffset();
int failsafe = start;
while (start + 1 < lineElement.getEndOffset()) {
try {
if (doc.getText(start, 1).charAt(0) != ' ') {
break;
}
} catch (BadLocationException ex) {
throw (BadLocationException) new BadLocationException(
"calling getText(" + start + ", " + (start + 1)
+ ") on doc of length: " + doc.getLength(), start
).initCause(ex);
}
start++;
}
return start > caretOffset ? failsafe : start;
}
示例2: checkNoTrailingWhitespace
import javax.swing.text.Element; //導入方法依賴的package包/類
public static void checkNoTrailingWhitespace(Document doc) {
Element lineRoot = DocumentUtilities.getParagraphRootElement(doc);
CharSequence docText = DocumentUtilities.getText(doc);
for (int i = 0; i < lineRoot.getElementCount(); i++) {
Element lineElem = lineRoot.getElement(i);
int lineLastOffset = lineElem.getEndOffset() - 2;
if (lineLastOffset >= lineElem.getStartOffset()) { // At least one non newline char
switch (docText.charAt(lineLastOffset)) {
case ' ':
case '\t':
throw new IllegalStateException("Trailing whitespace exists at lineIndex=" + i + // NOI18N
", lineStartOffset=" + lineElem.getStartOffset() + // NOI18N
", lineEndOffset=" + lineElem.getEndOffset() + // NOI18N
'\n' + dumpLines(null, doc));
}
}
}
}
示例3: moveLineDown
import javax.swing.text.Element; //導入方法依賴的package包/類
public void moveLineDown(RTextArea textArea, int line)
throws BadLocationException {
Document doc = textArea.getDocument();
Element root = doc.getDefaultRootElement();
Element elem = root.getElement(line);
int start = elem.getStartOffset();
int end = elem.getEndOffset();
int caret = textArea.getCaretPosition();
int caretOffset = caret - start;
String text = doc.getText(start, end-start);
doc.remove(start, end-start);
Element elem2 = root.getElement(line); // not "line+1" - removed.
//int start2 = elem2.getStartOffset();
int end2 = elem2.getEndOffset();
doc.insertString(end2, text, null);
elem = root.getElement(line+1);
textArea.setCaretPosition(elem.getStartOffset()+caretOffset);
}
示例4: removeChar
import javax.swing.text.Element; //導入方法依賴的package包/類
public static void removeChar(JTextComponent tc, boolean nextChar) throws BadLocationException {
List<Position> regions = regionsCopy(tc);
Document doc = tc.getDocument();
doc.putProperty(RECTANGULAR_DO_NOT_RESET_AFTER_DOCUMENT_CHANGE, Boolean.TRUE);
int regionsLength = regions.size();
Element lineRoot = null;
int lineIndex = 0;
for (int i = 1; i < regionsLength; i += 2) {
int offset = regions.get(i).getOffset();
if (lineRoot == null) {
lineRoot = doc.getDefaultRootElement();
lineIndex = lineRoot.getElementIndex(offset);
}
Element line = lineRoot.getElement(lineIndex++);
if (nextChar) {
if (offset < line.getEndOffset() - 1) {
doc.remove(offset, 1);
}
} else { // Previous char (Backspace)
if (offset > line.getStartOffset()) {
doc.remove(offset - 1, 1);
}
}
}
}
示例5: getWordStartImpl
import javax.swing.text.Element; //導入方法依賴的package包/類
private static int getWordStartImpl(RSyntaxDocument doc,
Element elem, int offs) throws BadLocationException {
int start = elem.getStartOffset();
int wordStart = offs;
while (wordStart >= start) {
char ch = doc.charAt(wordStart);
// Ignore newlines so we work when caret is at end of line
if (!isIdentifierChar(ch) && ch != '\n') {
break;
}
wordStart--;
}
return wordStart==offs ? offs : wordStart + 1;
}
示例6: removeIfEmpty
import javax.swing.text.Element; //導入方法依賴的package包/類
void removeIfEmpty(Element elem) {
if (elem.getEndOffset() - elem.getStartOffset() < 2) {
try {
document.remove(elem.getStartOffset(), elem.getEndOffset());
} catch (Exception ex) {
//ex.printStackTrace();
}
}
}
示例7: 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 "";
}
示例8: getPreviousWord
import javax.swing.text.Element; //導入方法依賴的package包/類
@Override
protected int getPreviousWord(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);
// If caret is at the beginning of a word, we should return the
// previous word
int start = elem.getStartOffset();
if (offs > start) {
char ch = doc.charAt(offs);
if (isIdentifierChar(ch)) {
offs--;
}
}
else { // offs == start => previous word is on previous line
if (line == 0) {
return -1;
}
elem = root.getElement(--line);
offs = elem.getEndOffset() - 1;
}
int prevWordStart = getPreviousWordStartInLine(doc, elem, offs);
while (prevWordStart == -1 && line > 0) {
line--;
elem = root.getElement(line);
prevWordStart = getPreviousWordStartInLine(doc, elem,
elem.getEndOffset());
}
return prevWordStart;
}
示例9: removeIfEmpty
import javax.swing.text.Element; //導入方法依賴的package包/類
void removeIfEmpty(Element elem) {
if (elem.getEndOffset() - elem.getStartOffset() < 2) {
try {
document.remove(elem.getStartOffset(), elem.getEndOffset());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
示例10: modelToView
import javax.swing.text.Element; //導入方法依賴的package包/類
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
EditorUI editorUI = getEditorUI();
Rectangle ret = new Rectangle();
BaseDocument doc = (BaseDocument)getDocument();
if (pos < 0 || pos > doc.getLength()) {
throw new BadLocationException("Invalid offset", pos); // NOI18N
}
ret.y = getYFromPos(pos);
try {
synchronized (modelToViewDG) {
modelToViewDG.r = ret; // set the current rectangle
Element lineElement = doc.getParagraphElement(pos);
int bolPos = lineElement.getStartOffset();
int eolPos = lineElement.getEndOffset() - 1;
DrawEngine.getDrawEngine().draw(modelToViewDG, editorUI,
bolPos, eolPos,
getBaseX(ret.y), ret.y, pos
);
modelToViewDG.r = null;
}
} catch (BadLocationException e) {
Utilities.annotateLoggable(e);
}
return ret;
}
示例11: handleDecreaseIndent
import javax.swing.text.Element; //導入方法依賴的package包/類
/**
* Actually does the "de-indentation." This method finds where the
* given element's leading whitespace ends, then, if there is indeed
* leading whitespace, removes either the last char in it (if it is a
* tab), or removes up to the number of spaces equal to a tab in the
* specified document (i.e., if the tab size was 5 and there were 3
* spaces at the end of the leading whitespace, the three will be
* removed; if there were 8 spaces, only the first 5 would be
* removed).
*
* @param elem The element to "de-indent."
* @param doc The document containing the specified element.
* @param tabSize The size of a tab, in spaces.
*/
private void handleDecreaseIndent(Element elem, Document doc,
int tabSize)
throws BadLocationException {
int start = elem.getStartOffset();
int end = elem.getEndOffset() - 1; // Why always true??
doc.getText(start,end-start, s);
int i = s.offset;
end = i+s.count;
if (end>i) {
// If the first character is a tab, remove it.
if (s.array[i]=='\t') {
doc.remove(start, 1);
}
// Otherwise, see if the first character is a space. If it
// is, remove all contiguous whitespaces at the beginning of
// this line, up to the tab size.
else if (s.array[i]==' ') {
i++;
int toRemove = 1;
while (i<end && s.array[i]==' ' && toRemove<tabSize) {
i++;
toRemove++;
}
doc.remove(start, toRemove);
}
}
}
示例12: getLeadingWhitespace
import javax.swing.text.Element; //導入方法依賴的package包/類
/**
* 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);
}
示例13: fetchPreviousNonEmptyRegion
import javax.swing.text.Element; //導入方法依賴的package包/類
private boolean fetchPreviousNonEmptyRegion() {
while (--modElementIndex >= 0) {
Element modElement = modRootElement.getElement(modElementIndex);
modElementStartOffset = modElement.getStartOffset();
modElementEndOffset = modElement.getEndOffset();
if (modElementStartOffset >= modElementEndOffset)// Empty region - continue
continue;
return true;
}
return false;
}
示例14: 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();
}
示例15: click
import javax.swing.text.Element; //導入方法依賴的package包/類
private boolean click(Point point)
{
if (!this.canActionEvent) return false;
point.x -= 0.5F;
int index = this.pane.viewToModel(point);
if (index == -1) return false;
StyledDocument doc = this.pane.getStyledDocument();
Element element = doc.getCharacterElement(index);
ActionLink action = ActionLink.getActionFromAttributes(element.getAttributes());
if (action != null)
{
int begin = element.getStartOffset();
int end = element.getEndOffset();
String string = null;
try
{
string = doc.getText(begin, end - begin);
}
catch (BadLocationException ex)
{
ex.printStackTrace();
}
if (string == null) return false;
ActionLinkEvent e = new ActionLinkEvent();
e.console = this;
e.action = action;
e.document = doc;
e.beginIndex = begin;
e.endIndex = end;
e.element = element;
this.actionEvents.offer(e);
}
else
{
return false;
}
return true;
}