KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
23 import java.awt.Point JavaDoc;
24 import java.awt.Rectangle JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26 import java.beans.PropertyEditor JavaDoc;
27 import java.beans.PropertyEditorSupport JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import javax.swing.JComboBox JavaDoc;
30 import javax.swing.JComponent JavaDoc;
31 import javax.swing.JFrame JavaDoc;
32 import javax.swing.RepaintManager JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow;
35 import org.openide.nodes.AbstractNode;
36 import org.openide.nodes.Children;
37 import org.openide.nodes.Node;
38 import org.openide.nodes.PropertySupport;
39 import org.openide.nodes.Sheet;
40
41 // This test class tests the main functionality of the property sheet
42
public class TagsAndEditorsTest extends ExtTestCase {
43     private PropertySheet ps = null;
44     JFrame JavaDoc jf = null;
45     
46     public TagsAndEditorsTest(String JavaDoc name) {
47         super(name);
48     }
49     
50     static {
51         installCorePropertyEditors();
52     }
53     
54 /*
55  * This test creates a Property, Editor and Node. First test checks if initialized
56  * editor contains the same value as property. The second checks if the property
57  * value is changed if the same change will be done in the editor.
58  */

59     protected void setUp() throws Exception JavaDoc {
60         PropUtils.forceRadioButtons = false;
61         jf = new JFrame JavaDoc();
62         // Create new TestProperty
63
try {
64             PropUtils.forceRadioButtons=false;
65             ps = new PropertySheet();
66             //ensure no stored value in preferences:
67
sleep();
68             
69             setSortingMode(ps, PropertySheet.UNSORTED);
70             
71             jf.getContentPane().add(ps);
72             jf.setLocation(20,20);
73             jf.setSize(300, 400);
74             
75             new WaitWindow(jf);
76         } catch (Exception JavaDoc e) {
77             e.printStackTrace();
78             fail("FAILED - Exception thrown "+e.getClass().toString());
79         }
80     }
81     
82     static {
83         System.err.println("CLASSPATH: " + System.getProperty("java.class.path"));
84         System.setProperty("org.openide.explorer.propertysheet.ComboInplaceEditor", "1");
85     }
86     public void testEditableEmptyTagEditor() throws Exception JavaDoc {
87         if (!canSafelyRunFocusTests()) {
88             return;
89         }
90         Node n = new TNode(new EditableEmptyTagsEditor());
91         setCurrentNode(n,ps);
92         requestFocus(ps);
93         clickCell(ps.table, 1, 1);
94         Component JavaDoc c = focusComp();
95         assertTrue("Clicking on an editable property that returns a 1 element " +
96                 "array from getTags() should send focus to a combo box's child editor component not " + focusComp(),
97                 c.getParent() instanceof JComboBox JavaDoc);
98     }
99     
100     public void testSingleTagEditor() throws Exception JavaDoc {
101         if (!canSafelyRunFocusTests()) {
102             return;
103         }
104         Node n = new TNode(new SingleTagEditor());
105         setCurrentNode(n, ps);
106         clickCell(ps.table, 1, 1);
107         assertTrue("Clicking on an editable property that returns a 1 element " +
108                 "array from getTags() should send focus to a combo boxnot a " + focusComp(),
109                 focusComp() instanceof JComboBox JavaDoc);
110     }
111     
112     public void testEditableSingleTagEditor() throws Exception JavaDoc {
113         if (!canSafelyRunFocusTests()) {
114             return;
115         }
116         Node n = new TNode(new EditableSingleTagEditor());
117         setCurrentNode(n, ps);
118         clickCell(ps.table, 1, 1);
119         Component JavaDoc c = focusComp();
120         assertTrue("Clicking on an editable property that returns a 1 element " +
121                 "array from getTags() should send focus to a combo box's child editor component",
122                 c.getParent() instanceof JComboBox JavaDoc);
123     }
124     
125     public void testEmptyTagEditor() throws Exception JavaDoc {
126         if (!canSafelyRunFocusTests()) {
127             return;
128         }
129         requestFocus(ps);
130         Node n = new TNode(new EmptyTagsEditor());
131         setCurrentNode(n, ps);
132         clickCell(ps.table, 1, 1);
133         assertTrue("Clicking on an editable property that returns a 0 " +
134                 "length array from getTags() should send focus to a combo box",
135                 focusComp() instanceof JComboBox JavaDoc);
136     }
137     
138     public void testPropertyMarkingAlignment() throws Exception JavaDoc {
139         if (!canSafelyRunFocusTests()) {
140             return;
141         }
142         Node n = new TNode(new PropertyEditor JavaDoc[]{
143             new BadEditorWithTags(),
144             new BadEditorWithoutTags()
145         });
146         setCurrentNode(n, ps);
147     }
148     
149     public void testPropertySheetRepaintsCellOnPropertyChange() throws Exception JavaDoc {
150         if (!canSafelyRunFocusTests()) {
151             return;
152         }
153         Node n = new TNode(new SingleTagEditor());
154         setCurrentNode(n, ps);
155         Rectangle JavaDoc test = ps.table.getCellRect(1, 1, true);
156         RM rm = new RM(test, ps.table);
157         RepaintManager.setCurrentManager(rm);
158         sleep();
159         sleep();
160         Node.Property prop = n.getPropertySets()[0].getProperties()[0];
161         prop.setValue("new value");
162         Thread.currentThread().sleep(1000);
163         sleep();
164         rm.assertRectRepainted();
165     }
166     
167     private class RM extends RepaintManager JavaDoc {
168         private Rectangle JavaDoc rect;
169         private Rectangle JavaDoc repaintedRect = null;
170         private JComponent JavaDoc repaintedComponent = null;
171         private JComponent JavaDoc target = null;
172         public RM(Rectangle JavaDoc rect, JComponent JavaDoc target) {
173             this.rect = rect;
174             this.target = target;
175         }
176         
177         public void assertRectRepainted() {
178             assertNotNull("No component repainted", repaintedComponent);
179             assertSame("Wrong component repainted:" + repaintedComponent, repaintedComponent, target);
180             assertEquals("Wrong rectangle repainted:" + repaintedRect + " should be " + rect, rect, repaintedRect);
181         }
182         
183         public synchronized void addDirtyRegion(JComponent JavaDoc c, int x, int y, int w, int h) {
184             super.addDirtyRegion(c, x, y, w, h);
185             if (repaintedComponent == null) {
186                 repaintedComponent = c;
187                 repaintedRect = new Rectangle JavaDoc(x, y, w, h);
188             }
189         }
190     }
191     
192     private void clickCell(final SheetTable tb, final int row, final int col) throws Exception JavaDoc {
193         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
194             public void run() {
195                 Rectangle JavaDoc r = tb.getCellRect(row, col, false);
196                 Point JavaDoc toClick = r.getLocation();
197                 toClick.x += 15;
198                 toClick.y +=3;
199                 MouseEvent JavaDoc me = new MouseEvent JavaDoc(tb, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), MouseEvent.BUTTON1_MASK, toClick.x, toClick.y, 2, false);
200                 tb.dispatchEvent(me);
201             }
202         });
203         sleep();
204     }
205     
206     
207     //Node definition
208
public class TNode extends AbstractNode {
209         private PropertyEditor JavaDoc[] ed;
210         //create Node
211
public TNode(PropertyEditor JavaDoc[] ed) {
212             super(Children.LEAF);
213             setName("TNode"); // or, super.setName if needed
214
setDisplayName("TNode");
215             this.ed = ed;
216         }
217         
218         public TNode(PropertyEditor JavaDoc ed) {
219             this(new PropertyEditor JavaDoc[] {ed});
220         }
221         
222         //clone existing Node
223
public Node cloneNode() {
224             return new TNode(ed);
225         }
226         
227         // Create a property sheet:
228
protected Sheet createSheet() {
229             Sheet sheet = super.createSheet();
230             // Make sure there is a "Properties" set:
231
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
232             if (props == null) {
233                 props = Sheet.createPropertiesSet();
234                 sheet.put(props);
235             }
236             for (int i=0; i < ed.length; i++) {
237                 props.put(new TProperty(this, ed[i], true));
238             }
239             return sheet;
240         }
241         // Method firing changes
242
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
243             firePropertyChange(s,o1,o2);
244         }
245     }
246     
247     private String JavaDoc stripClassName(Object JavaDoc o) {
248         String JavaDoc s = o.getClass().getName();
249         int idx = s.indexOf('$');
250         return s.substring(idx+1);
251     }
252     
253     private static int ct = 0;
254     // Property definition
255
public class TProperty extends PropertySupport {
256         private Object JavaDoc myValue = "Value";
257         private PropertyEditor JavaDoc ed;
258         private Node n;
259         
260         public TProperty(Node n, PropertyEditor JavaDoc ed, boolean writable) {
261             super(stripClassName(ed) + "-" + (ct++), String JavaDoc.class, stripClassName(ed) + ct, "", true, writable);
262             this.ed = ed;
263             this.n = n;
264         }
265         
266         public Object JavaDoc getValue() {
267             return myValue;
268         }
269         
270         public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
271             Object JavaDoc oldVal = myValue;
272             myValue = value;
273             ((TNode)n).fireMethod(getName(), oldVal, myValue);
274         }
275         
276         public PropertyEditor JavaDoc getPropertyEditor() {
277             return ed;
278         }
279     }
280     
281     
282     private static Exception JavaDoc throwMe=null;
283     private synchronized void setSortingMode(final PropertySheet ps, final int mode) throws Exception JavaDoc {
284         throwMe = null;
285         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
286             public void run() {
287                 try {
288                     ps.setSortingMode(mode);
289                 } catch (Exception JavaDoc e) {
290                     throwMe = e;
291                 }
292             }
293         });
294         if (throwMe != null) {
295             Exception JavaDoc ex = throwMe;
296             throwMe = null;
297             throw (throwMe);
298         }
299     }
300     
301     public static class SingleTagEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
302         PropertyEnv env;
303         
304         public SingleTagEditor() {
305         }
306         
307         public String JavaDoc[] getTags() {
308             return new String JavaDoc[]{"lonely tag"};
309         }
310         
311         public void attachEnv(PropertyEnv env) {
312             this.env = env;
313         }
314         
315         public boolean supportsCustomEditor() {
316             return false;
317         }
318         
319         public void setValue(Object JavaDoc newValue) {
320             super.setValue(newValue);
321         }
322     }
323     
324     public static class EditableSingleTagEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
325         PropertyEnv env;
326         
327         public EditableSingleTagEditor() {
328         }
329         
330         public String JavaDoc[] getTags() {
331             return new String JavaDoc[]{"lonely tag"};
332         }
333         
334         public void attachEnv(PropertyEnv env) {
335             this.env = env;
336             env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE);
337         }
338         
339         public boolean supportsCustomEditor() {
340             return false;
341         }
342         
343         public void setValue(Object JavaDoc newValue) {
344             super.setValue(newValue);
345         }
346     }
347     
348     public static class EmptyTagsEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
349         PropertyEnv env;
350         
351         public EmptyTagsEditor() {
352         }
353         
354         public String JavaDoc[] getTags() {
355             return new String JavaDoc[0];
356         }
357         
358         public void attachEnv(PropertyEnv env) {
359             this.env = env;
360         }
361         
362         public boolean supportsCustomEditor() {
363             return false;
364         }
365         
366         public void setValue(Object JavaDoc newValue) {
367             super.setValue(newValue);
368         }
369     }
370     
371     public static class EditableEmptyTagsEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
372         PropertyEnv env;
373         
374         public EditableEmptyTagsEditor() {
375         }
376         
377         public String JavaDoc[] getTags() {
378             return new String JavaDoc[0];
379         }
380         
381         public void attachEnv(PropertyEnv env) {
382             this.env = env;
383             env.getFeatureDescriptor().setValue("canEditAsText", Boolean.TRUE);
384         }
385         
386         public boolean supportsCustomEditor() {
387             return false;
388         }
389         
390         public void setValue(Object JavaDoc newValue) {
391             super.setValue(newValue);
392         }
393     }
394     
395     
396     public static class BadEditorWithoutTags extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
397         PropertyEnv env;
398         
399         public BadEditorWithoutTags() {
400         }
401         
402         public String JavaDoc[] getTags() {
403             return null;
404         }
405         
406         public void attachEnv(PropertyEnv env) {
407             this.env = env;
408             env.setState(env.STATE_INVALID);
409         }
410         
411         public boolean supportsCustomEditor() {
412             return false;
413         }
414         
415         public void setValue(Object JavaDoc newValue) {
416             super.setValue(newValue);
417         }
418         
419         public Object JavaDoc getValue() {
420             return Boolean.FALSE;
421         }
422     }
423     
424     public static class BadEditorWithTags extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
425         PropertyEnv env;
426         
427         public BadEditorWithTags() {
428         }
429         
430         public String JavaDoc[] getTags() {
431             return new String JavaDoc[] {"a","b","c","d","Value"};
432         }
433         
434         public void attachEnv(PropertyEnv env) {
435             this.env = env;
436             env.setState(env.STATE_INVALID);
437         }
438         
439         public boolean supportsCustomEditor() {
440             return false;
441         }
442         
443         public void setValue(Object JavaDoc newValue) {
444             super.setValue(newValue);
445         }
446         
447         public Object JavaDoc getValue() {
448             return Boolean.FALSE;
449         }
450     }
451 }
452
Popular Tags