Java Code Examples for javax.swing.JComponent.getTopLevelAncestor()

The following are Jave code examples for showing how to use getTopLevelAncestor() of the javax.swing.JComponent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: NonEditabilityTest.java   View Source Code Vote up 4 votes
public void testClickInvokesCustomEditor() throws Exception {
    if( !ExtTestCase.canSafelyRunFocusTests() )
        return;
    
    Node n = new ANode();
    setCurrentNode (n, sheet);

    sleep();

    requestFocus (sheet.table);
    sleep();

    Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
    if (owner == sheet.table) { //sanity check to avoid random failures on some window managers

        System.out.println ("About to click cell");

        Rectangle r = sheet.table.getCellRect(1, 1, false);
        final MouseEvent me = new MouseEvent (sheet.table, MouseEvent.MOUSE_PRESSED,
            System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, r.x + 3,
            r.y + 3, 2, false);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                sheet.table.dispatchEvent(me);
            }
        });

        sleep();
        sleep();

        System.out.println ("Now checking focus");

        owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        assertTrue ("Focus owner should be custom editor, not " + owner, owner instanceof JTextArea);

        JComponent jc = (JComponent) owner;
        assertTrue ("Custom editor should have been invoked, but focus owner's top level ancestor is not a dialog", jc.getTopLevelAncestor() instanceof Dialog);

        Dialog d = (Dialog) jc.getTopLevelAncestor();

        d.setVisible(false);
    }

    requestFocus (sheet.table);
    sleep();

    owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
    if (owner == sheet.table) { //sanity check to avoid random failures on some window managers
        pressKey(sheet.table, KeyEvent.VK_SPACE);
        sleep();

        owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
        assertTrue ("After pressing a key, focus owner should still be the table, not " + owner, sheet.table == owner);
    }

}