1 19 20 23 24 package org.openide.explorer.view; 25 26 import java.lang.ref.*; 27 import java.awt.event.*; 28 import java.awt.*; 29 import java.beans.PropertyVetoException ; 30 import java.lang.reflect.InvocationTargetException ; 31 import java.lang.reflect.Method ; 32 import java.util.*; 33 import javax.swing.JList ; 34 import javax.swing.SwingUtilities ; 35 import javax.swing.tree.TreeSelectionModel ; 36 37 import org.openide.*; 38 import org.openide.explorer.*; 39 import org.openide.nodes.*; 40 import org.openide.util.*; 41 import junit.framework.*; 42 import junit.textui.TestRunner; 43 import org.netbeans.junit.*; 44 45 54 public class SelectionModeTest extends NbTestCase { 55 56 ExplorerManager mgr; 57 TreeView tree; 58 Node[] singleSelection, contiguousSelection, discontiguousSelection; 59 60 public SelectionModeTest(String name) { 61 super(name); 62 } 63 64 public static void main (String args[]) { 65 TestRunner.run (new NbTestSuite (SelectionModeTest.class)); 66 System.exit(0); 67 } 68 69 70 protected boolean runInEQ() { 71 return true; 72 } 73 74 76 protected void setUp () { 77 System.setProperty ("org.openide.util.Lookup", "-"); 80 81 87 final Children parents = new Children.Array (); 88 Node root = new AbstractNode (parents); 89 root.setName ("root"); 90 91 tree = new BeanTreeView (); 92 94 final ExplorerPanel p = new ExplorerPanel (); 95 p.setName ("SelectionModeTest"); 96 97 p.add (tree, BorderLayout.CENTER); 98 p.getExplorerManager ().setRootContext (root); 99 p.open (); 100 101 final Children ones = new Children.Array (); 102 Node parent_one = new AbstractNode (ones); 103 parent_one.setName ("parent_one"); 104 final Children twos = new Children.Array (); 105 Node parent_two = new AbstractNode (twos); 106 parent_two.setName ("parent_two"); 107 108 final Node one1 = new AbstractNode (Children.LEAF); 109 one1.setName("one1"); 110 111 final Node one2 = new AbstractNode (Children.LEAF); 112 one2.setName("one2"); 113 114 ones.add(new Node[] { one1, one2 }); 115 116 final Node two1 = new AbstractNode (Children.LEAF); 117 two1.setName("two1"); 118 119 twos.add (new Node[] { two1 }); 120 121 parents.add (new Node[] { parent_one, parent_two }); 122 123 124 singleSelection = new Node[] {parent_two}; 126 contiguousSelection = new Node[] {one1, one2}; 127 discontiguousSelection = new Node[] {one2, two1}; 128 129 mgr = p.getExplorerManager(); 130 } 131 132 134 public void testSingleSelectionMode () throws Exception { 135 setSelectionMode (tree, TreeSelectionModel.SINGLE_TREE_SELECTION); 137 PropertyVetoException exp = null; 138 139 try { 141 assertTrue ("[MODE: SINGLE_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", trySelection (mgr, singleSelection)); 144 } catch (PropertyVetoException e) { 145 fail ("[MODE: SINGLE_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); } 148 149 try { 151 exp = null; 152 assertTrue ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes contiguous] Can't be getSelectedNodes equal setSelectedNodes.", !trySelection (mgr, contiguousSelection)); 155 } catch (PropertyVetoException e) { 156 exp = e; 158 } finally { 159 if (exp==null) 160 fail ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException was NOT thrown."); } 162 163 try { 165 exp = null; 166 assertTrue ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", trySelection (mgr, discontiguousSelection)); 168 } catch (PropertyVetoException e) { 169 exp = e; 171 } finally { 172 if (exp==null) 173 fail ("[MODE: SINGLE_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); } 175 176 } 177 178 180 public void testContigousSelection () throws Exception { 181 setSelectionMode (tree, TreeSelectionModel.CONTIGUOUS_TREE_SELECTION); 183 PropertyVetoException exp = null; 184 185 try { 187 assertTrue ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", trySelection (mgr, singleSelection)); 190 } catch (PropertyVetoException e) { 191 fail ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); } 194 195 try { 197 exp = null; 198 assertTrue ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] getSelectedNodes is NOT equal setSelectedNodes.", trySelection (mgr, contiguousSelection)); 201 } catch (PropertyVetoException e) { 202 fail ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException can't be thrown."); } 205 206 try { 208 exp = null; 210 assertTrue ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", trySelection (mgr, discontiguousSelection)); 212 } catch (PropertyVetoException e) { 213 exp = e; 215 } finally { 216 if (exp==null) 217 fail ("[MODE: CONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); } 219 220 } 221 222 224 public void testDiscontigousSelection () throws Exception { 225 setSelectionMode (tree, TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); 227 PropertyVetoException exp = null; 228 229 try { 231 assertTrue ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: single node] getSelectedNodes is NOT equal setSelectedNodes.", trySelection (mgr, singleSelection)); 234 } catch (PropertyVetoException e) { 235 fail ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: single node] PropertyVetoException can't be thrown."); } 238 239 try { 241 assertTrue ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] getSelectedNodes is NOT equal setSelectedNodes.", trySelection (mgr, contiguousSelection)); 244 } catch (PropertyVetoException e) { 245 fail ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes contiguous] PropertyVetoException can't be thrown."); } 248 249 try { 251 assertTrue ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] Can't be getSelectedNodes equal setSelectedNodes.", trySelection (mgr, discontiguousSelection)); 254 } catch (PropertyVetoException e) { 255 fail ("[MODE: DISCONTIGUOUS_TREE_SELECTION][NODES: two nodes discontiguous] PropertyVetoException was NOT thrown."); } 258 259 } 260 261 262 267 private boolean trySelection (ExplorerManager mgr, Node[] selected) throws PropertyVetoException { 268 mgr.setSelectedNodes (selected); 269 if (selected!=null) { 270 return Arrays.equals (selected, mgr.getSelectedNodes ()); 271 } 272 return true; 273 } 274 275 278 private void setSelectionMode (TreeView tree, int mode) { 279 try { 280 Class c = tree.getClass (); 281 Method m = c.getMethod ("setSelectionMode", new Class [] {Integer.TYPE}); 282 m.invoke (tree, new Object [] {new Integer (mode)}); 283 } catch (NoSuchMethodException nsme) { 284 fail ("The method setSelectionMode can't be called on this object. See enh #11928."); 285 } catch (IllegalAccessException iae) { 286 fail ("IllegalAccessException thrown from setSelectionMode."); 287 } catch (InvocationTargetException ite) { 288 fail ("InvocationTargetException thrown from setSelectionMode."); 289 } 290 } 291 292 } 293 | Popular Tags |