1 19 20 package org.openide.explorer.view; 21 22 import java.awt.Dimension ; 23 import java.awt.EventQueue ; 24 import java.awt.event.ActionEvent ; 25 import java.lang.reflect.InvocationTargetException ; 26 import javax.swing.AbstractAction ; 27 import javax.swing.Action ; 28 import javax.swing.JFrame ; 29 import javax.swing.JScrollBar ; 30 import javax.swing.JScrollPane ; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.explorer.ExplorerManager; 33 import org.openide.nodes.AbstractNode; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.FilterNode; 36 import org.openide.nodes.Node; 37 import org.openide.util.HelpCtx; 38 import org.openide.util.Lookup; 39 import org.openide.util.RequestProcessor; 40 import org.openide.util.actions.NodeAction; 41 import org.openide.util.actions.SystemAction; 42 43 47 public final class TreeViewTest extends NbTestCase { 48 49 private TestTreeView treeView; 50 private JFrame testWindow; 51 private volatile boolean isScrolledDown; 52 private final Object semaphore = new Object (); 53 54 public TreeViewTest(String testName) { 55 super(testName); 56 } 57 58 63 public void testAutoscrollOnOff() throws InterruptedException { 64 assert !EventQueue.isDispatchThread(); 65 66 testWindow = new ExplorerWindow(); 67 testWindow.getContentPane().add(treeView = new TestTreeView()); 68 69 class WindowDisplayer implements Runnable { 70 public void run() { 71 testWindow.pack(); 72 testWindow.setVisible(true); 73 } 74 } 75 76 class Detector implements Runnable { 77 public void run() { 78 if (!EventQueue.isDispatchThread()) { 79 EventQueue.invokeLater(this); 80 return; 81 } 82 83 isScrolledDown = !treeView.isUp(); 84 85 synchronized (semaphore) { 86 semaphore.notify(); 87 } 88 } 89 } 90 91 class Tester implements Runnable { 92 private final boolean autoscroll; 93 private final int part; 94 Tester(boolean autoscroll, int part) { 95 this.autoscroll = autoscroll; 96 this.part = part; 97 } 98 public void run() { 99 assert (part == 1) || (part == 2); 100 if (part == 1) { 101 treeView.collapse(); 102 treeView.scrollUp(); 103 assert treeView.isUp(); 104 } else { 105 treeView.setAutoscroll(autoscroll); 106 treeView.expand(); RequestProcessor.getDefault().post(new Detector(), 1000 ); 108 } 109 } 110 } 111 112 try { 113 EventQueue.invokeAndWait(new WindowDisplayer()); 114 } catch (InvocationTargetException ex) { 115 ex.printStackTrace(); 116 } 117 118 Thread.currentThread().sleep(5000); 120 121 EventQueue.invokeLater(new Tester(true, 1)); 122 Thread.currentThread().sleep(2000); EventQueue.invokeLater(new Tester(true, 2)); 124 synchronized (semaphore) { 125 semaphore.wait(); 126 } 127 assertTrue("Check the view has scrolled", isScrolledDown); 128 129 EventQueue.invokeLater(new Tester(false, 1)); 130 Thread.currentThread().sleep(2000); EventQueue.invokeLater(new Tester(false, 2)); 132 synchronized (semaphore) { 133 semaphore.wait(); 134 } 135 assertTrue("Check the view has not scrolled", !isScrolledDown); 136 137 EventQueue.invokeLater(new Tester(true, 1)); Thread.currentThread().sleep(2000); 139 } 140 141 142 private static final class TestTreeView extends BeanTreeView { 143 144 private final Node rootNode; 145 final JScrollBar verticalScrollBar; 146 private transient ExplorerManager explManager; 147 148 TestTreeView() { 149 super(); 150 tree.setAutoscrolls(true); 151 152 setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 153 verticalScrollBar = getVerticalScrollBar(); 154 155 rootNode = new AbstractNode(new TreeChildren()); 156 rootNode.setDisplayName("Root node"); 157 158 tree.setRowHeight(20); 159 160 Dimension prefSize = new Dimension (200, 6 * tree.getRowHeight() + 8); 161 prefSize.width = (int) (prefSize.width * 1.25f) 162 + verticalScrollBar.getWidth(); 163 setPreferredSize(prefSize); 164 } 165 166 public void addNotify() { 167 super.addNotify(); 168 explManager = ExplorerManager.find(this); 169 explManager.setRootContext(rootNode); 170 collapse(); 171 } 172 173 void setAutoscroll(boolean autoscroll) { 174 tree.setScrollsOnExpand(autoscroll); 175 } 176 177 void scrollUp() { 178 verticalScrollBar.setValue(verticalScrollBar.getMinimum()); 179 } 180 181 boolean isUp() { 182 return verticalScrollBar.getValue() 183 == verticalScrollBar.getMinimum(); 184 } 185 186 void expand() { 187 tree.expandRow(4); 188 } 189 190 void collapse() { 191 tree.collapseRow(4); 192 } 193 194 final static class TreeChildren extends Children.Array { 195 196 private static final char[] letters 197 = new char[] {'A', 'B', 'C', 'D', 'E'}; 198 199 TreeChildren() { 200 this(-1); 201 } 202 203 TreeChildren(final int first) { 204 super(); 205 206 Node[] childNodes = new Node[5]; 207 int i; 208 if (first == -1) { 209 for (i = 0; i < childNodes.length; i++) { 210 AbstractNode childNode = new AbstractNode(new TreeChildren(i)); 211 childNode.setDisplayName("Child node " + i); 212 childNodes[i] = childNode; 213 } 214 } else { 215 for (i = 0; i < childNodes.length; i++) { 216 AbstractNode childNode = new AbstractNode(Children.LEAF); 217 StringBuffer buf = new StringBuffer (3); 218 childNode.setDisplayName(buf.append(first) 219 .append('.') 220 .append(letters[i]) 221 .toString()); 222 childNodes[i] = childNode; 223 } 224 } 225 add(childNodes); 226 } 227 228 } 229 230 231 } 232 233 234 private static final class ExplorerWindow extends JFrame 235 implements ExplorerManager.Provider { 236 237 private final ExplorerManager explManager = new ExplorerManager(); 238 239 ExplorerWindow() { 240 super("TreeView test"); } 242 243 public ExplorerManager getExplorerManager() { 244 return explManager; 245 } 246 247 } 248 249 252 private static class MyAction extends NodeAction { 253 254 public boolean enable(Node[] nodes) { 255 return true; 256 } 257 258 public void performAction(Node[] nodes) { 259 } 260 261 public HelpCtx getHelpCtx() { 262 return HelpCtx.DEFAULT_HELP; 263 } 264 265 public String getName() { 266 return "My Action"; 267 } 268 269 public Action createContextAwareInstance(Lookup actionContext) { 270 return new MyDelegateAction(actionContext); 271 } 272 } 273 274 277 private static class MyDelegateAction extends AbstractAction { 278 Lookup contextLookup; 279 280 public MyDelegateAction(Lookup contextLookup) { 281 this.contextLookup = contextLookup; 282 } 283 284 public void actionPerformed(ActionEvent e) { 285 } 286 } 287 288 private static class NodeWhichHasItselfInLookup extends AbstractNode { 289 public NodeWhichHasItselfInLookup() { 290 super(Children.LEAF); 291 } 292 293 public Action getPreferredAction() { 294 return SystemAction.get(MyAction.class); 295 } 296 } 297 298 private static class NodeWhichDoesntHaveItselfInLookup extends AbstractNode { 299 public NodeWhichDoesntHaveItselfInLookup() { 300 super(Children.LEAF, Lookup.EMPTY); 301 } 302 303 public Action getPreferredAction() { 304 return SystemAction.get(MyAction.class); 305 } 306 } 307 308 312 public void testTakeActionNodeInLookup() { 313 doTestTakeAction(new NodeWhichHasItselfInLookup()); 314 } 315 316 320 public void testTakeActionNodeNotInLookup() { 321 doTestTakeAction(new NodeWhichDoesntHaveItselfInLookup()); 322 } 323 324 328 public void testTakeActionNodeInLookupAndFiltered() { 329 doTestTakeAction(new FilterNode(new NodeWhichHasItselfInLookup())); 330 } 331 332 337 public void testTakeActionNodeNotInLookupAndFiltered() { 338 doTestTakeAction(new FilterNode(new NodeWhichDoesntHaveItselfInLookup())); 339 } 340 341 private void doTestTakeAction(Node node) { 342 Action a = TreeView.takeAction(node.getPreferredAction(), node); 345 int count = ((MyDelegateAction)a).contextLookup.lookup(new Lookup.Template(Node.class)).allInstances().size(); 346 assertEquals("The context lookup created by TreeView.takeAction() should contain the node only once.", 1, count); 347 } 348 349 } 350 | Popular Tags |