1 19 20 package org.openide.explorer.propertysheet; 21 22 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel; 23 import java.awt.BorderLayout ; 24 import java.awt.Component ; 25 import java.awt.Container ; 26 import java.awt.FlowLayout ; 27 import java.awt.KeyboardFocusManager ; 28 import java.awt.Point ; 29 import java.awt.event.ActionEvent ; 30 import java.awt.event.ActionListener ; 31 import java.awt.event.FocusEvent ; 32 import java.awt.event.FocusListener ; 33 import java.awt.event.KeyEvent ; 34 import java.awt.event.MouseEvent ; 35 import java.awt.event.WindowAdapter ; 36 import java.awt.event.WindowEvent ; 37 import java.beans.PropertyEditor ; 38 import java.beans.PropertyEditorSupport ; 39 import java.lang.reflect.InvocationTargetException ; 40 import javax.swing.JButton ; 41 import javax.swing.JComboBox ; 42 import javax.swing.JComponent ; 43 import javax.swing.JFrame ; 44 import javax.swing.JPanel ; 45 import javax.swing.SwingUtilities ; 46 import javax.swing.event.ChangeEvent ; 47 import javax.swing.event.ChangeListener ; 48 import org.netbeans.junit.NbTestCase; 49 import org.netbeans.modules.openide.explorer.UIException; 50 import org.openide.DialogDisplayer; 51 import org.openide.ErrorManager; 52 import org.openide.NotifyDescriptor; 53 import org.openide.nodes.AbstractNode; 54 import org.openide.nodes.Children; 55 import org.openide.nodes.Node; 56 import org.openide.nodes.PropertySupport; 57 import org.openide.nodes.Sheet; 58 59 61 public class PropertyPanelInDialogTest extends NbTestCase { 62 63 static { 64 ComboTest.registerPropertyEditors(); 65 } 66 67 public PropertyPanelInDialogTest(String name) { 68 super(name); 69 } 70 71 76 77 PropertyPanel basicRen; 78 PropertyPanel tagsRen1; 79 PropertyPanel tagsRen2; 80 PropertyPanel tagsRen3; 81 PropertyPanel boolRen; 82 PropertyPanel custRen; 83 PropertyPanel custRen2; 84 PropertyPanel exRen; 85 PropertyPanel numRen; 86 PropertyPanel edRen; 87 88 private TNode tn; 89 private BasicProperty basicProp; 90 private TagsProperty tags1; 91 private TagsProperty tags2; 92 private TagsProperty tags3; 93 private BooleanProperty booleanProp; 94 private EditorCustom ec; 95 private CustomProperty customProp; 96 private CustomProperty customProp2; 97 private BasicEditor te; 98 private boolean setup=false; 99 private JFrame jf=null; 100 private JPanel jp=null; 101 private int SLEEP_LENGTH=200; 102 103 PropertyPanel[] renderers; 104 JButton launcher; 105 106 protected void tearDown() { 107 if (jf != null) { 108 jf.hide(); 109 jf.dispose(); 110 } 111 } 112 113 protected void setUp() throws Exception { 114 117 if (setup) return; 118 basicProp= new BasicProperty("basicProp", true); 120 tags1 = new TagsProperty("tags1", true, new String [] {"What","is","the","meaning","of","life"}); 121 tags2 = new TagsProperty("tags2", true, new String [] {"NetBeans","can be ","really","cool"}); 122 tags3 = new TagsProperty("tags3", true, new String [] {"Behold","the","power","of","cheese"}); 123 booleanProp = new BooleanProperty("booleanProp", true); 124 customProp = new CustomProperty("CustomProp", true); 125 customProp2 = new CustomProperty("CustomProp2", true); 126 ExceptionProperty exProp = new ExceptionProperty("Exception prop", true); 127 NumProperty numProp = new NumProperty("Int prop", true); 128 EditableNumProperty edProp = new EditableNumProperty("Editable", true); 129 130 131 te = new BasicEditor(); 133 ec = new EditorCustom(); 134 tn = new TNode(); 136 137 jf = new JFrame (); 138 jf.getContentPane().setLayout(new BorderLayout ()); 139 jp = new JPanel (); 140 jp.setLayout(new FlowLayout ()); 141 jf.getContentPane().add(jp, BorderLayout.CENTER); 142 jf.setLocation(20,20); 143 jf.setSize(600, 200); 144 145 basicRen = new PropertyPanel(basicProp); 146 tagsRen1 = new PropertyPanel(tags1); 147 tagsRen2 = new PropertyPanel(tags2); 148 tagsRen3 = new PropertyPanel(tags3); 149 boolRen = new PropertyPanel(booleanProp); 150 custRen = new PropertyPanel(customProp); 151 custRen2 = new PropertyPanel(customProp2); 152 exRen = new PropertyPanel(exProp); 153 numRen = new PropertyPanel(numProp); 154 edRen = new PropertyPanel(edProp); 155 tagsRen2.putClientProperty("radioButtonMax", new Integer (10)); 156 157 renderers = new PropertyPanel[] { 158 basicRen, tagsRen1, tagsRen2, boolRen, custRen, edRen, numRen 159 }; 160 161 launcher = new JButton ("Invoke dialog"); 162 launcher.addActionListener(new ActionListener () { 163 public void actionPerformed(ActionEvent ae) { 164 invokeDlg(); 165 } 166 }); 167 168 jf.getContentPane().add(launcher); 169 new WaitWindow(jf); jf.toFront(); 171 ExtTestCase.requestFocus(launcher); 172 sleep(); 173 Thread.currentThread().sleep(300); 174 sleep(); 175 currRen = basicRen; 176 setup = true; 177 } 178 179 180 public void testBooleanEditor() throws Exception { 181 currRen = boolRen; 182 checkOneRenderer(); 183 } 184 185 186 public void testStringEditor() throws Exception { 187 currRen = basicRen; 188 checkOneRenderer(); 189 } 190 191 public void testComboEditor() throws Exception { 192 currRen = tagsRen1; 193 checkOneRenderer(); 194 } 195 196 public void testEditableCombo() throws Exception { 197 currRen = edRen; 198 checkOneRenderer(); 199 } 200 201 public void testRadioEditor() throws Exception { 202 currRen = tagsRen2; 203 checkOneRenderer(); 204 } 205 206 207 PropertyPanel currRen=null; 208 NotifyDescriptor not = null; 209 Object notifyResult=null; 210 211 private void invokeDlg() { 212 NotifyDescriptor not = new NotifyDescriptor(currRen, "Boo!", NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.QUESTION_MESSAGE, 213 new Object [] {NotifyDescriptor.OK_OPTION, NotifyDescriptor.CANCEL_OPTION}, null); 214 215 notifyResult = DialogDisplayer.getDefault().notify(not); 216 try { 217 Thread.currentThread().sleep(300); 218 } catch (Exception e){} 219 ExtTestCase.waitForDialog(); 220 } 221 222 static Boolean functioning = null; 223 private static final boolean canBeRun() { 224 if (functioning == null) { 225 functioning = ExtTestCase.canSafelyRunFocusTests() ? Boolean.TRUE : Boolean.FALSE; 226 if (Boolean.FALSE.equals(functioning)) { 227 System.err.println("Platform focus behavior not sane. Not " + 228 "running PropertyPanelInDialogTest"); 229 } 230 } 231 return functioning.booleanValue(); 232 } 233 234 public void checkOneRenderer() throws Exception { 235 if (!canBeRun()) { 236 return; 237 } 238 SwingUtilities.invokeLater(new Runnable () { 239 public void run() { 240 invokeDlg(); 241 } 242 }); 243 sleep(); 244 sleep(); 245 ExtTestCase.waitForDialog(); 246 247 while (!currRen.isVisible()) { 248 System.currentTimeMillis(); 249 } 250 251 if (currRen.getProperty() != tags2) { 252 currRen.requestFocus(); 253 } else { 254 clickOn(currRen, 80, 25); 255 } 256 257 if (currRen == boolRen) { 258 clickOn(currRen, 40, 13); 259 } 260 261 sleep(); 262 263 ExtTestCase.waitForComponentOrChildToGetFocus(currRen); 264 265 Component focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 266 assertTrue("After showing dialog, focus owner should be child of renderer, but it is " + focus, currRen == focus || currRen.isAncestorOf(focus)); 267 268 Container anc = null; 269 if (focus instanceof JComponent ) { 270 JComponent jcp = (JComponent ) focus; 271 anc = jcp.getTopLevelAncestor(); 272 } 273 assertTrue(anc != null); 274 275 pressKey(currRen, KeyEvent.VK_ESCAPE); 276 releaseKey(currRen, KeyEvent.VK_ESCAPE); 277 typeKey(currRen, KeyEvent.VK_ESCAPE); 278 279 if (focus instanceof JComboBox ) { 280 pressKey(currRen, KeyEvent.VK_ESCAPE); 282 releaseKey(currRen, KeyEvent.VK_ESCAPE); 283 typeKey(currRen, KeyEvent.VK_ESCAPE); 284 } 285 286 int ct = 0; 287 while (anc.isShowing()) { 289 System.err.println("Iter"); 290 sleep(); 291 if (ct++ > 1000) { 292 return; 293 } 294 } 295 296 sleep(); 297 sleep(); 298 sleep(); 299 ExtTestCase.waitForAnythingToGetFocus(); 300 sleep(); 301 focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 302 assertTrue("After closing dialog, focus should not be null.", focus != null); 303 ExtTestCase.waitForFrame(); 304 assertTrue("After closing dialog, focus should be on the button in the main frame - perhaps the dialog did not close?" + focus, 305 focus == launcher); 306 307 SwingUtilities.invokeLater(new Runnable () { 308 public void run() { 309 invokeDlg(); 310 } 311 }); 312 313 while (!currRen.isVisible()) { 314 System.err.println("Waiting"); 315 } 316 317 if (currRen.getProperty() != tags2) { 319 clickOn(currRen); 320 } else { 321 clickOn(currRen, 80, 25); 322 } 323 324 if (currRen == boolRen) { 325 clickOn(currRen, 40, 13); 326 } 327 328 sleep(); 329 sleep(); 330 331 ExtTestCase.waitForComponentOrChildToGetFocus(currRen); 332 333 focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 334 assertTrue("After showing dialog, focus owner should be child of renderer, but it is " + focus, currRen == focus || currRen.isAncestorOf(focus)); 335 336 anc = null; 337 if (focus instanceof JComponent ) { 338 JComponent jcp = (JComponent ) focus; 339 anc = jcp.getTopLevelAncestor(); 340 } 341 342 assertTrue(anc != null); 343 pressKey(currRen, KeyEvent.VK_ENTER); 344 releaseKey(currRen, KeyEvent.VK_ENTER); 345 typeKey(currRen, KeyEvent.VK_ENTER); 346 347 if (focus instanceof JComboBox ) { 348 pressKey(currRen, KeyEvent.VK_ENTER); 350 releaseKey(currRen, KeyEvent.VK_ENTER); 351 typeKey(currRen, KeyEvent.VK_ENTER); 352 } 353 354 ct = 0; 355 while (anc.isShowing()) { 357 System.err.println("Iter2"); 358 sleep(); 359 if (ct++ > 1000) { 360 return; 361 } 362 } 363 sleep(); 364 ExtTestCase.waitForAnythingToGetFocus(); 365 366 focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 367 assertTrue("After closing dialog, focus should not be null.", focus != null); 368 ExtTestCase.waitForFrame(); 369 focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 370 assertTrue("After closing dialog, focus should be on the button in the main frame - perhaps the dialog did not close?" + focus, 371 focus == launcher); 372 } 373 374 private class FL implements FocusListener { 375 private FocusEvent gainedEvent=null; 376 private FocusEvent lostEvent=null; 377 private int gainedCount=0; 378 private int lostCount=0; 379 public void assertGained() { 380 assertNotNull("No focus gained received after clicking on an editable renderer", gainedEvent); 381 assertTrue("Received wrong number of focus gained events for a single click on a renderer " + gainedCount, gainedCount == 1); 382 } 383 384 public void assertLost() { 385 assertNotNull("No focus lost event received after clicking away from a focused, editable renderer", lostEvent); 386 assertTrue("Received wrong number of focus lost events for a single click away from a focused renderer" + lostCount, lostCount == 1); 387 } 388 389 public void focusGained(FocusEvent e) { 390 gainedEvent = e; 391 gainedCount++; 392 } 393 394 public void focusLost(FocusEvent e) { 395 lostEvent = e; 396 lostCount++; 397 } 398 } 399 400 private class CL implements ChangeListener { 401 402 private ChangeEvent e; 403 public void assertEvent(String msg) { 404 sleep(); assertNotNull(msg, e); 406 e = null; 407 } 408 409 public void assertNoEvent(String msg) { 410 sleep(); 411 assertNull(e); 412 e = null; 413 } 414 415 public void stateChanged(ChangeEvent e) { 416 this.e = e; 417 } 418 419 } 420 421 private static class TestGCVal extends Object { 422 public String toString() { 423 return "TestGCVal"; 424 } 425 } 426 427 private static class WaitWindow extends WindowAdapter { 428 boolean shown=false; 429 public WaitWindow(JFrame f) { 430 f.addWindowListener(this); 431 f.show(); 432 if (!shown) { 433 synchronized(this) { 434 try { 435 wait(5000); 437 } catch (Exception e) {} 438 } 439 } 440 } 441 442 public void windowOpened(WindowEvent e) { 443 shown = true; 444 synchronized(this) { 445 notifyAll(); 447 ((JFrame ) e.getSource()).removeWindowListener(this); 448 } 449 } 450 } 451 452 private void sleep() { 453 455 try { 456 Thread.currentThread().sleep(SLEEP_LENGTH); 457 } catch (InterruptedException ie) { 458 } 460 461 462 463 465 try { 466 SwingUtilities.invokeAndWait(new Runnable () { 468 public void run() { 469 System.currentTimeMillis(); 470 } 471 }); 472 SwingUtilities.invokeAndWait(new Runnable () { 474 public void run() { 475 System.currentTimeMillis(); 476 } 477 }); 478 } catch (Exception e) { 479 } 480 481 482 } 483 484 private void changeProperty(PropertyPanel ren, Node.Property newProp) { 485 ren.setProperty(newProp); 486 } 487 488 private void clickOn(final PropertyPanel ren, final int fromRight, final int fromTop) throws Exception { 489 SwingUtilities.invokeAndWait(new Runnable () { 490 public void run() { 491 Point toClick = new Point (ren.getWidth() - fromRight, fromTop); 492 Component target=ren.getComponentAt(toClick); 493 if (target == null) { 494 target = ren; 495 } 496 toClick = SwingUtilities.convertPoint(ren, toClick, target); 497 MouseEvent me = new MouseEvent (target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 499 target.dispatchEvent(me); 500 me = new MouseEvent (target, MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 501 target.dispatchEvent(me); 502 me = new MouseEvent (target, MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 503 } 504 }); 505 sleep(); 506 } 507 508 private void clickOn(final PropertyPanel ren) throws Exception { 509 SwingUtilities.invokeAndWait(new Runnable () { 510 public void run() { 511 Point toClick = new Point (5,5); 512 Component target=ren.getComponentAt(toClick); 513 MouseEvent me = new MouseEvent (target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 514 target.dispatchEvent(me); 515 } 516 }); 517 sleep(); 518 } 519 520 521 private void releaseKey(final Component target, final int key) throws Exception { 522 SwingUtilities.invokeAndWait(new Runnable () { 523 public void run() { 524 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key); 525 target.dispatchEvent(ke); 526 } 527 }); 528 sleep(); 529 } 530 531 private void pressKey(final Component target, final int key) throws Exception { 532 SwingUtilities.invokeAndWait(new Runnable () { 533 public void run() { 534 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key); 535 target.dispatchEvent(ke); 536 } 537 }); 538 sleep(); 539 } 540 541 private void shiftPressKey(final Component target, final int key) throws Exception { 542 SwingUtilities.invokeAndWait(new Runnable () { 543 public void run() { 544 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_MASK, key, (char) key); 545 target.dispatchEvent(ke); 546 } 547 }); 548 sleep(); 549 } 550 551 552 private void typeKey(final Component target, final int key) throws Exception { 553 SwingUtilities.invokeAndWait(new Runnable () { 554 public void run() { 555 556 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key); 557 System.err.println("Typing key to " + target); 558 target.dispatchEvent(ke); 559 } 560 }); 561 sleep(); 562 } 563 564 public class TNode extends AbstractNode { 566 public TNode() { 568 super(Children.LEAF); 569 setName("TNode"); setDisplayName("TNode"); 571 createSheet(); 572 } 573 public Node cloneNode() { 575 return new TNode(); 576 } 577 578 public void addProp(Node.Property p) { 579 props.put(p); 580 this.firePropertyChange(PROP_PROPERTY_SETS, null, null); 581 this.firePropertySetsChange(null, null); 582 } 583 584 Sheet sheet=null; 585 Sheet.Set props=null; 586 protected Sheet createSheet() { 588 sheet = super.createSheet(); 589 props = sheet.get(Sheet.PROPERTIES); 591 if (props == null) { 592 props = Sheet.createPropertiesSet(); 593 sheet.put(props); 594 } 595 props.put(basicProp); 596 props.put(tags1); 597 props.put(tags2); 598 props.put(tags3); 599 props.put(booleanProp); 600 props.put(customProp); 601 602 return sheet; 603 } 604 public void fireMethod(String s, Object o1, Object o2) { 606 firePropertyChange(s,o1,o2); 607 } 608 } 609 610 public class BasicProperty extends PropertySupport { 612 private Object myValue = "Value"; 613 public BasicProperty(String name, boolean isWriteable) { 615 super(name, Object .class, name, "", true, isWriteable); 616 } 617 public Object getValue() { 619 return myValue; 620 } 621 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 623 Object oldVal = myValue; 624 myValue = value; 625 tn.fireMethod(getName(), oldVal, myValue); 626 } 627 public PropertyEditor getPropertyEditor() { 629 return te; 630 } 631 } 632 633 public class BasicEditor extends PropertyEditorSupport implements ExPropertyEditor { 635 PropertyEnv env; 636 637 public BasicEditor() { 639 } 640 641 645 public void attachEnv(PropertyEnv env) { 646 this.env = env; 647 } 648 649 public boolean supportsCustomEditor() { 651 return false; 652 } 653 654 public void setValue(Object newValue) { 656 super.setValue(newValue); 657 } 658 659 public String getAsText() { 660 return getValue() == null ? "null" : getValue().toString(); 661 } 662 } 663 664 665 private static class PseudoWindowsLookAndFeel extends WindowsLookAndFeel { 666 public boolean isSupportedLookAndFeel() { 667 return true; 668 } 669 } 670 671 public class TagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 672 PropertyEnv env; 673 String [] tags; 674 public TagsEditor(String [] tags) { 675 this.tags = tags; 676 } 677 678 public String [] getTags() { 679 return tags; 680 } 681 682 public void attachEnv(PropertyEnv env) { 683 this.env = env; 684 } 685 686 public boolean supportsCustomEditor() { 687 return false; 688 } 689 690 public void setValue(Object newValue) { 691 super.setValue(newValue); 692 } 693 694 695 } 696 697 public class TagsProperty extends PropertySupport { 699 private Object myValue = "Value"; 700 private String [] tags; 701 public TagsProperty(String name, boolean isWriteable, String [] tags) { 703 super(name, Object .class, name, "", true, isWriteable); 704 this.tags = tags; 705 } 706 public Object getValue() { 708 return myValue; 709 } 710 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 712 Object oldVal = myValue; 713 myValue = value; 714 tn.fireMethod(getName(), oldVal, myValue); 715 } 716 public PropertyEditor getPropertyEditor() { 718 return new TagsEditor(tags); 719 } 720 721 public String getShortDescription() { 722 return "I have tags!"; 723 } 724 } 725 726 public class BooleanProperty extends PropertySupport { 728 private Boolean myValue = Boolean.FALSE; 729 public BooleanProperty(String name, boolean isWriteable) { 731 super(name, Boolean .class, name, "", true, isWriteable); 732 } 733 public Object getValue() { 735 return myValue; 736 } 737 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 739 Object oldVal = myValue; 740 myValue = (Boolean ) value; 741 tn.fireMethod(getName(), oldVal, myValue); 742 } 743 } 744 745 public class CustomProperty extends PropertySupport { 746 private Object myValue = "Value"; 747 public CustomProperty(String name, boolean isWriteable) { 749 super(name, Object .class, name, "", true, isWriteable); 750 } 751 public Object getValue() { 753 return myValue; 754 } 755 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 757 Object oldVal = myValue; 758 myValue = value; 759 tn.fireMethod(getName(), oldVal, myValue); 760 } 761 public PropertyEditor getPropertyEditor() { 763 return ec; 764 } 765 } 766 767 public class ExceptionProperty extends PropertySupport { 768 private Object myValue = "Value"; 769 public ExceptionProperty(String name, boolean isWriteable) { 771 super(name, Object .class, name, "", true, isWriteable); 772 } 773 public Object getValue() { 775 return myValue; 776 } 777 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 779 Object oldVal = myValue; 780 myValue = value; 781 tn.fireMethod(getName(), oldVal, myValue); 782 } 783 public PropertyEditor getPropertyEditor() { 785 return exed; 786 } 787 } 788 789 private ExEditor exed = new ExEditor(); 790 public static class ExEditor extends PropertyEditorSupport { 791 private Object myVal="Value"; 792 public ExEditor() {} 793 public void setAsText(String val) { 794 if (val.equals("Value")) { 796 myVal = val; 797 } else { 798 IllegalArgumentException iae = new IllegalArgumentException ("No!"); 799 UIException.annotateUser(iae, "NoNo!", "Localized message", null, 800 null); 801 throw iae; 802 } 803 } 804 805 public void setValue(Object newValue) { 806 myVal = newValue; 807 firePropertyChange(); 808 } 809 810 public Object getValue() { 811 return "Value"; 812 } 813 } 814 815 816 public class EditorCustom extends PropertyEditorSupport implements ExPropertyEditor { 818 PropertyEnv env; 819 820 public EditorCustom() { 822 } 823 824 828 public void attachEnv(PropertyEnv env) { 829 this.env = env; 830 } 831 832 public boolean supportsCustomEditor() { 834 return true; 835 } 836 837 public void setValue(Object newValue) { 839 super.setValue(newValue); 840 } 841 842 public String getAsText() { 843 return getValue() == null ? "null" : getValue().toString(); 844 } 845 846 public Component getCustomEditor() { 847 return new JPanel (); 848 } 849 } 850 851 public class NumProperty extends PropertySupport { 852 private Integer myValue = new Integer (4); 853 public NumProperty(String name, boolean isWriteable) { 855 super(name, Integer .class, name, "", true, isWriteable); 856 } 857 public Object getValue() { 859 return myValue; 860 } 861 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 863 if (!(value instanceof Integer )) { 864 throw new IllegalArgumentException ("Not an integer - " + value); 865 } 866 Object oldVal = myValue; 867 myValue = (Integer ) value; 868 tn.fireMethod(getName(), oldVal, myValue); 869 } 870 public PropertyEditor getPropertyEditor() { 872 return new NumberedTagsEditor(); 873 } 874 875 public Object getValue(String key) { 876 if ("canEditAsText".equals(key)) { 877 return Boolean.TRUE; 878 } else { 879 return super.getValue(key); 880 } 881 } 882 } 883 884 public class EditableNumProperty extends TagsProperty { 885 public EditableNumProperty(String name, boolean isWriteable) { 886 super(name, isWriteable, new String []{"boo"}); 887 } 888 889 public PropertyEditor getPropertyEditor() { 890 return new PropertyPanelInDialogTest.EditableTagsEditor(); 891 } 892 } 893 894 895 public class NumberedTagsEditor extends PropertyEditorSupport { 897 private int val=3; 898 public NumberedTagsEditor() { 900 } 901 902 public String [] getTags() { 903 return new String [] {"zero","one","two","three","four","five","six","seven"}; 904 } 905 906 907 public void setValue(Object newValue) { 909 val = ((Integer ) newValue).intValue(); 910 firePropertyChange(); 911 } 912 913 public String getAsText() { 914 return getTags()[((Integer ) getValue()).intValue()]; 915 } 916 917 public void setAsText(String txt) { 918 String [] t = getTags(); 919 for (int i=0; i < t.length; i++) { 920 if (txt.trim().equals(t[i])) { 921 setValue(new Integer (i)); 922 return; 923 } 924 } 925 IllegalArgumentException iae = new IllegalArgumentException (txt); 926 UIException.annotateUser(iae, txt, txt + " is not a valid value", 927 null, null); 928 } 929 930 public Object getValue() { 931 return new Integer (val); 932 } 933 934 public Component getCustomEditor() { 935 return new JPanel (); 936 } 937 } 938 939 public class EditableTagsEditor extends TagsEditor implements ExPropertyEditor { 940 private Object val="woof"; 941 public EditableTagsEditor() { 942 super(new String [] {"miaou","woof","moo","quack"}); 943 } 944 public void attachEnv(PropertyEnv env) { 945 env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE); 946 } 947 public void setAsText(String s) { 948 setValue(s); 949 } 950 public void setValue(Object val) { 951 this.val = val; 952 } 953 public Object getValue() { 954 return val; 955 } 956 public String getAsText() { 957 return val.toString(); 958 } 959 } 960 } 961 | Popular Tags |