KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > propertysheet > NewPropertyPanelTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.explorer.propertysheet;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.BorderLayout JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Container JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.FlowLayout JavaDoc;
29 import java.awt.Graphics JavaDoc;
30 import java.awt.KeyboardFocusManager JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import java.awt.Point JavaDoc;
34 import java.awt.Rectangle JavaDoc;
35 import java.awt.Window JavaDoc;
36 import javax.swing.text.JTextComponent JavaDoc;
37 import org.openide.explorer.propertysheet.PropertySheet;
38 import java.awt.event.ActionEvent JavaDoc;
39 import java.awt.event.ActionListener JavaDoc;
40 import java.awt.event.FocusEvent JavaDoc;
41 import java.awt.event.FocusListener JavaDoc;
42 import java.awt.event.KeyEvent JavaDoc;
43 import java.awt.event.MouseEvent JavaDoc;
44 import java.awt.event.WindowAdapter JavaDoc;
45 import java.awt.event.WindowEvent JavaDoc;
46 import java.awt.image.BufferedImage JavaDoc;
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 JavaDoc;
53 import java.io.File JavaDoc;
54 import java.lang.ref.WeakReference JavaDoc;
55 import java.lang.reflect.*;
56 import javax.swing.*;
57 import javax.swing.ImageIcon JavaDoc;
58 import javax.swing.JPanel JavaDoc;
59 import javax.swing.JTextField JavaDoc;
60 import javax.swing.event.ChangeEvent JavaDoc;
61 import javax.swing.event.ChangeListener JavaDoc;
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 /* A comprehensive test of PropertyPanel */
69 public class NewPropertyPanelTest extends NbTestCase {
70     
71     static {
72         ComboTest.registerPropertyEditors();
73     }
74     
75     public NewPropertyPanelTest(String JavaDoc name) {
76         super(name);
77     }
78     
79     public static void main(String JavaDoc args[]) {
80 // LookAndFeel lf = UIManager.getLookAndFeel();
81
/* try {
82             UIManager.setLookAndFeel(new com.jgoodies.plaf.plastic.Plastic3DLookAndFeel());
83         } catch (Exception e) {
84             e.printStackTrace();
85         }
86  */

87         
88         
89         TestRunner.run(suite ());
90         
91         
92         /*
93         boolean go=false;
94         try {
95             UIManager.setLookAndFeel(new PseudoWindowsLookAndFeel());
96             go = true;
97         } catch (NoClassDefFoundError e) {
98             System.err.println("Couldn't run tests on windows look and feel");
99         } catch (UnsupportedLookAndFeelException e) {
100             System.err.println("Couldn't run tests on windows look and feel");
101         }
102         if (go) {
103             TestRunner.run(suite ());
104         }
105         go=false;
106         try {
107             UIManager.setLookAndFeel(new com.sun.java.swing.plaf.gtk.GTKLookAndFeel());
108             go = true;
109         } catch (NoClassDefFoundError e) {
110             System.err.println("Couldn't run tests on GTK look and feel");
111             e.printStackTrace();
112         } catch (UnsupportedLookAndFeelException e) {
113             System.err.println("Couldn't run tests on GTK look and feel");
114             e.printStackTrace();
115         }
116         if (go) {
117             TestRunner.run(suite ());
118         }
119         try {
120             UIManager.setLookAndFeel(lf);
121         } catch (Exception e) {
122             //highly unlikely
123         }
124          */

125         try {
126 // new NewPropertyPanelTest("goo").setUp();
127
} catch (Exception JavaDoc 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 /*
146  * This test creates a Property, Editor and Node. First test checks if initialized
147  * editor contains the same value as property. The second checks if the property
148  * value is changed if the same change will be done in the editor.
149  */

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 JavaDoc jf=null;
166     private JPanel JavaDoc jp=null;
167     private int SLEEP_LENGTH=10; //Make this longer to see what the test is doing
168

169     protected void tearDown() {
170         if (jf != null) {
171 // jf.hide();
172
// jf.dispose();
173
}
174     }
175    
176     protected void setUp() throws Exception JavaDoc {
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             // Create new BasicEditor
189
te = new BasicEditor();
190             // Create new TNode
191
tn = new TNode();
192
193             System.err.println("Crating frame");
194             jf = new JFrame JavaDoc();
195             jf.getContentPane().setLayout(new BorderLayout JavaDoc());
196             jp = new JPanel JavaDoc();
197             jp.setLayout(new FlowLayout JavaDoc());
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); //block until window open
233
System.err.println("Window shown");
234         } catch (Exception JavaDoc e) {
235             e.printStackTrace();
236         } finally {
237             setup = true;
238         }
239     }
240     
241     public void testReadOnlyMode() throws Exception JavaDoc {
242         requestFocus (focusButton);
243         changeProperty (stringPanel, stringProp);
244         setPreferences(stringPanel, PropertyPanel.PREF_READ_ONLY | PropertyPanel.PREF_INPUT_STATE);
245         Component JavaDoc owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
246         
247         requestFocus(stringPanel);
248         Component JavaDoc 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 JavaDoc 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 JavaDoc {
262         requestFocus (focusButton);
263         sleep();
264         changeProperty (filePanel, fileProp);
265         sleep();
266         setEnabled (filePanel, false);
267         sleep();
268         Component JavaDoc owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
269         
270         requestFocus(filePanel);
271         Component JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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         //XXX better to do this for JFileChooser, but it has bugs - it *is* still focusable
299

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 JavaDoc 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 JavaDoc {
319         Dimension JavaDoc d = filePanel.getPreferredSize();
320         requestFocus(filePanel);
321         sleep();
322         Component JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
344         //This test is in referenced to a filed issue that the old property
345
//panel did not send focus lost events properly
346

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 JavaDoc {
374         requestFocus (tagsPanel);
375         
376         System.err.println("TAGS PANEL BOUNDS: " + tagsPanel.getBounds());
377         
378         Component JavaDoc 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         /*
402         //Commenting this out for now - too heavily depends to on the look and
403         //feel's behavior
404         assertTrue("After shifting focus away from a tableUI combo box, its popup should no longer be open",
405             !((JComboBox) focusOwner).isPopupVisible());
406          */

407         
408     }
409     
410     
411     public void testClientProperties() throws Exception JavaDoc {
412         tagsPanel.putClientProperty("radioButtonMax", new Integer JavaDoc(100));
413         sleep();
414         sleep();
415         requestFocus(tagsPanel);
416         Component JavaDoc 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 JavaDoc 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 JavaDoc(100));
441         sleep();
442         tagsPanel.putClientProperty("useLabels", Boolean.TRUE);
443         
444         sleep();
445         
446     }
447        
448     public void testReplaceProperty() throws Exception JavaDoc {
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         //Set the value in a way that will trigger an env property change
471
//on a property the panel should no longer be listening to
472
tagsProp.getPropertyEditor().setAsText("c"); //will be an invalid value
473

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 JavaDoc focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner();
479
480         changeProperty (filePanel, stringProp);
481         requestFocus(filePanel);
482         
483         Component JavaDoc 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 JavaDoc {
490         stringPanel.setChangeImmediate(false);
491         assertTrue ("Panel contains the wrong property: " + stringPanel.getProperty(), stringPanel.getProperty() == stringProp);
492         
493         requestFocus(stringPanel);
494         Component JavaDoc 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 JavaDoc);
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 JavaDoc 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         //pressKey(focusOwner, KeyEvent.VK_ENTER);
512
stringPanel.updateValue();
513         Object JavaDoc 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 JavaDoc {
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 // assertTrue ("After setting an illegal value, the outer component should be an IconPanel to show the illegal value mark, not " + newOuter,
537
// newOuter instanceof IconPanel);
538

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 JavaDoc {
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 JavaDoc msg) {
570             assertNull(msg, event);
571         }
572         
573         public void assertEvent (String JavaDoc 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 JavaDoc {
587         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
588             public void run() {
589                 throwMe = null;
590                 try {
591                     pp.setPreferences(preferences);
592                 } catch (Exception JavaDoc e) {
593                     throwMe = e;
594                 }
595             }
596         });
597         if (throwMe != null) {
598             Exception JavaDoc e = throwMe;
599             throwMe = null;
600             throw e;
601         } else {
602             sleep();
603         }
604     }
605     
606     
607     private class FL implements FocusListener JavaDoc {
608         private FocusEvent JavaDoc gainedEvent=null;
609         private FocusEvent JavaDoc lostEvent=null;
610         private int gainedCount=0;
611         private int lostCount=0;
612         public void assertGained(String JavaDoc msg) {
613             int currGainedCount = gainedCount;
614             gainedCount = 0;
615             FocusEvent JavaDoc 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 JavaDoc msg) {
622             int currLostCount = lostCount;
623             lostCount = 0;
624             FocusEvent JavaDoc 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 JavaDoc e) {
631             gainedEvent = e;
632             gainedCount++;
633         }
634         
635         public void focusLost(java.awt.event.FocusEvent JavaDoc e) {
636             lostEvent = e;
637             lostCount++;
638         }
639     }
640     
641     private class CL implements ChangeListener JavaDoc {
642         
643         private ChangeEvent JavaDoc e;
644         public void assertEvent(String JavaDoc msg) {
645             sleep(); //give the event time to happen
646
assertNotNull (msg, e);
647             e = null;
648         }
649         
650         public void assertNoEvent(String JavaDoc msg) {
651             sleep();
652             assertNull (e);
653             e = null;
654         }
655         
656         public void stateChanged(ChangeEvent JavaDoc e) {
657             this.e = e;
658         }
659         
660     }
661     
662     private static class TestGCVal extends Object JavaDoc {
663         public String JavaDoc toString() {
664             return "TestGCVal";
665         }
666     }
667
668     private static class WaitWindow extends WindowAdapter JavaDoc {
669         boolean shown=false;
670         public WaitWindow (JFrame JavaDoc f) {
671             f.addWindowListener(this);
672             f.show();
673             f.toFront();
674             f.requestFocus();
675             if (!shown) {
676                 synchronized(this) {
677                     try {
678                         //System.err.println("Waiting for window");
679
wait(6000);
680                     } catch (Exception JavaDoc e) {}
681                 }
682             }
683             int ct = 0;
684             while (!f.isShowing()) {
685                 ct++;
686                 try {
687                     Thread.currentThread().sleep(400);
688                 } catch (Exception JavaDoc e) {
689                     
690                 }
691                 if (ct > 100) {
692                     break;
693                 }
694             }
695             ct=0;
696             Container JavaDoc c = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
697             while (c != f) {
698                 try {
699                     Thread.currentThread().sleep(400);
700                 } catch (Exception JavaDoc 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 JavaDoc e) {
712             shown = true;
713             synchronized(this) {
714                 //System.err.println("window opened");
715
notifyAll();
716                 ((JFrame JavaDoc) e.getSource()).removeWindowListener(this);
717             }
718         }
719     }
720     
721     private void sleep() {
722          //useful when running interactively
723

724         try {
725             Thread.currentThread().sleep(SLEEP_LENGTH);
726         } catch (InterruptedException JavaDoc ie) {
727             //go away
728
}
729          
730         
731          
732         //runs faster -uncomment for production use
733

734         try {
735             //jf.getTreeLock().wait();
736
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
737                 public void run() {
738                     System.currentTimeMillis();
739                 }
740             });
741             //jf.getTreeLock().wait();
742
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
743                 public void run() {
744                     System.currentTimeMillis();
745                 }
746             });
747         } catch (Exception JavaDoc e) {
748         }
749         
750         
751     }
752     
753     private void requestFocus(final JComponent jc) throws Exception JavaDoc {
754         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
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 JavaDoc {
763         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
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 JavaDoc {
771         SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
772             public void run() {
773                 Point JavaDoc toClick = new Point JavaDoc(ren.getWidth() - fromRight, fromTop);
774                 Component JavaDoc 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 JavaDoc me = new MouseEvent JavaDoc (target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
779                 target.dispatchEvent(me);
780                 me = new MouseEvent JavaDoc (target, MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
781                 target.dispatchEvent(me);
782                 me = new MouseEvent JavaDoc (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 JavaDoc {
789         SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
790             public void run() {
791                 Point JavaDoc toClick = new Point JavaDoc(5,5);
792                 Component JavaDoc target=ren.getComponentAt(toClick);
793                 MouseEvent JavaDoc me = new MouseEvent JavaDoc (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 JavaDoc {
801         SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
802             public void run() {
803                 ren.setEnabled(val);
804             }
805         });
806         sleep();
807     }
808     
809     private Exception JavaDoc throwMe = null;
810     private String JavaDoc flushResult = null;
811     private String JavaDoc flushValue(final PropertyPanel ren) throws Exception JavaDoc {
812         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
813             public void run() {
814                 try {
815                     //flushResult = ren.flushValue();
816
} catch (Exception JavaDoc 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 JavaDoc target, final int key) throws Exception JavaDoc {
834         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
835             public void run() {
836                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc (target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key);
837                 target.dispatchEvent(ke);
838             }
839         });
840         sleep();
841     }
842     
843     private Exception JavaDoc throwMe2 = null;
844     private void pressKey (final Component JavaDoc target, final int key) throws Exception JavaDoc {
845         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
846             public void run() {
847                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc (target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key);
848                 try {
849                     target.dispatchEvent(ke);
850                 } catch (Exception JavaDoc e) {
851                     throwMe2 = e;
852                 }
853             }
854         });
855         sleep();
856         if (throwMe2 != null) {
857             Exception JavaDoc e1 = throwMe2;
858             throwMe2 = null;
859             throw e1;
860         }
861     }
862     
863     private void shiftPressKey (final Component JavaDoc target, final int key) throws Exception JavaDoc {
864         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
865             public void run() {
866                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc (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 JavaDoc target, final int key) throws Exception JavaDoc {
875         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
876             public void run() {
877                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc (target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key);
878                 target.dispatchEvent(ke);
879             }
880         });
881         sleep();
882     }
883     
884     //Node definition
885
public class TNode extends AbstractNode {
886         //create Node
887
public TNode() {
888             super (Children.LEAF);
889             setName("TNode"); // or, super.setName if needed
890
setDisplayName("TNode");
891             createSheet();
892         }
893         //clone existing Node
894
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         // Create a property sheet:
907
protected Sheet createSheet() {
908             sheet = super.createSheet();
909             // Make sure there is a "Properties" set:
910
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         // Method firing changes
921
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
922             firePropertyChange(s,o1,o2);
923         }
924     }
925     
926     // Property definition
927
public class BasicProperty extends PropertySupport {
928         private Object JavaDoc myValue = "Value";
929         // Create new Property
930
public BasicProperty(String JavaDoc name, boolean isWriteable) {
931             super(name, Object JavaDoc.class, name, "", true, isWriteable);
932         }
933         // get property value
934
public Object JavaDoc getValue() {
935             return myValue;
936         }
937         // set property value
938
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException {
939             System.err.println("BASICPROP setValue to " + value + " (was " + myValue+")");
940             Object JavaDoc oldVal = myValue;
941             myValue = value;
942             tn.fireMethod(getName(), oldVal, myValue);
943         }
944         // get the property editor
945
public PropertyEditor getPropertyEditor() {
946             return te;
947         }
948     }
949
950     // Property definition
951
public class FileProperty extends PropertySupport {
952         private Object JavaDoc myValue = new File JavaDoc("aFile");
953         // Create new Property
954
public FileProperty(String JavaDoc name, boolean isWriteable) {
955             super(name, File JavaDoc.class, name, "", true, isWriteable);
956         }
957         // get property value
958
public Object JavaDoc getValue() {
959             return myValue;
960         }
961         // set property value
962
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException {
963             Object JavaDoc oldVal = myValue;
964             myValue = value;
965             tn.fireMethod(getName(), oldVal, myValue);
966         }
967     }
968
969     // Property definition
970
public class TagsProperty extends PropertySupport {
971         private Object JavaDoc myValue = "Value";
972         // Create new Property
973
public TagsProperty(String JavaDoc name, boolean isWriteable) {
974             super(name, Object JavaDoc.class, name, "", true, isWriteable);
975         }
976         // get property value
977
public Object JavaDoc getValue() {
978             return myValue;
979         }
980         // set property value
981
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException {
982             Object JavaDoc oldVal = myValue;
983             myValue = value;
984             tn.fireMethod(getName(), oldVal, myValue);
985         }
986         // get the property editor
987
PropertyEditor editor = null;
988         public PropertyEditor getPropertyEditor() {
989             if (editor == null) {
990                 editor = new TagsEditor();
991             }
992             return editor;
993         }
994     }
995     
996     // Property definition
997
public class StringProperty extends PropertySupport {
998         private Object JavaDoc myValue = "way up high";
999         // Create new Property
1000
public StringProperty(String JavaDoc name, boolean isWriteable) {
1001            super(name, String JavaDoc.class, name, "", true, isWriteable);
1002        }
1003        // get property value
1004
public Object JavaDoc getValue() {
1005            return myValue;
1006        }
1007        // set property value
1008
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException {
1009            System.err.println("SETVALUE ON STRINGPROPERTY: " + value);
1010            Object JavaDoc 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 JavaDoc[] getTags() {
1024            return new String JavaDoc[] {"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 JavaDoc getCustomEditor() {
1041            return new JColorChooser();
1042        }
1043        
1044        public void setValue(Object JavaDoc 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    // Editor definition
1057
public class BasicEditor extends PropertyEditorSupport implements ExPropertyEditor, PropertyChangeListener, VetoableChangeListener {
1058        PropertyEnv env;
1059        
1060        // Create new BasicEditor
1061
public BasicEditor() {
1062        }
1063        
1064        /*
1065         * This method is called by the IDE to pass
1066         * the environment to the property editor.
1067         */

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        // Set that this Editor doesn't support custom Editor
1081
public boolean supportsCustomEditor() {
1082            return true;
1083        }
1084        
1085        // Set the Property value threw the Editor
1086
public void setValue(Object JavaDoc newValue) {
1087            System.err.println(" BasicEditor.setValue: " + newValue);
1088            super.setValue(newValue);
1089        }
1090        
1091        public String JavaDoc getAsText() {
1092            return getValue() == null ? "null" : getValue().toString();
1093        }
1094
1095        private Component JavaDoc custom;
1096        public Component JavaDoc 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 JavaDoc {
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 JavaDoc pve = new PropertyVetoException JavaDoc("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 JavaDoc s) {
1127            System.err.println(" BasicEditor.setAsText: " + s);
1128            if ("invalidValue".equals(s)) {
1129                IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
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 JavaDoc implements ActionListener JavaDoc {
1140        JTextField JavaDoc valueField=new JTextField JavaDoc();
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 JavaDoc());
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 JavaDoc 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