KickJava   Java API By Example, From Geeks To Geeks.

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


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

54     protected void setUp() throws Exception JavaDoc {
55         try {
56             // Create new TNode
57
tn = new TNode();
58             
59             //Replacing NodeOp w/ JFrame to eliminate depending on full IDE init
60
//and long delay while waiting for property sheet thus requested to
61
//initialize
62
final JFrame JavaDoc jf = new JFrame JavaDoc();
63             ps = new PropertySheet();
64             jf.getContentPane().setLayout(new BorderLayout JavaDoc());
65             jf.getContentPane().add(ps, BorderLayout.CENTER);
66             jf.setLocation(30,30);
67             jf.setSize(500,500);
68             new ExtTestCase.WaitWindow(jf);
69             
70             SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
71                 public void run() {
72                     ps.setNodes(new Node[] {tn});
73                 }
74             });
75             
76             sleep();
77             ensurePainted(ps);
78         } catch (Exception JavaDoc e) {
79             fail("FAILED - Exception thrown "+e.getClass().toString());
80         }
81     }
82     
83     private void ensurePainted(final PropertySheet ps) throws Exception JavaDoc {
84         //issues 39205 & 39206 - ensure the property sheet really repaints
85
//before we get the value, or the value in the editor will not
86
//have changed
87
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
88             public void run() {
89                 Graphics JavaDoc g = ps.getGraphics();
90                 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight());
91             }
92         });
93     }
94     
95     
96     public void testNullChangePerformedAndReflectedInPropertySheet() throws Exception JavaDoc {
97         System.err.println(".testNullChangePerformedAndReflectedInPropertySheet");
98         int count = ps.table.getRowCount();
99         assertTrue("Property sheet should contain three rows ", count==3);
100         L l = new L();
101         tn.addPropertyChangeListener(l);
102         
103         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
104             public void run() {
105                 tn.replaceProps();
106             }
107         });
108         Thread.currentThread().sleep(500);
109         // SwingUtilities.invokeAndWait (new Runnable(){public void run() {System.currentTimeMillis();}});
110

111         l.assertEventReceived();
112         
113         assertTrue("Should only be one property", tn.getPropertySets()[0].getProperties().length == 1);
114         sleep();
115         ensurePainted(ps);
116         int rc = ps.table.getRowCount();
117         assertTrue("Property sheet should now only show 2 rows, not " + rc, rc == 2);
118     }
119     
120     private Exception JavaDoc throwMe = null;
121     public void testSetSheetChangesPropertySheetContents() throws Exception JavaDoc {
122         System.err.println(".testSetSheetChangesPropertySheetContents");
123         final TNode2 tnd = new TNode2();
124         throwMe = null;
125         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
126             public void run() {
127                 try {
128                     ps.setNodes(new Node[] {tnd});
129                 } catch (Exception JavaDoc e) {
130                     throwMe = e;
131                 }
132             }
133         });
134         
135         if (throwMe != null) {
136             throw throwMe;
137         }
138         sleep();
139         
140         int rowCount = ps.table.getRowCount();
141         assertTrue("With a single property in a single property set, row count should be 2 but is " + rowCount, rowCount == 2);
142         
143         
144         L2 l2 = new L2();
145         tnd.addNodeListener(l2);
146         throwMe = null;
147         
148         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
149             public void run() {
150                 try {
151                     System.err.println("Replacing property sets");
152                     tnd.replaceSets();
153                 } catch (Exception JavaDoc e) {
154                     throwMe = e;
155                 }
156             }
157         });
158         if (throwMe != null) {
159             throw throwMe;
160         }
161         sleep();
162         
163         System.err.println("EnsurePainted");
164         ensurePainted(ps);
165         
166         System.err.println("Asserting event received");
167         l2.assertEventReceived();
168         
169         int nueCount = ps.table.getRowCount();
170         System.err.println("Checking count - it is " + nueCount);
171         assertTrue("With two properties in two property sets, row count should be 4 but is " + nueCount, nueCount == 4);
172         
173         
174     }
175     
176     private void sleep() throws Exception JavaDoc {
177         Thread.currentThread().sleep(500);
178         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
179             public void run() {
180                 System.currentTimeMillis();
181             }
182         });
183     }
184     
185     private class L implements PropertyChangeListener JavaDoc {
186         private boolean eventReceived = false;
187         
188         public void assertEventReceived() {
189             assertTrue("null null null property change not received on sets change", eventReceived);
190             eventReceived = false;
191         }
192         
193         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
194             if (evt.getPropertyName() == null && evt.getOldValue() == null && evt.getNewValue() == null) {
195                 eventReceived = true;
196             }
197             System.err.println("Event: " + evt);
198         }
199     }
200     
201     private class L2 extends NodeAdapter {
202         private boolean eventReceived = false;
203         
204         public void assertEventReceived() {
205             assertTrue("AbstractNode did not fire PROP_PROPERTY_SETS when setSheet() was called", eventReceived);
206             eventReceived = false;
207         }
208         
209         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
210             if (AbstractNode.PROP_PROPERTY_SETS.equals(evt.getPropertyName()) && evt.getOldValue() == null && evt.getNewValue() == null) {
211                 eventReceived = true;
212             }
213             System.err.println("Event: " + evt + " name " + evt.getPropertyName());
214         }
215     }
216     
217     //Node definition
218
public class TNode extends AbstractNode {
219         //create Node
220
public TNode() {
221             super(Children.LEAF);
222             setName("TNode"); // or, super.setName if needed
223
setDisplayName("TNode");
224         }
225         //clone existing Node
226
public Node cloneNode() {
227             return new TNode();
228         }
229         
230         public void replaceProps() {
231             sets = null;
232             firePropertyChange(null, null, null);
233         }
234         
235         Node.PropertySet[] sets = null;
236         
237         private boolean firstTime = true;
238         // Create a property sheet:
239
public Node.PropertySet[] getPropertySets() {
240             if (sets == null) {
241                 System.err.println("Create sheet");
242                 Sheet sheet = new Sheet();
243                 // Make sure there is a "Properties" set:
244
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
245                 props = Sheet.createPropertiesSet();
246                 sheet.put(props);
247                 TProperty tp = new TProperty("property", true);
248                 props.put(tp);
249                 if (firstTime) {
250                     props.put(new TProperty("second", true));
251                     System.err.println("first time");
252                     firstTime = false;
253                 } else {
254                     System.err.println("Second time");
255                 }
256                 sets = sheet.toArray();
257             }
258             return sets;
259         }
260         // Method firing changes
261
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
262             System.err.println("firing");
263         }
264     }
265     
266     //Node definition
267
public class TNode2 extends AbstractNode {
268         //create Node
269
public TNode2() {
270             super(Children.LEAF);
271             setName("TNode2"); // or, super.setName if needed
272
setDisplayName("TNode2");
273         }
274         //clone existing Node
275
public Node cloneNode() {
276             return new TNode();
277         }
278         
279         public void replaceSets() {
280             Sheet sheet = new Sheet();
281             Sheet.Set props = sheet.get(Sheet.PROPERTIES);
282             if (props == null) {
283                 props = Sheet.createPropertiesSet();
284             }
285             props.put(new TProperty("after - first", true));
286             sheet.put(props);
287             props = sheet.get(Sheet.EXPERT);
288             if (props == null) {
289                 props = Sheet.createExpertSet();
290             }
291             props.put(new TProperty("after - second", true));
292             sheet.put(props);
293             setSheet(sheet);
294         }
295         
296         // Create a property sheet:
297
protected Sheet createSheet() {
298             Sheet sheet = super.createSheet();
299             // Make sure there is a "Properties" set:
300
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
301             if (props == null) {
302                 props = Sheet.createPropertiesSet();
303                 sheet.put(props);
304             }
305             props.put(new TProperty("before", true));
306             return sheet;
307         }
308         
309         
310     }
311     
312     // Property definition
313
public class TProperty extends PropertySupport {
314         private Object JavaDoc myValue = "Value";
315         // Create new Property
316
public TProperty(String JavaDoc name, boolean isWriteable) {
317             super(name, String JavaDoc.class, name, "", true, isWriteable);
318         }
319         // get property value
320
public Object JavaDoc getValue() {
321             return myValue;
322         }
323         
324         // set property value
325
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
326             Object JavaDoc oldVal = myValue;
327             myValue = value;
328         }
329     }
330     
331     
332     private static TNode tn;
333     private static TProperty tp;
334     private static String JavaDoc initEditorValue;
335     private static String JavaDoc initPropertyValue;
336     private static String JavaDoc postChangePropertyValue;
337     private static String JavaDoc postChangeEditorValue;
338 }
339
Popular Tags