1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.Component ; 23 import java.awt.Point ; 24 import java.awt.Rectangle ; 25 import java.awt.event.MouseEvent ; 26 import java.beans.PropertyEditor ; 27 import java.beans.PropertyEditorSupport ; 28 import java.lang.reflect.InvocationTargetException ; 29 import javax.swing.JComboBox ; 30 import javax.swing.JComponent ; 31 import javax.swing.JFrame ; 32 import javax.swing.RepaintManager ; 33 import javax.swing.SwingUtilities ; 34 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow; 35 import org.openide.nodes.AbstractNode; 36 import org.openide.nodes.Children; 37 import org.openide.nodes.Node; 38 import org.openide.nodes.PropertySupport; 39 import org.openide.nodes.Sheet; 40 41 public class TagsAndEditorsTest extends ExtTestCase { 43 private PropertySheet ps = null; 44 JFrame jf = null; 45 46 public TagsAndEditorsTest(String name) { 47 super(name); 48 } 49 50 static { 51 installCorePropertyEditors(); 52 } 53 54 59 protected void setUp() throws Exception { 60 PropUtils.forceRadioButtons = false; 61 jf = new JFrame (); 62 try { 64 PropUtils.forceRadioButtons=false; 65 ps = new PropertySheet(); 66 sleep(); 68 69 setSortingMode(ps, PropertySheet.UNSORTED); 70 71 jf.getContentPane().add(ps); 72 jf.setLocation(20,20); 73 jf.setSize(300, 400); 74 75 new WaitWindow(jf); 76 } catch (Exception e) { 77 e.printStackTrace(); 78 fail("FAILED - Exception thrown "+e.getClass().toString()); 79 } 80 } 81 82 static { 83 System.err.println("CLASSPATH: " + System.getProperty("java.class.path")); 84 System.setProperty("org.openide.explorer.propertysheet.ComboInplaceEditor", "1"); 85 } 86 public void testEditableEmptyTagEditor() throws Exception { 87 if (!canSafelyRunFocusTests()) { 88 return; 89 } 90 Node n = new TNode(new EditableEmptyTagsEditor()); 91 setCurrentNode(n,ps); 92 requestFocus(ps); 93 clickCell(ps.table, 1, 1); 94 Component c = focusComp(); 95 assertTrue("Clicking on an editable property that returns a 1 element " + 96 "array from getTags() should send focus to a combo box's child editor component not " + focusComp(), 97 c.getParent() instanceof JComboBox ); 98 } 99 100 public void testSingleTagEditor() throws Exception { 101 if (!canSafelyRunFocusTests()) { 102 return; 103 } 104 Node n = new TNode(new SingleTagEditor()); 105 setCurrentNode(n, ps); 106 clickCell(ps.table, 1, 1); 107 assertTrue("Clicking on an editable property that returns a 1 element " + 108 "array from getTags() should send focus to a combo boxnot a " + focusComp(), 109 focusComp() instanceof JComboBox ); 110 } 111 112 public void testEditableSingleTagEditor() throws Exception { 113 if (!canSafelyRunFocusTests()) { 114 return; 115 } 116 Node n = new TNode(new EditableSingleTagEditor()); 117 setCurrentNode(n, ps); 118 clickCell(ps.table, 1, 1); 119 Component c = focusComp(); 120 assertTrue("Clicking on an editable property that returns a 1 element " + 121 "array from getTags() should send focus to a combo box's child editor component", 122 c.getParent() instanceof JComboBox ); 123 } 124 125 public void testEmptyTagEditor() throws Exception { 126 if (!canSafelyRunFocusTests()) { 127 return; 128 } 129 requestFocus(ps); 130 Node n = new TNode(new EmptyTagsEditor()); 131 setCurrentNode(n, ps); 132 clickCell(ps.table, 1, 1); 133 assertTrue("Clicking on an editable property that returns a 0 " + 134 "length array from getTags() should send focus to a combo box", 135 focusComp() instanceof JComboBox ); 136 } 137 138 public void testPropertyMarkingAlignment() throws Exception { 139 if (!canSafelyRunFocusTests()) { 140 return; 141 } 142 Node n = new TNode(new PropertyEditor []{ 143 new BadEditorWithTags(), 144 new BadEditorWithoutTags() 145 }); 146 setCurrentNode(n, ps); 147 } 148 149 public void testPropertySheetRepaintsCellOnPropertyChange() throws Exception { 150 if (!canSafelyRunFocusTests()) { 151 return; 152 } 153 Node n = new TNode(new SingleTagEditor()); 154 setCurrentNode(n, ps); 155 Rectangle test = ps.table.getCellRect(1, 1, true); 156 RM rm = new RM(test, ps.table); 157 RepaintManager.setCurrentManager(rm); 158 sleep(); 159 sleep(); 160 Node.Property prop = n.getPropertySets()[0].getProperties()[0]; 161 prop.setValue("new value"); 162 Thread.currentThread().sleep(1000); 163 sleep(); 164 rm.assertRectRepainted(); 165 } 166 167 private class RM extends RepaintManager { 168 private Rectangle rect; 169 private Rectangle repaintedRect = null; 170 private JComponent repaintedComponent = null; 171 private JComponent target = null; 172 public RM(Rectangle rect, JComponent target) { 173 this.rect = rect; 174 this.target = target; 175 } 176 177 public void assertRectRepainted() { 178 assertNotNull("No component repainted", repaintedComponent); 179 assertSame("Wrong component repainted:" + repaintedComponent, repaintedComponent, target); 180 assertEquals("Wrong rectangle repainted:" + repaintedRect + " should be " + rect, rect, repaintedRect); 181 } 182 183 public synchronized void addDirtyRegion(JComponent c, int x, int y, int w, int h) { 184 super.addDirtyRegion(c, x, y, w, h); 185 if (repaintedComponent == null) { 186 repaintedComponent = c; 187 repaintedRect = new Rectangle (x, y, w, h); 188 } 189 } 190 } 191 192 private void clickCell(final SheetTable tb, final int row, final int col) throws Exception { 193 SwingUtilities.invokeAndWait(new Runnable () { 194 public void run() { 195 Rectangle r = tb.getCellRect(row, col, false); 196 Point toClick = r.getLocation(); 197 toClick.x += 15; 198 toClick.y +=3; 199 MouseEvent me = new MouseEvent (tb, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 200 tb.dispatchEvent(me); 201 } 202 }); 203 sleep(); 204 } 205 206 207 public class TNode extends AbstractNode { 209 private PropertyEditor [] ed; 210 public TNode(PropertyEditor [] ed) { 212 super(Children.LEAF); 213 setName("TNode"); setDisplayName("TNode"); 215 this.ed = ed; 216 } 217 218 public TNode(PropertyEditor ed) { 219 this(new PropertyEditor [] {ed}); 220 } 221 222 public Node cloneNode() { 224 return new TNode(ed); 225 } 226 227 protected Sheet createSheet() { 229 Sheet sheet = super.createSheet(); 230 Sheet.Set props = sheet.get(Sheet.PROPERTIES); 232 if (props == null) { 233 props = Sheet.createPropertiesSet(); 234 sheet.put(props); 235 } 236 for (int i=0; i < ed.length; i++) { 237 props.put(new TProperty(this, ed[i], true)); 238 } 239 return sheet; 240 } 241 public void fireMethod(String s, Object o1, Object o2) { 243 firePropertyChange(s,o1,o2); 244 } 245 } 246 247 private String stripClassName(Object o) { 248 String s = o.getClass().getName(); 249 int idx = s.indexOf('$'); 250 return s.substring(idx+1); 251 } 252 253 private static int ct = 0; 254 public class TProperty extends PropertySupport { 256 private Object myValue = "Value"; 257 private PropertyEditor ed; 258 private Node n; 259 260 public TProperty(Node n, PropertyEditor ed, boolean writable) { 261 super(stripClassName(ed) + "-" + (ct++), String .class, stripClassName(ed) + ct, "", true, writable); 262 this.ed = ed; 263 this.n = n; 264 } 265 266 public Object getValue() { 267 return myValue; 268 } 269 270 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 271 Object oldVal = myValue; 272 myValue = value; 273 ((TNode)n).fireMethod(getName(), oldVal, myValue); 274 } 275 276 public PropertyEditor getPropertyEditor() { 277 return ed; 278 } 279 } 280 281 282 private static Exception throwMe=null; 283 private synchronized void setSortingMode(final PropertySheet ps, final int mode) throws Exception { 284 throwMe = null; 285 SwingUtilities.invokeAndWait(new Runnable () { 286 public void run() { 287 try { 288 ps.setSortingMode(mode); 289 } catch (Exception e) { 290 throwMe = e; 291 } 292 } 293 }); 294 if (throwMe != null) { 295 Exception ex = throwMe; 296 throwMe = null; 297 throw (throwMe); 298 } 299 } 300 301 public static class SingleTagEditor extends PropertyEditorSupport implements ExPropertyEditor { 302 PropertyEnv env; 303 304 public SingleTagEditor() { 305 } 306 307 public String [] getTags() { 308 return new String []{"lonely tag"}; 309 } 310 311 public void attachEnv(PropertyEnv env) { 312 this.env = env; 313 } 314 315 public boolean supportsCustomEditor() { 316 return false; 317 } 318 319 public void setValue(Object newValue) { 320 super.setValue(newValue); 321 } 322 } 323 324 public static class EditableSingleTagEditor extends PropertyEditorSupport implements ExPropertyEditor { 325 PropertyEnv env; 326 327 public EditableSingleTagEditor() { 328 } 329 330 public String [] getTags() { 331 return new String []{"lonely tag"}; 332 } 333 334 public void attachEnv(PropertyEnv env) { 335 this.env = env; 336 env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE); 337 } 338 339 public boolean supportsCustomEditor() { 340 return false; 341 } 342 343 public void setValue(Object newValue) { 344 super.setValue(newValue); 345 } 346 } 347 348 public static class EmptyTagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 349 PropertyEnv env; 350 351 public EmptyTagsEditor() { 352 } 353 354 public String [] getTags() { 355 return new String [0]; 356 } 357 358 public void attachEnv(PropertyEnv env) { 359 this.env = env; 360 } 361 362 public boolean supportsCustomEditor() { 363 return false; 364 } 365 366 public void setValue(Object newValue) { 367 super.setValue(newValue); 368 } 369 } 370 371 public static class EditableEmptyTagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 372 PropertyEnv env; 373 374 public EditableEmptyTagsEditor() { 375 } 376 377 public String [] getTags() { 378 return new String [0]; 379 } 380 381 public void attachEnv(PropertyEnv env) { 382 this.env = env; 383 env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE); 384 } 385 386 public boolean supportsCustomEditor() { 387 return false; 388 } 389 390 public void setValue(Object newValue) { 391 super.setValue(newValue); 392 } 393 } 394 395 396 public static class BadEditorWithoutTags extends PropertyEditorSupport implements ExPropertyEditor { 397 PropertyEnv env; 398 399 public BadEditorWithoutTags() { 400 } 401 402 public String [] getTags() { 403 return null; 404 } 405 406 public void attachEnv(PropertyEnv env) { 407 this.env = env; 408 env.setState(env.STATE_INVALID); 409 } 410 411 public boolean supportsCustomEditor() { 412 return false; 413 } 414 415 public void setValue(Object newValue) { 416 super.setValue(newValue); 417 } 418 419 public Object getValue() { 420 return Boolean.FALSE; 421 } 422 } 423 424 public static class BadEditorWithTags extends PropertyEditorSupport implements ExPropertyEditor { 425 PropertyEnv env; 426 427 public BadEditorWithTags() { 428 } 429 430 public String [] getTags() { 431 return new String [] {"a","b","c","d","Value"}; 432 } 433 434 public void attachEnv(PropertyEnv env) { 435 this.env = env; 436 env.setState(env.STATE_INVALID); 437 } 438 439 public boolean supportsCustomEditor() { 440 return false; 441 } 442 443 public void setValue(Object newValue) { 444 super.setValue(newValue); 445 } 446 447 public Object getValue() { 448 return Boolean.FALSE; 449 } 450 } 451 } 452 | Popular Tags |