KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
24 import java.awt.Container JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import org.openide.explorer.propertysheet.ExtTestCase.WaitWindow;
30 import org.openide.nodes.AbstractNode;
31 import org.openide.nodes.Children;
32 import org.openide.nodes.Node;
33 import org.openide.nodes.PropertySupport;
34 import org.openide.nodes.Sheet;
35 import org.openide.util.HelpCtx;
36
37 /** Test finding help IDs in the property sheet.
38  * @author Jesse Glick
39  * @see "#14701"
40  */

41 public class FindHelpTest extends ExtTestCase {
42     
43     public FindHelpTest(String JavaDoc name) {
44         super(name);
45     }
46     
47     protected boolean runInEQ() {
48         return false;
49     }
50     
51     private PropertySheet sheet = null;
52     private JFrame JavaDoc frame = null;
53     protected void setUp() throws Exception JavaDoc {
54         JFrame JavaDoc jf = new JFrame JavaDoc();
55         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
56         sheet = new PropertySheet();
57         jf.getContentPane().add(sheet);
58         
59         jf.setBounds(20, 20, 200, 400);
60         frame = jf;
61         new WaitWindow(jf);
62     }
63     
64     public void testFindHelpOnProperty() throws Exception JavaDoc {
65         Node n = new WithPropertyHelpNode();
66         setCurrentNode(n, sheet);
67         
68         sleep();
69         
70         PropertySheet.HelpAction act = sheet.helpAction;
71         
72         assertTrue("No help context found", act.getContext() != null);
73         
74         assertTrue("Help action should be enabled", act.isEnabled());
75         
76     }
77     
78     public void testFindPropertiesHelpOnNode() throws Exception JavaDoc {
79         Node n = new WithPropertiesHelpNode();
80         setCurrentNode(n, sheet);
81         
82         sleep();
83         
84         PropertySheet.HelpAction act = sheet.helpAction;
85         
86         assertTrue("No help context found", act.getContext() != null);
87         
88         assertTrue("Help action should be enabled", act.isEnabled());
89     }
90     
91     public void testNoHelpProvided() throws Exception JavaDoc {
92         Node n = new HelplessNode();
93         setCurrentNode(n, sheet);
94         
95         sleep();
96         
97         PropertySheet.HelpAction act = sheet.helpAction;
98         
99         assertFalse("A help context was found on a node with no properties help", act.getContext() != null);
100         
101         assertFalse("Help action should be disabled", act.isEnabled());
102         
103     }
104     
105     public void testSetHelpProvided() throws Exception JavaDoc {
106         Node n = new WithTabsSetHelpNode();
107         setCurrentNode(n, sheet);
108         
109         sleep();
110         
111         PropertySheet.HelpAction act = sheet.helpAction;
112         
113         HelpCtx ctx = act.getContext();
114         assertTrue("A help context should have been found", ctx != null);
115         
116         assertTrue("Wrong help context returned: " + ctx.getHelpID(), "set-help-id".equals(ctx.getHelpID()));
117     }
118     
119     // XXX test use of ExPropertyEditor.PROPERTY_HELP_ID
120

121     private static Collection JavaDoc findChildren(Component JavaDoc p, Class JavaDoc c) {
122         Collection JavaDoc x = new LinkedList JavaDoc();
123         findChildren(p, c, x);
124         return x;
125     }
126     
127     private static void findChildren(Component JavaDoc p, Class JavaDoc c, Collection JavaDoc x) {
128         if (c.isInstance(p)) {
129             x.add(p);
130         } else if (p instanceof Container JavaDoc) {
131             Component JavaDoc[] k = ((Container JavaDoc)p).getComponents();
132             for (int i = 0; i < k.length; i++) {
133                 findChildren(k[i], c, x);
134             }
135         }
136     }
137     
138     /**
139      * A node which provides no help - the help action should always be disabled
140      */

141     private static final class HelplessNode extends AbstractNode {
142         public HelplessNode() {
143             super(Children.LEAF);
144         }
145         public HelpCtx getHelpCtx() {
146             return HelpCtx.DEFAULT_HELP;
147         }
148         protected Sheet createSheet() {
149             Sheet s = super.createSheet();
150             Sheet.Set ss = Sheet.createPropertiesSet();
151             ss.put(new WithHelpProperty("prop1", "row-help-1"));
152             ss.put(new WithHelpProperty("prop2", "row-help-2"));
153             ss.put(new WithHelpProperty("prop3", null));
154             s.put(ss);
155             ss = Sheet.createExpertSet();
156             ss.put(new WithHelpProperty("prop4", "row-help-4"));
157             ss.put(new WithHelpProperty("prop5", null));
158             s.put(ss);
159             return s;
160         }
161     }
162     
163     /**
164      * A node whose properties provide their own help IDs
165      */

166     private static final class WithPropertyHelpNode extends AbstractNode {
167         public WithPropertyHelpNode() {
168             super(Children.LEAF);
169         }
170         public HelpCtx getHelpCtx() {
171             return new HelpCtx("node-help");
172         }
173         protected Sheet createSheet() {
174             Sheet s = super.createSheet();
175             Sheet.Set ss = Sheet.createPropertiesSet();
176             ss.setValue("helpID", "properties-help");
177             ss.put(new WithHelpProperty("prop1", "row-help-1"));
178             ss.put(new WithHelpProperty("prop2", "row-help-2"));
179             ss.put(new WithHelpProperty("prop3", null));
180             s.put(ss);
181             ss = Sheet.createExpertSet();
182             ss.put(new WithHelpProperty("prop4", "row-help-4"));
183             ss.put(new WithHelpProperty("prop5", null));
184             s.put(ss);
185             return s;
186         }
187     }
188     
189     /**
190      * A node which uses the per-node key for property sheet specific help -
191      * the help action should be enabled for all its properties
192      */

193     private static final class WithPropertiesHelpNode extends AbstractNode {
194         public WithPropertiesHelpNode() {
195             super(Children.LEAF);
196             setValue("propertiesHelpID", "propertiesHelp");
197         }
198         public HelpCtx getHelpCtx() {
199             return new HelpCtx("node-help");
200         }
201         protected Sheet createSheet() {
202             Sheet s = super.createSheet();
203             Sheet.Set ss = Sheet.createPropertiesSet();
204             ss.put(new WithoutHelpProperty("prop1"));
205             ss.put(new WithoutHelpProperty("prop2"));
206             ss.put(new WithoutHelpProperty("prop3"));
207             s.put(ss);
208             ss = Sheet.createExpertSet();
209             ss.put(new WithoutHelpProperty("prop4"));
210             ss.put(new WithoutHelpProperty("prop5"));
211             s.put(ss);
212             return s;
213         }
214     }
215     
216     private static final class WithTabsSetHelpNode extends AbstractNode {
217         public WithTabsSetHelpNode() {
218             super(Children.LEAF);
219         }
220         public HelpCtx getHelpCtx() {
221             return new HelpCtx("node-help");
222         }
223         protected Sheet createSheet() {
224             Sheet s = super.createSheet();
225             Sheet.Set ss = Sheet.createPropertiesSet();
226             ss.put(new WithoutHelpProperty("prop1"));
227             ss.put(new WithoutHelpProperty("prop2"));
228             ss.put(new WithoutHelpProperty("prop3"));
229             ss.setValue("tabName", "Tab 1");
230             ss.setValue("helpID", "set-help-id");
231             s.put(ss);
232             ss = Sheet.createExpertSet();
233             ss.put(new WithoutHelpProperty("prop4"));
234             ss.put(new WithoutHelpProperty("prop5"));
235             ss.setValue("tabName", "Tab 2");
236             s.put(ss);
237             return s;
238         }
239     }
240     
241     
242     private static final class WithHelpProperty extends PropertySupport.ReadOnly {
243         public WithHelpProperty(String JavaDoc name, String JavaDoc helpID) {
244             super(name, String JavaDoc.class, name, name);
245             if (helpID != null) {
246                 setValue("helpID", helpID);
247             }
248         }
249         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
250             return "value-" + getName();
251         }
252     }
253     
254     private static final class WithoutHelpProperty extends PropertySupport.ReadOnly {
255         public WithoutHelpProperty(String JavaDoc name) {
256             super(name, String JavaDoc.class, name, name);
257         }
258         public Object JavaDoc getValue() throws IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
259             return "value-" + getName();
260         }
261     }
262     
263     
264 }
265
Popular Tags