1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Graphics ; 24 import java.beans.PropertyChangeListener ; 25 import java.beans.PropertyEditor ; 26 import java.beans.PropertyEditorSupport ; 27 import java.lang.reflect.InvocationTargetException ; 28 import javax.swing.JFrame ; 29 import javax.swing.SwingUtilities ; 30 import org.netbeans.junit.NbTestCase; 31 import org.openide.nodes.AbstractNode; 32 import org.openide.nodes.Children; 33 import org.openide.nodes.Node; 34 import org.openide.nodes.PropertySupport; 35 import org.openide.nodes.Sheet; 36 37 public class PropertySheetTest extends NbTestCase { 39 public PropertySheetTest(String name) { 40 super(name); 41 } 42 43 protected boolean runInEQ() { 44 return false; 45 } 46 47 private static boolean setup = false; 48 53 protected void setUp() throws Exception { 54 if (setup) return; 55 setup = true; 56 tp = new TProperty("TProperty", true); 58 te = new TEditor(); 60 tn = new TNode(); 62 63 System.err.println("RUNNING ON THREAD " + Thread.currentThread()); 64 65 final JFrame jf = new JFrame (); 69 final PropertySheet ps = new PropertySheet(); 70 jf.getContentPane().setLayout(new BorderLayout ()); 71 jf.getContentPane().add(ps, BorderLayout.CENTER); 72 jf.setLocation(30,30); 73 jf.setSize(500,500); 74 75 SwingUtilities.invokeAndWait(new Runnable () { 76 public void run() { 77 ps.setNodes(new Node[] {tn}); 78 jf.show(); 80 } 81 }); 82 83 85 jf.show(); 86 new ExtTestCase.WaitWindow(jf); 87 88 89 try { 90 92 } catch (Exception e) { 93 94 } 95 96 System.err.println("Current node set "); 97 try { 98 99 for (int i = 0; i < 10; i++) { 101 if (te.getAsText().equals("null")) { 102 System.err.println("Checking editor getAsText - " + te.getAsText()); 103 Thread.sleep(1000); 105 } else break; 106 } 107 109 initEditorValue = te.getAsText(); 110 System.err.println("Got initial editor value " + initEditorValue); 111 112 initPropertyValue = tp.getValue().toString(); 113 System.err.println("Got initial property value " + initPropertyValue); 114 115 116 tp.setValue("Test2"); 118 postChangePropertyValue = tp.getValue().toString(); 119 120 System.err.println("Post change property value is " + postChangePropertyValue); 121 122 123 for (int i = 0; i < 100; i++) { 125 if (te.getAsText().equals(initEditorValue)) { 126 Thread.sleep(50); 128 } else { 129 System.err.println("value was updated"); 130 break; 131 } 132 } 133 134 SwingUtilities.invokeAndWait(new Runnable () { 138 public void run() { 139 Graphics g = ps.getGraphics(); 140 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight()); 141 } 142 }); 143 144 postChangeEditorValue = te.getAsText(); 146 System.err.println("postEditorChangeValue = " + postChangeEditorValue); 147 148 } catch (Exception e) { 149 fail("FAILED - Exception thrown "+e.getClass().toString()); 150 } finally { 151 jf.hide(); 152 jf.dispose(); 153 } 154 } 155 156 public void testInitializeEditorValue() throws Exception { 157 assertTrue("Editor wasn't initialized successfuly (null) - value was " + initEditorValue,!initEditorValue.equals("null")); 158 } 159 160 public void testPropertyEQEditorValueAfterInit() throws Exception { 161 assertEquals("Editor was initialized to the same value as the Property, value was " + initPropertyValue, initPropertyValue, initEditorValue); 162 } 163 164 public void testSetPropertyValue() throws Exception { 165 assertTrue("Property value wasn't successfuly changed. Initial property value, " + initPropertyValue + " should not match " + postChangePropertyValue,!initPropertyValue.equals(postChangePropertyValue)); 166 } 167 168 public void testSetEditorValue() throws Exception { 169 assertTrue("Editor value wasn't changed successfuly. Initial editor value, " + initEditorValue + " should not match " + postChangeEditorValue,!initEditorValue.equals(postChangeEditorValue)); 170 } 171 172 public void testPropertyEQEditorValueAfterChange() throws Exception { 173 assertEquals("Editor value doesn't reflect the Property value. Post change property value, " + postChangePropertyValue + " should equal " + postChangeEditorValue, postChangePropertyValue, postChangeEditorValue); 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 protected Sheet createSheet() { 192 Sheet sheet = super.createSheet(); 193 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 195 if (props == null) { 196 props = Sheet.createPropertiesSet(); 197 sheet.put(props); 198 } 199 props.put(tp); 200 return sheet; 201 } 202 public void fireMethod(String s, Object o1, Object o2) { 204 System.err.println("TNode firing change " + s + " from " + o1 + " to " + o2); 205 firePropertyChange(s,o1,o2); 206 } 207 } 208 209 public class TProperty extends PropertySupport { 211 private Object myValue = "Value"; 212 public TProperty(String name, boolean isWriteable) { 214 super(name, Object .class, name, "", true, isWriteable); 215 } 216 public Object getValue() { 218 return myValue; 219 } 220 221 222 223 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 225 System.err.println("TProperty setValue: " + value); 226 Object oldVal = myValue; 227 myValue = value; 228 System.err.println("TProperty triggering node property change"); 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 System.err.println("Property change listener added to property editor " + System.identityHashCode(this) + " - " + l); 260 super.addPropertyChangeListener(l); 261 } 262 263 public void removePropertyChangeListener(PropertyChangeListener l) { 264 System.err.println("Property change listener removed from property editor " + System.identityHashCode(this) + " - " + l); 265 super.removePropertyChangeListener(l); 266 } 267 268 269 270 public void setValue(Object newValue) { 272 System.err.println("TEditor.setValue: " + newValue); 273 super.setValue(newValue); 274 } 275 276 public void firePropertyChange() { 277 System.err.println("TEditor.firePropertyChange"); 278 super.firePropertyChange(); 279 } 280 } 281 282 private static TNode tn; 283 private static TProperty tp; 284 private static TEditor te; 285 private static String initEditorValue; 286 private static String initPropertyValue; 287 private static String postChangePropertyValue; 288 private static String postChangeEditorValue; 289 } 290 | Popular Tags |