javax.swing.text.View#getViewIndex ( )源码实例Demo

下面列出了javax.swing.text.View#getViewIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: netbeans   文件: ActionFactory.java
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    AbstractDocument adoc = (AbstractDocument)target.getDocument();

    // Dump fold hierarchy
    FoldHierarchy hierarchy = FoldHierarchy.get(target);
    adoc.readLock();
    try {
        hierarchy.lock();
        try {
            /*DEBUG*/System.err.println("FOLD HIERARCHY DUMP:\n" + hierarchy); // NOI18N
            TokenHierarchy<?> th = TokenHierarchy.get(adoc);
            /*DEBUG*/System.err.println("TOKEN HIERARCHY DUMP:\n" + (th != null ? th : "<NULL-TH>")); // NOI18N

        } finally {
            hierarchy.unlock();
        }
    } finally {
        adoc.readUnlock();
    }

    View rootView = null;
    TextUI textUI = target.getUI();
    if (textUI != null) {
        rootView = textUI.getRootView(target); // Root view impl in BasicTextUI
        if (rootView != null && rootView.getViewCount() == 1) {
            rootView = rootView.getView(0); // Get DocumentView
        }
    }
    if (rootView != null) {
        String rootViewDump = (rootView instanceof DocumentView)
                ? ((DocumentView)rootView).toStringDetail()
                : rootView.toString();
        /*DEBUG*/System.err.println("DOCUMENT VIEW: " + System.identityHashCode(rootView) + // NOI18N
                "\n" + rootViewDump); // NOI18N
        int caretOffset = target.getCaretPosition();
        int caretViewIndex = rootView.getViewIndex(caretOffset, Position.Bias.Forward);
        /*DEBUG*/System.err.println("caretOffset=" + caretOffset + ", caretViewIndex=" + caretViewIndex); // NOI18N
        if (caretViewIndex >= 0 && caretViewIndex < rootView.getViewCount()) {
            View caretView = rootView.getView(caretViewIndex);
            /*DEBUG*/System.err.println("caretView: " + caretView); // NOI18N
        }
        /*DEBUG*/System.err.println(FixLineSyntaxState.lineInfosToString(adoc));
        // Check the hierarchy correctness
        //org.netbeans.editor.view.spi.ViewUtilities.checkViewHierarchy(rootView);
    }
    
    if (adoc instanceof BaseDocument) {
        /*DEBUG*/System.err.println("DOCUMENT:\n" + ((BaseDocument)adoc).toStringDetail()); // NOI18N
    }
}
 
源代码2 项目: netbeans   文件: DrawEngineTest.java
private void checkModelToViewCorrectness(JEditorPane jep) throws Exception {
        Document doc = jep.getDocument();
        
        assertTrue("Expecting BaseTextUI", jep.getUI() instanceof BaseTextUI);
        BaseTextUI btui = (BaseTextUI) jep.getUI();
        Insets margin = btui.getEditorUI().getTextMargin();
        int charWidth = btui.getEditorUI().defaultSpaceWidth;
        int charHeight = btui.getEditorUI().getLineHeight();

//        System.out.println("### charWidth = " + charWidth + ", charHeight = " + charHeight
//            + ", docLen = " + doc.getLength()
//            + ", jep.width = " + jep.getWidth()
//            + ", jep.height = " + jep.getHeight());
        
        for(int offset = 0; offset <= doc.getLength(); offset++) {
            // model-to-view translation
            Rectangle r = jep.modelToView(offset);
            assertNotNull("No m2v translation: offset = " + offset + ", docLen = " + doc.getLength(), r);

            View rootView = Utilities.getRootView(jep, DrawEngineDocView.class);
            int line = rootView.getViewIndex(offset, Position.Bias.Forward);
            int col = offset - rootView.getView(line).getStartOffset();

// XXX: this would be necessary for handling tabs, but it uses DrawEngineLineView
//      and therefore is not independent from the tested code, the inverse transformation
//      will be needed in checkViewToModel
//            int col = Utilities.getVisualColumn((BaseDocument)doc, offset);
//            int nextCol = offset >= rootView.getView(line).getEndOffset() - 1 ? col + 1 : Utilities.getVisualColumn((BaseDocument)doc, offset + 1);
//            System.out.println("### offset = " + offset + ", col = " + col + ", nextCol = " + nextCol + ", docLen = " + doc.getLength());

            Rectangle r2 = new Rectangle(
                margin.left + col * charWidth,
                margin.top + line * charHeight,
// XXX: see above comment about the tabs handling
//                (nextCol - col) * charWidth,
                charWidth,
                charHeight
            );

            assertEquals("Incorrect m2v translation: offset = " + offset + ", docLen = " + doc.getLength(), r2, r);
        }
    }