1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Graphics ; 25 import java.awt.GraphicsEnvironment ; 26 import java.awt.Image ; 27 import java.awt.KeyboardFocusManager ; 28 import java.awt.Rectangle ; 29 import java.awt.Transparency ; 30 import java.awt.event.ActionEvent ; 31 import java.awt.event.KeyEvent ; 32 import java.awt.image.BufferedImage ; 33 import java.awt.image.ColorModel ; 34 import java.beans.PropertyChangeListener ; 35 import java.beans.PropertyEditor ; 36 import java.beans.PropertyEditorManager ; 37 import java.beans.PropertyEditorSupport ; 38 import java.lang.reflect.InvocationTargetException ; 39 import javax.swing.AbstractAction ; 40 import javax.swing.Icon ; 41 import javax.swing.ImageIcon ; 42 import javax.swing.JComponent ; 43 import javax.swing.JFrame ; 44 import javax.swing.JTextField ; 45 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow; 46 import org.openide.nodes.AbstractNode; 47 import org.openide.nodes.Children; 48 import org.openide.nodes.Node; 49 import org.openide.nodes.PropertySupport; 50 import org.openide.nodes.Sheet; 51 52 56 public class PropertyMarkingTest extends GraphicsTestCase { 57 private static boolean setup=false; 58 59 static { 60 String [] syspesp = PropertyEditorManager.getEditorSearchPath(); 61 String [] nbpesp = new String [] { 62 "org.netbeans.beaninfo.editors", "org.openide.explorer.propertysheet.editors", }; 65 String [] allpesp = new String [syspesp.length + nbpesp.length]; 66 System.arraycopy(nbpesp, 0, allpesp, 0, nbpesp.length); 67 System.arraycopy(syspesp, 0, allpesp, nbpesp.length, syspesp.length); 68 PropertyEditorManager.setEditorSearchPath(allpesp); 69 } 70 71 public PropertyMarkingTest(String name) { 72 super(name); 73 } 74 75 static SheetTable tb=null; 76 static JFrame jf=null; 77 82 protected void setUp() throws Exception { 83 if (setup) return; 84 PropUtils.forceRadioButtons=false; 85 86 try { 87 88 tp = new TProperty("oh", true); 89 tp1 = new TProperty2("the", true); 90 tp2 = new TProperty2("pretty", true); 91 tp3 = new TProperty2("pictures",true); 92 tp4 = new TProperty3("I can create",true); 93 postSetAction = new PostSetAction(); 94 95 tn = new TNode(); 96 97 final PropertySheet ps = new PropertySheet(); 98 99 ps.setCurrentNode(tn); 101 sleep(); 102 ps.setSortingMode(PropertySheet.UNSORTED); 103 104 jf = new JFrame (); 105 jf.getContentPane().add(ps); 106 jf.setLocation(20,20); 107 jf.setSize(300, 400); 108 new WaitWindow(jf); 109 tb = ps.table; 110 111 ps.setSortingMode(ps.SORTED_BY_NAMES); 112 jf.repaint(); 113 114 115 } catch (Exception e) { 116 e.printStackTrace(); 117 fail("FAILED - Exception thrown "+e.getClass().toString()); 118 } finally { 119 setup = true; 120 } 121 } 122 123 public void testPostSetAction() throws Exception { 124 if (!canSafelyRunFocusTests()) { 125 System.err.println("Cannot run post set " + 126 "action test - testing machine's focus behavior is not sane"); 127 return; 128 } 129 pressCell(tb, 1, 1); 130 131 sleep(); 132 requestFocus(tb); 133 System.err.println("WILL TYPE MIAOU"); 134 JComponent focusOwner = (JComponent )KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 135 assertTrue("Focus should be on a JTextField, not " + focusOwner, focusOwner instanceof JTextField ); 136 137 typeString("MIAOU", focusOwner); 138 139 sleep(); 140 sleep(); 141 System.err.println("MIAOU TYPED"); 142 143 postSetAction.assertNotPerformed(); 144 System.err.println("ACTION NOT PERFORMED AS IT SHOULD BE"); 145 146 pressKey(tb, KeyEvent.VK_ENTER); 147 System.err.println("SENDING ENTER TO " + tb); 148 Thread.currentThread().sleep(1000); 149 150 sleep(); 151 152 System.err.println("TYPING ENTER TO " + tb); 153 typeKey(tb, KeyEvent.VK_ENTER); 154 sleep(); 155 sleep(); 156 157 System.err.println("POST SET ACTION PERFORMED: " + postSetAction.performed); 158 postSetAction.assertPerformed(); 159 click(tb, 1, 0); 160 } 161 162 195 196 197 public class TNode extends AbstractNode { 199 public TNode() { 201 super(Children.LEAF); 202 setName("TNode"); setDisplayName("TNode"); 204 } 205 public Node cloneNode() { 207 return new TNode(); 208 } 209 210 public void addProp(Node.Property p) { 211 props.put(p); 212 this.firePropertyChange(PROP_PROPERTY_SETS, null, null); 213 this.firePropertySetsChange(null, null); 214 } 215 216 Sheet sheet=null; 217 Sheet.Set props=null; 218 protected Sheet createSheet() { 220 sheet = super.createSheet(); 221 props = sheet.get(Sheet.PROPERTIES); 223 if (props == null) { 224 props = Sheet.createPropertiesSet(); 225 sheet.put(props); 226 } 227 props.put(tp); 228 props.put(tp1); 229 props.put(tp2); 230 props.put(tp3); 231 props.put(tp4); 232 return sheet; 233 } 234 public void fireMethod(String s, Object o1, Object o2) { 236 firePropertyChange(s,o1,o2); 237 } 238 } 239 240 public class TProperty extends PropertySupport { 242 private Object myValue = "Value"; 243 public TProperty(String name, boolean isWriteable) { 245 super(name, String .class, name, "", true, isWriteable); 246 } 247 public Object getValue() { 249 return myValue; 250 } 251 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 253 Object oldVal = myValue; 254 myValue = value; 255 tn.fireMethod(getName(), oldVal, myValue); 256 } 257 public PropertyEditor getPropertyEditor() { 259 return new TEditor(); 260 } 261 262 public Object getValue(String key) { 263 if ("nameIcon".equals(key)) { 264 return new NameIcon(); 265 } else if ("valueIcon".equals(key)) { 266 return new ValueIcon(); 267 } else if ("postSetAction".equals(key)) { 268 return postSetAction; 269 } 270 return super.getValue(key); 271 } 272 } 273 274 public class TEditor extends PropertyEditorSupport { 276 PropertyEnv env; 277 278 public TEditor() { 280 } 281 282 286 public void attachEnv(PropertyEnv env) { 287 this.env = env; 288 } 289 290 public boolean supportsCustomEditor() { 292 return false; 293 } 294 295 public void setValue(Object newValue) { 297 super.setValue(newValue); 298 } 299 300 public String getAsText() { 301 return getValue() == null ? "null" : getValue().toString(); 302 } 303 } 304 305 306 public class TagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 307 PropertyEnv env; 308 309 public TagsEditor() { 310 } 311 312 public String [] getTags() { 313 return new String [] {"a","b","c","d","Value"}; 314 } 315 316 public void attachEnv(PropertyEnv env) { 317 this.env = env; 318 env.setState(env.STATE_INVALID); 319 } 320 321 public boolean supportsCustomEditor() { 322 return false; 323 } 324 325 public void setValue(Object newValue) { 326 super.setValue(newValue); 327 } 328 } 329 330 public class TProperty2 extends PropertySupport { 332 private Object myValue = "Value"; 333 public TProperty2(String name, boolean isWriteable) { 335 super(name, Object .class, name, "", true, isWriteable); 336 } 337 public Object getValue() { 339 return myValue; 340 } 341 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 343 Object oldVal = myValue; 344 myValue = value; 345 tn.fireMethod(getName(), oldVal, myValue); 346 } 347 public PropertyEditor getPropertyEditor() { 349 return new TagsEditor(); 350 } 351 } 352 353 public class TProperty3 extends PropertySupport { 355 private Boolean myValue = Boolean.FALSE; 356 public TProperty3(String name, boolean isWriteable) { 358 super(name, Boolean .class, name, "", true, isWriteable); 359 } 360 public Object getValue() { 362 return myValue; 363 } 364 365 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 367 Object oldVal = myValue; 368 myValue = (Boolean ) value; 369 tn.fireMethod(getName(), oldVal, myValue); 370 } 371 372 public Object getValue(String key) { 373 if ("nameIcon".equals(key)) { 374 return new NameIcon(); 375 } else if ("valueIcon".equals(key)) { 376 return new ValueIcon(); 377 } else if ("postSetAction".equals(key)) { 378 return postSetAction; 379 } 380 return super.getValue(key); 381 } 382 383 public PropertyEditor getPropertyEditor() { 384 return new WrapperEx(super.getPropertyEditor()); 385 } 386 387 public class WrapperEx implements ExPropertyEditor { 388 private PropertyEditor orig; 389 public WrapperEx(PropertyEditor orig) { 390 this.orig = orig; 391 } 392 393 394 public void attachEnv(PropertyEnv env) { 395 env.setState(myValue == Boolean.FALSE ? env.STATE_INVALID : env.STATE_VALID); 396 } 397 398 public void addPropertyChangeListener(PropertyChangeListener listener) { 399 orig.addPropertyChangeListener(listener); 400 } 401 402 public String getAsText() { 403 return orig.getAsText(); 404 } 405 406 public Component getCustomEditor() { 407 return orig.getCustomEditor(); 408 } 409 410 public String getJavaInitializationString() { 411 return orig.getJavaInitializationString(); 412 } 413 414 public String [] getTags() { 415 return orig.getTags(); 416 } 417 418 public Object getValue() { 419 return orig.getValue(); 420 } 421 422 public boolean isPaintable() { 423 return orig.isPaintable(); 424 } 425 426 public void paintValue(Graphics gfx, Rectangle box) { 427 orig.paintValue(gfx, box); 428 } 429 430 public void removePropertyChangeListener(PropertyChangeListener listener) { 431 orig.removePropertyChangeListener(listener); 432 } 433 434 public void setAsText(String text) throws IllegalArgumentException { 435 orig.setAsText(text); 436 } 437 438 public void setValue(Object value) { 439 orig.setValue(value); 440 } 441 442 public boolean supportsCustomEditor() { 443 return true; 444 } 445 } 446 } 447 448 public class BadEditor extends PropertyEditorSupport implements ExPropertyEditor { 449 PropertyEnv env; 450 451 public BadEditor() { 452 } 453 454 public String [] getTags() { 455 return null; 457 } 458 459 public void attachEnv(PropertyEnv env) { 460 this.env = env; 461 env.setState(env.STATE_INVALID); 462 } 463 464 public boolean supportsCustomEditor() { 465 return true; 466 } 467 468 public void setValue(Object newValue) { 469 super.setValue(newValue); 470 } 471 } 472 473 private class NameIcon implements Icon { 474 475 public int getIconHeight() { 476 return 12; 477 } 478 479 public int getIconWidth() { 480 return 12; 481 } 482 483 public void paintIcon(Component c, Graphics g, int x, int y) { 484 Color col = g.getColor(); 485 try { 486 g.setColor(Color.BLUE); 487 g.drawRect(x, y, getIconWidth(), getIconHeight()); 488 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5); 489 } finally { 490 g.setColor(col); 491 } 492 } 493 494 } 495 496 private class ValueIcon implements Icon { 497 498 public int getIconHeight() { 499 return 12; 500 } 501 502 public int getIconWidth() { 503 return 12; 504 } 505 506 public void paintIcon(Component c, Graphics g, int x, int y) { 507 Color col = g.getColor(); 508 try { 509 g.setColor(Color.GREEN); 510 g.drawRect(x, y, getIconWidth(), getIconHeight()); 511 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5); 512 } finally { 513 g.setColor(col); 514 } 515 } 516 } 517 518 private class PostSetAction extends AbstractAction { 519 boolean performed = false; 520 521 public void assertPerformed() { 522 assertTrue("Action was not performed", performed); 523 performed = false; 524 } 525 526 public void assertNotPerformed() { 527 assertTrue("Action should not be performed before an appropriate event is triggered", !performed); 528 } 529 530 public void actionPerformed(ActionEvent e) { 531 performed = true; 532 } 533 534 } 535 536 537 private static TNode tn; 538 private static TProperty tp; 539 private static TProperty2 tp1; 540 private static TProperty2 tp2; 541 private static TProperty2 tp3; 542 private static TProperty3 tp4; 543 private static TEditor te; 544 private static PostSetAction postSetAction; 545 private static String initEditorValue; 546 private static String initPropertyValue; 547 private static String postChangePropertyValue; 548 private static String postChangeEditorValue; 549 550 private static final BufferedImage toBufferedImage(Image img) { 552 new ImageIcon (img); 554 BufferedImage rep = createBufferedImage(img.getWidth(null), img.getHeight(null)); 555 Graphics g = rep.createGraphics(); 556 g.drawImage(img, 0, 0, null); 557 g.dispose(); 558 img.flush(); 559 return rep; 560 } 561 562 563 private static final BufferedImage createBufferedImage(int width, int height) { 564 ColorModel model = GraphicsEnvironment.getLocalGraphicsEnvironment(). 565 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(Transparency.BITMASK); 566 BufferedImage buffImage = new BufferedImage (model, 567 model.createCompatibleWritableRaster(width, height), model.isAlphaPremultiplied(), null); 568 return buffImage; 569 } 570 571 572 573 } 574 | Popular Tags |