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


Java JEditorPane.setSelectionStart方法代码示例

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


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

示例1: insertChar

import javax.swing.JEditorPane; //导入方法依赖的package包/类
protected void insertChar(String original, char insertText, String expected, String selection, boolean codeTemplateMode) throws Exception {
    String source = original;
    String reformatted = expected;
    Formatter formatter = getFormatter(null);

    int sourcePos = source.indexOf('^');
    assertNotNull(sourcePos);
    source = source.substring(0, sourcePos) + source.substring(sourcePos+1);

    int reformattedPos = reformatted.indexOf('^');
    assertNotNull(reformattedPos);
    reformatted = reformatted.substring(0, reformattedPos) + reformatted.substring(reformattedPos+1);

    JEditorPane ta = getPane(source);
    Caret caret = ta.getCaret();
    caret.setDot(sourcePos);
    if (selection != null) {
        int start = original.indexOf(selection);
        assertTrue(start != -1);
        assertTrue("Ambiguous selection - multiple occurrences of selection string",
                original.indexOf(selection, start+1) == -1);
        ta.setSelectionStart(start);
        ta.setSelectionEnd(start+selection.length());
        assertEquals(selection, ta.getSelectedText());
    }

    BaseDocument doc = (BaseDocument) ta.getDocument();

    if (codeTemplateMode) {
        // Copied from editor/codetemplates/src/org/netbeans/lib/editor/codetemplates/CodeTemplateInsertHandler.java
        String EDITING_TEMPLATE_DOC_PROPERTY = "processing-code-template"; // NOI18N
        doc.putProperty(EDITING_TEMPLATE_DOC_PROPERTY, Boolean.TRUE);
    }

    if (formatter != null) {
        configureIndenters(doc, formatter, true);
    }

    setupDocumentIndentation(doc, null);

    runKitAction(ta, DefaultEditorKit.defaultKeyTypedAction, ""+insertText);

    String formatted = doc.getText(0, doc.getLength());
    assertEquals(reformatted, formatted);

    if (reformattedPos != -1) {
        assertEquals(reformattedPos, caret.getDot());
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:50,代码来源:CslTestBase.java

示例2: getPane

import javax.swing.JEditorPane; //导入方法依赖的package包/类
protected JEditorPane getPane(String text) throws Exception {
    if (!SwingUtilities.isEventDispatchThread()) {
        fail("You must run this test from the event dispatch thread! To do that, add @Override protected boolean runInEQ() { return true } from your testcase!");
    }
    String BEGIN = "$start$"; // NOI18N
    String END = "$end$"; // NOI18N
    int sourceStartPos = text.indexOf(BEGIN);
    int caretPos = -1;
    int sourceEndPos = -1;
    if (sourceStartPos != -1) {
        text = text.substring(0, sourceStartPos) + text.substring(sourceStartPos+BEGIN.length());
        sourceEndPos = text.indexOf(END);
        assertTrue(sourceEndPos != -1);
        text = text.substring(0, sourceEndPos) + text.substring(sourceEndPos+END.length());
    } else {
        caretPos = text.indexOf('^');
        if (caretPos != -1) {
            text = text.substring(0, caretPos) + text.substring(caretPos+1);
        }
    }

    JEditorPane pane = new JEditorPane();
    pane.setContentType(getPreferredMimeType());
    final NbEditorKit kit = ((NbEditorKit)getEditorKit(getPreferredMimeType()));


    Thread preload = new Thread(new Runnable() {

        @Override
        public void run() {
            // Preload actions and other stuff
            if (kit instanceof Callable) {
                try {
                    ((Callable) kit).call();
                } catch (Exception ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
            kit.getActions();
        }
    });
    preload.start();
    preload.join();
    pane.setEditorKit(kit);
    pane.setText(text);

    BaseDocument bdoc = (BaseDocument)pane.getDocument();

    bdoc.putProperty(org.netbeans.api.lexer.Language.class, getPreferredLanguage().getLexerLanguage());
    bdoc.putProperty("mimeType", getPreferredMimeType());

    //bdoc.insertString(0, text, null);
    if (sourceStartPos != -1) {
        assertTrue(sourceEndPos != -1);
        pane.setSelectionStart(sourceStartPos);
        pane.setSelectionEnd(sourceEndPos);
    } else if (caretPos != -1) {
        pane.getCaret().setDot(caretPos);
    }
    pane.getCaret().setSelectionVisible(true);

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


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