1 19 20 package org.netbeans.modules.viewmodel; 21 22 import java.awt.event.ActionEvent ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import java.util.Vector ; 30 import javax.swing.AbstractAction ; 31 import javax.swing.Action ; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.modules.viewmodel.TreeModelNode; 34 import org.netbeans.modules.viewmodel.TreeModelRoot; 35 import org.netbeans.modules.viewmodel.TreeTable; 36 import org.netbeans.spi.viewmodel.*; 37 import org.openide.nodes.Node; 38 import org.openide.util.RequestProcessor; 39 40 41 42 47 public class BasicTest extends NbTestCase { 48 49 private String helpID = "A test help ID"; 51 public BasicTest (String s) { 52 super (s); 53 } 54 55 public void testBasic () throws Exception { 56 ArrayList l = new ArrayList (); 57 CompoundModel cm = new CompoundModel (); 58 l.add (cm); 59 Models.CompoundModel mcm = Models.createCompoundModel(l, helpID); 60 TreeTable tt = (TreeTable) Models.createView(mcm); 61 waitFinished (); 62 Node n = tt.getExplorerManager (). 63 getRootContext (); 64 checkNode (n, ""); 65 if (cm.exception != null) 66 cm.exception.printStackTrace (); 67 assertNull ("Threading problem", cm.exception); 68 assertEquals(n.getValue("propertiesHelpID"), helpID); 72 } 73 74 private void checkNode (Node n, String name) { 75 Node[] ns = n.getChildren ().getNodes (); 78 waitFinished (); 79 80 ns = n.getChildren ().getNodes (); 81 if (name.length () < 4) { 82 assertEquals (name, 3, ns.length); 83 checkNode (ns [0], name + "a"); 84 checkNode (ns [1], name + "b"); 85 checkNode (ns [2], name + "c"); 86 } else 87 assertEquals (ns.length, 0); 88 89 if (name.length () > 0) { 90 n.getDisplayName (); 92 String sd = n.getShortDescription (); 93 n.getActions (false); 94 waitFinished (); 95 assertEquals (name, n.getDisplayName ()); 96 assertEquals (name + "WWW", sd); 97 assertEquals (1, n.getActions (false).length); 98 } 99 } 100 101 static void waitFinished () { 102 TreeModelNode.getRequestProcessor ().post (new Runnable () { 103 public void run () {} 104 }).waitFinished (); 105 } 106 107 108 public void testMnemonics() throws Exception { 109 ArrayList l = new ArrayList (); 110 CompoundModel cm = new CompoundModel (); 111 l.add (cm); 112 TestColumnModel tcm = new TestColumnModel(); 113 l.add(tcm); 114 Models.CompoundModel mcm = Models.createCompoundModel(l); 115 TreeTable tt = (TreeTable) Models.createView(mcm); 116 Node.Property[] columns = tt.columns; 117 assertEquals(2, columns.length); 118 assertEquals(new Character ('e'), columns[1].getValue("ColumnMnemonicCharTTV")); 119 } 120 121 public static class CompoundModel implements TreeModel, 122 NodeModel, NodeActionsProvider, TableModel, TreeExpansionModel { 123 124 125 private Vector listeners = new Vector (); 126 127 private Throwable exception; 128 129 private Map callNumbers = new HashMap (); 130 protected synchronized void addCall (String methodName, Object node) { 131 Map m = (Map ) callNumbers.get (methodName); 132 if (m == null) 133 callNumbers.put (methodName, m = new HashMap ()); 134 if (m.containsKey (node)) { 135 Object info = m.get(node); 136 if (info instanceof Exception ) { 137 System.err.println ("Second call of " + methodName + " method for the same node " + node); 138 System.err.println("First was at:"); 139 ((Exception ) info).printStackTrace(); 140 System.err.println("Second is:"); 141 Thread.dumpStack(); 142 m.put (node, new Integer (2)); 143 } else { 144 int numCalls = ((Integer ) info).intValue() + 1; 145 System.err.println (numCalls+". call of " + methodName + " method for the same node " + node); 146 Thread.dumpStack(); 147 m.put (node, new Integer (numCalls)); 148 } 149 } else { 150 m.put (node, new Exception ()); 151 } 152 } 153 154 void checkThread () { 155 try { 156 assertTrue ("The right thread", TreeModelNode.getRequestProcessor ().isRequestProcessorThread ()); 157 } catch (Throwable t) { 158 exception = t; 159 } 160 166 } 167 168 170 175 public Object getRoot () { 176 addCall ("getRoot", null); 177 return ROOT; 178 } 179 180 189 public Object [] getChildren (Object parent, int from, int to) 190 throws UnknownTypeException { 191 addCall ("getChildren", parent); 192 if (parent == ROOT) 193 return new Object [] {"a", "b", "c"}; 194 if (parent instanceof String ) 195 return new Object [] {parent + "a", parent + "b", parent + "c"}; 196 throw new UnknownTypeException (parent); 197 } 198 199 208 public int getChildrenCount (Object node) throws UnknownTypeException { 209 addCall ("getChildrenCount", node); 210 if (node == ROOT) 211 return 3; 212 if (node instanceof String ) 213 return 3; 214 throw new UnknownTypeException (node); 215 } 216 217 224 public boolean isLeaf (Object node) throws UnknownTypeException { 225 addCall ("isLeaf", node); 226 if (node == ROOT) 227 return false; 228 if (node instanceof String ) 229 return ((String ) node).length () > 3; 230 throw new UnknownTypeException (node); 231 } 232 233 234 236 243 public String getDisplayName (Object node) throws UnknownTypeException { 244 addCall ("getDisplayName", node); 245 if (node instanceof String ) 247 return (String ) node; 248 throw new UnknownTypeException (node); 249 } 250 251 258 public String getShortDescription (Object node) 259 throws UnknownTypeException { 260 addCall ("getShortDescription", node); 261 if (node == ROOT) 263 return ""; 264 if (node instanceof String ) 265 return node + "WWW"; 266 throw new UnknownTypeException (node); 267 } 268 269 276 public String getIconBase (Object node) 277 throws UnknownTypeException { 278 addCall ("getIconBase", node); 279 if (node instanceof String ) 281 return node + "XXX"; 282 throw new UnknownTypeException (node); 283 } 284 285 286 288 295 public void performDefaultAction (Object node) throws UnknownTypeException { 296 } 297 298 305 public Action [] getActions (Object node) throws UnknownTypeException { 306 if (node == ROOT) 308 return new Action [0]; 309 if (node instanceof String ) 310 return new Action [] { 311 new AbstractAction ((String ) node) { 312 public void actionPerformed (ActionEvent ev) { 313 314 } 315 }, 316 }; 317 throw new UnknownTypeException (node); 318 } 319 320 321 323 329 public ColumnModel[] getColumns () { 330 return new ColumnModel [0]; 331 } 332 333 334 336 public Object getValueAt (Object node, String columnID) throws 337 UnknownTypeException { 338 addCall ("getValueAt", node); 339 checkThread (); 340 if (node instanceof String ) { 341 if (columnID.equals ("1")) 342 return node + "1"; 343 if (columnID.equals ("2")) 344 return node + "2"; 345 } 346 throw new UnknownTypeException (node); 347 } 348 349 public boolean isReadOnly (Object node, String columnID) throws 350 UnknownTypeException { 351 addCall ("isReadOnly", node); 352 checkThread (); 353 if (node instanceof String ) { 354 if (columnID.equals ("1")) 355 return true; 356 if (columnID.equals ("2")) 357 return true; 358 } 359 throw new UnknownTypeException (node); 360 } 361 362 public void setValueAt (Object node, String columnID, Object value) throws 363 UnknownTypeException { 364 throw new UnknownTypeException (node); 365 } 366 367 368 370 private Set toBeExpandedTest = new HashSet (); 371 private Set expandedTest = new HashSet (); 372 { 373 toBeExpandedTest.add ("a"); 374 toBeExpandedTest.add ("ab"); 375 toBeExpandedTest.add ("abc"); 376 } 377 378 384 public boolean isExpanded (Object node) throws UnknownTypeException { 385 if (node instanceof String ) 386 return toBeExpandedTest.contains (node); 387 throw new UnknownTypeException (node); 388 } 389 390 395 public void nodeExpanded (Object node) { 396 if (!toBeExpandedTest.contains (node)) { 397 System.err.println("This node should not be expanded: " + node); 398 Thread.dumpStack(); 399 } 400 expandedTest.add (node); 401 } 402 403 408 public void nodeCollapsed (Object node) { 409 System.err.println("nodeCollapsed " + node); 410 Thread.dumpStack(); 411 } 412 413 414 416 421 public void addModelListener (ModelListener l) { 422 listeners.add (l); 423 } 424 425 430 public void removeModelListener (ModelListener l) { 431 listeners.remove (l); 432 } 433 434 public void fire () { 435 Vector v = (Vector ) listeners.clone (); 436 int i, k = v.size (); 437 for (i = 0; i < k; i++) 438 ((ModelListener) v.get (i)).modelChanged (null); 439 } 440 441 public void fire (ModelEvent event) { 442 Vector v = (Vector ) listeners.clone (); 443 int i, k = v.size (); 444 for (i = 0; i < k; i++) { 445 ((ModelListener) v.get (i)).modelChanged (event); 446 } 447 } 448 } 449 450 private static class TestColumnModel extends ColumnModel { 451 public Class getType() { 452 return String .class; 453 } 454 455 public String getDisplayName() { 456 return "Test"; 457 } 458 459 public Character getDisplayedMnemonic() { 460 return new Character ('e'); 461 } 462 463 public String getID() { 464 return "xx"; 465 } 466 467 } 468 } 469 | Popular Tags |