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.lang.reflect.InvocationTargetException ; 27 import javax.swing.JFrame ; 28 import javax.swing.SwingUtilities ; 29 import org.netbeans.junit.NbTestCase; 30 import org.openide.nodes.AbstractNode; 31 import org.openide.nodes.Children; 32 import org.openide.nodes.Node; 33 import org.openide.nodes.NodeAdapter; 34 import org.openide.nodes.PropertySupport; 35 import org.openide.nodes.Sheet; 36 37 public class PropertiesFlushTest extends NbTestCase { 39 private PropertySheet ps = null; 40 public PropertiesFlushTest(String name) { 41 super(name); 42 } 43 44 protected boolean runInEQ() { 45 return false; 46 } 47 48 private static boolean setup = false; 49 54 protected void setUp() throws Exception { 55 try { 56 tn = new TNode(); 58 59 final JFrame jf = new JFrame (); 63 ps = new PropertySheet(); 64 jf.getContentPane().setLayout(new BorderLayout ()); 65 jf.getContentPane().add(ps, BorderLayout.CENTER); 66 jf.setLocation(30,30); 67 jf.setSize(500,500); 68 new ExtTestCase.WaitWindow(jf); 69 70 SwingUtilities.invokeAndWait(new Runnable () { 71 public void run() { 72 ps.setNodes(new Node[] {tn}); 73 } 74 }); 75 76 sleep(); 77 ensurePainted(ps); 78 } catch (Exception e) { 79 fail("FAILED - Exception thrown "+e.getClass().toString()); 80 } 81 } 82 83 private void ensurePainted(final PropertySheet ps) throws Exception { 84 SwingUtilities.invokeAndWait(new Runnable () { 88 public void run() { 89 Graphics g = ps.getGraphics(); 90 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight()); 91 } 92 }); 93 } 94 95 96 public void testNullChangePerformedAndReflectedInPropertySheet() throws Exception { 97 System.err.println(".testNullChangePerformedAndReflectedInPropertySheet"); 98 int count = ps.table.getRowCount(); 99 assertTrue("Property sheet should contain three rows ", count==3); 100 L l = new L(); 101 tn.addPropertyChangeListener(l); 102 103 SwingUtilities.invokeAndWait(new Runnable () { 104 public void run() { 105 tn.replaceProps(); 106 } 107 }); 108 Thread.currentThread().sleep(500); 109 111 l.assertEventReceived(); 112 113 assertTrue("Should only be one property", tn.getPropertySets()[0].getProperties().length == 1); 114 sleep(); 115 ensurePainted(ps); 116 int rc = ps.table.getRowCount(); 117 assertTrue("Property sheet should now only show 2 rows, not " + rc, rc == 2); 118 } 119 120 private Exception throwMe = null; 121 public void testSetSheetChangesPropertySheetContents() throws Exception { 122 System.err.println(".testSetSheetChangesPropertySheetContents"); 123 final TNode2 tnd = new TNode2(); 124 throwMe = null; 125 SwingUtilities.invokeAndWait(new Runnable () { 126 public void run() { 127 try { 128 ps.setNodes(new Node[] {tnd}); 129 } catch (Exception e) { 130 throwMe = e; 131 } 132 } 133 }); 134 135 if (throwMe != null) { 136 throw throwMe; 137 } 138 sleep(); 139 140 int rowCount = ps.table.getRowCount(); 141 assertTrue("With a single property in a single property set, row count should be 2 but is " + rowCount, rowCount == 2); 142 143 144 L2 l2 = new L2(); 145 tnd.addNodeListener(l2); 146 throwMe = null; 147 148 SwingUtilities.invokeAndWait(new Runnable () { 149 public void run() { 150 try { 151 System.err.println("Replacing property sets"); 152 tnd.replaceSets(); 153 } catch (Exception e) { 154 throwMe = e; 155 } 156 } 157 }); 158 if (throwMe != null) { 159 throw throwMe; 160 } 161 sleep(); 162 163 System.err.println("EnsurePainted"); 164 ensurePainted(ps); 165 166 System.err.println("Asserting event received"); 167 l2.assertEventReceived(); 168 169 int nueCount = ps.table.getRowCount(); 170 System.err.println("Checking count - it is " + nueCount); 171 assertTrue("With two properties in two property sets, row count should be 4 but is " + nueCount, nueCount == 4); 172 173 174 } 175 176 private void sleep() throws Exception { 177 Thread.currentThread().sleep(500); 178 SwingUtilities.invokeAndWait(new Runnable () { 179 public void run() { 180 System.currentTimeMillis(); 181 } 182 }); 183 } 184 185 private class L implements PropertyChangeListener { 186 private boolean eventReceived = false; 187 188 public void assertEventReceived() { 189 assertTrue("null null null property change not received on sets change", eventReceived); 190 eventReceived = false; 191 } 192 193 public void propertyChange(PropertyChangeEvent evt) { 194 if (evt.getPropertyName() == null && evt.getOldValue() == null && evt.getNewValue() == null) { 195 eventReceived = true; 196 } 197 System.err.println("Event: " + evt); 198 } 199 } 200 201 private class L2 extends NodeAdapter { 202 private boolean eventReceived = false; 203 204 public void assertEventReceived() { 205 assertTrue("AbstractNode did not fire PROP_PROPERTY_SETS when setSheet() was called", eventReceived); 206 eventReceived = false; 207 } 208 209 public void propertyChange(PropertyChangeEvent evt) { 210 if (AbstractNode.PROP_PROPERTY_SETS.equals(evt.getPropertyName()) && evt.getOldValue() == null && evt.getNewValue() == null) { 211 eventReceived = true; 212 } 213 System.err.println("Event: " + evt + " name " + evt.getPropertyName()); 214 } 215 } 216 217 public class TNode extends AbstractNode { 219 public TNode() { 221 super(Children.LEAF); 222 setName("TNode"); setDisplayName("TNode"); 224 } 225 public Node cloneNode() { 227 return new TNode(); 228 } 229 230 public void replaceProps() { 231 sets = null; 232 firePropertyChange(null, null, null); 233 } 234 235 Node.PropertySet[] sets = null; 236 237 private boolean firstTime = true; 238 public Node.PropertySet[] getPropertySets() { 240 if (sets == null) { 241 System.err.println("Create sheet"); 242 Sheet sheet = new Sheet(); 243 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 245 props = Sheet.createPropertiesSet(); 246 sheet.put(props); 247 TProperty tp = new TProperty("property", true); 248 props.put(tp); 249 if (firstTime) { 250 props.put(new TProperty("second", true)); 251 System.err.println("first time"); 252 firstTime = false; 253 } else { 254 System.err.println("Second time"); 255 } 256 sets = sheet.toArray(); 257 } 258 return sets; 259 } 260 public void fireMethod(String s, Object o1, Object o2) { 262 System.err.println("firing"); 263 } 264 } 265 266 public class TNode2 extends AbstractNode { 268 public TNode2() { 270 super(Children.LEAF); 271 setName("TNode2"); setDisplayName("TNode2"); 273 } 274 public Node cloneNode() { 276 return new TNode(); 277 } 278 279 public void replaceSets() { 280 Sheet sheet = new Sheet(); 281 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 282 if (props == null) { 283 props = Sheet.createPropertiesSet(); 284 } 285 props.put(new TProperty("after - first", true)); 286 sheet.put(props); 287 props = sheet.get(Sheet.EXPERT); 288 if (props == null) { 289 props = Sheet.createExpertSet(); 290 } 291 props.put(new TProperty("after - second", true)); 292 sheet.put(props); 293 setSheet(sheet); 294 } 295 296 protected Sheet createSheet() { 298 Sheet sheet = super.createSheet(); 299 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 301 if (props == null) { 302 props = Sheet.createPropertiesSet(); 303 sheet.put(props); 304 } 305 props.put(new TProperty("before", true)); 306 return sheet; 307 } 308 309 310 } 311 312 public class TProperty extends PropertySupport { 314 private Object myValue = "Value"; 315 public TProperty(String name, boolean isWriteable) { 317 super(name, String .class, name, "", true, isWriteable); 318 } 319 public Object getValue() { 321 return myValue; 322 } 323 324 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 326 Object oldVal = myValue; 327 myValue = value; 328 } 329 } 330 331 332 private static TNode tn; 333 private static TProperty tp; 334 private static String initEditorValue; 335 private static String initPropertyValue; 336 private static String postChangePropertyValue; 337 private static String postChangeEditorValue; 338 } 339 | Popular Tags |