KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
23 import java.awt.BorderLayout JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Container JavaDoc;
26 import java.awt.FlowLayout JavaDoc;
27 import java.awt.KeyboardFocusManager JavaDoc;
28 import java.awt.Point JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.FocusEvent JavaDoc;
32 import java.awt.event.FocusListener JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import java.awt.event.MouseEvent JavaDoc;
35 import java.awt.event.WindowAdapter JavaDoc;
36 import java.awt.event.WindowEvent JavaDoc;
37 import java.beans.PropertyEditor JavaDoc;
38 import java.beans.PropertyEditorSupport JavaDoc;
39 import java.lang.reflect.InvocationTargetException JavaDoc;
40 import javax.swing.JButton JavaDoc;
41 import javax.swing.JComboBox JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.JFrame JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.SwingUtilities JavaDoc;
46 import javax.swing.event.ChangeEvent JavaDoc;
47 import javax.swing.event.ChangeListener JavaDoc;
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 /** Tests that PropertyPanel honors Escape and Enter keys when
60  * in a dialog */

61 public class PropertyPanelInDialogTest extends NbTestCase {
62     
63     static {
64         ComboTest.registerPropertyEditors();
65     }
66     
67     public PropertyPanelInDialogTest(String JavaDoc name) {
68         super(name);
69     }
70     
71 /*
72  * This test creates a Property, Editor and Node. First test checks if initialized
73  * editor contains the same value as property. The second checks if the property
74  * value is changed if the same change will be done in the editor.
75  */

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 JavaDoc jf=null;
100     private JPanel JavaDoc jp=null;
101     private int SLEEP_LENGTH=200;
102     
103     PropertyPanel[] renderers;
104     JButton JavaDoc 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 JavaDoc {
114         // UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
115
// UIManager.setLookAndFeel(new com.sun.java.swing.plaf.gtk.GTKLookAndFeel());
116

117         if (setup) return;
118         // Create new TesBasicProperty
119
basicProp= new BasicProperty("basicProp", true);
120         tags1 = new TagsProperty("tags1", true, new String JavaDoc[] {"What","is","the","meaning","of","life"});
121         tags2 = new TagsProperty("tags2", true, new String JavaDoc[] {"NetBeans","can be ","really","cool"});
122         tags3 = new TagsProperty("tags3", true, new String JavaDoc[] {"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         // Create new BasicEditor
132
te = new BasicEditor();
133         ec = new EditorCustom();
134         // Create new TNode
135
tn = new TNode();
136         
137         jf = new JFrame JavaDoc();
138         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
139         jp = new JPanel JavaDoc();
140         jp.setLayout(new FlowLayout JavaDoc());
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 JavaDoc(10));
156         
157         renderers = new PropertyPanel[] {
158             basicRen, tagsRen1, tagsRen2, boolRen, custRen, edRen, numRen
159         };
160         
161         launcher = new JButton JavaDoc("Invoke dialog");
162         launcher.addActionListener(new ActionListener JavaDoc() {
163             public void actionPerformed(ActionEvent JavaDoc ae) {
164                 invokeDlg();
165             }
166         });
167         
168         jf.getContentPane().add(launcher);
169         new WaitWindow(jf); //block until window open
170
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 JavaDoc {
181         currRen = boolRen;
182         checkOneRenderer();
183     }
184     
185     
186     public void testStringEditor() throws Exception JavaDoc {
187         currRen = basicRen;
188         checkOneRenderer();
189     }
190     
191     public void testComboEditor() throws Exception JavaDoc {
192         currRen = tagsRen1;
193         checkOneRenderer();
194     }
195     
196     public void testEditableCombo() throws Exception JavaDoc {
197         currRen = edRen;
198         checkOneRenderer();
199     }
200     
201     public void testRadioEditor() throws Exception JavaDoc {
202         currRen = tagsRen2;
203         checkOneRenderer();
204     }
205     
206     
207     PropertyPanel currRen=null;
208     NotifyDescriptor not = null;
209     Object JavaDoc notifyResult=null;
210     
211     private void invokeDlg() {
212         NotifyDescriptor not = new NotifyDescriptor(currRen, "Boo!", NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.QUESTION_MESSAGE,
213                 new Object JavaDoc[] {NotifyDescriptor.OK_OPTION, NotifyDescriptor.CANCEL_OPTION}, null);
214         
215         notifyResult = DialogDisplayer.getDefault().notify(not);
216         try {
217             Thread.currentThread().sleep(300);
218         } catch (Exception JavaDoc e){}
219         ExtTestCase.waitForDialog();
220     }
221     
222     static Boolean JavaDoc 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 JavaDoc {
235         if (!canBeRun()) {
236             return;
237         }
238         SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc 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 JavaDoc anc = null;
269         if (focus instanceof JComponent JavaDoc) {
270             JComponent JavaDoc jcp = (JComponent JavaDoc) 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 JavaDoc) {
280             //do a second set, the first should just close the popup
281
pressKey(currRen, KeyEvent.VK_ESCAPE);
282             releaseKey(currRen, KeyEvent.VK_ESCAPE);
283             typeKey(currRen, KeyEvent.VK_ESCAPE);
284         }
285         
286         int ct = 0;
287         //#47044
288
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 JavaDoc() {
308             public void run() {
309                 invokeDlg();
310             }
311         });
312         
313         while (!currRen.isVisible()) {
314             System.err.println("Waiting");
315         }
316         
317         //clickOn(currRen);
318
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 JavaDoc) {
338             JComponent JavaDoc jcp = (JComponent JavaDoc) 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 JavaDoc) {
348             //do a second set, the first should just close the popup
349
pressKey(currRen, KeyEvent.VK_ENTER);
350             releaseKey(currRen, KeyEvent.VK_ENTER);
351             typeKey(currRen, KeyEvent.VK_ENTER);
352         }
353         
354         ct = 0;
355         //#47044
356
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 JavaDoc {
375         private FocusEvent JavaDoc gainedEvent=null;
376         private FocusEvent JavaDoc 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 JavaDoc e) {
390             gainedEvent = e;
391             gainedCount++;
392         }
393         
394         public void focusLost(FocusEvent JavaDoc e) {
395             lostEvent = e;
396             lostCount++;
397         }
398     }
399     
400     private class CL implements ChangeListener JavaDoc {
401         
402         private ChangeEvent JavaDoc e;
403         public void assertEvent(String JavaDoc msg) {
404             sleep(); //give the event time to happen
405
assertNotNull(msg, e);
406             e = null;
407         }
408         
409         public void assertNoEvent(String JavaDoc msg) {
410             sleep();
411             assertNull(e);
412             e = null;
413         }
414         
415         public void stateChanged(ChangeEvent JavaDoc e) {
416             this.e = e;
417         }
418         
419     }
420     
421     private static class TestGCVal extends Object JavaDoc {
422         public String JavaDoc toString() {
423             return "TestGCVal";
424         }
425     }
426     
427     private static class WaitWindow extends WindowAdapter JavaDoc {
428         boolean shown=false;
429         public WaitWindow(JFrame JavaDoc f) {
430             f.addWindowListener(this);
431             f.show();
432             if (!shown) {
433                 synchronized(this) {
434                     try {
435                         //System.err.println("Waiting for window");
436
wait(5000);
437                     } catch (Exception JavaDoc e) {}
438                 }
439             }
440         }
441         
442         public void windowOpened(WindowEvent JavaDoc e) {
443             shown = true;
444             synchronized(this) {
445                 //System.err.println("window opened");
446
notifyAll();
447                 ((JFrame JavaDoc) e.getSource()).removeWindowListener(this);
448             }
449         }
450     }
451     
452     private void sleep() {
453         //useful when running interactively
454

455         try {
456             Thread.currentThread().sleep(SLEEP_LENGTH);
457         } catch (InterruptedException JavaDoc ie) {
458             //go away
459
}
460         
461         
462         
463         //runs faster -uncomment for production use
464

465         try {
466             //jf.getTreeLock().wait();
467
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
468                 public void run() {
469                     System.currentTimeMillis();
470                 }
471             });
472             //jf.getTreeLock().wait();
473
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
474                 public void run() {
475                     System.currentTimeMillis();
476                 }
477             });
478         } catch (Exception JavaDoc 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 JavaDoc {
489         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
490             public void run() {
491                 Point JavaDoc toClick = new Point JavaDoc(ren.getWidth() - fromRight, fromTop);
492                 Component JavaDoc target=ren.getComponentAt(toClick);
493                 if (target == null) {
494                     target = ren;
495                 }
496                 toClick = SwingUtilities.convertPoint(ren, toClick, target);
497                 // System.err.println("Target component is " + target.getClass().getName() + " - " + target + " clicking at " + toClick);
498
MouseEvent JavaDoc me = new MouseEvent JavaDoc(target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
499                 target.dispatchEvent(me);
500                 me = new MouseEvent JavaDoc(target, MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
501                 target.dispatchEvent(me);
502                 me = new MouseEvent JavaDoc(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 JavaDoc {
509         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
510             public void run() {
511                 Point JavaDoc toClick = new Point JavaDoc(5,5);
512                 Component JavaDoc target=ren.getComponentAt(toClick);
513                 MouseEvent JavaDoc me = new MouseEvent JavaDoc(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 JavaDoc target, final int key) throws Exception JavaDoc {
522         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
523             public void run() {
524                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(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 JavaDoc target, final int key) throws Exception JavaDoc {
532         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
533             public void run() {
534                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(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 JavaDoc target, final int key) throws Exception JavaDoc {
542         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
543             public void run() {
544                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(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 JavaDoc target, final int key) throws Exception JavaDoc {
553         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
554             public void run() {
555                 
556                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(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     //Node definition
565
public class TNode extends AbstractNode {
566         //create Node
567
public TNode() {
568             super(Children.LEAF);
569             setName("TNode"); // or, super.setName if needed
570
setDisplayName("TNode");
571             createSheet();
572         }
573         //clone existing Node
574
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         // Create a property sheet:
587
protected Sheet createSheet() {
588             sheet = super.createSheet();
589             // Make sure there is a "Properties" set:
590
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         // Method firing changes
605
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
606             firePropertyChange(s,o1,o2);
607         }
608     }
609     
610     // Property definition
611
public class BasicProperty extends PropertySupport {
612         private Object JavaDoc myValue = "Value";
613         // Create new Property
614
public BasicProperty(String JavaDoc name, boolean isWriteable) {
615             super(name, Object JavaDoc.class, name, "", true, isWriteable);
616         }
617         // get property value
618
public Object JavaDoc getValue() {
619             return myValue;
620         }
621         // set property value
622
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
623             Object JavaDoc oldVal = myValue;
624             myValue = value;
625             tn.fireMethod(getName(), oldVal, myValue);
626         }
627         // get the property editor
628
public PropertyEditor JavaDoc getPropertyEditor() {
629             return te;
630         }
631     }
632     
633     // Editor definition
634
public class BasicEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
635         PropertyEnv env;
636         
637         // Create new BasicEditor
638
public BasicEditor() {
639         }
640         
641         /*
642          * This method is called by the IDE to pass
643          * the environment to the property editor.
644          */

645         public void attachEnv(PropertyEnv env) {
646             this.env = env;
647         }
648         
649         // Set that this Editor doesn't support custom Editor
650
public boolean supportsCustomEditor() {
651             return false;
652         }
653         
654         // Set the Property value threw the Editor
655
public void setValue(Object JavaDoc newValue) {
656             super.setValue(newValue);
657         }
658         
659         public String JavaDoc 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 JavaDoc implements ExPropertyEditor {
672         PropertyEnv env;
673         String JavaDoc[] tags;
674         public TagsEditor(String JavaDoc[] tags) {
675             this.tags = tags;
676         }
677         
678         public String JavaDoc[] 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 JavaDoc newValue) {
691             super.setValue(newValue);
692         }
693         
694         
695     }
696     
697     // Property definition
698
public class TagsProperty extends PropertySupport {
699         private Object JavaDoc myValue = "Value";
700         private String JavaDoc[] tags;
701         // Create new Property
702
public TagsProperty(String JavaDoc name, boolean isWriteable, String JavaDoc[] tags) {
703             super(name, Object JavaDoc.class, name, "", true, isWriteable);
704             this.tags = tags;
705         }
706         // get property value
707
public Object JavaDoc getValue() {
708             return myValue;
709         }
710         // set property value
711
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
712             Object JavaDoc oldVal = myValue;
713             myValue = value;
714             tn.fireMethod(getName(), oldVal, myValue);
715         }
716         // get the property editor
717
public PropertyEditor JavaDoc getPropertyEditor() {
718             return new TagsEditor(tags);
719         }
720         
721         public String JavaDoc getShortDescription() {
722             return "I have tags!";
723         }
724     }
725     
726     // Property definition
727
public class BooleanProperty extends PropertySupport {
728         private Boolean JavaDoc myValue = Boolean.FALSE;
729         // Create new Property
730
public BooleanProperty(String JavaDoc name, boolean isWriteable) {
731             super(name, Boolean JavaDoc.class, name, "", true, isWriteable);
732         }
733         // get property value
734
public Object JavaDoc getValue() {
735             return myValue;
736         }
737         // set property value
738
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
739             Object JavaDoc oldVal = myValue;
740             myValue = (Boolean JavaDoc) value;
741             tn.fireMethod(getName(), oldVal, myValue);
742         }
743     }
744     
745     public class CustomProperty extends PropertySupport {
746         private Object JavaDoc myValue = "Value";
747         // Create new Property
748
public CustomProperty(String JavaDoc name, boolean isWriteable) {
749             super(name, Object JavaDoc.class, name, "", true, isWriteable);
750         }
751         // get property value
752
public Object JavaDoc getValue() {
753             return myValue;
754         }
755         // set property value
756
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
757             Object JavaDoc oldVal = myValue;
758             myValue = value;
759             tn.fireMethod(getName(), oldVal, myValue);
760         }
761         // get the property editor
762
public PropertyEditor JavaDoc getPropertyEditor() {
763             return ec;
764         }
765     }
766     
767     public class ExceptionProperty extends PropertySupport {
768         private Object JavaDoc myValue = "Value";
769         // Create new Property
770
public ExceptionProperty(String JavaDoc name, boolean isWriteable) {
771             super(name, Object JavaDoc.class, name, "", true, isWriteable);
772         }
773         // get property value
774
public Object JavaDoc getValue() {
775             return myValue;
776         }
777         // set property value
778
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
779             Object JavaDoc oldVal = myValue;
780             myValue = value;
781             tn.fireMethod(getName(), oldVal, myValue);
782         }
783         // get the property editor
784
public PropertyEditor JavaDoc getPropertyEditor() {
785             return exed;
786         }
787     }
788     
789     private ExEditor exed = new ExEditor();
790     public static class ExEditor extends PropertyEditorSupport JavaDoc {
791         private Object JavaDoc myVal="Value";
792         public ExEditor() {}
793         public void setAsText(String JavaDoc val) {
794             //System.err.println("SetAsText");
795
if (val.equals("Value")) {
796                 myVal = val;
797             } else {
798                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc("No!");
799                 UIException.annotateUser(iae, "NoNo!", "Localized message", null,
800                                          null);
801                 throw iae;
802             }
803         }
804         
805         public void setValue(Object JavaDoc newValue) {
806             myVal = newValue;
807             firePropertyChange();
808         }
809         
810         public Object JavaDoc getValue() {
811             return "Value";
812         }
813     }
814     
815     
816     // Editor definition
817
public class EditorCustom extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
818         PropertyEnv env;
819         
820         // Create new BasicEditor
821
public EditorCustom() {
822         }
823         
824         /*
825          * This method is called by the IDE to pass
826          * the environment to the property editor.
827          */

828         public void attachEnv(PropertyEnv env) {
829             this.env = env;
830         }
831         
832         // Set that this Editor doesn't support custom Editor
833
public boolean supportsCustomEditor() {
834             return true;
835         }
836         
837         // Set the Property value threw the Editor
838
public void setValue(Object JavaDoc newValue) {
839             super.setValue(newValue);
840         }
841         
842         public String JavaDoc getAsText() {
843             return getValue() == null ? "null" : getValue().toString();
844         }
845         
846         public Component JavaDoc getCustomEditor() {
847             return new JPanel JavaDoc();
848         }
849     }
850     
851     public class NumProperty extends PropertySupport {
852         private Integer JavaDoc myValue = new Integer JavaDoc(4);
853         // Create new Property
854
public NumProperty(String JavaDoc name, boolean isWriteable) {
855             super(name, Integer JavaDoc.class, name, "", true, isWriteable);
856         }
857         // get property value
858
public Object JavaDoc getValue() {
859             return myValue;
860         }
861         // set property value
862
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
863             if (!(value instanceof Integer JavaDoc)) {
864                 throw new IllegalArgumentException JavaDoc("Not an integer - " + value);
865             }
866             Object JavaDoc oldVal = myValue;
867             myValue = (Integer JavaDoc) value;
868             tn.fireMethod(getName(), oldVal, myValue);
869         }
870         // get the property editor
871
public PropertyEditor JavaDoc getPropertyEditor() {
872             return new NumberedTagsEditor();
873         }
874         
875         public Object JavaDoc getValue(String JavaDoc 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 JavaDoc name, boolean isWriteable) {
886             super(name, isWriteable, new String JavaDoc[]{"boo"});
887         }
888         
889         public PropertyEditor JavaDoc getPropertyEditor() {
890             return new PropertyPanelInDialogTest.EditableTagsEditor();
891         }
892     }
893     
894     
895     // Combo must display text, not numbers
896
public class NumberedTagsEditor extends PropertyEditorSupport JavaDoc {
897         private int val=3;
898         // Create new BasicEditor
899
public NumberedTagsEditor() {
900         }
901         
902         public String JavaDoc[] getTags() {
903             return new String JavaDoc[] {"zero","one","two","three","four","five","six","seven"};
904         }
905         
906         
907         // Set the Property value threw the Editor
908
public void setValue(Object JavaDoc newValue) {
909             val = ((Integer JavaDoc) newValue).intValue();
910             firePropertyChange();
911         }
912         
913         public String JavaDoc getAsText() {
914             return getTags()[((Integer JavaDoc) getValue()).intValue()];
915         }
916         
917         public void setAsText(String JavaDoc txt) {
918             String JavaDoc[] t = getTags();
919             for (int i=0; i < t.length; i++) {
920                 if (txt.trim().equals(t[i])) {
921                     setValue(new Integer JavaDoc(i));
922                     return;
923                 }
924             }
925             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(txt);
926             UIException.annotateUser(iae, txt, txt + " is not a valid value",
927                                      null, null);
928         }
929         
930         public Object JavaDoc getValue() {
931             return new Integer JavaDoc(val);
932         }
933         
934         public Component JavaDoc getCustomEditor() {
935             return new JPanel JavaDoc();
936         }
937     }
938     
939     public class EditableTagsEditor extends TagsEditor implements ExPropertyEditor {
940         private Object JavaDoc val="woof";
941         public EditableTagsEditor() {
942             super(new String JavaDoc[] {"miaou","woof","moo","quack"});
943         }
944         public void attachEnv(PropertyEnv env) {
945             env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE);
946         }
947         public void setAsText(String JavaDoc s) {
948             setValue(s);
949         }
950         public void setValue(Object JavaDoc val) {
951             this.val = val;
952         }
953         public Object JavaDoc getValue() {
954             return val;
955         }
956         public String JavaDoc getAsText() {
957             return val.toString();
958         }
959     }
960 }
961
Popular Tags