Java Code Examples for javax.swing.text.View#getViewIndex()

The following examples show how to use javax.swing.text.View#getViewIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActionFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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
    }
}
 
Example 2
Source File: DrawEngineTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
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);
        }
    }