KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.beans.PropertyEditor JavaDoc;
26 import java.beans.PropertyEditorSupport JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import javax.swing.JFrame JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.openide.nodes.AbstractNode;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.Node;
34 import org.openide.nodes.PropertySupport;
35 import org.openide.nodes.Sheet;
36
37 // This test class tests the main functionality of the property sheet
38
public class PropertySheetTest extends NbTestCase {
39     public PropertySheetTest(String JavaDoc name) {
40         super(name);
41     }
42     
43     protected boolean runInEQ() {
44         return false;
45     }
46     
47     private static boolean setup = false;
48 /*
49  * This test creates a Property, Editor and Node. First test checks if initialized
50  * editor contains the same value as property. The second checks if the property
51  * value is changed if the same change will be done in the editor.
52  */

53     protected void setUp() throws Exception JavaDoc {
54         if (setup) return;
55         setup = true;
56         // Create new TestProperty
57
tp = new TProperty("TProperty", true);
58         // Create new TEditor
59
te = new TEditor();
60         // Create new TNode
61
tn = new TNode();
62         
63         System.err.println("RUNNING ON THREAD " + Thread.currentThread());
64         
65         //Replacing NodeOp w/ JFrame to eliminate depending on full IDE init
66
//and long delay while waiting for property sheet thus requested to
67
//initialize
68
final JFrame JavaDoc jf = new JFrame JavaDoc();
69         final PropertySheet ps = new PropertySheet();
70         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
71         jf.getContentPane().add(ps, BorderLayout.CENTER);
72         jf.setLocation(30,30);
73         jf.setSize(500,500);
74         
75         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
76             public void run() {
77                 ps.setNodes(new Node[] {tn});
78                 //ps.setCurrentNode(tn);
79
jf.show();
80             }
81         });
82         
83         // ps.setNodes(new Node[] {tn});
84

85         jf.show();
86         new ExtTestCase.WaitWindow(jf);
87         
88         
89         try {
90             // Thread.currentThread().sleep(500);
91

92         } catch (Exception JavaDoc e) {
93             
94         }
95         
96         System.err.println("Current node set ");
97         try {
98             
99             // Wait for the initialization
100
for (int i = 0; i < 10; i++) {
101                 if (te.getAsText().equals("null")) {
102                     System.err.println("Checking editor getAsText - " + te.getAsText());
103                     //System.out.println("null");
104
Thread.sleep(1000);
105                 } else break;
106             }
107             // Test if the initialization was sucessfull
108

109             initEditorValue = te.getAsText();
110             System.err.println("Got initial editor value " + initEditorValue);
111             
112             initPropertyValue = tp.getValue().toString();
113             System.err.println("Got initial property value " + initPropertyValue);
114             
115             
116             //Set new value to the Property
117
tp.setValue("Test2");
118             postChangePropertyValue = tp.getValue().toString();
119             
120             System.err.println("Post change property value is " + postChangePropertyValue);
121             
122             
123             // Wait for the reinitialization
124
for (int i = 0; i < 100; i++) {
125                 if (te.getAsText().equals(initEditorValue)) {
126                     //System.err.println(i + " value not updated ");;
127
Thread.sleep(50);
128                 } else {
129                     System.err.println("value was updated");
130                     break;
131                 }
132             }
133             
134             //issues 39205 & 39206 - ensure the property sheet really repaints
135
//before we get the value, or the value in the editor will not
136
//have changed
137
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
138                 public void run() {
139                     Graphics JavaDoc g = ps.getGraphics();
140                     ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight());
141                 }
142             });
143             
144             // Test if the reinitialization was sucessfull
145
postChangeEditorValue = te.getAsText();
146             System.err.println("postEditorChangeValue = " + postChangeEditorValue);
147             
148         } catch (Exception JavaDoc e) {
149             fail("FAILED - Exception thrown "+e.getClass().toString());
150         } finally {
151             jf.hide();
152             jf.dispose();
153         }
154     }
155     
156     public void testInitializeEditorValue() throws Exception JavaDoc {
157         assertTrue("Editor wasn't initialized successfuly (null) - value was " + initEditorValue,!initEditorValue.equals("null"));
158     }
159     
160     public void testPropertyEQEditorValueAfterInit() throws Exception JavaDoc {
161         assertEquals("Editor was initialized to the same value as the Property, value was " + initPropertyValue, initPropertyValue, initEditorValue);
162     }
163     
164     public void testSetPropertyValue() throws Exception JavaDoc {
165         assertTrue("Property value wasn't successfuly changed. Initial property value, " + initPropertyValue + " should not match " + postChangePropertyValue,!initPropertyValue.equals(postChangePropertyValue));
166     }
167     
168     public void testSetEditorValue() throws Exception JavaDoc {
169         assertTrue("Editor value wasn't changed successfuly. Initial editor value, " + initEditorValue + " should not match " + postChangeEditorValue,!initEditorValue.equals(postChangeEditorValue));
170     }
171     
172     public void testPropertyEQEditorValueAfterChange() throws Exception JavaDoc {
173         assertEquals("Editor value doesn't reflect the Property value. Post change property value, " + postChangePropertyValue + " should equal " + postChangeEditorValue, postChangePropertyValue, postChangeEditorValue);
174     }
175     
176     
177     //Node definition
178
public class TNode extends AbstractNode {
179         //create Node
180
public TNode() {
181             super(Children.LEAF);
182             setName("TNode"); // or, super.setName if needed
183
setDisplayName("TNode");
184         }
185         //clone existing Node
186
public Node cloneNode() {
187             return new TNode();
188         }
189         
190         // Create a property sheet:
191
protected Sheet createSheet() {
192             Sheet sheet = super.createSheet();
193             // Make sure there is a "Properties" set:
194
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
195             if (props == null) {
196                 props = Sheet.createPropertiesSet();
197                 sheet.put(props);
198             }
199             props.put(tp);
200             return sheet;
201         }
202         // Method firing changes
203
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
204             System.err.println("TNode firing change " + s + " from " + o1 + " to " + o2);
205             firePropertyChange(s,o1,o2);
206         }
207     }
208     
209     // Property definition
210
public class TProperty extends PropertySupport {
211         private Object JavaDoc myValue = "Value";
212         // Create new Property
213
public TProperty(String JavaDoc name, boolean isWriteable) {
214             super(name, Object JavaDoc.class, name, "", true, isWriteable);
215         }
216         // get property value
217
public Object JavaDoc getValue() {
218             return myValue;
219         }
220         
221         
222         
223         // set property value
224
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
225             System.err.println("TProperty setValue: " + value);
226             Object JavaDoc oldVal = myValue;
227             myValue = value;
228             System.err.println("TProperty triggering node property change");
229             tn.fireMethod(getName(), oldVal, myValue);
230         }
231         // get the property editor
232
public PropertyEditor JavaDoc getPropertyEditor() {
233             return te;
234         }
235     }
236     
237     // Editor definition
238
public class TEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
239         PropertyEnv env;
240         
241         // Create new TEditor
242
public TEditor() {
243         }
244         
245         /*
246          * This method is called by the IDE to pass
247          * the environment to the property editor.
248          */

249         public void attachEnv(PropertyEnv env) {
250             this.env = env;
251         }
252         
253         // Set that this Editor doesn't support custom Editor
254
public boolean supportsCustomEditor() {
255             return false;
256         }
257         
258         public void addPropertyChangeListener(PropertyChangeListener JavaDoc l) {
259             System.err.println("Property change listener added to property editor " + System.identityHashCode(this) + " - " + l);
260             super.addPropertyChangeListener(l);
261         }
262         
263         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
264             System.err.println("Property change listener removed from property editor " + System.identityHashCode(this) + " - " + l);
265             super.removePropertyChangeListener(l);
266         }
267         
268         
269         
270         // Set the Property value threw the Editor
271
public void setValue(Object JavaDoc newValue) {
272             System.err.println("TEditor.setValue: " + newValue);
273             super.setValue(newValue);
274         }
275         
276         public void firePropertyChange() {
277             System.err.println("TEditor.firePropertyChange");
278             super.firePropertyChange();
279         }
280     }
281     
282     private static TNode tn;
283     private static TProperty tp;
284     private static TEditor te;
285     private static String JavaDoc initEditorValue;
286     private static String JavaDoc initPropertyValue;
287     private static String JavaDoc postChangePropertyValue;
288     private static String JavaDoc postChangeEditorValue;
289 }
290
Popular Tags