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


Java Document.getDefaultRootElement方法代码示例

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


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

示例1: getLine

import javax.swing.text.Document; //导入方法依赖的package包/类
/** Get the line object from the given position.
 * @param doc document for which the line is being retrieved
 * @param offset position in the document
 * @param original whether to retrieve the original line (true) before
 *   the modifications were done or the current line (false)
 * @return the line object
 */
public static Line getLine(Document doc, int offset, boolean original) {
    DataObject dob = getDataObject(doc);
    if (dob != null) {
        LineCookie lc = (LineCookie)dob.getCookie(LineCookie.class);
        if (lc != null) {
            Line.Set lineSet = lc.getLineSet();
            if (lineSet != null) {
                Element lineRoot = (doc instanceof AbstractDocument)
                    ? ((AbstractDocument)doc).getParagraphElement(0).getParentElement()
                    : doc.getDefaultRootElement();
                int lineIndex = lineRoot.getElementIndex(offset);
                return original
                       ? lineSet.getOriginal(lineIndex)
                       : lineSet.getCurrent(lineIndex);
            }
        }
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:NbEditorUtilities.java

示例2: moveLineUp

import javax.swing.text.Document; //导入方法依赖的package包/类
private void moveLineUp(RTextArea textArea, int line)
							throws BadLocationException {
	Document doc = textArea.getDocument();
	Element root = doc.getDefaultRootElement();
	int lineCount = root.getElementCount();
	Element elem = root.getElement(line);
	int start = elem.getStartOffset();
	int end = line==lineCount-1 ? elem.getEndOffset()-1 :
							elem.getEndOffset();
	int caret = textArea.getCaretPosition();
	int caretOffset = caret - start;
	String text = doc.getText(start, end-start);
	if (line==lineCount-1) {
		start--; // Remove previous line's ending \n
	}
	doc.remove(start, end-start);
	Element elem2 = root.getElement(line-1);
	int start2 = elem2.getStartOffset();
	//int end2 = elem2.getEndOffset();
	if (line==lineCount-1) {
		text += '\n';
	}
	doc.insertString(start2, text, null);
	//caretOffset = Math.min(start2+caretOffset, end2-1);
	textArea.setCaretPosition(start2+caretOffset);
}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:27,代码来源:RTextAreaEditorKit.java

示例3: moveLineDown

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

示例4: actionPerformed

import javax.swing.text.Document; //导入方法依赖的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 lineStart = map.getElement(lineIndex).getStartOffset();

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

示例5: getToolTip

import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
public JComponent getToolTip(double x, double y, Shape allocation) {
    Container container = getContainer();
    if (container instanceof JEditorPane) {
        JEditorPane editorPane = (JEditorPane) getContainer();
        JEditorPane tooltipPane = new JEditorPane();
        EditorKit kit = editorPane.getEditorKit();
        Document doc = getDocument();
        if (kit != null && doc != null) {
            Element lineRootElement = doc.getDefaultRootElement();
            tooltipPane.putClientProperty(FoldViewFactory.DISPLAY_ALL_FOLDS_EXPANDED_PROPERTY, true);
            try {
                // Start-offset of the fold => line start => position
                int lineIndex = lineRootElement.getElementIndex(fold.getStartOffset());
                Position pos = doc.createPosition(
                        lineRootElement.getElement(lineIndex).getStartOffset());
                // DocumentView.START_POSITION_PROPERTY
                tooltipPane.putClientProperty("document-view-start-position", pos); // NOI18N
                // End-offset of the fold => line end => position
                lineIndex = lineRootElement.getElementIndex(fold.getEndOffset());
                pos = doc.createPosition(lineRootElement.getElement(lineIndex).getEndOffset());
                // DocumentView.END_POSITION_PROPERTY
                tooltipPane.putClientProperty("document-view-end-position", pos); // NOI18N
                tooltipPane.putClientProperty("document-view-accurate-span", true); // NOI18N
                // Set the same kit and document
                tooltipPane.setEditorKit(kit);
                tooltipPane.setDocument(doc);
                tooltipPane.setEditable(false);
                return new FoldToolTip(editorPane, tooltipPane, getBorderColor());
            } catch (BadLocationException e) {
                // => return null
            }
        }
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:37,代码来源:FoldView.java

示例6: getElement

import javax.swing.text.Document; //导入方法依赖的package包/类
public Element getElement() {
    if (view != null) {
        return view.getElement();
    }
    Document doc = getDocument();
    return (doc != null) ? doc.getDefaultRootElement() : null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:8,代码来源:LockView.java

示例7: addParserHighlight

import javax.swing.text.Document; //导入方法依赖的package包/类
/**
 * Adds a highlight from a parser.
 *
 * @param notice The notice from a {@link Parser}.
 * @return A tag with which to reference the highlight.
 * @throws BadLocationException
 * @see #clearParserHighlights()
 * @see #clearParserHighlights(Parser)
 */
HighlightInfo addParserHighlight(ParserNotice notice, HighlightPainter p)
							throws BadLocationException {

	Document doc = textArea.getDocument();
	TextUI mapper = textArea.getUI();

	int start = notice.getOffset();
	int end = 0;
	if (start==-1) { // Could just define an invalid line number
		int line = notice.getLine();
		Element root = doc.getDefaultRootElement();
		if (line>=0 && line<root.getElementCount()) {
			Element elem = root.getElement(line);
			start = elem.getStartOffset();
			end = elem.getEndOffset();
		}
	}
	else {
		end = start + notice.getLength();
	}

	// Always layered highlights for parser highlights.
	SyntaxLayeredHighlightInfoImpl i = new SyntaxLayeredHighlightInfoImpl();
	i.setPainter(p);
	i.setStartOffset(doc.createPosition(start));
	// HACK: Use "end-1" to prevent chars the user types at the "end" of
	// the highlight to be absorbed into the highlight (default Highlight
	// behavior).
	i.setEndOffset(doc.createPosition(end-1));
	i.notice = notice;//i.color = notice.getColor();

	parserHighlights.add(i);
	mapper.damageRange(textArea, start, end);
	return i;

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

示例8: performGoto

import javax.swing.text.Document; //导入方法依赖的package包/类
/**
 * Perform the goto operation.
 *
 * @return whether the dialog should be made invisible or not
 */
private boolean performGoto() {
    JTextComponent c = getTargetComponent();
    String text = (String) gotoCombo.getEditor().getItem();
    if (c != null) {
        try {
            int lineNumber = Integer.parseInt(text);
            if (lineNumber == 0) { // Works in vim to jump to begining
                lineNumber = 1;
            }
            int lineIndex = lineNumber - 1;
            Document doc = c.getDocument();
            Element rootElem = doc.getDefaultRootElement();
            int lineCount = rootElem.getElementCount();
            if (lineIndex >= 0) {
                if (lineIndex >= lineCount) {
                    lineIndex = lineCount - 1;
                }
                int offset = rootElem.getElement(lineIndex).getStartOffset();
                c.setCaretPosition(offset);
                return true;
            } // else: lineIndex < 0 => beep and return false
        } catch (NumberFormatException e) {
            // Contains letters or other chars -> attempt bookmarks
            BookmarkManager lockedBookmarkManager = BookmarkManager.getLocked();
            try {
                BookmarkInfo bookmark = lockedBookmarkManager.findBookmarkByNameOrKey(text, false);
                if (bookmark != null) {
                    BookmarkUtils.postOpenEditor(bookmark);
                    return true;
                } // else: unknown bookmark => beep
            } finally {
                lockedBookmarkManager.unlock();
            }
        }
    }
    c.getToolkit().beep();
    return false;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:44,代码来源:GotoLineOrBookmarkPanel.java

示例9: noticeContainsPosition

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

示例10: getParagraphElement

import javax.swing.text.Document; //导入方法依赖的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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:DocumentUtilities.java

示例11: extendRectangularSelection

import javax.swing.text.Document; //导入方法依赖的package包/类
/**
 * Extend rectangular selection either by char in a specified selection
 * or by word (if ctrl is pressed).
 *
 * @param toRight true for right or false for left.
 * @param ctrl 
 */
public void extendRectangularSelection(boolean toRight, boolean ctrl) {
    JTextComponent c = component;
    Document doc = c.getDocument();
    int dotOffset = getDot();
    Element lineRoot = doc.getDefaultRootElement();
    int lineIndex = lineRoot.getElementIndex(dotOffset);
    Element lineElement = lineRoot.getElement(lineIndex);
    float charWidth;
    LockedViewHierarchy lvh = ViewHierarchy.get(c).lock();
    try {
        charWidth = lvh.getDefaultCharWidth();
    } finally {
        lvh.unlock();
    }
    int newDotOffset = -1;
    try {
        int newlineOffset = lineElement.getEndOffset() - 1;
        Rectangle newlineRect = c.modelToView(newlineOffset);
        if (!ctrl) {
            if (toRight) {
                if (rsDotRect.x < newlineRect.x) {
                    newDotOffset = dotOffset + 1;
                } else {
                    rsDotRect.x += charWidth;
                }
            } else { // toLeft
                if (rsDotRect.x > newlineRect.x) {
                    rsDotRect.x -= charWidth;
                    if (rsDotRect.x < newlineRect.x) { // Fix on rsDotRect
                        newDotOffset = newlineOffset;
                    }
                } else {
                    newDotOffset = Math.max(dotOffset - 1, lineElement.getStartOffset());
                }
            }

        } else { // With Ctrl
            int numVirtualChars = 8; // Number of virtual characters per one Ctrl+Shift+Arrow press
            if (toRight) {
                if (rsDotRect.x < newlineRect.x) {
                    newDotOffset = Math.min(Utilities.getNextWord(c, dotOffset), lineElement.getEndOffset() - 1);
                } else { // Extend virtually
                    rsDotRect.x += numVirtualChars * charWidth;
                }
            } else { // toLeft
                if (rsDotRect.x > newlineRect.x) { // Virtually extended
                    rsDotRect.x -= numVirtualChars * charWidth;
                    if (rsDotRect.x < newlineRect.x) {
                        newDotOffset = newlineOffset;
                    }
                } else {
                    newDotOffset = Math.max(Utilities.getPreviousWord(c, dotOffset), lineElement.getStartOffset());
                }
            }
        }

        if (newDotOffset != -1) {
            rsDotRect = c.modelToView(newDotOffset);
            moveDot(newDotOffset); // updates rs and fires state change
        } else {
            updateRectangularSelectionPaintRect();
            fireStateChanged();
        }
    } catch (BadLocationException ex) {
        // Leave selection as is
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:75,代码来源:BaseCaret.java

示例12: noticeContainsPointInView

import javax.swing.text.Document; //导入方法依赖的package包/类
/**
 * Since <code>viewToModel()</code> returns the <em>closest</em> model
 * position, and the position doesn't <em>necessarily</em> contain the
 * point passed in as an argument, this method checks whether the point is
 * indeed contained in the view rectangle for the specified offset.
 *
 * @param notice The parser notice.
 * @param p The point possibly contained in the view range of the
 *        parser notice.
 * @return Whether the parser notice actually contains the specified point
 *         in the view.
 */
private boolean noticeContainsPointInView(ParserNotice notice,
		Point p) {

	try {

		int start, end;
		if (notice.getKnowsOffsetAndLength()) {
			start = notice.getOffset();
			end = start + notice.getLength() - 1;
		}
		else {
			Document doc = textArea.getDocument();
			Element root = doc.getDefaultRootElement();
			int line = notice.getLine();
			// Defend against possible bad user-defined notices.
			if (line<0) {
				return false;
			}
			Element elem = root.getElement(line);
			start = elem.getStartOffset();
			end = elem.getEndOffset() - 1;
		}

		Rectangle r1 = textArea.modelToView(start);
		Rectangle r2 = textArea.modelToView(end);
		if (r1.y!=r2.y) {
			// If the notice spans multiple lines, give them the benefit
			// of the doubt.  This is only "wrong" if the user is in empty
			// space "to the right" of the error marker when it ends at the
			// end of a line anyway.
			return true;
		}

		r1.y--; // Be a tiny bit lenient.
		r1.height += 2; // Ditto
		return p.x>=r1.x && p.x<(r2.x+r2.width) &&
				p.y>=r1.y && p.y<(r1.y+r1.height);

	} catch (BadLocationException ble) { // Never occurs
		// Give them the benefit of the doubt, should 99% of the time be
		// true anyway
		return true;
	}

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

示例13: actionPerformedImpl

import javax.swing.text.Document; //导入方法依赖的package包/类
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {

	int newPos = 0;

	try {

		// Is line wrap enabled?
		if (textArea.getLineWrap()) {
			int offs = textArea.getCaretPosition();
			// TODO:  Replace Utilities call with custom version
			// to cut down on all of the modelToViews, as each call
			// causes TokenList => expensive!
			int begOffs = Utilities.getRowStart(textArea, offs);
			// TODO: line wrap doesn't currently toggle between
			// the first non-whitespace char and the actual start
			// of the line line the no-line-wrap version does.
			newPos = begOffs;
		}

		// No line wrap - optimized for performance!
		else {

			// We use the elements instead of calling
			// getLineOfOffset(), etc. to speed things up just a
			// tad (i.e. micro-optimize).
			int caretPosition = textArea.getCaretPosition();
			Document document = textArea.getDocument();
			Element map = document.getDefaultRootElement();
			int currentLineNum = map.getElementIndex(caretPosition);
			Element currentLineElement = map.getElement(currentLineNum);
			int currentLineStart = currentLineElement.getStartOffset();
			int currentLineEnd = currentLineElement.getEndOffset();
			int count = currentLineEnd - currentLineStart;
			if (count>0) { // If there are chars in the line...
				document.getText(currentLineStart, count, currentLine);
				int firstNonWhitespace = getFirstNonWhitespacePos();
				firstNonWhitespace = currentLineStart +
						(firstNonWhitespace - currentLine.offset);
				if (caretPosition!=firstNonWhitespace) {
					newPos = firstNonWhitespace;
				}
				else {
					newPos = currentLineStart;
				}
			}
			else { // Empty line (at end of the document only).
				newPos = currentLineStart;
			}

		}

		if (select) {
			textArea.moveCaretPosition(newPos);
		}
		else {
			textArea.setCaretPosition(newPos);
		}
		//e.consume();

	} catch (BadLocationException ble) {
		/* Shouldn't ever happen. */
		UIManager.getLookAndFeel().provideErrorFeedback(textArea);
		ble.printStackTrace();
	}

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

示例14: lineRootElement

import javax.swing.text.Document; //导入方法依赖的package包/类
public static Element lineRootElement(Document doc) {
    return (doc instanceof StyledDocument)
        ? ((StyledDocument)doc).getParagraphElement(0).getParentElement()
        : doc.getDefaultRootElement();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:6,代码来源:IndentImpl.java

示例15: forPane

import javax.swing.text.Document; //导入方法依赖的package包/类
static String forPane(JEditorPane p) {
    if (p == null) return null;
 
    String selection = p.getSelectedText ();
 
    if ( selection != null && selection.length() > 0 ) {
        return selection;
    } else {
 
        // try to guess which word is underneath the caret's dot.
 
        Document doc = p.getDocument();
        Element lineRoot;
 
        if (doc instanceof StyledDocument) {
            lineRoot = NbDocument.findLineRootElement((StyledDocument)doc);
        } else {
            lineRoot = doc.getDefaultRootElement();
        }
        int dot = p.getCaret().getDot();
        Element line = lineRoot.getElement(lineRoot.getElementIndex(dot));
 
        if (line == null) return null;
 
        String text = null;
        try {
            text = doc.getText(line.getStartOffset(),
                line.getEndOffset() - line.getStartOffset());
        } catch (BadLocationException e) {
            return null;
        }
        
        if ( text == null )
            return null;
        int pos = dot - line.getStartOffset();

        if ( pos < 0 || pos >= text.length() )
            return null;

        int bix, eix;

        for( bix = Character.isJavaIdentifierPart( text.charAt( pos ) ) ? pos : pos - 1;
                bix >= 0 && Character.isJavaIdentifierPart( text.charAt( bix ) ); bix-- );
        for( eix = pos; eix < text.length() && Character.isJavaIdentifierPart( text.charAt( eix )); eix++ );

        return bix == eix ? null : text.substring( bix + 1, eix  );
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:49,代码来源:GetJavaWord.java


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