1 19 20 25 26 package org.openide.explorer.propertysheet; 27 28 import java.awt.Color ; 29 import java.awt.Component ; 30 import java.awt.DisplayMode ; 31 import java.awt.Font ; 32 import java.awt.Graphics ; 33 import java.awt.GraphicsEnvironment ; 34 import java.awt.Image ; 35 import java.awt.KeyboardFocusManager ; 36 import java.awt.Point ; 37 import java.awt.Rectangle ; 38 import java.awt.event.KeyEvent ; 39 import java.awt.event.MouseEvent ; 40 import java.awt.event.WindowAdapter ; 41 import java.awt.event.WindowEvent ; 42 import java.awt.image.BufferedImage ; 43 import org.openide.*; 44 import org.openide.nodes.*; 45 import org.openide.explorer.propertysheet.*; 46 import org.openide.explorer.propertysheet.editors.*; 47 import java.beans.*; 48 import java.lang.reflect.*; 49 import javax.swing.*; 50 import javax.swing.ImageIcon ; 51 import junit.framework.*; 52 import junit.textui.TestRunner; 53 import org.netbeans.junit.*; 54 import org.openide.util.Lookup; 55 import org.openide.util.Utilities; 56 57 58 62 public class ComboTest extends NbTestCase { 63 private static boolean setup=false; 64 65 static { 66 registerPropertyEditors(); 67 } 68 69 public static void registerPropertyEditors () { 70 } 72 73 public ComboTest(String name) { 74 super(name); 75 } 76 77 static SheetTable tb=null; 78 static JFrame jf=null; 79 84 protected void setUp() throws Exception { 85 if (setup) return; 86 87 try { 88 89 tp = new TProperty("oh", true); 90 tp1 = new TProperty2("the", true); 91 postSetAction = new PostSetAction(); 92 93 tn = new TNode(); 94 final PropertySheet ps = new PropertySheet(); 96 97 ps.setCurrentNode(tn); 99 sleep(); 100 ps.setSortingMode(PropertySheet.UNSORTED); 101 102 jf = new JFrame(); 103 jf.getContentPane().add (ps); 104 jf.setLocation (20,20); 105 jf.setSize (300, 400); 106 new WaitWindow(jf); 107 tb = ps.table; 108 109 SwingUtilities.invokeLater(new Runnable () { 110 public void run() { 111 try { 112 ps.setSortingMode(ps.SORTED_BY_NAMES); 113 }catch (Exception e){} 114 } 115 }); 116 117 118 119 } catch (Exception e) { 120 e.printStackTrace(); 121 fail("FAILED - Exception thrown "+e.getClass().toString()); 122 } finally { 123 setup = true; 124 } 125 } 126 127 public void tearDown() { 128 jf.hide(); 129 jf.dispose(); 130 } 131 132 133 134 static boolean checkGraphicsEnvironment() { 135 if (GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) { 136 System.err.println("Cannot run test in a headless environment"); 137 } 138 DisplayMode dm = 139 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode(); 140 int i = dm.getBitDepth(); 141 if (i == dm.BIT_DEPTH_MULTI || i >= 16) { 142 return true; 143 } 144 return false; 145 } 146 147 public void testFoo () throws Exception { 148 } 149 150 151 152 private static class WaitWindow extends WindowAdapter { 153 boolean shown=false; 154 public WaitWindow (JFrame f) { 155 f.addWindowListener(this); 156 f.show(); 157 if (!shown) { 158 synchronized(this) { 159 try { 160 wait(5000); 161 } catch (Exception e) {} 162 } 163 } 164 } 165 166 public void windowOpened(WindowEvent e) { 167 shown = true; 168 synchronized(this) { 169 notifyAll(); 170 ((JFrame) e.getSource()).removeWindowListener(this); 171 } 172 } 173 } 174 175 private static final int SLEEP_LENGTH=1000; 176 private void sleep() { 177 179 try { 180 SwingUtilities.invokeAndWait(new Runnable () { 183 public void run() { 184 System.currentTimeMillis(); 185 } 186 }); 187 SwingUtilities.invokeAndWait(new Runnable () { 189 public void run() { 190 System.currentTimeMillis(); 191 } 192 }); 193 } catch (Exception e) { 194 } 195 196 197 } 198 199 200 private static Color checkColor=null; 201 private Exception throwMe=null; 202 203 private static int count=0; 204 206 private synchronized void assertPixelFromImage(final Image i, final Component c, final int imageX, final int imageY, final int compX, final int compY) throws Exception { 207 final BufferedImage bi = i instanceof BufferedImage ? (BufferedImage ) i : toBufferedImage(i); 208 throwMe = null; 209 sleep(); 210 211 int rgb = bi.getRGB(imageX, imageY); 212 Color color = new Color (rgb); 213 214 215 JFrame jf = new JFrame("assertPixelFromImage " + count + " (look for the yellow line)") { 218 public void paint (Graphics g) { 219 new ImageIcon (bi).paintIcon(this, g, 25, 25); 220 g.setColor (Color.YELLOW); 221 g.drawLine(imageX+20, imageY+25, imageX+25, imageY+25); 222 } 223 }; 224 jf.setLocation (500,500); 225 jf.setSize (100,100); 226 jf.show(); 227 228 try { 229 assertPixel(c, color, compX, compY); 230 } catch (Exception e) { 231 throwMe = e; 232 } 233 if (throwMe != null) { 234 throw throwMe; 235 } 236 } 237 238 private Exception throwMe2=null; 239 private synchronized void assertPixel(final Component c, final Color toMatch, final int x, final int y) throws Exception { 240 sleep(); 241 throwMe2 = null; 242 if (true) { 243 doAssertPixel(c, toMatch, x, y); 244 return; 245 } 246 SwingUtilities.invokeAndWait(new Runnable () { 247 public void run() { 248 try { 249 doAssertPixel(c, toMatch, x, y); 250 } catch (Exception e) { 251 throwMe2 = e; 252 } 253 } 254 }); 255 if (throwMe2 != null) { 256 throw throwMe2; 257 } 258 } 259 260 private synchronized void doAssertPixel(final Component c, final Color toMatch, final int x, final int y) throws Exception { 261 final BufferedImage bi = new BufferedImage (700, 700, BufferedImage.TYPE_INT_RGB); 262 263 sleep(); 264 ((JComponent) c).paintAll(bi.getGraphics()); 265 sleep(); 266 int[] cArr = new int[3]; 267 bi.getData().getPixel(x, y, cArr); 268 checkColor = new Color (cArr[0], cArr[1], cArr[2]); 269 270 271 JFrame jf = new JFrame("Assert pixel test " + count + " (look for the yellow line)") { 274 public void paint (Graphics g) { 275 new ImageIcon (bi).paintIcon(this, g, 25, 25); 276 g.setColor (Color.YELLOW); 277 g.drawLine(x+20, y+25, x+25, y+25); 278 } 279 }; 280 jf.setLocation (400,400); 281 jf.setSize (500,500); 282 jf.show(); 283 count++; 284 285 assertEquals("Color at " + x + "," + y + " does not match", toMatch, checkColor); 286 287 } 288 289 290 private void clickOn (final SheetTable tb, final int row, final int col) throws Exception { 291 SwingUtilities.invokeAndWait (new Runnable () { 292 public void run() { 293 Rectangle r = tb.getCellRect(row, col, false); 294 Point toClick = r.getLocation(); 295 toClick.x += 15; 296 toClick.y +=3; 297 MouseEvent me = new MouseEvent (tb, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 298 tb.dispatchEvent(me); 299 } 300 }); 301 sleep(); 302 } 303 304 private void releaseKey (final Component target, final int key) throws Exception { 305 SwingUtilities.invokeAndWait(new Runnable () { 306 public void run() { 307 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key); 308 target.dispatchEvent(ke); 309 } 310 }); 311 sleep(); 312 } 313 314 private void pressKey (final Component target, final int key) throws Exception { 315 SwingUtilities.invokeAndWait(new Runnable () { 316 public void run() { 317 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key); 318 target.dispatchEvent(ke); 319 } 320 }); 321 sleep(); 322 } 323 324 private void typeKey (final Component target, final int key) throws Exception { 325 SwingUtilities.invokeAndWait(new Runnable () { 326 public void run() { 327 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key); 328 target.dispatchEvent(ke); 329 } 330 }); 331 sleep(); 332 } 333 334 public class TNode extends AbstractNode { 336 public TNode() { 338 super (Children.LEAF); 339 setName("TNode"); setDisplayName("TNode"); 341 } 342 public Node cloneNode() { 344 return new TNode(); 345 } 346 347 public void addProp (Node.Property p) { 348 props.put(p); 349 this.firePropertyChange(PROP_PROPERTY_SETS, null, null); 350 this.firePropertySetsChange(null, null); 351 } 352 353 Sheet sheet=null; 354 Sheet.Set props=null; 355 protected Sheet createSheet() { 357 sheet = super.createSheet(); 358 props = sheet.get(Sheet.PROPERTIES); 360 if (props == null) { 361 props = Sheet.createPropertiesSet(); 362 sheet.put(props); 363 } 364 props.put(tp); 365 props.put(tp1); 366 return sheet; 367 } 368 public void fireMethod(String s, Object o1, Object o2) { 370 firePropertyChange(s,o1,o2); 371 } 372 } 373 374 public static void main(String args[]) { 375 LookAndFeel lf = UIManager.getLookAndFeel(); 376 TestRunner.run(suite ()); 377 } 378 379 public static Test suite() { 380 return new ComboTestSuite (); 381 } 382 383 private static final class ComboTestSuite extends NbTestSuite { 384 public ComboTestSuite () { 385 super (ComboTest.class); 386 } 387 388 public void run (final TestResult tr) { 389 super.run (tr); 390 } 391 } 392 393 public class TProperty extends PropertySupport { 395 private Object myValue = "Value"; 396 public TProperty(String name, boolean isWriteable) { 398 super(name, String .class, name, "", true, isWriteable); 399 } 400 public Object getValue() { 402 return myValue; 403 } 404 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 406 Object oldVal = myValue; 407 myValue = value; 408 tn.fireMethod(getName(), oldVal, myValue); 409 } 410 public PropertyEditor getPropertyEditor() { 412 return new TEditor(); 413 } 414 415 public Object getValue(String key) { 416 if ("nameIcon".equals(key)) { 417 return new NameIcon(); 418 } else if ("valueIcon".equals(key)) { 419 return new ValueIcon(); 420 } else if ("postSetAction".equals(key)) { 421 return postSetAction; 422 } 423 return super.getValue(key); 424 } 425 } 426 427 public class TEditor extends PropertyEditorSupport { 429 PropertyEnv env; 430 431 public TEditor() { 433 } 434 435 439 public void attachEnv(PropertyEnv env) { 440 this.env = env; 441 } 442 443 public boolean supportsCustomEditor() { 445 return false; 446 } 447 448 public void setValue(Object newValue) { 450 super.setValue(newValue); 451 } 452 453 public String getAsText() { 454 return getValue() == null ? "null" : getValue().toString(); 455 } 456 } 457 458 459 public class TagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 460 PropertyEnv env; 461 462 public TagsEditor() { 463 } 464 465 public String [] getTags() { 466 return new String [] {"a","b","c","d","Value"}; 467 } 468 469 public void attachEnv(PropertyEnv env) { 470 this.env = env; 471 472 env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE); 473 } 474 475 public boolean supportsCustomEditor() { 476 return false; 477 } 478 479 public void setValue(Object newValue) { 480 super.setValue(newValue); 481 } 482 } 483 484 public class TProperty2 extends PropertySupport { 486 private Object myValue = "Value"; 487 public TProperty2(String name, boolean isWriteable) { 489 super(name, Object .class, name, "", true, isWriteable); 490 } 491 public Object getValue() { 493 return myValue; 494 } 495 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 497 Object oldVal = myValue; 498 myValue = value; 499 tn.fireMethod(getName(), oldVal, myValue); 500 } 501 public PropertyEditor getPropertyEditor() { 503 return new TagsEditor(); 504 } 505 506 public Object getValue(String key) { 507 if ("canEditAsText".equals(key)) { 508 return Boolean.TRUE; 509 } else { 510 return super.getValue(key); 511 } 512 } 513 } 514 515 public class TProperty3 extends PropertySupport { 517 private Boolean myValue = Boolean.FALSE; 518 public TProperty3(String name, boolean isWriteable) { 520 super(name, Boolean .class, name, "", true, isWriteable); 521 } 522 public Object getValue() { 524 return myValue; 525 } 526 527 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 529 Object oldVal = myValue; 530 myValue = (Boolean ) value; 531 tn.fireMethod(getName(), oldVal, myValue); 532 } 533 534 public Object getValue(String key) { 535 if ("nameIcon".equals(key)) { 536 return new NameIcon(); 537 } else if ("valueIcon".equals(key)) { 538 return new ValueIcon(); 539 } else if ("postSetAction".equals(key)) { 540 return postSetAction; 541 } 542 return super.getValue(key); 543 } 544 545 public PropertyEditor getPropertyEditor() { 546 return new WrapperEx(super.getPropertyEditor()); 547 } 548 549 public class WrapperEx implements ExPropertyEditor { 550 private PropertyEditor orig; 551 public WrapperEx (PropertyEditor orig) { 552 this.orig = orig; 553 } 554 555 556 public void attachEnv(PropertyEnv env) { 557 env.setState(myValue == Boolean.FALSE ? env.STATE_INVALID : env.STATE_VALID); 558 } 559 560 public void addPropertyChangeListener(PropertyChangeListener listener) { 561 orig.addPropertyChangeListener(listener); 562 } 563 564 public String getAsText() { 565 return orig.getAsText(); 566 } 567 568 public java.awt.Component getCustomEditor() { 569 return orig.getCustomEditor(); 570 } 571 572 public String getJavaInitializationString() { 573 return orig.getJavaInitializationString(); 574 } 575 576 public String [] getTags() { 577 return orig.getTags(); 578 } 579 580 public Object getValue() { 581 return orig.getValue(); 582 } 583 584 public boolean isPaintable() { 585 return orig.isPaintable(); 586 } 587 588 public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) { 589 orig.paintValue(gfx, box); 590 } 591 592 public void removePropertyChangeListener(PropertyChangeListener listener) { 593 orig.removePropertyChangeListener(listener); 594 } 595 596 public void setAsText(String text) throws java.lang.IllegalArgumentException { 597 orig.setAsText(text); 598 } 599 600 public void setValue(Object value) { 601 orig.setValue(value); 602 } 603 604 public boolean supportsCustomEditor() { 605 return orig.supportsCustomEditor(); 606 } 607 } 608 } 609 610 public class BadEditor extends PropertyEditorSupport implements ExPropertyEditor { 611 PropertyEnv env; 612 613 public BadEditor() { 614 } 615 616 public String [] getTags() { 617 return null; 619 } 620 621 public void attachEnv(PropertyEnv env) { 622 this.env = env; 623 env.setState(env.STATE_INVALID); 624 } 625 626 public boolean supportsCustomEditor() { 627 return false; 628 } 629 630 public void setValue(Object newValue) { 631 super.setValue(newValue); 632 } 633 } 634 635 private class NameIcon implements Icon { 636 637 public int getIconHeight() { 638 return 12; 639 } 640 641 public int getIconWidth() { 642 return 12; 643 } 644 645 public void paintIcon(Component c, Graphics g, int x, int y) { 646 Color col = g.getColor(); 647 try { 648 g.setColor(Color.BLUE); 649 g.drawRect(x, y, getIconWidth(), getIconHeight()); 650 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5); 651 } finally { 652 g.setColor(col); 653 } 654 } 655 656 } 657 658 private class ValueIcon implements Icon { 659 660 public int getIconHeight() { 661 return 12; 662 } 663 664 public int getIconWidth() { 665 return 12; 666 } 667 668 public void paintIcon(Component c, Graphics g, int x, int y) { 669 Color col = g.getColor(); 670 try { 671 g.setColor(Color.GREEN); 672 g.drawRect(x, y, getIconWidth(), getIconHeight()); 673 g.fillRect(x+3, y+3, getIconWidth()-5, getIconHeight()-5); 674 } finally { 675 g.setColor(col); 676 } 677 } 678 } 679 680 private class PostSetAction extends AbstractAction { 681 boolean performed = false; 682 683 public void assertPerformed() { 684 assertTrue ("Action was not performed", performed); 685 performed = false; 686 } 687 688 public void assertNotPerformed() { 689 assertTrue ("Action should not be performed before an appropriate event is triggered", !performed); 690 } 691 692 public void actionPerformed(java.awt.event.ActionEvent e) { 693 performed = true; 694 } 695 696 } 697 698 699 private static TNode tn; 700 private static TProperty tp; 701 private static TProperty2 tp1; 702 private static TEditor te; 703 private static PostSetAction postSetAction; 704 private static String initEditorValue; 705 private static String initPropertyValue; 706 private static String postChangePropertyValue; 707 private static String postChangeEditorValue; 708 709 private static final BufferedImage toBufferedImage(Image img) { 711 new javax.swing.ImageIcon (img); 713 java.awt.image.BufferedImage rep = createBufferedImage(img.getWidth(null), img.getHeight(null)); 714 java.awt.Graphics g = rep.createGraphics(); 715 g.drawImage(img, 0, 0, null); 716 g.dispose(); 717 img.flush(); 718 return rep; 719 } 720 721 722 private static final java.awt.image.BufferedImage createBufferedImage(int width, int height) { 723 java.awt.image.ColorModel model = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(). 724 getDefaultScreenDevice().getDefaultConfiguration().getColorModel(java.awt.Transparency.BITMASK); 725 java.awt.image.BufferedImage buffImage = new java.awt.image.BufferedImage (model, 726 model.createCompatibleWritableRaster(width, height), model.isAlphaPremultiplied(), null); 727 return buffImage; 728 } 729 730 731 732 } 733 | Popular Tags |