KickJava   Java API By Example, From Geeks To Geeks.

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


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.FlowLayout JavaDoc;
26 import java.awt.Point JavaDoc;
27 import java.awt.event.FocusEvent JavaDoc;
28 import java.awt.event.FocusListener JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import java.awt.event.WindowAdapter JavaDoc;
32 import java.awt.event.WindowEvent JavaDoc;
33 import java.beans.PropertyEditor JavaDoc;
34 import java.beans.PropertyEditorSupport JavaDoc;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import javax.swing.JFrame JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.SwingUtilities JavaDoc;
40 import javax.swing.border.Border JavaDoc;
41 import javax.swing.border.TitledBorder JavaDoc;
42 import javax.swing.event.ChangeEvent JavaDoc;
43 import javax.swing.event.ChangeListener JavaDoc;
44 import org.netbeans.junit.NbTestCase;
45 import org.netbeans.modules.openide.explorer.UIException;
46 import org.openide.ErrorManager;
47 import org.openide.nodes.AbstractNode;
48 import org.openide.nodes.Children;
49 import org.openide.nodes.Node;
50 import org.openide.nodes.PropertySupport;
51 import org.openide.nodes.Sheet;
52
53 /* A comprehensive test of RendererPropertyDisplayer */
54 public class RendererDisplayerTest extends NbTestCase {
55     
56     static {
57         ComboTest.registerPropertyEditors();
58     }
59     
60     public RendererDisplayerTest(String JavaDoc name) {
61         super(name);
62     }
63 /*
64  * This test creates a Property, Editor and Node. First test checks if initialized
65  * editor contains the same value as property. The second checks if the property
66  * value is changed if the same change will be done in the editor.
67  */

68     
69     RendererPropertyDisplayer basicRen;
70     RendererPropertyDisplayer tagsRen1;
71     RendererPropertyDisplayer tagsRen2;
72     RendererPropertyDisplayer tagsRen3;
73     RendererPropertyDisplayer boolRen;
74     RendererPropertyDisplayer custRen;
75     RendererPropertyDisplayer custRen2;
76     RendererPropertyDisplayer exRen;
77     RendererPropertyDisplayer numRen;
78     RendererPropertyDisplayer edRen;
79     
80     private TNode tn;
81     private BasicProperty basicProp;
82     private TagsProperty tags1;
83     private TagsProperty tags2;
84     private TagsProperty tags3;
85     private BooleanProperty booleanProp;
86     private EditorCustom ec;
87     private CustomProperty customProp;
88     private CustomProperty customProp2;
89     private BasicEditor te;
90     private boolean setup=false;
91     private JFrame JavaDoc jf=null;
92     private JPanel JavaDoc jp=null;
93     private int SLEEP_LENGTH=10;
94     
95     protected void tearDown() {
96         if (jf != null) {
97             // jf.hide();
98
// jf.dispose();
99
}
100     }
101     
102     protected void setUp() throws Exception JavaDoc {
103         // UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
104
// UIManager.setLookAndFeel(new com.sun.java.swing.plaf.gtk.GTKLookAndFeel());
105
PropUtils.forceRadioButtons =false;
106         if (setup) return;
107         // Create new TesBasicProperty
108
basicProp= new BasicProperty("basicProp", true);
109         tags1 = new TagsProperty("tags1", true, new String JavaDoc[] {"What","is","the","meaning","of","life"});
110         tags2 = new TagsProperty("tags2", true, new String JavaDoc[] {"Austrolopithecines","automatically","engender","every","one"});
111         tags3 = new TagsProperty("tags3", true, new String JavaDoc[] {"Behold","the","power","of","cheese"});
112         booleanProp = new BooleanProperty("I am boolean, hear me roar", true);
113         customProp = new CustomProperty("CustomProp", true);
114         customProp2 = new CustomProperty("CustomProp2", true);
115         ExceptionProperty exProp = new ExceptionProperty("Exception prop", true);
116         NumProperty numProp = new NumProperty("Int prop", true);
117         EditableNumProperty edProp = new EditableNumProperty("Editable", true);
118         
119         
120         // Create new BasicEditor
121
te = new BasicEditor();
122         ec = new EditorCustom();
123         // Create new TNode
124
tn = new TNode();
125         
126         jf = new JFrame JavaDoc();
127         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
128         jp = new JPanel JavaDoc();
129         jp.setLayout(new FlowLayout JavaDoc());
130         jf.getContentPane().add(jp, BorderLayout.CENTER);
131         jf.setLocation(20,20);
132         jf.setSize(600, 200);
133         
134         basicRen = new RendererPropertyDisplayer(basicProp);
135         tagsRen1 = new RendererPropertyDisplayer(tags1);
136         tagsRen2 = new RendererPropertyDisplayer(tags2);
137         tagsRen3 = new RendererPropertyDisplayer(tags3);
138         boolRen = new RendererPropertyDisplayer(booleanProp);
139         custRen = new RendererPropertyDisplayer(customProp);
140         custRen2 = new RendererPropertyDisplayer(customProp2);
141         exRen = new RendererPropertyDisplayer(exProp);
142         numRen = new RendererPropertyDisplayer(numProp);
143         edRen = new RendererPropertyDisplayer(edProp);
144         
145         tagsRen2.setRadioButtonMax(10);
146         
147         jp.add(basicRen);
148         jp.add(tagsRen1);
149         jp.add(tagsRen2);
150         jp.add(tagsRen3);
151         jp.add(boolRen);
152         jp.add(custRen);
153         jp.add(custRen2);
154         jp.add(exRen);
155         jp.add(numRen);
156         jp.add(edRen);
157         new WaitWindow(jf); //block until window open
158
setup = true;
159     }
160     
161     public void testUseTitle() throws Exception JavaDoc {
162         sleep();
163         sleep();
164         tagsRen2.setUseLabels(true);
165         sleep();
166         
167         Component JavaDoc c = tagsRen2.getRenderer(tagsRen2);
168         if (!(c instanceof InplaceEditor)) {
169             fail("Did not get an inplace editor for a renderer");
170         }
171         InplaceEditor ine = (InplaceEditor) c;
172         InplaceEditor innermost = PropUtils.findInnermostInplaceEditor(ine);
173         if (!(innermost instanceof RadioInplaceEditor)) {
174             fail("Should have gotten an instance of RadioInplaceEditor, not " + innermost);
175         }
176         
177         Border JavaDoc b = innermost.getComponent().getBorder();
178         assertNotNull("Border should not be null when set to use labels", b);
179         
180         assertTrue("Border should be an instance of TitledBorder, not " + b, b instanceof TitledBorder JavaDoc);
181         
182         
183         boolRen.setUseLabels(true);
184         c = boolRen.getRenderer(boolRen);
185         if (!(c instanceof InplaceEditor)) {
186             fail("Did not get an inplace editor for a renderer");
187         }
188         ine = (InplaceEditor) c;
189         innermost = PropUtils.findInnermostInplaceEditor(ine);
190         if (!(innermost instanceof CheckboxInplaceEditor)) {
191             fail("Should have gotten an instance of RadioInplaceEditor, not " + innermost);
192         }
193         CheckboxInplaceEditor cb = (CheckboxInplaceEditor) innermost;
194         assertEquals("If using labels, checkbox text should be the property display name", cb.getText(), boolRen.getProperty().getDisplayName());
195     }
196     
197     private class FL implements FocusListener JavaDoc {
198         private FocusEvent JavaDoc gainedEvent=null;
199         private FocusEvent JavaDoc lostEvent=null;
200         private int gainedCount=0;
201         private int lostCount=0;
202         public void assertGained() {
203             assertNotNull("No focus gained received after clicking on an editable renderer", gainedEvent);
204             assertTrue("Received wrong number of focus gained events for a single click on a renderer " + gainedCount, gainedCount == 1);
205         }
206         
207         public void assertLost() {
208             assertNotNull("No focus lost event received after clicking away from a focused, editable renderer", lostEvent);
209             assertTrue("Received wrong number of focus lost events for a single click away from a focused renderer" + lostCount, lostCount == 1);
210         }
211         
212         public void focusGained(FocusEvent JavaDoc e) {
213             gainedEvent = e;
214             gainedCount++;
215         }
216         
217         public void focusLost(FocusEvent JavaDoc e) {
218             lostEvent = e;
219             lostCount++;
220         }
221     }
222     
223     private class CL implements ChangeListener JavaDoc {
224         
225         private ChangeEvent JavaDoc e;
226         public void assertEvent(String JavaDoc msg) {
227             sleep(); //give the event time to happen
228
assertNotNull(msg, e);
229             e = null;
230         }
231         
232         public void assertNoEvent(String JavaDoc msg) {
233             sleep();
234             assertNull(e);
235             e = null;
236         }
237         
238         public void stateChanged(ChangeEvent JavaDoc e) {
239             this.e = e;
240         }
241         
242     }
243     
244     private static class TestGCVal extends Object JavaDoc {
245         public String JavaDoc toString() {
246             return "TestGCVal";
247         }
248     }
249     
250     private static class WaitWindow extends WindowAdapter JavaDoc {
251         boolean shown=false;
252         public WaitWindow(JFrame JavaDoc f) {
253             f.addWindowListener(this);
254             f.show();
255             if (!shown) {
256                 synchronized(this) {
257                     try {
258                         //System.err.println("Waiting for window");
259
wait(5000);
260                     } catch (Exception JavaDoc e) {}
261                 }
262             }
263         }
264         
265         public void windowOpened(WindowEvent JavaDoc e) {
266             shown = true;
267             synchronized(this) {
268                 //System.err.println("window opened");
269
notifyAll();
270                 ((JFrame JavaDoc) e.getSource()).removeWindowListener(this);
271             }
272         }
273     }
274     
275     private void sleep() {
276         //useful when running interactively
277

278         try {
279             Thread.currentThread().sleep(SLEEP_LENGTH);
280         } catch (InterruptedException JavaDoc ie) {
281             //go away
282
}
283         
284         
285         
286         //runs faster -uncomment for production use
287

288         try {
289             //jf.getTreeLock().wait();
290
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
291                 public void run() {
292                     System.currentTimeMillis();
293                 }
294             });
295             //jf.getTreeLock().wait();
296
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
297                 public void run() {
298                     System.currentTimeMillis();
299                 }
300             });
301         } catch (Exception JavaDoc e) {
302         }
303         
304         
305     }
306     
307     private void changeProperty(final RendererPropertyDisplayer ren, final Node.Property newProp) throws Exception JavaDoc {
308         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
309             public void run() {
310                 ren.setProperty(newProp);
311             }
312         });
313     }
314     
315     private void clickOn(final RendererPropertyDisplayer ren, final int fromRight, final int fromTop) throws Exception JavaDoc {
316         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
317             public void run() {
318                 Point JavaDoc toClick = new Point JavaDoc(ren.getWidth() - fromRight, fromTop);
319                 Component JavaDoc target=ren.getComponentAt(toClick);
320                 toClick = SwingUtilities.convertPoint(ren, toClick, target);
321                 System.err.println("Target component is " + target.getClass().getName() + " - " + target + " clicking at " + toClick);
322                 
323                 MouseEvent JavaDoc me = new MouseEvent JavaDoc(target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
324                 target.dispatchEvent(me);
325                 me = new MouseEvent JavaDoc(target, MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
326                 target.dispatchEvent(me);
327                 me = new MouseEvent JavaDoc(target, MouseEvent.MOUSE_CLICKED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
328             }
329         });
330         sleep();
331     }
332     
333     private void clickOn(final RendererPropertyDisplayer ren) throws Exception JavaDoc {
334         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
335             public void run() {
336                 Point JavaDoc toClick = new Point JavaDoc(5,5);
337                 Component JavaDoc target=ren.getComponentAt(toClick);
338                 MouseEvent JavaDoc me = new MouseEvent JavaDoc(target, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
339                 target.dispatchEvent(me);
340             }
341         });
342         sleep();
343     }
344     
345     private void setEnabled(final RendererPropertyDisplayer ren,final boolean val) throws Exception JavaDoc {
346         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
347             public void run() {
348                 ren.setEnabled(val);
349             }
350         });
351         sleep();
352     }
353     
354     private Exception JavaDoc throwMe = null;
355     private String JavaDoc flushResult = null;
356     private String JavaDoc flushValue(final RendererPropertyDisplayer ren) throws Exception JavaDoc {
357         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
358             public void run() {
359                 try {
360                     //flushResult = ren.flushValue();
361
} catch (Exception JavaDoc e) {
362                     throwMe = e;
363                     flushResult = null;
364                 }
365             }
366         });
367         if (throwMe != null) {
368             try {
369                 throw throwMe;
370             } finally {
371                 throwMe = null;
372             }
373         }
374         return flushResult;
375     }
376     
377     
378     private void releaseKey(final Component JavaDoc target, final int key) throws Exception JavaDoc {
379         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
380             public void run() {
381                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(target, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, key, (char) key);
382                 target.dispatchEvent(ke);
383             }
384         });
385         sleep();
386     }
387     
388     private void pressKey(final Component JavaDoc target, final int key) throws Exception JavaDoc {
389         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
390             public void run() {
391                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, key, (char) key);
392                 target.dispatchEvent(ke);
393             }
394         });
395         sleep();
396     }
397     
398     private void shiftPressKey(final Component JavaDoc target, final int key) throws Exception JavaDoc {
399         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
400             public void run() {
401                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.SHIFT_MASK, key, (char) key);
402                 target.dispatchEvent(ke);
403             }
404         });
405         sleep();
406     }
407     
408     
409     private void typeKey(final Component JavaDoc target, final int key) throws Exception JavaDoc {
410         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
411             public void run() {
412                 KeyEvent JavaDoc ke = new KeyEvent JavaDoc(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, (char) key);
413                 target.dispatchEvent(ke);
414             }
415         });
416         sleep();
417     }
418     
419     //Node definition
420
public class TNode extends AbstractNode {
421         //create Node
422
public TNode() {
423             super(Children.LEAF);
424             setName("TNode"); // or, super.setName if needed
425
setDisplayName("TNode");
426             createSheet();
427         }
428         //clone existing Node
429
public Node cloneNode() {
430             return new TNode();
431         }
432         
433         public void addProp(Node.Property p) {
434             props.put(p);
435             this.firePropertyChange(PROP_PROPERTY_SETS, null, null);
436             this.firePropertySetsChange(null, null);
437         }
438         
439         Sheet sheet=null;
440         Sheet.Set props=null;
441         // Create a property sheet:
442
protected Sheet createSheet() {
443             sheet = super.createSheet();
444             // Make sure there is a "Properties" set:
445
props = sheet.get(Sheet.PROPERTIES);
446             if (props == null) {
447                 props = Sheet.createPropertiesSet();
448                 sheet.put(props);
449             }
450             props.put(basicProp);
451             props.put(tags1);
452             props.put(tags2);
453             props.put(tags3);
454             props.put(booleanProp);
455             props.put(customProp);
456             
457             return sheet;
458         }
459         // Method firing changes
460
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
461             firePropertyChange(s,o1,o2);
462         }
463     }
464     
465     // Property definition
466
public class BasicProperty extends PropertySupport {
467         private Object JavaDoc myValue = "Value";
468         // Create new Property
469
public BasicProperty(String JavaDoc name, boolean isWriteable) {
470             super(name, Object JavaDoc.class, name, "", true, isWriteable);
471         }
472         // get property value
473
public Object JavaDoc getValue() {
474             return myValue;
475         }
476         // set property value
477
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
478             Object JavaDoc oldVal = myValue;
479             myValue = value;
480             tn.fireMethod(getName(), oldVal, myValue);
481         }
482         // get the property editor
483
public PropertyEditor JavaDoc getPropertyEditor() {
484             return te;
485         }
486     }
487     
488     // Editor definition
489
public class BasicEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
490         PropertyEnv env;
491         
492         // Create new BasicEditor
493
public BasicEditor() {
494         }
495         
496         /*
497          * This method is called by the IDE to pass
498          * the environment to the property editor.
499          */

500         public void attachEnv(PropertyEnv env) {
501             this.env = env;
502         }
503         
504         // Set that this Editor doesn't support custom Editor
505
public boolean supportsCustomEditor() {
506             return false;
507         }
508         
509         // Set the Property value threw the Editor
510
public void setValue(Object JavaDoc newValue) {
511             super.setValue(newValue);
512         }
513         
514         public String JavaDoc getAsText() {
515             return getValue() == null ? "null" : getValue().toString();
516         }
517     }
518     
519     
520     private static class PseudoWindowsLookAndFeel extends WindowsLookAndFeel {
521         public boolean isSupportedLookAndFeel() {
522             return true;
523         }
524     }
525     
526     public class TagsEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
527         PropertyEnv env;
528         String JavaDoc[] tags;
529         public TagsEditor(String JavaDoc[] tags) {
530             this.tags = tags;
531         }
532         
533         public String JavaDoc[] getTags() {
534             return tags;
535         }
536         
537         public void attachEnv(PropertyEnv env) {
538             this.env = env;
539         }
540         
541         public boolean supportsCustomEditor() {
542             return false;
543         }
544         
545         public void setValue(Object JavaDoc newValue) {
546             super.setValue(newValue);
547         }
548         
549         
550     }
551     
552     // Property definition
553
public class TagsProperty extends PropertySupport {
554         private Object JavaDoc myValue = "Value";
555         private String JavaDoc[] tags;
556         // Create new Property
557
public TagsProperty(String JavaDoc name, boolean isWriteable, String JavaDoc[] tags) {
558             super(name, Object JavaDoc.class, name, "", true, isWriteable);
559             this.tags = tags;
560         }
561         // get property value
562
public Object JavaDoc getValue() {
563             return myValue;
564         }
565         // set property value
566
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
567             Object JavaDoc oldVal = myValue;
568             myValue = value;
569             tn.fireMethod(getName(), oldVal, myValue);
570         }
571         // get the property editor
572
public PropertyEditor JavaDoc getPropertyEditor() {
573             return new TagsEditor(tags);
574         }
575         
576         public String JavaDoc getShortDescription() {
577             return "I have tags!";
578         }
579     }
580     
581     // Property definition
582
public class BooleanProperty extends PropertySupport {
583         private Boolean JavaDoc myValue = Boolean.FALSE;
584         // Create new Property
585
public BooleanProperty(String JavaDoc name, boolean isWriteable) {
586             super(name, Boolean JavaDoc.class, name, "", true, isWriteable);
587         }
588         // get property value
589
public Object JavaDoc getValue() {
590             return myValue;
591         }
592         // set property value
593
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
594             Object JavaDoc oldVal = myValue;
595             myValue = (Boolean JavaDoc) value;
596             tn.fireMethod(getName(), oldVal, myValue);
597         }
598     }
599     
600     public class CustomProperty extends PropertySupport {
601         private Object JavaDoc myValue = "Value";
602         // Create new Property
603
public CustomProperty(String JavaDoc name, boolean isWriteable) {
604             super(name, Object JavaDoc.class, name, "", true, isWriteable);
605         }
606         // get property value
607
public Object JavaDoc getValue() {
608             return myValue;
609         }
610         // set property value
611
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
612             Object JavaDoc oldVal = myValue;
613             myValue = value;
614             tn.fireMethod(getName(), oldVal, myValue);
615         }
616         // get the property editor
617
public PropertyEditor JavaDoc getPropertyEditor() {
618             return ec;
619         }
620     }
621     
622     public class ExceptionProperty extends PropertySupport {
623         private Object JavaDoc myValue = "Value";
624         // Create new Property
625
public ExceptionProperty(String JavaDoc name, boolean isWriteable) {
626             super(name, Object JavaDoc.class, name, "", true, isWriteable);
627         }
628         // get property value
629
public Object JavaDoc getValue() {
630             return myValue;
631         }
632         // set property value
633
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
634             Object JavaDoc oldVal = myValue;
635             myValue = value;
636             tn.fireMethod(getName(), oldVal, myValue);
637         }
638         // get the property editor
639
public PropertyEditor JavaDoc getPropertyEditor() {
640             return exed;
641         }
642     }
643     
644     private ExEditor exed = new ExEditor();
645     public static class ExEditor extends PropertyEditorSupport JavaDoc {
646         private Object JavaDoc myVal="Value";
647         public ExEditor() {}
648         public void setAsText(String JavaDoc val) {
649             //System.err.println("SetAsText");
650
if (val.equals("Value")) {
651                 myVal = val;
652             } else {
653                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc("No!");
654                 UIException.annotateUser(iae, "NoNo!", "Localized message", null,
655                                          null);
656                 throw iae;
657             }
658         }
659         
660         public void setValue(Object JavaDoc newValue) {
661             myVal = newValue;
662             firePropertyChange();
663         }
664         
665         public Object JavaDoc getValue() {
666             return "Value";
667         }
668     }
669     
670     
671     // Editor definition
672
public class EditorCustom extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
673         PropertyEnv env;
674         
675         // Create new BasicEditor
676
public EditorCustom() {
677         }
678         
679         /*
680          * This method is called by the IDE to pass
681          * the environment to the property editor.
682          */

683         public void attachEnv(PropertyEnv env) {
684             this.env = env;
685         }
686         
687         // Set that this Editor doesn't support custom Editor
688
public boolean supportsCustomEditor() {
689             return true;
690         }
691         
692         // Set the Property value threw the Editor
693
public void setValue(Object JavaDoc newValue) {
694             super.setValue(newValue);
695         }
696         
697         public String JavaDoc getAsText() {
698             return getValue() == null ? "null" : getValue().toString();
699         }
700         
701         public Component JavaDoc getCustomEditor() {
702             return new JPanel JavaDoc();
703         }
704     }
705     
706     public class NumProperty extends PropertySupport {
707         private Integer JavaDoc myValue = new Integer JavaDoc(4);
708         // Create new Property
709
public NumProperty(String JavaDoc name, boolean isWriteable) {
710             super(name, Integer JavaDoc.class, name, "", true, isWriteable);
711         }
712         // get property value
713
public Object JavaDoc getValue() {
714             return myValue;
715         }
716         // set property value
717
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
718             if (!(value instanceof Integer JavaDoc)) {
719                 throw new IllegalArgumentException JavaDoc("Not an integer - " + value);
720             }
721             Object JavaDoc oldVal = myValue;
722             myValue = (Integer JavaDoc) value;
723             tn.fireMethod(getName(), oldVal, myValue);
724         }
725         // get the property editor
726
public PropertyEditor JavaDoc getPropertyEditor() {
727             return new NumberedTagsEditor();
728         }
729     }
730     
731     public class EditableNumProperty extends TagsProperty {
732         public EditableNumProperty(String JavaDoc name, boolean isWriteable) {
733             super(name, isWriteable, new String JavaDoc[]{"boo"});
734         }
735         
736         public PropertyEditor JavaDoc getPropertyEditor() {
737             return new RendererDisplayerTest.EditableTagsEditor();
738         }
739     }
740     
741     
742     // Combo must display text, not numbers
743
public class NumberedTagsEditor extends PropertyEditorSupport JavaDoc {
744         private int val=3;
745         // Create new BasicEditor
746
public NumberedTagsEditor() {
747         }
748         
749         public String JavaDoc[] getTags() {
750             return new String JavaDoc[] {"zero","one","two","three","four","five","six","seven"};
751         }
752         
753         
754         // Set the Property value threw the Editor
755
public void setValue(Object JavaDoc newValue) {
756             val = ((Integer JavaDoc) newValue).intValue();
757             firePropertyChange();
758         }
759         
760         public String JavaDoc getAsText() {
761             return getTags()[((Integer JavaDoc) getValue()).intValue()];
762         }
763         
764         public void setAsText(String JavaDoc txt) {
765             String JavaDoc[] t = getTags();
766             for (int i=0; i < t.length; i++) {
767                 if (txt.trim().equals(t[i])) {
768                     setValue(new Integer JavaDoc(i));
769                     return;
770                 }
771             }
772             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(txt);
773             UIException.annotateUser(iae, txt, txt + " is not a valid value",
774                                      null, null);
775         }
776         
777         public Object JavaDoc getValue() {
778             return new Integer JavaDoc(val);
779         }
780         
781         public Component JavaDoc getCustomEditor() {
782             return new JPanel JavaDoc();
783         }
784     }
785     
786     public class EditableTagsEditor extends TagsEditor implements ExPropertyEditor {
787         private Object JavaDoc val="woof";
788         public EditableTagsEditor() {
789             super(new String JavaDoc[] {"miaou","woof","moo","quack"});
790         }
791         public void attachEnv(PropertyEnv env) {
792             env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE);
793         }
794         public void setAsText(String JavaDoc s) {
795             setValue(s);
796         }
797         public void setValue(Object JavaDoc val) {
798             this.val = val;
799         }
800         public Object JavaDoc getValue() {
801             return val;
802         }
803         public String JavaDoc getAsText() {
804             return val.toString();
805         }
806         public boolean supportsCustomEditor() {
807             return true;
808         }
809         public Component JavaDoc getCustomEditor() {
810             return new JLabel JavaDoc("You called?");
811         }
812     }
813 }
814
Popular Tags