1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.reflect.InvocationTargetException ; 25 import org.netbeans.junit.NbTestCase; 26 import org.openide.nodes.AbstractNode; 27 import org.openide.nodes.Children; 28 import org.openide.nodes.Node; 29 import org.openide.nodes.NodeEvent; 30 import org.openide.nodes.NodeListener; 31 import org.openide.nodes.NodeMemberEvent; 32 import org.openide.nodes.NodeReorderEvent; 33 import org.openide.nodes.PropertySupport; 34 import org.openide.nodes.Sheet; 35 36 public class ProxyNodeTest extends NbTestCase { 38 public ProxyNodeTest(String name) { 39 super(name); 40 } 41 42 protected boolean runInEQ() { 43 return false; 44 } 45 46 47 ProxyNode pn = null; 48 TNode a = null; 49 TNode b = null; 50 TProperty pa = null; 51 TProperty pb = null; 52 ProxyNodeListener nodeL = null; 53 ProxyNodePCL pcl = null; 54 protected void setUp() throws Exception { 55 pa = new TProperty(); 56 pb = new TProperty(); 57 a = new TNode(pa); 58 b = new TNode(pb); 59 pn = new ProxyNode(new Node[] {a,b}); 60 nodeL = new ProxyNodeListener(); 61 pcl = new ProxyNodePCL(); 62 pn.addNodeListener(nodeL); 63 pn.addPropertyChangeListener(pcl); 64 } 65 66 private Node.Property findProxyProperty() { 67 return pn.getPropertySets()[0].getProperties()[0]; 68 } 69 70 public void testProxyPropertyCreated() { 71 assertNotNull("Proxied properties each have one property, but couldn't find matching ProxyProperty", findProxyProperty()); 72 } 73 74 public void testProxyPropertyReflectsValueChanges() throws Exception { 75 Object o = findProxyProperty().getValue(); 76 String name = findProxyProperty().getName(); 77 pa.setValue("Foo"); 78 nodeL.assertNoEvent("Setting a property value should not fire to NodeListeners"); 79 pcl.assertFired("Property change should have been fired on " + pa.getName(), pa.getName()); 80 } 81 82 public void testProxyPropertyHandlesNodeDeletion() { 83 a.destroyMe(); 84 nodeL.assertNotDestroyed(); 85 b.destroyMe(); 86 nodeL.assertNodeDestroyedFired("Destroying both nodes represented by a proxy node should make it fire NodeDestroyed"); 87 } 88 89 public void testDisplayNameNotFiredToNodeListeners() { 90 System.err.println("First display name " + pn.getDisplayName()); 91 a.setDisplayName("New name"); 92 System.err.println("Second display name " + pn.getDisplayName()); 93 nodeL.assertFired("Setting display name on a proxied node should trigger a property change event", Node.PROP_DISPLAY_NAME); 94 pcl.assertNoEvent("Setting display name should not cause an event on NodeListeners"); 95 } 96 97 public void testCookieChangeGoesToTheCorrectListenerOnly() { 98 a.fireCookies(); 99 nodeL.assertFired("One node firing prop cookies should cause an event to node listeners", Node.PROP_COOKIE); 100 pcl.assertNoEvent("No event should have been fired to a property change listener on a cookies change"); 101 } 102 103 104 public class ProxyNodeListener implements NodeListener { 105 public void childrenAdded(NodeMemberEvent ev) { 106 } 107 108 public void childrenRemoved(NodeMemberEvent ev) { 109 } 110 111 public void childrenReordered(NodeReorderEvent ev) { 112 } 113 114 NodeEvent ev = null; 115 public void nodeDestroyed(NodeEvent ev) { 116 this.ev = ev; 117 } 118 119 public void assertNotDestroyed() { 120 assertNull(ev); 121 } 122 123 public void assertNodeDestroyedFired(String msg) { 124 assertNotNull(msg + " - NodeDestroyed not fired", ev); 125 } 126 127 private PropertyChangeEvent evt = null; 128 public void propertyChange(PropertyChangeEvent evt) { 129 this.evt = evt; 130 } 131 132 public void assertNoEvent(String msg) { 133 assertNull(msg + "- event was fired to NodeListener but should not have been - " + (evt != null ? evt.getPropertyName() : ""), evt); 134 assertNull(msg + "- no node event should have been generated, but one was " + ev, ev); 135 } 136 137 public void assertFired(String msg, String propName) { 138 assertTrue(msg + "- event was not fired to NodeListener but should have been", evt != null && propName.equals(evt.getPropertyName())); 139 } 140 } 141 142 public class ProxyNodePCL implements PropertyChangeListener { 143 private PropertyChangeEvent evt = null; 144 public void propertyChange(PropertyChangeEvent evt) { 145 this.evt = evt; 146 } 147 148 public void assertNoEvent(String msg) { 149 assertNull(msg + "- event was fired to NodeListener but should not have been", evt); 150 } 151 152 public void assertFired(String msg, String propName) { 153 assertTrue(msg + "- event was not fired to PropertyChangeListener but should have been", evt != null && propName.equals(evt.getPropertyName())); 154 } 155 } 156 157 public class TNode extends AbstractNode { 159 private TProperty myprop; 160 public TNode(TProperty myprop) { 162 super(Children.LEAF); 163 setName("TNode"); 164 setDisplayName("TNode"); 165 this.myprop = myprop; 166 } 167 public Node cloneNode() { 169 return new TNode(myprop); 170 } 171 172 protected Sheet createSheet() { 174 Sheet sheet = super.createSheet(); 175 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 177 if (props == null) { 178 props = Sheet.createPropertiesSet(); 179 sheet.put(props); 180 } 181 props.put(myprop); 182 myprop.setFiringNode(this); 183 return sheet; 184 } 185 public void fireMethod(String s, Object o1, Object o2) { 187 firePropertyChange(s,o1,o2); 188 } 189 190 public void setDisplayName(String s) { 191 String old = super.getDisplayName(); 192 super.setDisplayName(s); 193 fireDisplayNameChange(old, s); 194 } 195 196 public void destroyMe() { 197 fireNodeDestroyed(); 198 } 199 200 public void fireCookies() { 201 fireCookieChange(); 202 } 203 } 204 205 public class TProperty extends PropertySupport { 207 private Object myValue = "Value"; 208 public TProperty() { 210 super("TProperty", String .class, "TProperty", "", true, true); 211 } 212 public Object getValue() { 214 return myValue; 215 } 216 private TNode node = null; 217 void setFiringNode(TNode n) { 218 this.node = n; 219 } 220 221 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 223 System.err.println("TProperty setValue: " + value); 224 Object oldVal = myValue; 225 myValue = value; 226 System.err.println("TProperty triggering node property change"); 227 if (node != null) { 228 node.fireMethod(getName(), oldVal, myValue); 229 } 230 } 231 } 232 233 } 234 | Popular Tags |