KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Graphics JavaDoc;
26 import java.awt.KeyboardFocusManager JavaDoc;
27 import java.awt.Window JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.util.MissingResourceException JavaDoc;
30 import javax.swing.AbstractButton JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import javax.swing.JDialog JavaDoc;
33 import javax.swing.JFrame JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
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 import org.openide.util.NbBundle;
41
42 // This test class tests the main functionality of the property sheet
43
public class RestoreDefaultValueTest extends ExtTestCase {
44     private PropertySheet ps = null;
45     public RestoreDefaultValueTest(String JavaDoc name) {
46         super(name);
47     }
48     
49     protected boolean runInEQ() {
50         return false;
51     }
52     
53     static {
54         ExtTestCase.installCorePropertyEditors();
55     }
56     
57     private static boolean setup = false;
58 /*
59  * This test creates a Property, Editor and Node. First test checks if initialized
60  * editor contains the same value as property. The second checks if the property
61  * value is changed if the same change will be done in the editor.
62  */

63     protected void setUp() throws Exception JavaDoc {
64         // Create new TNode
65
tn = new TNode();
66         
67         
68         //Replacing NodeOp w/ JFrame to eliminate depending on full IDE init
69
//and long delay while waiting for property sheet thus requested to
70
//initialize
71
final JFrame JavaDoc jf = new JFrame JavaDoc();
72         ps = new PropertySheet();
73         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
74         jf.getContentPane().add(ps, BorderLayout.CENTER);
75         jf.setLocation(30,30);
76         jf.setSize(500,500);
77         
78         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
79             public void run() {
80                 ps.setNodes(new Node[] {tn});
81                 //ps.setCurrentNode(tn);
82
jf.show();
83                 jf.toFront();
84             }
85         });
86         
87         new ExtTestCase.WaitWindow(jf);
88         
89         try {
90             ensurePainted(ps);
91         } catch (Exception JavaDoc e) {
92             fail("FAILED - Exception thrown "+e.getClass().toString());
93         }
94     }
95     
96     private void ensurePainted(final PropertySheet ps) throws Exception JavaDoc {
97         //issues 39205 & 39206 - ensure the property sheet really repaints
98
//before we get the value, or the value in the editor will not
99
//have changed
100
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
101             public void run() {
102                 Graphics JavaDoc g = ps.getGraphics();
103                 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight());
104             }
105         });
106     }
107     
108     
109     public void testRestoreDefaultValueWorks() throws Exception JavaDoc {
110         System.err.println("testRestoreDefaultValueWorks");
111         if (!super.canSafelyRunFocusTests()) {
112             return;
113         }
114         
115         sleep();
116         sleep();
117         sleep();
118         
119         assertNotNull("Property not created", tp);
120         
121         final Action JavaDoc invoke = ps.table.getActionMap().get("invokeCustomEditor");
122         assertNotNull("Incompatible change - no action from table for the key" +
123                 "\"invokeCustomEditor\".", invoke);
124         
125         pressCell(ps.table, 1, 1);
126         
127         Window JavaDoc w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
128         
129         
130         sleep();
131         sleep();
132         SwingUtilities.invokeLater(new Runnable JavaDoc() {
133             public void run() {
134                 invoke.actionPerformed(null);
135             }
136         });
137         
138         int ct = 0;
139         while (!(w instanceof JDialog JavaDoc)) {
140             if (ct++ > 10000) {
141                 fail("No dialog ever shown");
142             }
143             sleep();
144             w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
145         }
146         sleep();
147         System.out.println(" Dlg now showing");
148         final AbstractButton JavaDoc jb = findResetToDefaultButton((JDialog JavaDoc)w);
149         
150         if (jb == null) {
151             fail("Could not find Reset To Default Button in dialog");
152         }
153         
154         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
155             public void run() {
156                 jb.doClick();
157             }
158         });
159         
160         
161         
162         Thread.currentThread().sleep(10000);
163         
164     }
165     
166     private AbstractButton JavaDoc findResetToDefaultButton(JDialog JavaDoc jd) {
167         String JavaDoc txt = null;
168         try {
169             txt = NbBundle.getMessage(PropertyDialogManager.class, "CTL_Default");
170         } catch (MissingResourceException JavaDoc mre) {
171             mre.printStackTrace();
172             fail("Bundle key CTL_DEFAULT missing from org.openide.explorer.propertysheet.Bundle.properties");
173         }
174         return findButton(jd.getContentPane(), txt);
175     }
176     
177     private AbstractButton JavaDoc findButton(Container JavaDoc c, String JavaDoc s) {
178         if (c instanceof AbstractButton JavaDoc && s.equals(((AbstractButton JavaDoc) c).getText())) {
179             return ((AbstractButton JavaDoc) c);
180         } else {
181             Component JavaDoc[] cs = c.getComponents();
182             for (int i=0; i < cs.length; i++) {
183                 if (cs[i] instanceof Container JavaDoc) {
184                     AbstractButton JavaDoc result = findButton((Container JavaDoc) cs[i], s);
185                     if (result != null) {
186                         return result;
187                     }
188                 }
189             }
190         }
191         return null;
192     }
193     
194     //Node definition
195
public class TNode extends AbstractNode {
196         //create Node
197
public TNode() {
198             super(Children.LEAF);
199             setName("TNode"); // or, super.setName if needed
200
setDisplayName("TNode");
201         }
202         //clone existing Node
203
public Node cloneNode() {
204             return new TNode();
205         }
206         
207         public void destroy() {
208             fireNodeDestroyed();
209         }
210         
211         // Create a property sheet:
212
protected Sheet createSheet() {
213             Sheet sheet = super.createSheet();
214             // Make sure there is a "Properties" set:
215
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
216             if (props == null) {
217                 props = Sheet.createPropertiesSet();
218                 sheet.put(props);
219             }
220             tp = new TProperty("property", true);
221             props.put(tp);
222             return sheet;
223         }
224         // Method firing changes
225
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
226             firePropertyChange(s,o1,o2);
227         }
228     }
229     
230     // Property definition
231
public class TProperty extends PropertySupport {
232         private String JavaDoc myValue = "Value";
233         // Create new Property
234
public TProperty(String JavaDoc name, boolean isWriteable) {
235             super(name, String JavaDoc.class, name, "", true, isWriteable);
236         }
237         // get property value
238
public Object JavaDoc getValue() {
239             return myValue;
240         }
241         
242         // set property value
243
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
244             Object JavaDoc oldVal = myValue;
245             myValue = (String JavaDoc) value;
246             tn.fireMethod(getName(), oldVal, myValue);
247         }
248         
249         public boolean supportsDefaultValue() {
250             return true;
251         }
252         
253         private boolean defValue = false;
254         public boolean isDefaultValue() {
255             return defValue;
256         }
257         
258         private boolean rdv = false;
259         public void restoreDefaultValue() {
260             defValue = true;
261             rdv = true;
262             System.err.println("RestoreDefaultValue");
263             try {
264                 setValue("default");
265             } catch (Exception JavaDoc e) {
266                 e.printStackTrace();
267                 throw new RuntimeException JavaDoc(e.getMessage());
268             }
269         }
270         
271         public void assertRestoreDefaultValueCalled() {
272             assertTrue("Restore default value not called", rdv);
273             rdv = false;
274         }
275         
276         public void assertRestoreDefaultValueNotCalled() {
277             assertFalse("Restore default value not called", rdv);
278         }
279     }
280     
281     private TNode tn;
282     private TProperty tp;
283 }
284
Popular Tags