1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Graphics ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyEditor ; 27 import java.beans.PropertyEditorSupport ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.Arrays ; 30 import javax.swing.JFrame ; 31 import javax.swing.SwingUtilities ; 32 import org.netbeans.junit.NbTestCase; 33 import org.openide.nodes.AbstractNode; 34 import org.openide.nodes.Children; 35 import org.openide.nodes.Node; 36 import org.openide.nodes.NodeEvent; 37 import org.openide.nodes.NodeListener; 38 import org.openide.nodes.NodeMemberEvent; 39 import org.openide.nodes.NodeReorderEvent; 40 import org.openide.nodes.PropertySupport; 41 import org.openide.nodes.Sheet; 42 43 public class NodeDeletionTest extends NbTestCase { 45 private PropertySheet ps = null; 46 public NodeDeletionTest(String name) { 47 super(name); 48 } 49 50 protected boolean runInEQ() { 51 return false; 52 } 53 54 private static boolean setup = false; 55 60 protected void setUp() throws Exception { 61 te = new TEditor(); 63 tn = new TNode(); 65 66 final JFrame jf = new JFrame (); 70 ps = new PropertySheet(); 71 jf.getContentPane().setLayout(new BorderLayout ()); 72 jf.getContentPane().add(ps, BorderLayout.CENTER); 73 jf.setLocation(30,30); 74 jf.setSize(500,500); 75 76 SwingUtilities.invokeAndWait(new Runnable () { 77 public void run() { 78 ps.setNodes(new Node[] {tn}); 79 jf.show(); 81 } 82 }); 83 84 jf.show(); 85 new ExtTestCase.WaitWindow(jf); 86 87 try { 88 for (int i = 0; i < 10; i++) { 90 if (te.getAsText().equals("null")) { 91 Thread.sleep(1000); 93 } else break; 94 } 95 ensurePainted(ps); 96 97 } catch (Exception e) { 98 fail("FAILED - Exception thrown "+e.getClass().toString()); 99 } 100 } 101 102 private void ensurePainted(final PropertySheet ps) throws Exception { 103 SwingUtilities.invokeAndWait(new Runnable () { 107 public void run() { 108 Graphics g = ps.getGraphics(); 109 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight()); 110 } 111 }); 112 } 113 114 115 public void testNodeDestroyClearsPropertySheet() throws Exception { 116 int count = ps.table.getRowCount(); 117 assertTrue("Property sheet should contain one property set and one property but table has " + count + " rows", count==2); 118 tn.destroy(); 119 Thread.currentThread().yield(); 120 Thread.currentThread().sleep(500); 121 SwingUtilities.invokeAndWait(new Runnable () { 122 public void run() { 123 System.currentTimeMillis(); 124 } 125 }); 126 int postCount = ps.table.getRowCount(); 127 assertTrue("Property sheet should synchronously reflect node destruction" + 128 " even if destroyed on a non EQ thread", postCount == 0); 129 } 130 131 public void testProxyNodeReflectsNodeDestruction() throws Exception { 132 TNode node1 = new TNode(); 133 TNode node2 = new TNode(); 134 ProxyNode proxy = new ProxyNode(new Node[] {node1, node2}); 135 L l = new L(); 136 proxy.addNodeListener(l); 137 138 node1.destroy(); 139 assertTrue("Proxy node should not represent a destroyed node", 140 !Arrays.asList(proxy.getOriginalNodes()).contains(node1)); 141 142 l.assertNotDestroyed(); 143 144 node2.destroy(); 145 l.assertDestroyed(); 146 } 147 148 private class L implements NodeListener { 149 boolean destroyed = false; 150 151 public void assertDestroyed() { 152 assertTrue("Node was not destroyed", destroyed); 153 } 154 155 public void assertNotDestroyed() { 156 assertTrue("Node was not destroyed", !destroyed); 157 } 158 159 public void childrenAdded(NodeMemberEvent ev) { 160 } 161 162 public void childrenRemoved(NodeMemberEvent ev) { 163 } 164 165 public void childrenReordered(NodeReorderEvent ev) { 166 } 167 168 public void nodeDestroyed(NodeEvent ev) { 169 destroyed = true; 170 } 171 172 public void propertyChange(PropertyChangeEvent evt) { 173 } 174 175 } 176 177 public class TNode extends AbstractNode { 179 public TNode() { 181 super(Children.LEAF); 182 setName("TNode"); setDisplayName("TNode"); 184 } 185 public Node cloneNode() { 187 return new TNode(); 188 } 189 190 public void destroy() { 191 fireNodeDestroyed(); 192 } 193 194 protected Sheet createSheet() { 196 Sheet sheet = super.createSheet(); 197 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 199 if (props == null) { 200 props = Sheet.createPropertiesSet(); 201 sheet.put(props); 202 } 203 TProperty tp = new TProperty("property", true); 204 props.put(tp); 205 return sheet; 206 } 207 public void fireMethod(String s, Object o1, Object o2) { 209 firePropertyChange(s,o1,o2); 210 } 211 } 212 213 public class TProperty extends PropertySupport { 215 private Object myValue = "Value"; 216 public TProperty(String name, boolean isWriteable) { 218 super(name, Object .class, name, "", true, isWriteable); 219 } 220 public Object getValue() { 222 return myValue; 223 } 224 225 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 227 Object oldVal = myValue; 228 myValue = value; 229 tn.fireMethod(getName(), oldVal, myValue); 230 } 231 public PropertyEditor getPropertyEditor() { 233 return te; 234 } 235 } 236 237 public class TEditor extends PropertyEditorSupport implements ExPropertyEditor { 239 PropertyEnv env; 240 241 public TEditor() { 243 } 244 245 249 public void attachEnv(PropertyEnv env) { 250 this.env = env; 251 } 252 253 public boolean supportsCustomEditor() { 255 return false; 256 } 257 258 public void addPropertyChangeListener(PropertyChangeListener l) { 259 super.addPropertyChangeListener(l); 260 } 261 262 public void removePropertyChangeListener(PropertyChangeListener l) { 263 super.removePropertyChangeListener(l); 264 } 265 266 267 268 public void setValue(Object newValue) { 270 super.setValue(newValue); 271 } 272 273 public void firePropertyChange() { 274 super.firePropertyChange(); 275 } 276 } 277 278 private TNode tn; 279 private TProperty tp; 280 private TEditor te; 281 } 282 | Popular Tags |