1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.BorderLayout ; 23 import java.awt.BorderLayout ; 24 import java.awt.Color ; 25 import java.awt.Component ; 26 import java.awt.Container ; 27 import java.awt.Dimension ; 28 import java.awt.FlowLayout ; 29 import java.awt.Graphics ; 30 import java.awt.KeyboardFocusManager ; 31 import javax.swing.JFrame ; 32 import java.util.StringTokenizer ; 33 import java.awt.Point ; 34 import java.awt.Rectangle ; 35 import java.awt.Window ; 36 import javax.swing.text.JTextComponent ; 37 import org.openide.explorer.propertysheet.PropertySheet; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.ActionListener ; 40 import java.awt.event.FocusEvent ; 41 import java.awt.event.FocusListener ; 42 import java.awt.event.KeyEvent ; 43 import java.awt.event.MouseEvent ; 44 import java.awt.event.WindowAdapter ; 45 import java.awt.event.WindowEvent ; 46 import java.awt.image.BufferedImage ; 47 import org.openide.*; 48 import org.openide.nodes.*; 49 import org.openide.explorer.propertysheet.*; 50 import org.openide.explorer.propertysheet.editors.*; 51 import java.beans.*; 52 import java.beans.PropertyVetoException ; 53 import java.io.File ; 54 import java.lang.ref.WeakReference ; 55 import java.lang.reflect.*; 56 import javax.swing.*; 57 import javax.swing.ImageIcon ; 58 import javax.swing.JPanel ; 59 import javax.swing.JTextField ; 60 import javax.swing.event.ChangeEvent ; 61 import javax.swing.event.ChangeListener ; 62 import junit.framework.*; 63 import junit.textui.TestRunner; 64 import org.netbeans.junit.*; 65 import org.openide.ErrorManager; 66 import org.openide.util.Lookup; 67 68 69 public class NewPropertyPanelTest extends NbTestCase { 70 71 static { 72 ComboTest.registerPropertyEditors(); 73 } 74 75 public NewPropertyPanelTest(String name) { 76 super(name); 77 } 78 79 public static void main(String args[]) { 80 87 88 89 TestRunner.run(suite ()); 90 91 92 125 try { 126 } catch (Exception e){} 128 129 } 130 131 static int idx = -1; 132 public static Test suite() { 133 return new CustomEditorDisplayerSuite (); 134 } 135 136 private static class CustomEditorDisplayerSuite extends NbTestSuite { 137 public CustomEditorDisplayerSuite () { 138 super (NewPropertyPanelTest.class); 139 } 140 141 public void run (final TestResult tr) { 142 super.run (tr); 143 } 144 } 145 150 151 private JButton focusButton; 152 PropertyPanel customPanel; 153 PropertyPanel filePanel; 154 PropertyPanel tagsPanel; 155 PropertyPanel stringPanel; 156 157 private TNode tn; 158 private BasicProperty basicProp; 159 private FileProperty fileProp; 160 private BasicEditor te; 161 private TagsProperty tagsProp; 162 private StringProperty stringProp; 163 164 private boolean setup=false; 165 private JFrame jf=null; 166 private JPanel jp=null; 167 private int SLEEP_LENGTH=10; 169 protected void tearDown() { 170 if (jf != null) { 171 } 174 } 175 176 protected void setUp() throws Exception { 177 PropUtils.forceRadioButtons=false; 178 179 180 try { 181 if (setup) return; 182 basicProp= new BasicProperty("basicProp", true); 183 fileProp= new FileProperty("FileProp", true); 184 tagsProp = new TagsProperty("TagsProp", true); 185 stringProp = new StringProperty("StringProp", true); 186 187 focusButton = new JButton("Somewhere over the rainbow"); 188 te = new BasicEditor(); 190 tn = new TNode(); 192 193 System.err.println("Crating frame"); 194 jf = new JFrame (); 195 jf.getContentPane().setLayout(new BorderLayout ()); 196 jp = new JPanel (); 197 jp.setLayout(new FlowLayout ()); 198 jf.getContentPane().add(jp, BorderLayout.CENTER); 199 jf.setLocation (20,20); 200 jf.setSize (600, 600); 201 202 synchronized (jp.getTreeLock()) { 203 System.err.println("BasicProp = " + basicProp); 204 205 customPanel = new PropertyPanel(basicProp); 206 filePanel = new PropertyPanel(fileProp); 207 tagsPanel = new PropertyPanel(tagsProp); 208 stringPanel = new PropertyPanel(stringProp); 209 210 tagsPanel.setBackground(Color.GREEN); 211 212 filePanel.setBackground(Color.YELLOW); 213 214 stringPanel.setBackground(Color.PINK); 215 216 customPanel.setPreferences(PropertyPanel.PREF_CUSTOM_EDITOR); 217 218 filePanel.setPreferences(PropertyPanel.PREF_CUSTOM_EDITOR); 219 220 jp.add(customPanel); 221 222 jp.add(filePanel); 223 224 jp.add (focusButton); 225 226 jp.add (stringPanel); 227 228 jp.add (tagsPanel); 229 } 230 231 System.err.println("Waiting for window"); 232 new WaitWindow(jf); System.err.println("Window shown"); 234 } catch (Exception e) { 235 e.printStackTrace(); 236 } finally { 237 setup = true; 238 } 239 } 240 241 public void testReadOnlyMode() throws Exception { 242 requestFocus (focusButton); 243 changeProperty (stringPanel, stringProp); 244 setPreferences(stringPanel, PropertyPanel.PREF_READ_ONLY | PropertyPanel.PREF_INPUT_STATE); 245 Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 246 247 requestFocus(stringPanel); 248 Component owner2 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 249 250 assertSame("Requesting focus on a read only inline editor panel should not change the focus owner, but it was " + owner2, owner, owner2); 251 252 requestFocus(stringPanel); 253 setPreferences(filePanel, PropertyPanel.PREF_READ_ONLY | PropertyPanel.PREF_CUSTOM_EDITOR); 254 jf.repaint(); 255 Component owner3 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 256 257 assertSame("Requesting focus on a read only custom editor panel should not change the focus owner, not " + owner3, owner, owner3); 258 } 259 260 261 public void testEnabled() throws Exception { 262 requestFocus (focusButton); 263 sleep(); 264 changeProperty (filePanel, fileProp); 265 sleep(); 266 setEnabled (filePanel, false); 267 sleep(); 268 Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 269 270 requestFocus(filePanel); 271 Component owner2 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 272 273 assertSame("Requesting focus on a disabled inline editor panel should not change the focus owner, but it was " + owner2, owner, owner2); 274 275 setPreferences(filePanel, PropertyPanel.PREF_CUSTOM_EDITOR); 276 277 requestFocus(filePanel); 278 Component owner3 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 279 280 assertSame("Requesting focus on a disabled custom editor panel should not change the focus owner, not " + owner3, owner, owner3); 281 282 setPreferences(filePanel, PropertyPanel.PREF_INPUT_STATE); 283 284 Component owner4 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 285 286 assertSame("Changing preferences on a property panel should not enable it to be focused if it is disabled" + owner4, owner, owner4); 287 288 setEnabled(filePanel, true); 289 290 requestFocus(filePanel); 291 292 Component owner5 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 293 assertNotSame("Setting enabled to true should restore focusability", owner5, owner); 294 assertTrue("Setting enabled to true and requesting focus should set focus to a child of the panel", filePanel.isAncestorOf(owner5)); 295 296 requestFocus(focusButton); 297 owner5 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 298 300 changeProperty(filePanel, stringProp); 301 setEnabled(filePanel, false); 302 requestFocus(focusButton); 303 owner5 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 304 305 setPreferences(filePanel, PropertyPanel.PREF_CUSTOM_EDITOR); 306 requestFocus(focusButton); 307 jp.repaint(); 308 sleep(); 309 310 requestFocus(filePanel); 311 Component owner6 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 312 313 assertTrue("Setting focus on a disabled custom editor component should neither set focus to it nor its children, but focus is on " + owner6, 314 owner6 != filePanel && !filePanel.isAncestorOf(owner6)); 315 } 316 317 318 public void testChangingPreferences() throws Exception { 319 Dimension d = filePanel.getPreferredSize(); 320 requestFocus(filePanel); 321 sleep(); 322 Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 323 324 assertTrue ("After requesting focus on an enabled property panel containing a file custom editor, focus should be on some child of the property panel, but it is " + owner, 325 filePanel.isAncestorOf(owner)); 326 327 sleep(); 328 329 setPreferences(filePanel, PropertyPanel.PREF_INPUT_STATE); 330 jf.validate(); 331 jf.repaint(); 332 Dimension d2 = filePanel.getPreferredSize(); 333 assertTrue ("Panel returned same preferred size in inline and custom editor mode. Probably the inner component wasn't changed when the preferences were changed.", 334 !d.equals(d2)); 335 sleep(); 336 337 Component newOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); 338 assertTrue ("After changing preferences from custom editor to non-custom editor in a displayed property panel, focus owner should still be in panel, but it is " + newOwner, 339 filePanel.isAncestorOf(newOwner)); 340 } 341 342 343 public void testPropertyPanelSendsFocusLost() throws Exception { 344 347 requestFocus(focusButton); 348 FL fliFile = new FL(); 349 FL fliBasic = new FL(); 350 filePanel.addFocusListener(fliFile); 351 customPanel.addFocusListener(fliBasic); 352 353 requestFocus(filePanel); 354 fliFile.assertGained("Setting focus to a custom editor did not generate a focus gained event"); 355 356 requestFocus(customPanel); 357 fliFile.assertLost("Setting focus from one custom editor to another did not generate a focus lost event from the first one"); 358 359 fliBasic.assertGained("Setting focus to a custom editor from another did not generate a focus gained event from the second one"); 360 361 requestFocus(focusButton); 362 fliBasic.assertLost("Setting focus to a non property panel component did not generate a focus lost event"); 363 364 setPreferences (filePanel, PropertyPanel.PREF_INPUT_STATE); 365 366 requestFocus(filePanel); 367 fliFile.assertGained("Setting focus to a property panel displaying an inline editor from a non-property panel component did not generate a focus gained event"); 368 369 requestFocus(focusButton); 370 fliFile.assertLost("Setting focus away from a property panel displaying an inline editor to a non-property panel component did not generate a focus lost event"); 371 } 372 373 public void testTableUIBehavior () throws Exception { 374 requestFocus (tagsPanel); 375 376 System.err.println("TAGS PANEL BOUNDS: " + tagsPanel.getBounds()); 377 378 Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 379 assertTrue ("After requesting focus on a property with tags, a combo box should have focus, not " + focusOwner, 380 focusOwner instanceof JComboBox); 381 382 assertTrue("After requesting focus on a property with tags, with TableUI false, the combo's popup should not be open", 383 !((JComboBox) focusOwner).isPopupVisible()); 384 385 requestFocus(focusButton); 386 387 tagsPanel.setPreferences(PropertyPanel.PREF_TABLEUI); 388 389 requestFocus(tagsPanel); 390 focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 391 392 assertTrue ("After requesting focus on a property with tags, a combo box should have focus, not " + focusOwner, 393 focusOwner instanceof JComboBox); 394 395 assertTrue("After requesting focus on a property with tags, with TableUI true, the combo's popup should be open", 396 ((JComboBox) focusOwner).isPopupVisible()); 397 398 sleep(); 399 requestFocus(focusButton); 400 401 407 408 } 409 410 411 public void testClientProperties() throws Exception { 412 tagsPanel.putClientProperty("radioButtonMax", new Integer (100)); 413 sleep(); 414 sleep(); 415 requestFocus(tagsPanel); 416 Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 417 418 assertTrue ("After setting focus to a panel for a property editor with tags below the radio button threshold, focus should be on a radio button, but was " + focusOwner, 419 focusOwner instanceof JRadioButton); 420 421 requestFocus (focusButton); 422 423 tagsPanel.putClientProperty("radioButtonMax", null); 424 sleep(); 425 sleep(); 426 requestFocus(tagsPanel); 427 Component focusOwner2 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 428 429 assertNotSame("Focus owner should not be same as previous if the value of radioButtonMax has changed significantly", 430 focusOwner, focusOwner2); 431 432 assertTrue("After resetting radioButtonMax client property and requesting focus, a radio button should not be what gets focus on requestFocus", 433 (!(focusOwner2 instanceof JRadioButton))); 434 435 assertTrue("After resetting radioButtonMax client property and requesting focus, a combo box should be what gets focus on requestFocus", 436 focusOwner2 instanceof JComboBox); 437 438 requestFocus (focusButton); 439 440 tagsPanel.putClientProperty("radioButtonMax", new Integer (100)); 441 sleep(); 442 tagsPanel.putClientProperty("useLabels", Boolean.TRUE); 443 444 sleep(); 445 446 } 447 448 public void testReplaceProperty() throws Exception { 449 Node.Property p = stringPanel.getProperty(); 450 requestFocus(stringPanel); 451 typeKey(stringPanel, KeyEvent.VK_W); 452 typeKey(stringPanel, KeyEvent.VK_H); 453 typeKey(stringPanel, KeyEvent.VK_O); 454 typeKey(stringPanel, KeyEvent.VK_A); 455 456 changeProperty(stringPanel, tagsProp); 457 458 Node.Property p2 = stringPanel.getProperty(); 459 460 assertNotSame("After replacing the property, it should not be the former one", 461 p2, p); 462 463 setPreferences(stringPanel, PropertyPanel.PREF_CUSTOM_EDITOR); 464 465 changeProperty(stringPanel, fileProp); 466 467 PCL pcl = new PCL(); 468 stringPanel.addPropertyChangeListener(pcl); 469 470 tagsProp.getPropertyEditor().setAsText("c"); 474 pcl.assertNoEvent("Change in a property editor not related to the current property in a propertypanel whose property was changed nonetheless triggered a state changed event from the panel"); 475 476 filePanel.setPreferences(PropertyPanel.PREF_CUSTOM_EDITOR); 477 requestFocus(filePanel); 478 Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 479 480 changeProperty (filePanel, stringProp); 481 requestFocus(filePanel); 482 483 Component focusOwner2 = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 484 485 assertNotSame("After changing the property in a custom editor panel to one with different property type, the component focus gets set to on a call to requestFocus should not be the same type as before", 486 focusOwner.getClass(), focusOwner2.getClass()); 487 } 488 489 public void testWriteValues() throws Exception { 490 stringPanel.setChangeImmediate(false); 491 assertTrue ("Panel contains the wrong property: " + stringPanel.getProperty(), stringPanel.getProperty() == stringProp); 492 493 requestFocus(stringPanel); 494 Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); 495 assertTrue ("Requesting focus on a property panel for a string property should set focus to a JTextField not " + focusOwner, 496 focusOwner instanceof JTextComponent ); 497 498 typeKey(stringPanel, KeyEvent.VK_T); 499 typeKey(stringPanel, KeyEvent.VK_H); 500 typeKey(stringPanel, KeyEvent.VK_E); 501 typeKey(stringPanel, KeyEvent.VK_R); 502 typeKey(stringPanel, KeyEvent.VK_E); 503 typeKey(stringPanel, KeyEvent.VK_S); 504 typeKey(stringPanel, KeyEvent.VK_SPACE); 505 typeKey(stringPanel, KeyEvent.VK_A); 506 507 Object o = stringPanel.getProperty().getValue(); 508 assertTrue ("After typing into a property panel string editor, property value should not be what was typed", 509 !o.equals("THERES A")); 510 511 stringPanel.updateValue(); 513 Object o2 = stringPanel.getProperty().getValue(); 514 515 assertNotSame(o,o2); 516 assertTrue ("After typing into a property panel string editor and pressing enter, property value should be what was typed, but it was <" + o + ">", 517 o2.equals("THERES A")); 518 } 519 520 521 public void testMarkingUpdates() throws Exception { 522 requestFocus(tagsPanel); 523 sleep(); 524 525 InplaceEditor outer = ((EditablePropertyDisplayer) tagsPanel.inner).getInplaceEditor(); 526 assertTrue ("Should be a button panel for component supporting a custom editor", outer instanceof ButtonPanel); 527 assertTrue ("Button panel should contain a combo box", ((ButtonPanel) outer).getInplaceEditor() instanceof ComboInplaceEditor); 528 529 sleep(); 530 sleep(); 531 pressKey(tagsPanel, KeyEvent.VK_DOWN); 532 pressKey(tagsPanel, KeyEvent.VK_UP); 533 pressKey(tagsPanel, KeyEvent.VK_UP); 534 535 InplaceEditor newOuter = ((EditablePropertyDisplayer) tagsPanel.inner).getInplaceEditor(); 536 539 540 pressKey(tagsPanel, KeyEvent.VK_UP); 541 542 sleep(); 543 sleep(); 544 newOuter = ((EditablePropertyDisplayer) tagsPanel.inner).getInplaceEditor(); 545 sleep(); 546 assertTrue ("After setting a legal value, the outer component should be an IconPanel to show the illegal value mark, but it is " + newOuter, 547 newOuter instanceof ButtonPanel); 548 } 549 550 public void testSuppressEditorButton () throws Exception { 551 requestFocus (tagsPanel); 552 InplaceEditor outer = ((EditablePropertyDisplayer) tagsPanel.inner).getInplaceEditor(); 553 assertTrue ("Should be a button panel for component supporting a custom editor", outer instanceof ButtonPanel); 554 555 tagsPanel.putClientProperty("suppressCustomEditor", Boolean.TRUE); 556 sleep(); 557 sleep(); 558 sleep(); 559 560 outer = ((EditablePropertyDisplayer) tagsPanel.inner).getInplaceEditor(); 561 562 assertTrue ("Should be no button panel when suppressing custom editor", !(outer instanceof ButtonPanel)); 563 } 564 565 566 private class PCL implements PropertyChangeListener { 567 private PropertyChangeEvent event; 568 569 public void assertNoEvent (String msg) { 570 assertNull(msg, event); 571 } 572 573 public void assertEvent (String msg) { 574 PropertyChangeEvent pce = event; 575 event = null; 576 assertNotNull(msg, event); 577 } 578 579 public void propertyChange(PropertyChangeEvent evt) { 580 581 } 582 583 } 584 585 586 private void setPreferences (final PropertyPanel pp, final int preferences) throws Exception { 587 SwingUtilities.invokeAndWait(new Runnable () { 588 public void run() { 589 throwMe = null; 590 try { 591 pp.setPreferences(preferences); 592 } catch (Exception e) { 593 throwMe = e; 594 } 595 } 596 }); 597 if (throwMe != null) { 598 Exception e = throwMe; 599 throwMe = null; 600 throw e; 601 } else { 602 sleep(); 603 } 604 } 605 606 607 private class FL implements FocusListener { 608 private FocusEvent gainedEvent=null; 609 private FocusEvent lostEvent=null; 610 private int gainedCount=0; 611 private int lostCount=0; 612 public void assertGained(String msg) { 613 int currGainedCount = gainedCount; 614 gainedCount = 0; 615 FocusEvent gained = gainedEvent; 616 gainedEvent = null; 617 assertNotNull (msg, gained); 618 assertTrue("Received wrong number of focus gained events for a single click on a renderer " + currGainedCount, currGainedCount == 1); 619 } 620 621 public void assertLost(String msg) { 622 int currLostCount = lostCount; 623 lostCount = 0; 624 FocusEvent lost = lostEvent; 625 lostEvent = null; 626 assertNotNull (msg, lost); 627 assertTrue("Received wrong number of focus lost events for a single click away from a focused renderer" + currLostCount, currLostCount == 1); 628 } 629 630 public void focusGained(java.awt.event.FocusEvent e) { 631 gainedEvent = e; 632 gainedCount++; 633 } 634 635 public void focusLost(java.awt.event.FocusEvent e) { 636 lostEvent = e; 637 lostCount++; 638 } 639 } 640 641 private class CL implements ChangeListener { 642 643 private ChangeEvent e; 644 public void assertEvent(String msg) { 645 sleep(); assertNotNull (msg, e); 647 e = null; 648 } 649 650 public void assertNoEvent(String msg) { 651 sleep(); 652 assertNull (e); 653 e = null; 654 } 655 656 public void stateChanged(ChangeEvent e) { 657 this.e = e; 658 } 659 660 } 661 662 private static class TestGCVal extends Object { 663 public String toString() { 664 return "TestGCVal"; 665 } 666 } 667 668 private static class WaitWindow extends WindowAdapter { 669 boolean shown=false; 670 public WaitWindow (JFrame f) { 671 f.addWindowListener(this); 672 f.show(); 673 f.toFront(); 674 f.requestFocus(); 675 if (!shown) { 676 synchronized(this) { 677 try { 678 wait(6000); 680 } catch (Exception e) {} 681 } 682 } 683 int ct = 0; 684 while (!f.isShowing()) { 685 ct++; 686 try { 687 Thread.currentThread().sleep(400); 688 } catch (Exception e) { 689 690 } 691 if (ct > 100) { 692 break; 693 } 694 } 695 ct=0; 696 Container c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 697 while (c != f) { 698 try { 699 Thread.currentThread().sleep(400); 700 } catch (Exception e) { 701 702 } 703 c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); 704 ct++; 705 if (ct > 100) { 706 break; 707 } 708 } 709 } 710 711 public void windowOpened(WindowEvent e) { 712 shown = true; 713 synchronized(this) { 714 notifyAll(); 716 ((JFrame ) e.getSource()).removeWindowListener(this); 717 } 718 } 719 } 720 721 private void sleep() { 722 724 try { 725 Thread.currentThread().sleep(SLEEP_LENGTH); 726 } catch (InterruptedException ie) { 727 } 729 730 731 732 734 try { 735 SwingUtilities.invokeAndWait(new Runnable () { 737 public void run() { 738 System.currentTimeMillis(); 739 } 740 }); 741 SwingUtilities.invokeAndWait(new Runnable () { 743 public void run() { 744 System.currentTimeMillis(); 745 } 746 }); 747 } catch (Exception e) { 748 } 749 750 751 } 752 753 private void requestFocus(final JComponent jc) throws Exception { 754 SwingUtilities.invokeAndWait(new Runnable () { 755 public void run() { 756 jc.requestFocus(); 757 } 758 }); 759 sleep(); 760 } 761 762 private void changeProperty (final PropertyPanel ren, final Node.Property newProp) throws Exception { 763 SwingUtilities.invokeAndWait(new Runnable () { 764 public void run() { 765 ren.setProperty(newProp); 766 } 767 }); 768 } 769 770 private void clickOn (final JComponent ren, final int fromRight, final int fromTop) throws Exception { 771 SwingUtilities.invokeAndWait (new Runnable () { 772 public void run() { 773 Point toClick = new Point (ren.getWidth() - fromRight, fromTop); 774 Component target=ren.getComponentAt(toClick); 775 toClick = SwingUtilities.convertPoint(ren, toClick, target); 776 System.err.println("Target component is " + target.getClass().getName() + " - " + target + " clicking at " + toClick); 777 778 MouseEvent me = new MouseEvent (target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 779 target.dispatchEvent(me); 780 me = new MouseEvent (target, MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 781 target.dispatchEvent(me); 782 me = new MouseEvent (target, MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 783 } 784 }); 785 sleep(); 786 } 787 788 private void clickOn (final JComponent ren) throws Exception { 789 SwingUtilities.invokeAndWait (new Runnable () { 790 public void run() { 791 Point toClick = new Point (5,5); 792 Component target=ren.getComponentAt(toClick); 793 MouseEvent me = new MouseEvent (target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false); 794 target.dispatchEvent(me); 795 } 796 }); 797 sleep(); 798 } 799 800 private void setEnabled(final PropertyPanel ren,final boolean val) throws Exception { 801 SwingUtilities.invokeAndWait (new Runnable () { 802 public void run() { 803 ren.setEnabled(val); 804 } 805 }); 806 sleep(); 807 } 808 809 private Exception throwMe = null; 810 private String flushResult = null; 811 private String flushValue(final PropertyPanel ren) throws Exception { 812 SwingUtilities.invokeAndWait(new Runnable () { 813 public void run() { 814 try { 815 } catch (Exception e) { 817 throwMe = e; 818 flushResult = null; 819 } 820 } 821 }); 822 if (throwMe != null) { 823 try { 824 throw throwMe; 825 } finally { 826 throwMe = null; 827 } 828 } 829 return flushResult; 830 } 831 832 833 private void releaseKey (final Component target, final int key) throws Exception { 834 SwingUtilities.invokeAndWait(new Runnable () { 835 public void run() { 836 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key); 837 target.dispatchEvent(ke); 838 } 839 }); 840 sleep(); 841 } 842 843 private Exception throwMe2 = null; 844 private void pressKey (final Component target, final int key) throws Exception { 845 SwingUtilities.invokeAndWait(new Runnable () { 846 public void run() { 847 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key); 848 try { 849 target.dispatchEvent(ke); 850 } catch (Exception e) { 851 throwMe2 = e; 852 } 853 } 854 }); 855 sleep(); 856 if (throwMe2 != null) { 857 Exception e1 = throwMe2; 858 throwMe2 = null; 859 throw e1; 860 } 861 } 862 863 private void shiftPressKey (final Component target, final int key) throws Exception { 864 SwingUtilities.invokeAndWait(new Runnable () { 865 public void run() { 866 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_MASK, key, (char) key); 867 target.dispatchEvent(ke); 868 } 869 }); 870 sleep(); 871 } 872 873 874 private void typeKey (final Component target, final int key) throws Exception { 875 SwingUtilities.invokeAndWait(new Runnable () { 876 public void run() { 877 KeyEvent ke = new KeyEvent (target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key); 878 target.dispatchEvent(ke); 879 } 880 }); 881 sleep(); 882 } 883 884 public class TNode extends AbstractNode { 886 public TNode() { 888 super (Children.LEAF); 889 setName("TNode"); setDisplayName("TNode"); 891 createSheet(); 892 } 893 public Node cloneNode() { 895 return new TNode(); 896 } 897 898 public void addProp (Node.Property p) { 899 props.put(p); 900 this.firePropertyChange(PROP_PROPERTY_SETS, null, null); 901 this.firePropertySetsChange(null, null); 902 } 903 904 Sheet sheet=null; 905 Sheet.Set props=null; 906 protected Sheet createSheet() { 908 sheet = super.createSheet(); 909 props = sheet.get(Sheet.PROPERTIES); 911 if (props == null) { 912 props = Sheet.createPropertiesSet(); 913 sheet.put(props); 914 } 915 props.put(basicProp); 916 props.put(fileProp); 917 918 return sheet; 919 } 920 public void fireMethod(String s, Object o1, Object o2) { 922 firePropertyChange(s,o1,o2); 923 } 924 } 925 926 public class BasicProperty extends PropertySupport { 928 private Object myValue = "Value"; 929 public BasicProperty(String name, boolean isWriteable) { 931 super(name, Object .class, name, "", true, isWriteable); 932 } 933 public Object getValue() { 935 return myValue; 936 } 937 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 939 System.err.println("BASICPROP setValue to " + value + " (was " + myValue+")"); 940 Object oldVal = myValue; 941 myValue = value; 942 tn.fireMethod(getName(), oldVal, myValue); 943 } 944 public PropertyEditor getPropertyEditor() { 946 return te; 947 } 948 } 949 950 public class FileProperty extends PropertySupport { 952 private Object myValue = new File ("aFile"); 953 public FileProperty(String name, boolean isWriteable) { 955 super(name, File .class, name, "", true, isWriteable); 956 } 957 public Object getValue() { 959 return myValue; 960 } 961 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 963 Object oldVal = myValue; 964 myValue = value; 965 tn.fireMethod(getName(), oldVal, myValue); 966 } 967 } 968 969 public class TagsProperty extends PropertySupport { 971 private Object myValue = "Value"; 972 public TagsProperty(String name, boolean isWriteable) { 974 super(name, Object .class, name, "", true, isWriteable); 975 } 976 public Object getValue() { 978 return myValue; 979 } 980 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 982 Object oldVal = myValue; 983 myValue = value; 984 tn.fireMethod(getName(), oldVal, myValue); 985 } 986 PropertyEditor editor = null; 988 public PropertyEditor getPropertyEditor() { 989 if (editor == null) { 990 editor = new TagsEditor(); 991 } 992 return editor; 993 } 994 } 995 996 public class StringProperty extends PropertySupport { 998 private Object myValue = "way up high"; 999 public StringProperty(String name, boolean isWriteable) { 1001 super(name, String .class, name, "", true, isWriteable); 1002 } 1003 public Object getValue() { 1005 return myValue; 1006 } 1007 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 1009 System.err.println("SETVALUE ON STRINGPROPERTY: " + value); 1010 Object oldVal = myValue; 1011 myValue = value; 1012 tn.fireMethod(getName(), oldVal, myValue); 1013 } 1014 } 1015 1016 1017 public class TagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 1018 PropertyEnv env; 1019 1020 public TagsEditor() { 1021 } 1022 1023 public String [] getTags() { 1024 return new String [] {"a","b","c","d","Value"}; 1025 } 1026 1027 public void attachEnv(PropertyEnv env) { 1028 this.env = env; 1029 if ("c".equals(getValue())) { 1030 env.setState(env.STATE_INVALID); 1031 } else { 1032 env.setState(env.STATE_VALID); 1033 } 1034 } 1035 1036 public boolean supportsCustomEditor() { 1037 return true; 1038 } 1039 1040 public Component getCustomEditor() { 1041 return new JColorChooser(); 1042 } 1043 1044 public void setValue(Object newValue) { 1045 super.setValue(newValue); 1046 if (env != null) { 1047 if ("c".equals(newValue)) { 1048 env.setState(env.STATE_INVALID); 1049 } else { 1050 env.setState(env.STATE_NEEDS_VALIDATION); 1051 } 1052 } 1053 } 1054 } 1055 1056 public class BasicEditor extends PropertyEditorSupport implements ExPropertyEditor, PropertyChangeListener, VetoableChangeListener { 1058 PropertyEnv env; 1059 1060 public BasicEditor() { 1062 } 1063 1064 1068 public void attachEnv(PropertyEnv env) { 1069 if (env != null) { 1070 env.removeVetoableChangeListener(this); 1071 } 1072 this.env = env; 1073 1074 env.setState(env.STATE_VALID); 1075 env.addVetoableChangeListener(this); 1076 System.err.println(" ATTACHENV"); 1077 1078 } 1079 1080 public boolean supportsCustomEditor() { 1082 return true; 1083 } 1084 1085 public void setValue(Object newValue) { 1087 System.err.println(" BasicEditor.setValue: " + newValue); 1088 super.setValue(newValue); 1089 } 1090 1091 public String getAsText() { 1092 return getValue() == null ? "null" : getValue().toString(); 1093 } 1094 1095 private Component custom; 1096 public Component getCustomEditor() { 1097 if (custom == null) { 1098 custom = new BasicCustomEditor(this); 1099 } 1100 return custom; 1101 } 1102 1103 public void vetoNext() { 1104 env.setState(env.STATE_NEEDS_VALIDATION); 1105 vetoNextChange = true; 1106 System.err.println(" veto next"); 1107 } 1108 1109 public void propertyChange(PropertyChangeEvent evt) { 1110 1111 } 1112 1113 boolean vetoNextChange=false; 1114 public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException { 1115 System.err.println("GOT A VETOABLE CHANGE IN BASIC EDITOR"); 1116 PropertyEnv env = (PropertyEnv) e.getSource(); 1117 if ((vetoNextChange || "Dont allow validate".equals(getAsText())) && PropertyEnv.STATE_NEEDS_VALIDATION.equals(env.getState())) { 1118 System.err.println(" VETOING"); 1119 PropertyVetoException pve = new PropertyVetoException ("NoNoNoNoNo", e); 1120 ErrorManager.getDefault().annotate(pve, ErrorManager.USER, null, "You can't do that!", null, null); 1121 vetoNextChange=false; 1122 throw pve; 1123 } 1124 } 1125 1126 public void setAsText(String s) { 1127 System.err.println(" BasicEditor.setAsText: " + s); 1128 if ("invalidValue".equals(s)) { 1129 IllegalArgumentException iae = new IllegalArgumentException (); 1130 ErrorManager.getDefault().annotate(iae, ErrorManager.USER, "invalid value", "No way", null, null); 1131 throw iae; 1132 } 1133 setValue(s); 1134 } 1135 1136 } 1137 1138 1139 public class BasicCustomEditor extends JPanel implements ActionListener { 1140 JTextField valueField=new JTextField (); 1141 JButton setInvalidValueButton = new JButton("Invalid value"); 1142 JButton setDontAllowValidateButton = new JButton("Dont allow validate"); 1143 BasicEditor editor; 1144 public BasicCustomEditor(BasicEditor editor) { 1145 this.editor = editor; 1146 init(); 1147 } 1148 1149 private void init() { 1150 setLayout(new FlowLayout ()); 1151 valueField.addActionListener(this); 1152 setInvalidValueButton.addActionListener(this); 1153 setDontAllowValidateButton.addActionListener(this); 1154 valueField.setColumns(30); 1155 setBackground(Color.ORANGE); 1156 add (valueField); 1157 add (setInvalidValueButton); 1158 add (setDontAllowValidateButton); 1159 } 1160 boolean processing; 1161 public void actionPerformed(ActionEvent e) { 1162 processing = true; 1163 try { 1164 if (e.getSource() == setDontAllowValidateButton) { 1165 editor.vetoNext(); 1166 valueField.setText("dont allow validate"); 1167 } 1168 if (e.getSource() == setInvalidValueButton) { 1169 valueField.setText("invalidValue"); 1170 } 1171 if (e.getSource() == valueField) { 1172 editor.setAsText(valueField.getText()); 1173 } 1174 editor.env.setState(PropertyEnv.STATE_NEEDS_VALIDATION); 1175 } finally { 1176 processing = false; 1177 } 1178 } 1179 } 1180} 1181 | Popular Tags |