KickJava   Java API By Example, From Geeks To Geeks.

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


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

60     protected void setUp() throws Exception JavaDoc {
61         // Create new TEditor
62
te = new TEditor();
63         // Create new TNode
64
tn = new TNode();
65         
66         //Replacing NodeOp w/ JFrame to eliminate depending on full IDE init
67
//and long delay while waiting for property sheet thus requested to
68
//initialize
69
final JFrame JavaDoc jf = new JFrame JavaDoc();
70         ps = new PropertySheet();
71         jf.getContentPane().setLayout(new BorderLayout JavaDoc());
72         jf.getContentPane().add(ps, BorderLayout.CENTER);
73         jf.setLocation(30,30);
74         jf.setSize(500,500);
75         
76         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
77             public void run() {
78                 ps.setNodes(new Node[] {tn});
79                 //ps.setCurrentNode(tn);
80
jf.show();
81             }
82         });
83         
84         jf.show();
85         new ExtTestCase.WaitWindow(jf);
86         
87         try {
88             // Wait for the initialization
89
for (int i = 0; i < 10; i++) {
90                 if (te.getAsText().equals("null")) {
91                     //System.out.println("null");
92
Thread.sleep(1000);
93                 } else break;
94             }
95             ensurePainted(ps);
96             
97         } catch (Exception JavaDoc e) {
98             fail("FAILED - Exception thrown "+e.getClass().toString());
99         }
100     }
101     
102     private void ensurePainted(final PropertySheet ps) throws Exception JavaDoc {
103         //issues 39205 & 39206 - ensure the property sheet really repaints
104
//before we get the value, or the value in the editor will not
105
//have changed
106
SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
107             public void run() {
108                 Graphics JavaDoc g = ps.getGraphics();
109                 ps.paintImmediately(0,0,ps.getWidth(), ps.getHeight());
110             }
111         });
112     }
113     
114     
115     public void testNodeDestroyClearsPropertySheet() throws Exception JavaDoc {
116         int count = ps.table.getRowCount();
117         assertTrue("Property sheet should contain one property set and one property but table has " + count + " rows", count==2);
118         tn.destroy();
119         Thread.currentThread().yield();
120         Thread.currentThread().sleep(500);
121         SwingUtilities.invokeAndWait(new Runnable JavaDoc() {
122             public void run() {
123                 System.currentTimeMillis();
124             }
125         });
126         int postCount = ps.table.getRowCount();
127         assertTrue("Property sheet should synchronously reflect node destruction" +
128                 " even if destroyed on a non EQ thread", postCount == 0);
129     }
130     
131     public void testProxyNodeReflectsNodeDestruction() throws Exception JavaDoc {
132         TNode node1 = new TNode();
133         TNode node2 = new TNode();
134         ProxyNode proxy = new ProxyNode(new Node[] {node1, node2});
135         L l = new L();
136         proxy.addNodeListener(l);
137         
138         node1.destroy();
139         assertTrue("Proxy node should not represent a destroyed node",
140                 !Arrays.asList(proxy.getOriginalNodes()).contains(node1));
141         
142         l.assertNotDestroyed();
143         
144         node2.destroy();
145         l.assertDestroyed();
146     }
147     
148     private class L implements NodeListener {
149         boolean destroyed = false;
150         
151         public void assertDestroyed() {
152             assertTrue("Node was not destroyed", destroyed);
153         }
154         
155         public void assertNotDestroyed() {
156             assertTrue("Node was not destroyed", !destroyed);
157         }
158         
159         public void childrenAdded(NodeMemberEvent ev) {
160         }
161         
162         public void childrenRemoved(NodeMemberEvent ev) {
163         }
164         
165         public void childrenReordered(NodeReorderEvent ev) {
166         }
167         
168         public void nodeDestroyed(NodeEvent ev) {
169             destroyed = true;
170         }
171         
172         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
173         }
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         public void destroy() {
191             fireNodeDestroyed();
192         }
193         
194         // Create a property sheet:
195
protected Sheet createSheet() {
196             Sheet sheet = super.createSheet();
197             // Make sure there is a "Properties" set:
198
Sheet.Set props = sheet.get(Sheet.PROPERTIES);
199             if (props == null) {
200                 props = Sheet.createPropertiesSet();
201                 sheet.put(props);
202             }
203             TProperty tp = new TProperty("property", true);
204             props.put(tp);
205             return sheet;
206         }
207         // Method firing changes
208
public void fireMethod(String JavaDoc s, Object JavaDoc o1, Object JavaDoc o2) {
209             firePropertyChange(s,o1,o2);
210         }
211     }
212     
213     // Property definition
214
public class TProperty extends PropertySupport {
215         private Object JavaDoc myValue = "Value";
216         // Create new Property
217
public TProperty(String JavaDoc name, boolean isWriteable) {
218             super(name, Object JavaDoc.class, name, "", true, isWriteable);
219         }
220         // get property value
221
public Object JavaDoc getValue() {
222             return myValue;
223         }
224         
225         // set property value
226
public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc,IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
227             Object JavaDoc oldVal = myValue;
228             myValue = value;
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             super.addPropertyChangeListener(l);
260         }
261         
262         public void removePropertyChangeListener(PropertyChangeListener JavaDoc l) {
263             super.removePropertyChangeListener(l);
264         }
265         
266         
267         
268         // Set the Property value threw the Editor
269
public void setValue(Object JavaDoc newValue) {
270             super.setValue(newValue);
271         }
272         
273         public void firePropertyChange() {
274             super.firePropertyChange();
275         }
276     }
277     
278     private TNode tn;
279     private TProperty tp;
280     private TEditor te;
281 }
282
Popular Tags