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


Java StyledDocument.getText方法代码示例

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


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

示例1: findLineOffset

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/** return the offset of the first non-whitespace character on the line,
           or -1 when the line does not exist
 */
private static int findLineOffset(StyledDocument doc, int lineNumber) {
    int offset;
    try {
        offset = NbDocument.findLineOffset (doc, lineNumber - 1);
        int offset2 = NbDocument.findLineOffset (doc, lineNumber);
        try {
            String lineStr = doc.getText(offset, offset2 - offset);
            for (int i = 0; i < lineStr.length(); i++) {
                if (!Character.isWhitespace(lineStr.charAt(i))) {
                    offset += i;
                    break;
                }
            }
        } catch (BadLocationException ex) {
            // ignore
        }
    } catch (IndexOutOfBoundsException ioobex) {
        return -1;
    }
    return offset;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:EditorContextImpl.java

示例2: insideAttribute

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/**
 * Checks if caret position is inside attributes quotes
 *
 * @param doc edited document
 * @param caretOffset current caret location offset
 * @return true if caret inside attribute
 */
static boolean insideAttribute(StyledDocument doc, int caretOffset) {
    boolean insideQuotes = false;
    while (caretOffset > 0) {
        try {
            String chars = doc.getText(caretOffset - 1, 1);
            if (chars.equals("<") || chars.equals("\"") && insideQuotes) {
                return false;
            } else if (chars.equals("\"") && (doc.getText(caretOffset - 2, 1).equals("="))) {
                return true;
            } else if (chars.equals("\"")) {
                insideQuotes = true;
            }
            caretOffset--;
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    return false;
}
 
开发者ID:ArturWisniewski,项目名称:NB-Thymeleaf-Code-Completion,代码行数:27,代码来源:CompletionUtils.java

示例3: register

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
static void register(final JTextPane pane) {
    final StyledDocument doc = pane.getStyledDocument();
    String text = "";
    try {
        text = doc.getText(0, doc.getLength());
    } catch (BadLocationException ex) {
        Support.LOG.log(Level.SEVERE, null, ex);
    }
    final int[] boundaries = findBoundaries(text);
    if ((boundaries != null) && (boundaries.length != 0)) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Style defStyle = StyleContext.getDefaultStyleContext()
                                 .getStyle(StyleContext.DEFAULT_STYLE);
                final Style hlStyle = doc.addStyle("regularBlue-url", defStyle);      //NOI18N
                hlStyle.addAttribute(HyperlinkSupport.URL_ATTRIBUTE, new UrlAction());
                StyleConstants.setForeground(hlStyle, UIUtils.getLinkColor());
                StyleConstants.setUnderline(hlStyle, true);
                for (int i = 0; i < boundaries.length; i+=2) {
                    doc.setCharacterAttributes(boundaries[i], boundaries[i + 1] - boundaries[i], hlStyle, true);
                }
                pane.removeMouseListener(getUrlMouseListener());
                pane.addMouseListener(getUrlMouseListener());
            }
        });
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:30,代码来源:WebUrlHyperlinkSupport.java

示例4: getText

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/** Finds the text contained in this range.
* @return the text
* @exception IOException if any I/O problem occurred during document loading (if that was necessary)
* @exception BadLocationException if the positions are out of the bounds of the document
*/
public String getText() throws BadLocationException, IOException {
    StyledDocument doc = begin.getCloneableEditorSupport().openDocument();
    int p1 = begin.getOffset();
    int p2 = end.getOffset();

    return doc.getText(p1, p2 - p1);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:13,代码来源:PositionBounds.java

示例5: testRefreshProblem46885

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public void testRefreshProblem46885 () throws Exception {
    StyledDocument doc = support.openDocument ();
    
    doc.insertString (0, "A text", null);
    support.saveDocument ();
    
    content = "New";
    propL.firePropertyChange (CloneableEditorSupport.Env.PROP_TIME, null, null);
    
    waitAWT ();
    Task reloadTask = support.reloadDocument();
    reloadTask.waitFinished();
    
    String s = doc.getText (0, doc.getLength ());
    assertEquals ("Text has been updated", content, s);
    

    long oldtime = System.currentTimeMillis ();
    Thread.sleep(300);
    err.info("Document modified");
    doc.insertString (0, "A text", null);
    err.info("Document about to save");
    support.saveDocument ();
    err.info("Document saved");
    s = doc.getText (0, doc.getLength ());

    err.info("Current content: " + s);
    content = "NOT TO be loaded";
    propL.firePropertyChange (CloneableEditorSupport.Env.PROP_TIME, null, new Date (oldtime));
    
    waitAWT ();
    
    String s1 = doc.getText (0, doc.getLength ());
    err.info("New content: " + s1);
    assertEquals ("Text has not been updated", s, s1);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:37,代码来源:ReloadTest.java

示例6: getAsString

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
public static String getAsString(String file) {
    String result;
    try {
        FileObject testFile = Repository.getDefault().findResource(file);
        DataObject DO = DataObject.find(testFile);
        
        EditorCookie ec=(EditorCookie)(DO.getCookie(EditorCookie.class));
        StyledDocument doc=ec.openDocument();
        result=doc.getText(0, doc.getLength());
        //            result=Common.unify(result);
    } catch (Exception e){
        throw new AssertionFailedErrorException(e);
    }
    return result;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:Utilities.java

示例7: dataObjectToString

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/** Converts DataObject to String.
 */
public static String dataObjectToString(DataObject dataObject) throws IOException, BadLocationException {
    EditorCookie editorCookie = (EditorCookie) dataObject.getCookie(EditorCookie.class);
    
    if (editorCookie != null) {
        StyledDocument document = editorCookie.openDocument();
        if (document != null) {
            return  document.getText(0, document.getLength());
        }
    }
    return null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:14,代码来源:AbstractTestUtil.java

示例8: getText

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/** Finds the text contained in this range.
* @return the text
* @exception BadLocationException if the positions are out of the bounds of the document
*/
public String getText() throws BadLocationException {
    StyledDocument doc = this.guards.getDocument();
    int p1 = begin.getOffset();
    int p2 = end.getOffset();
    // #148542 - hotfix for negative length when p2 > p1 => return ""
    return (p1 <= p2) ? doc.getText(p1, p2 - p1) : "";
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:12,代码来源:PositionBounds.java

示例9: BooleanExpANTLRHandler

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
/**
 * Constructor
 * @param styledDocument the StyledDocument instance to analyse
 */
public BooleanExpANTLRHandler(StyledDocument styledDocument) {
    try {
        this.styledDocument = styledDocument;
        lexer = new FormalPropertyDescriptionLexer(new ANTLRInputStream(
                styledDocument.getText(0, styledDocument.getLength())));
        CommonTokenStream ts = new CommonTokenStream(lexer);
        parser = new FormalPropertyDescriptionParser(ts);
    } catch (BadLocationException ex) {
        Logger.getLogger(BooleanExpANTLRHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
开发者ID:Skypr,项目名称:BEAST,代码行数:16,代码来源:BooleanExpANTLRHandler.java

示例10: getText

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
static String getText (Line l) throws Exception {
    EditorCookie ec = (EditorCookie) l.getDataObject ().
        getCookie (EditorCookie.class);
    StyledDocument doc = ec.openDocument ();
    if (doc == null) return "";
    int off = NbDocument.findLineOffset (doc, l.getLineNumber ());
    int len = NbDocument.findLineOffset (doc, l.getLineNumber () + 1) - 
        off - 1;
    return doc.getText (off, len);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:11,代码来源:ImportDebugger.java

示例11: onContentMouseReleasedEvent

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
protected void onContentMouseReleasedEvent(MouseEvent e)
{
	Point mousePoint = e.getPoint();
	mousePoint.x -= 0.5F;
	int index = this.textContent.viewToModel(mousePoint);

	if (index == -1) return;

	StyledDocument doc = this.getDocument();
	Element element = doc.getCharacterElement(index);

	LinkHandler handler = LinkHandler.getLinkHandler(element.getAttributes());

	if (handler != 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;

		handler.execute(this, doc, string, begin, end, element);
	}
}
 
开发者ID:andykuo1,项目名称:candlelight,代码行数:33,代码来源:Console.java

示例12: getDocumentText

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private String getDocumentText() {
    String text = "";
    try {
        StyledDocument doc = getDocument();
        if (doc != null) {
            text = doc.getText(doc.getStartPosition().getOffset(), doc.getLength());
        }
    } catch (BadLocationException e) {
        Logger.getLogger("global").log(Level.WARNING, null, e);
    }
    return text;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:13,代码来源:HtmlEditorSupport.java

示例13: actionPerformed

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {
    if (ignoreComboAction)
        return; // not invoked by user, ignore

    GuardedBlock gBlock = codeData.getGuardedBlock(category, blockIndex);
    GuardBlockInfo gInfo = getGuardInfos(category)[blockIndex];
    int[] blockBounds = getGuardBlockBounds(category, blockIndex);
    int startOffset = blockBounds[0];
    int endOffset = blockBounds[1];
    int gHead = gBlock.getHeaderLength();
    int gFoot = gBlock.getFooterLength();
    JTextComponent editor = getEditor(category);
    StyledDocument doc = (StyledDocument) editor.getDocument();

    changed = true;

    JComboBox combo = (JComboBox) e.getSource();
    try {
        docListener.setActive(false);
        if (combo.getSelectedIndex() == 1) { // changing from default to custom
            NbDocument.unmarkGuarded(doc, startOffset, endOffset - startOffset);
            // keep last '\n' so we don't destroy next editable block's position
            doc.remove(startOffset, endOffset - startOffset - 1);
            // insert the custom code into the document
            String customCode = gBlock.getCustomCode();
            int customLength = customCode.length();
            if (gInfo.customizedCode != null) { // already was edited before
                customCode = customCode.substring(0, gHead)
                             + gInfo.customizedCode
                             + customCode.substring(customLength - gFoot);
                customLength = customCode.length();
            }
            if (customCode.endsWith("\n")) // NOI18N
                customCode = customCode.substring(0, customLength-1);
            doc.insertString(startOffset, customCode, null);
            gInfo.customized = true;
            // make guarded "header" and "footer", select the text in between
            NbDocument.markGuarded(doc, startOffset, gHead);
            NbDocument.markGuarded(doc, startOffset + customLength - gFoot, gFoot);
            editor.setSelectionStart(startOffset + gHead);
            editor.setSelectionEnd(startOffset + customLength - gFoot);
            editor.requestFocus();
            combo.setToolTipText(gBlock.getCustomEntry().getToolTipText());
        }
        else { // changing from custom to default
            // remember the customized code
            gInfo.customizedCode = doc.getText(startOffset + gHead,
                                               endOffset - gFoot - gHead - startOffset);
            NbDocument.unmarkGuarded(doc, endOffset - gFoot, gFoot);
            NbDocument.unmarkGuarded(doc, startOffset, gHead);
            // keep last '\n' so we don't destroy next editable block's position
            doc.remove(startOffset, endOffset - startOffset - 1);
            String defaultCode = gBlock.getDefaultCode();
            if (defaultCode.endsWith("\n")) // NOI18N
                defaultCode = defaultCode.substring(0, defaultCode.length()-1);
            doc.insertString(startOffset, defaultCode, null);
            gInfo.customized = false;
            // make the whole text guarded, cancel selection
            NbDocument.markGuarded(doc, startOffset, defaultCode.length()+1); // including '\n'
            if (editor.getSelectionStart() >= startOffset && editor.getSelectionEnd() <= endOffset)
                editor.setCaretPosition(startOffset);
            combo.setToolTipText(NbBundle.getMessage(CustomCodeData.class, "CTL_GuardCombo_Default_Hint")); // NOI18N
        }
        // we must create a new Position - current was moved away by inserting new string on it
        gInfo.position = NbDocument.createPosition(doc, startOffset, Position.Bias.Forward);

        docListener.setActive(true);
    }
    catch (BadLocationException ex) { // should not happen
        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:74,代码来源:CustomCodeView.java

示例14: getSelectedIdentifier

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private static String getSelectedIdentifier (
    StyledDocument doc,
    JEditorPane ep,
    int offset
) {
    String t = null;
    if (ep.getSelectionStart () <= offset && offset <= ep.getSelectionEnd ()) {
        t = ep.getSelectedText ();
    }
    if (t != null) {
        return t;
    }

    int line = NbDocument.findLineNumber (
        doc,
        offset
    );
    int col = NbDocument.findLineColumn (
        doc,
        offset
    );
    try {
        javax.swing.text.Element lineElem =
            org.openide.text.NbDocument.findLineRootElement (doc).
            getElement (line);

        if (lineElem == null) {
            return null;
        }
        int lineStartOffset = lineElem.getStartOffset ();
        int lineLen = lineElem.getEndOffset() - lineStartOffset;
        t = doc.getText (lineStartOffset, lineLen);
        int identStart = col;
        while (identStart > 0 &&
            (Character.isJavaIdentifierPart (
                t.charAt (identStart - 1)
            ) ||
            (t.charAt (identStart - 1) == '.'))) {
            identStart--;
        }
        int identEnd = col;
        while (identEnd < lineLen &&
               Character.isJavaIdentifierPart(t.charAt(identEnd))
        ) {
            identEnd++;
        }

        if (identStart == identEnd) {
            return null;
        }
        return t.substring (identStart, identEnd);
    } catch (javax.swing.text.BadLocationException e) {
        return null;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:56,代码来源:WatchPanel.java

示例15: doOpen

import javax.swing.text.StyledDocument; //导入方法依赖的package包/类
private static boolean doOpen(FileObject fo, int offset, String search) {
    try {
        DataObject od = DataObject.find(fo);
        EditorCookie ec = od.getCookie(EditorCookie.class);
        LineCookie lc = od.getCookie(LineCookie.class);

        // If the caller hasn't specified an offset, and the document is
        // already open, don't jump to a particular line!
        if (ec != null && offset == -1 && ec.getDocument() != null && search == null) {
            ec.open();
            return true;
        }

        // Simple text search if no known offset (e.g. broken/unparseable source)
        if ((search != null) && (offset == -1)) {
            StyledDocument doc = NbDocument.getDocument(od);

            try {
                String text = doc.getText(0, doc.getLength());
                int caretDelta = search.indexOf('^');
                if (caretDelta != -1) {
                    search = search.substring(0, caretDelta) + search.substring(caretDelta+1);
                } else {
                    caretDelta = 0;
                }
                offset = text.indexOf(search);
                if (offset != -1) {
                    offset += caretDelta;
                }
            } catch (BadLocationException ble) {
                LOG.log(Level.WARNING, null, ble);
            }
        }
        
        return NbDocument.openDocument(od, offset, Line.ShowOpenType.OPEN, Line.ShowVisibilityType.FOCUS);
    } catch (IOException e) {
        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
    }

    return false;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:42,代码来源:GsfUtilities.java


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