KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > viewmodel > ModelEventTest


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.netbeans.modules.viewmodel;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Vector JavaDoc;
27 import javax.swing.AbstractAction JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.modules.viewmodel.TreeModelNode;
32 import org.netbeans.modules.viewmodel.TreeModelRoot;
33 import org.netbeans.modules.viewmodel.TreeTable;
34 import org.netbeans.spi.viewmodel.*;
35 import org.openide.nodes.Node;
36 import org.openide.nodes.NodeListener;
37 import org.openide.util.RequestProcessor;
38
39
40
41 /**
42  *
43  */

44 public class ModelEventTest extends NbTestCase implements NodeListener {
45
46     BasicTest.CompoundModel cm;
47     Node n;
48     volatile Object JavaDoc event;
49     Vector JavaDoc propEvents = new Vector JavaDoc();
50
51     public ModelEventTest (String JavaDoc s) {
52         super (s);
53     }
54     
55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57         ArrayList JavaDoc l = new ArrayList JavaDoc ();
58         cm = new CompoundModel1 ();
59         l.add (cm);
60         TreeTable tt = (TreeTable) Models.createView
61             (Models.createCompoundModel (l));
62         BasicTest.waitFinished ();
63         n = tt.getExplorerManager ().
64             getRootContext ();
65         n.addNodeListener(this);
66     }
67
68     public void childrenAdded(org.openide.nodes.NodeMemberEvent ev) {
69         assertNull("Already fired", event);
70         event = ev;
71     }
72
73     public void childrenRemoved(org.openide.nodes.NodeMemberEvent ev) {
74         assertNull("Already fired", event);
75         event = ev;
76     }
77
78     public void childrenReordered(org.openide.nodes.NodeReorderEvent ev) {
79         assertNull("Already fired", event);
80         event = ev;
81     }
82
83     public void nodeDestroyed(org.openide.nodes.NodeEvent ev) {
84         assertNull("Already fired", event);
85         event = ev;
86     }
87
88     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc propertyChangeEvent) {
89         propEvents.add(propertyChangeEvent.getPropertyName());
90         /*
91         System.out.println("propertyChangeEvent = "+propertyChangeEvent);
92         assertNull("Already fired", event);
93         event = propertyChangeEvent;
94          */

95     }
96     
97     public void testDisplayName() {
98         ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.DISPLAY_NAME_MASK);
99         cm.fire(e);
100         try {
101             SwingUtilities.invokeAndWait (new Runnable JavaDoc () {
102                 public void run () {}
103             });
104         } catch (InterruptedException JavaDoc iex) {
105             fail(iex.toString());
106         } catch (InvocationTargetException JavaDoc itex) {
107             fail(itex.toString());
108         }
109         assertTrue("Display Name was not fired", propEvents.contains(Node.PROP_DISPLAY_NAME));
110         //assertNotNull("Was not fired", this.event);
111
}
112
113     public void testIcon() {
114         ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.ICON_MASK);
115         cm.fire(e);
116         try {
117             SwingUtilities.invokeAndWait (new Runnable JavaDoc () {
118                 public void run () {}
119             });
120         } catch (InterruptedException JavaDoc iex) {
121             fail(iex.toString());
122         } catch (InvocationTargetException JavaDoc itex) {
123             fail(itex.toString());
124         }
125         assertTrue("Icon was not fired", propEvents.contains(Node.PROP_ICON));
126     }
127
128     public void testShortDescription() {
129         ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.SHORT_DESCRIPTION_MASK);
130         cm.fire(e);
131         try {
132             SwingUtilities.invokeAndWait (new Runnable JavaDoc () {
133                 public void run () {}
134             });
135         } catch (InterruptedException JavaDoc iex) {
136             fail(iex.toString());
137         } catch (InvocationTargetException JavaDoc itex) {
138             fail(itex.toString());
139         }
140         assertTrue("Short Description was not fired", propEvents.contains(Node.PROP_SHORT_DESCRIPTION));
141     }
142
143     public void testChildren() {
144         n.getChildren().getNodes();
145         /*
146         ModelEvent e = new ModelEvent.NodeChanged(this, "Root", ModelEvent.NodeChanged.CHILDREN_MASK);
147         cm.fire(e);
148         try {
149             SwingUtilities.invokeAndWait (new Runnable () {
150                 public void run () {}
151             });
152         } catch (InterruptedException iex) {
153             fail(iex.toString());
154         } catch (InvocationTargetException itex) {
155             fail(itex.toString());
156         }
157         //assertTrue("Short Description was not fired", propEvents.contains(Node.PROP_));
158         assertNotNull("Children were not fired", this.event);
159          */

160     }
161
162     public final class CompoundModel1 extends BasicTest.CompoundModel {
163         
164         int dn = 0;
165         int ib = 0;
166         int sd = 0;
167         int cc = 0;
168         
169         protected void addCall (String JavaDoc methodName, Object JavaDoc node) {
170             // Ignore multiple calls
171
}
172
173         // init ....................................................................
174

175         public String JavaDoc getDisplayName (Object JavaDoc node) throws UnknownTypeException {
176             String JavaDoc dns = super.getDisplayName(node);
177             dns += (dn++);
178             return dns;
179         }
180         
181         public String JavaDoc getIconBase (Object JavaDoc node) throws UnknownTypeException {
182             String JavaDoc ibs = super.getIconBase(node);
183             ibs += (ib++);
184             return ibs;
185         }
186         
187         public String JavaDoc getShortDescription (Object JavaDoc node) throws UnknownTypeException {
188             String JavaDoc sds = super.getShortDescription(node);
189             sds += (sd++);
190             return sds;
191         }
192         
193         /**
194          * Returns number of children for given node.
195          *
196          * @param node the parent node
197          * @throws UnknownTypeException if this TreeModel implementation is not
198          * able to resolve children for given node type
199          *
200          * @return true if node is leaf
201          */

202         public synchronized int getChildrenCount (Object JavaDoc node) throws UnknownTypeException {
203             return super.getChildrenCount (node) + (cc++);
204         }
205         
206         public Object JavaDoc[] getChildren (Object JavaDoc parent, int from, int to) throws UnknownTypeException {
207             System.err.println("CompoundModel1.getChildren("+parent+", "+from+", "+to+")");
208             //Thread.dumpStack();
209
addCall ("getChildren", parent);
210             Object JavaDoc[] ch = new Object JavaDoc[3 + (cc - 1)];
211             if (parent == ROOT) {
212                 for (int i = 0; i < ch.length; i++) {
213                     // Use Character.valueOf() on 1.5
214
ch[i] = new Character JavaDoc((char) ('a' + i)).toString();
215                 }
216                 return ch;
217             }
218             if (parent instanceof String JavaDoc) {
219                 for (int i = 0; i < ch.length; i++) {
220                     // Use Character.valueOf() on 1.5
221
ch[i] = ((String JavaDoc) parent + new Character JavaDoc((char) ('a' + i)).toString());
222                 }
223                 return ch;
224             }
225             throw new UnknownTypeException (parent);
226         }
227     }
228 }
229
Popular Tags