KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > MVInnerComponentGetLookupTest


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.core.multiview;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.DefaultKeyboardFocusManager JavaDoc;
25 import java.awt.KeyboardFocusManager JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.beans.FeatureDescriptor JavaDoc;
28 import java.beans.PropertyChangeListener JavaDoc;
29 import java.util.*;
30 import javax.swing.AbstractAction JavaDoc;
31 import javax.swing.ActionMap JavaDoc;
32 import javax.swing.JTextField JavaDoc;
33
34 import junit.framework.*;
35 import org.netbeans.core.api.multiview.MultiViewHandler;
36 import org.netbeans.core.api.multiview.MultiViews;
37 import org.netbeans.core.spi.multiview.MultiViewDescription;
38 import org.netbeans.core.spi.multiview.MultiViewFactory;
39
40 import org.netbeans.junit.*;
41 import org.openide.nodes.Node;
42
43 import org.openide.util.lookup.AbstractLookup;
44 import org.openide.util.lookup.InstanceContent;
45 import org.openide.windows.TopComponent;
46
47 /**
48  *
49  * @author Milos Kleint
50  */

51 public class MVInnerComponentGetLookupTest extends org.openide.windows.TopComponentGetLookupTest {
52     
53     private TopComponent mvtc;
54     private TopComponent top2;
55     private TopComponent top3;
56     MultiViewDescription desc1;
57     MultiViewDescription desc2;
58     MultiViewDescription desc3;
59     
60     public MVInnerComponentGetLookupTest(String JavaDoc testName) {
61         super(testName);
62     }
63     
64     
65     public static void main(String JavaDoc[] args) {
66         junit.textui.TestRunner.run(suite());
67     }
68     
69     public static Test suite() {
70         return new NbTestSuite(MVInnerComponentGetLookupTest.class);
71     }
72     
73     
74     protected boolean runInEQ () {
75         return true;
76     }
77     
78     protected int checkAtLeastCount() {
79         return 0;
80     }
81     
82     
83     /** Setup component with lookup.
84      */

85     protected void setUp () {
86         final MVElemTopComponent elem1 = new MVElemTopComponent();
87         final MVElemTopComponent elem2 = new MVElemTopComponent();
88         final MVElemTopComponent elem3 = new MVElemTopComponent();
89         desc1 = new MVDesc("desc1", null, 0, elem1);
90         desc2 = new MVDesc("desc2", null, 0, elem2);
91         desc3 = new MVDesc("desc3", null, 0, elem3);
92         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
93         TopComponent mvtop = MultiViewFactory.createMultiView(descs, desc1);
94         top = (TopComponent)elem1;
95         top2 = (TopComponent)elem2;
96         top3 = (TopComponent)elem3;
97         lookup = mvtop.getLookup();
98         mvtop.open();
99         mvtop.requestActive();
100         mvtc = mvtop;
101     }
102     
103     public void testMVTCActivatedNodes() throws Exception JavaDoc {
104         ProChange change = new ProChange();
105         
106         TopComponent.getRegistry().addPropertyChangeListener(change);
107         Node[] nodes = new Node[] {new N("one"), new N("two")};
108         
109         assertEquals(TopComponent.getRegistry().getActivated(), mvtc);
110         top.setActivatedNodes(nodes);
111         Node[] ret = mvtc.getActivatedNodes();
112         assertNotNull(ret);
113         assertEquals(ret.length, 2);
114         
115         assertTrue(ret[0] == nodes[1] || ret[0] == nodes[0]);
116         assertTrue(ret[1] == nodes[0] || ret[1] == nodes[1]);
117         Node[] activ = TopComponent.getRegistry().getActivatedNodes();
118         assertEquals(activ.length, 2);
119         assertTrue(activ[0] == nodes[1] || activ[0] == nodes[0]);
120         assertTrue(activ[1] == nodes[0] || activ[1] == nodes[1]);
121 // assertEquals(1, change.count);
122

123         
124         
125         Node[] nodes2 = new Node[] {new N("three")};
126         top.setActivatedNodes(nodes2);
127         ret = mvtc.getActivatedNodes();
128         assertNotNull(ret);
129         assertEquals(ret.length, 1);
130         assertEquals(ret[0], nodes2[0]);
131         activ = TopComponent.getRegistry().getActivatedNodes();
132         assertEquals(activ.length, 1);
133         assertEquals(activ[0], nodes2[0]);
134 // assertEquals(2, change.count);
135

136     }
137     
138     public void testMVTCActivatedNodesOnElementChange() throws Exception JavaDoc {
139         Node[] nodes1 = new Node[] {new N("one"), new N("two")};
140         Node[] nodes2 = new Node[] {new N("three"), new N("four"), new N("five")};
141         Node[] nodes3 = new Node[] {new N("six")};
142         top.setActivatedNodes(nodes1);
143         top2.setActivatedNodes(nodes2);
144         top3.setActivatedNodes(nodes3);
145
146         assertEquals(TopComponent.getRegistry().getActivated(), mvtc);
147         // first element selected now..
148
Node[] ret = mvtc.getActivatedNodes();
149         assertNotNull(ret);
150         assertEquals(ret.length, 2);
151         
152         MultiViewHandler handler = MultiViews.findMultiViewHandler(mvtc);
153         // test related hack, easy establishing a connection from Desc->perspective
154
handler.requestActive(Accessor.DEFAULT.createPerspective(desc2));
155         ret = mvtc.getActivatedNodes();
156         assertNotNull(ret);
157         assertEquals(ret.length, 3);
158         handler.requestActive(Accessor.DEFAULT.createPerspective(desc3));
159         ret = mvtc.getActivatedNodes();
160         assertNotNull(ret);
161         assertEquals(ret.length, 1);
162         handler.requestActive(Accessor.DEFAULT.createPerspective(desc1));
163         ret = mvtc.getActivatedNodes();
164         assertNotNull(ret);
165         assertEquals(ret.length, 2);
166         
167     }
168
169     
170     private class ProChange implements PropertyChangeListener JavaDoc {
171         public int count = 0;
172         
173         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
174             if ("activatedNodes".equals(evt.getPropertyName())) {
175                 count = count + 1;
176             }
177         }
178         
179     }
180     
181 }
182
Popular Tags