当前位置: 首页>>代码示例>>Java>>正文


Java Element.getElementIndex方法代码示例

本文整理汇总了Java中javax.swing.text.Element.getElementIndex方法的典型用法代码示例。如果您正苦于以下问题:Java Element.getElementIndex方法的具体用法?Java Element.getElementIndex怎么用?Java Element.getElementIndex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.text.Element的用法示例。


在下文中一共展示了Element.getElementIndex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: insertUpdate

import javax.swing.text.Element; //导入方法依赖的package包/类
@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();
		}
	}
}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:19,代码来源:DefaultFoldManager.java

示例2: actionPerformed

import javax.swing.text.Element; //导入方法依赖的package包/类
/** The operation to perform when this action is triggered. */
public void actionPerformed(ActionEvent e) {
    JTextComponent target = getTextComponent(e);
    if (target != null) {
        Document doc = target.getDocument();
        Element map = doc.getDefaultRootElement();
        int offs = target.getCaretPosition();
        int lineIndex = map.getElementIndex(offs);
        int lineEnd = map.getElement(lineIndex).getEndOffset() - 1;

        if (select) {
            target.moveCaretPosition(lineEnd);
        } else {
            target.setCaretPosition(lineEnd);
        }
    }            
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:OutputEditorKit.java

示例3: actionPerformedImpl

import javax.swing.text.Element; //导入方法依赖的package包/类
@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();
	}

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:25,代码来源:RTextAreaEditorKit.java

示例4: 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;

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:22,代码来源:RSyntaxTextAreaEditorKit.java

示例5: 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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:32,代码来源:DocumentUtilities.java

示例6: reindent

import javax.swing.text.Element; //导入方法依赖的package包/类
public void reindent() throws BadLocationException {
    Document doc = context.document();
    int startOffset = context.startOffset();
    int endOffset = context.endOffset();
    
    pushFormattingContextDocument(doc);
    try {
        // Original formatter does not have reindentation of multiple lines
        // so reformat start line and continue for each line.
        Element lineRootElem = lineRootElement(doc);
        Position endPos = doc.createPosition(endOffset);
        do {
            startOffset = formatter.indentLine(doc, startOffset);
            int startLineIndex = lineRootElem.getElementIndex(startOffset) + 1;
            if (startLineIndex >= lineRootElem.getElementCount())
                break;
            Element lineElem = lineRootElem.getElement(startLineIndex);
            startOffset = lineElem.getStartOffset(); // Move to next line
        } while (startOffset < endPos.getOffset());
    } finally {
        popFormattingContextDocument(doc);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:LegacyFormattersProvider.java

示例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 "";
}
 
开发者ID:BlidiWajdi,项目名称:Mujeed-Arabic-Prolog,代码行数:12,代码来源:TextLineNumber.java

示例8: getAlreadyEnteredText

import javax.swing.text.Element; //导入方法依赖的package包/类
/**
 * Returns the text just before the current caret position that could be
 * the start of something auto-completable.<p>
 *
 * This method returns all characters before the caret that are matched
 * by  {@link #isValidChar(char)}.
 *
 * {@inheritDoc}
 */
@Override
public String getAlreadyEnteredText(JTextComponent comp) {
	
	Document doc = comp.getDocument();

	int dot = comp.getCaretPosition();
	Element root = doc.getDefaultRootElement();
	int index = root.getElementIndex(dot);
	Element elem = root.getElement(index);
	int start = elem.getStartOffset();
	int len = dot-start;
	try {
		doc.getText(start, len, seg);
	} catch (BadLocationException ble) {
		ble.printStackTrace();
		return EMPTY_STRING;
	}

	int segEnd = seg.offset + len;
	start = segEnd - 1;
	while (start>=seg.offset && isValidChar(seg.array[start])) {
		start--;
	}
	start++;

	len = segEnd - start;
	return len==0 ? EMPTY_STRING : new String(seg.array, start, len);

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:39,代码来源:DefaultCompletionProvider.java

示例9: replace

import javax.swing.text.Element; //导入方法依赖的package包/类
public void replace(DocumentFilter.FilterBypass fb, int offset, int length,String text, AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
   Element root = doc.getDefaultRootElement();
   int count = root.getElementCount();
   int index = root.getElementIndex(offset);
   Element cur = root.getElement(index);
   int promptPosition = cur.getStartOffset()+PROMPT.length();
   if(index==count-1 && offset-promptPosition>=0) {
     if(text.equals("\n")) {
  	   text = "\n"+PROMPT;
     }
     fb.replace(offset, length, text, attrs);
   }
}
 
开发者ID:BlidiWajdi,项目名称:Mujeed-Arabic-Prolog,代码行数:15,代码来源:NonEditableLineDocumentFilter.java

示例10: getCompletionsAt

import javax.swing.text.Element; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 */
@Override
public List<Completion> getCompletionsAt(JTextComponent tc, Point p) {

	int offset = tc.viewToModel(p);
	if (offset<0 || offset>=tc.getDocument().getLength()) {
		lastCompletionsAtText = null;
		return lastParameterizedCompletionsAt = null;
	}

	Segment s = new Segment();
	Document doc = tc.getDocument();
	Element root = doc.getDefaultRootElement();
	int line = root.getElementIndex(offset);
	Element elem = root.getElement(line);
	int start = elem.getStartOffset();
	int end = elem.getEndOffset() - 1;

	try {

		doc.getText(start, end-start, s);

		// Get the valid chars before the specified offset.
		int startOffs = s.offset + (offset-start) - 1;
		while (startOffs>=s.offset && isValidChar(s.array[startOffs])) {
			startOffs--;
		}

		// Get the valid chars at and after the specified offset.
		int endOffs = s.offset + (offset-start);
		while (endOffs<s.offset+s.count && isValidChar(s.array[endOffs])) {
			endOffs++;
		}

		int len = endOffs - startOffs - 1;
		if (len<=0) {
			return lastParameterizedCompletionsAt = null;
		}
		String text = new String(s.array, startOffs+1, len);

		if (text.equals(lastCompletionsAtText)) {
			return lastParameterizedCompletionsAt;
		}

		// Get a list of all Completions matching the text.
		List<Completion> list = getCompletionByInputText(text);
		lastCompletionsAtText = text;
		return lastParameterizedCompletionsAt = list;

	} catch (BadLocationException ble) {
		ble.printStackTrace(); // Never happens
	}

	lastCompletionsAtText = null;
	return lastParameterizedCompletionsAt = null;

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:60,代码来源:DefaultCompletionProvider.java

示例11: paintView

import javax.swing.text.Element; //导入方法依赖的package包/类
/**
 * Paints one view that corresponds to a line (or
 * multiple lines if folding takes effect).
 */
private void paintView(View view, Graphics g, int yBase) {
    JTextComponent component = editorUI.getComponent();
    if (component == null) return;
    BaseTextUI textUI = (BaseTextUI)component.getUI();

    Element rootElem = textUI.getRootView(component).getElement();
    int line = rootElem.getElementIndex(view.getStartOffset());

    String annotation = "";  // NOI18N
    AnnotateLine al = null;
    if (!elementAnnotations.isEmpty()) {
        al = getAnnotateLine(line);
        if (al != null) {
            annotation = getDisplayName(al);  // NOI18N
        }
    } else {
        annotation = elementAnnotationsSubstitute;
    }

    if (al != null && al.getRevision().equals(recentRevision)) {
        g.setColor(selectedColor());
    } else {
        g.setColor(foregroundColor());
    }
    int texty = yBase + editorUI.getLineAscent();
    int textx = 2;
    g.drawString(annotation, textx, texty);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:33,代码来源:AnnotationBar.java

示例12: 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);
}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:10,代码来源:RSyntaxTextAreaEditorKit.java

示例13: getLineCount

import javax.swing.text.Element; //导入方法依赖的package包/类
/** Count of rows between these two positions */
public static int getLineCount(@NonNull LineDocument doc, int startOffset, int endOffset) {
    if (startOffset > endOffset) {
        return 0;
    }
    Element lineRoot = doc.getParagraphElement(0).getParentElement();
    return lineRoot.getElementIndex(endOffset) - lineRoot.getElementIndex(startOffset) + 1;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:9,代码来源:LineDocumentUtils.java

示例14: discoverTagName

import javax.swing.text.Element; //导入方法依赖的package包/类
/**
 * Discovers the name of the tag being closed.  Assumes standard
 * SGML-style markup tags.
 *
 * @param doc The document to parse.
 * @param dot The location of the caret.  This should be right after
 *        the start of a closing tag token (e.g. "<code>&lt;/</code>"
 *        or "<code>[</code>" in the case of BBCode).
 * @return The name of the tag to close, or <code>null</code> if it
 *         could not be determined.
 */
private String discoverTagName(RSyntaxDocument doc, int dot) {

	Stack<String> stack = new Stack<String>();

	Element root = doc.getDefaultRootElement();
	int curLine = root.getElementIndex(dot);

	for (int i=0; i<=curLine; i++) {

		Token t = doc.getTokenListForLine(i);
		while (t!=null && t.isPaintable()) {

			if (t.getType()==Token.MARKUP_TAG_DELIMITER) {
				if (t.isSingleChar('<') || t.isSingleChar('[')) {
					t = t.getNextToken();
					while (t!=null && t.isPaintable()) {
						if (t.getType()==Token.MARKUP_TAG_NAME ||
								// Being lenient here and also checking
								// for attributes, in case they
								// (incorrectly) have whitespace between
								// the '<' char and the element name.
								t.getType()==Token.MARKUP_TAG_ATTRIBUTE) {
							stack.push(t.getLexeme());
							break;
						}
						t = t.getNextToken();
					}
				}
				else if (t.length()==2 && t.charAt(0)=='/' &&
						(t.charAt(1)=='>' ||
								t.charAt(1)==']')) {
					if (!stack.isEmpty()) { // Always true for valid XML
						stack.pop();
					}
				}
				else if (t.length()==2 &&
						(t.charAt(0)=='<' || t.charAt(0)=='[') &&
						t.charAt(1)=='/') {
					String tagName = null;
					if (!stack.isEmpty()) { // Always true for valid XML
						tagName = stack.pop();
					}
					if (t.getEndOffset()>=dot) {
						return tagName;
					}
				}
			}

			t = t==null ? null : t.getNextToken();

		}

	}

	return null; // Should never happen

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:69,代码来源:RSyntaxTextAreaEditorKit.java

示例15: setCollapsed

import javax.swing.text.Element; //导入方法依赖的package包/类
/**
 * Sets whether this <code>Fold</code> is collapsed.  Calling this method
 * will update both the text area and all <code>Gutter</code> components.
 *
 * @param collapsed Whether this fold should be collapsed.
 * @see #isCollapsed()
 * @see #toggleCollapsedState()
 */
public void setCollapsed(boolean collapsed) {

	if (collapsed!=this.collapsed) {

		// Change our fold state and cached info about folded line count.
		int lineCount = getLineCount();
		int linesToCollapse = lineCount - childCollapsedLineCount;
		if (!collapsed) { // If we're expanding
			linesToCollapse = -linesToCollapse;
		}
		//System.out.println("Hiding lines: " + linesToCollapse +
		//		" (" + lineCount + ", " + linesToCollapse + ")");
		this.collapsed = collapsed;
		if (parent!=null) {
			parent.updateChildCollapsedLineCount(linesToCollapse);
		}

		// If an end point of the selection is being hidden, move the caret
		// "out" of the fold.
		if (collapsed) {
			int dot = textArea.getSelectionStart(); // Forgive variable name
			Element root = textArea.getDocument().getDefaultRootElement();
			int dotLine = root.getElementIndex(dot);
			boolean updateCaret = containsLine(dotLine);
			if (!updateCaret) {
				int mark = textArea.getSelectionEnd();
				if (mark!=dot) {
					int markLine = root.getElementIndex(mark);
					updateCaret = containsLine(markLine);
				}
			}
			if (updateCaret) {
				dot = root.getElement(getStartLine()).getEndOffset() - 1;
				textArea.setCaretPosition(dot);
			}
		}

		textArea.foldToggled(this);

	}

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:51,代码来源:Fold.java


注:本文中的javax.swing.text.Element.getElementIndex方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。