1 19 20 package org.netbeans.modules.viewmodel; 21 22 import java.awt.event.ActionEvent ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.Vector ; 27 import javax.swing.AbstractAction ; 28 import javax.swing.Action ; 29 import javax.swing.SwingUtilities ; 30 import org.netbeans.junit.NbTestCase; 31 import org.netbeans.modules.viewmodel.TreeModelNode; 32 import org.netbeans.modules.viewmodel.TreeModelRoot; 33 import org.netbeans.modules.viewmodel.TreeTable; 34 import org.netbeans.spi.viewmodel.*; 35 import org.openide.nodes.Node; 36 import org.openide.nodes.NodeListener; 37 import org.openide.util.RequestProcessor; 38 39 40 41 44 public class ModelEventTest extends NbTestCase implements NodeListener { 45 46 BasicTest.CompoundModel cm; 47 Node n; 48 volatile Object event; 49 Vector propEvents = new Vector (); 50 51 public ModelEventTest (String s) { 52 super (s); 53 } 54 55 protected void setUp() throws Exception { 56 super.setUp(); 57 ArrayList l = new ArrayList (); 58 cm = new CompoundModel1 (); 59 l.add (cm); 60 TreeTable tt = (TreeTable) Models.createView 61 (Models.createCompoundModel (l)); 62 BasicTest.waitFinished (); 63 n = tt.getExplorerManager (). 64 getRootContext (); 65 n.addNodeListener(this); 66 } 67 68 public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) { 69 assertNull("Already fired", event); 70 event = ev; 71 } 72 73 public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) { 74 assertNull("Already fired", event); 75 event = ev; 76 } 77 78 public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) { 79 assertNull("Already fired", event); 80 event = ev; 81 } 82 83 public void nodeDestroyed(org.openide.nodes.NodeEvent ev) { 84 assertNull("Already fired", event); 85 event = ev; 86 } 87 88 public void propertyChange(java.beans.PropertyChangeEvent propertyChangeEvent) { 89 propEvents.add(propertyChangeEvent.getPropertyName()); 90 95 } 96 97 public void testDisplayName() { 98 ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.DISPLAY_NAME_MASK); 99 cm.fire(e); 100 try { 101 SwingUtilities.invokeAndWait (new Runnable () { 102 public void run () {} 103 }); 104 } catch (InterruptedException iex) { 105 fail(iex.toString()); 106 } catch (InvocationTargetException itex) { 107 fail(itex.toString()); 108 } 109 assertTrue("Display Name was not fired", propEvents.contains(Node.PROP_DISPLAY_NAME)); 110 } 112 113 public void testIcon() { 114 ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.ICON_MASK); 115 cm.fire(e); 116 try { 117 SwingUtilities.invokeAndWait (new Runnable () { 118 public void run () {} 119 }); 120 } catch (InterruptedException iex) { 121 fail(iex.toString()); 122 } catch (InvocationTargetException itex) { 123 fail(itex.toString()); 124 } 125 assertTrue("Icon was not fired", propEvents.contains(Node.PROP_ICON)); 126 } 127 128 public void testShortDescription() { 129 ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.SHORT_DESCRIPTION_MASK); 130 cm.fire(e); 131 try { 132 SwingUtilities.invokeAndWait (new Runnable () { 133 public void run () {} 134 }); 135 } catch (InterruptedException iex) { 136 fail(iex.toString()); 137 } catch (InvocationTargetException itex) { 138 fail(itex.toString()); 139 } 140 assertTrue("Short Description was not fired", propEvents.contains(Node.PROP_SHORT_DESCRIPTION)); 141 } 142 143 public void testChildren() { 144 n.getChildren().getNodes(); 145 160 } 161 162 public final class CompoundModel1 extends BasicTest.CompoundModel { 163 164 int dn = 0; 165 int ib = 0; 166 int sd = 0; 167 int cc = 0; 168 169 protected void addCall (String methodName, Object node) { 170 } 172 173 175 public String getDisplayName (Object node) throws UnknownTypeException { 176 String dns = super.getDisplayName(node); 177 dns += (dn++); 178 return dns; 179 } 180 181 public String getIconBase (Object node) throws UnknownTypeException { 182 String ibs = super.getIconBase(node); 183 ibs += (ib++); 184 return ibs; 185 } 186 187 public String getShortDescription (Object node) throws UnknownTypeException { 188 String sds = super.getShortDescription(node); 189 sds += (sd++); 190 return sds; 191 } 192 193 202 public synchronized int getChildrenCount (Object node) throws UnknownTypeException { 203 return super.getChildrenCount (node) + (cc++); 204 } 205 206 public Object [] getChildren (Object parent, int from, int to) throws UnknownTypeException { 207 System.err.println("CompoundModel1.getChildren("+parent+", "+from+", "+to+")"); 208 addCall ("getChildren", parent); 210 Object [] ch = new Object [3 + (cc - 1)]; 211 if (parent == ROOT) { 212 for (int i = 0; i < ch.length; i++) { 213 ch[i] = new Character ((char) ('a' + i)).toString(); 215 } 216 return ch; 217 } 218 if (parent instanceof String ) { 219 for (int i = 0; i < ch.length; i++) { 220 ch[i] = ((String ) parent + new Character ((char) ('a' + i)).toString()); 222 } 223 return ch; 224 } 225 throw new UnknownTypeException (parent); 226 } 227 } 228 } 229 | Popular Tags |