1 19 20 package org.openide.explorer.view; 21 22 import java.awt.BorderLayout ; 23 import java.beans.PropertyVetoException ; 24 import java.lang.reflect.InvocationTargetException ; 25 import javax.swing.JFrame ; 26 import javax.swing.JPanel ; 27 import javax.swing.JTree ; 28 import javax.swing.SwingUtilities ; 29 import javax.swing.tree.TreePath ; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.explorer.ExplorerManager; 32 import org.openide.nodes.AbstractNode; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.Node; 35 36 39 public class BeanTreeViewTest extends NbTestCase { 40 41 private static final int NO_OF_NODES = 3; 42 43 44 public BeanTreeViewTest(String name) { 45 super(name); 46 } 47 48 public void testFirstChildRemovalCausesSelectionOfSibling() throws Throwable { 49 doChildRemovalTest("foo"); 50 } 51 public void testSecondChildRemovalCausesSelectionOfSibling() throws Throwable { 52 doChildRemovalTest("bar"); 53 } 54 public void testThirdChildRemovalCausesSelectionOfSibling() throws Throwable { 55 doChildRemovalTest("bla"); 56 } 57 58 private void doChildRemovalTest(final String name) throws Throwable { 59 final AbstractNode root = new AbstractNode(new Children.Array()); 60 root.setName("test root"); 61 62 final Node[] children = { 63 createLeaf("foo"), 64 createLeaf("bar"), 65 createLeaf("bla"), 66 }; 67 68 root.getChildren().add(children); 69 70 final Panel p = new Panel (); 71 p.getExplorerManager().setRootContext(root); 72 73 final BeanTreeView btv = new BeanTreeView(); 74 p.add(BorderLayout.CENTER, btv); 75 76 JFrame f = new JFrame (); 77 f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); 78 f.getContentPane().add(BorderLayout.CENTER, p); 79 f.setVisible(true); 80 81 final JTree tree = btv.tree; 82 83 class AWTTst implements Runnable { 84 public void run() { 85 Node operateOn; 86 87 for (int i = 0; ; i++) { 88 if (name.equals(children[i].getName())) { 89 operateOn = children[i]; 91 break; 92 } 93 } 94 95 try { 96 p.getExplorerManager().setSelectedNodes(new Node[] { operateOn }); 97 } catch (PropertyVetoException e) { 98 fail("Unexpected PropertyVetoException from ExplorerManager.setSelectedNodes()"); 99 } 100 101 TreePath [] paths = tree.getSelectionPaths(); 102 assertNotNull("Before removal: one node should be selected, but there are none.", paths); 103 assertEquals("Before removal: one node should be selected, but there are none.", 1, paths.length); 104 assertEquals("Before removal: one node should be selected, but there are none.", operateOn, Visualizer.findNode(paths[0].getLastPathComponent())); 105 106 root.getChildren().remove(new Node[] { operateOn }); 108 109 assertNotNull("After removal: one node should be selected, but there are none.", tree.getSelectionPath()); 110 } 111 } 112 AWTTst awt = new AWTTst(); 113 try { 114 SwingUtilities.invokeAndWait(awt); 115 } catch (InvocationTargetException ex) { 116 throw ex.getTargetException(); 117 } 118 } 119 120 private static Node createLeaf(String name) { 121 AbstractNode n = new AbstractNode(Children.LEAF); 122 n.setName(name); 123 return n; 124 } 125 126 private static class Panel extends JPanel 127 implements ExplorerManager.Provider { 128 private ExplorerManager em = new ExplorerManager(); 129 130 public ExplorerManager getExplorerManager() { 131 return em; 132 } 133 } 134 } 135 | Popular Tags |