001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.core.multiview;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Component;
046: import java.awt.DefaultKeyboardFocusManager;
047: import java.awt.KeyboardFocusManager;
048: import java.awt.event.ActionEvent;
049: import java.beans.FeatureDescriptor;
050: import java.beans.PropertyChangeListener;
051: import java.util.*;
052: import javax.swing.AbstractAction;
053: import javax.swing.ActionMap;
054: import javax.swing.JTextField;
055:
056: import junit.framework.*;
057: import org.netbeans.core.api.multiview.MultiViewHandler;
058: import org.netbeans.core.api.multiview.MultiViews;
059: import org.netbeans.core.spi.multiview.MultiViewDescription;
060: import org.netbeans.core.spi.multiview.MultiViewFactory;
061:
062: import org.netbeans.junit.*;
063: import org.openide.nodes.Node;
064:
065: import org.openide.util.lookup.AbstractLookup;
066: import org.openide.util.lookup.InstanceContent;
067: import org.openide.windows.TopComponent;
068:
069: /**
070: *
071: * @author Milos Kleint
072: */
073: public class MVInnerComponentGetLookupTest extends
074: org.openide.windows.TopComponentGetLookupTest {
075:
076: private TopComponent mvtc;
077: private TopComponent top2;
078: private TopComponent top3;
079: MultiViewDescription desc1;
080: MultiViewDescription desc2;
081: MultiViewDescription desc3;
082:
083: public MVInnerComponentGetLookupTest(String testName) {
084: super (testName);
085: }
086:
087: public static void main(String[] args) {
088: junit.textui.TestRunner.run(suite());
089: }
090:
091: public static Test suite() {
092: return new NbTestSuite(MVInnerComponentGetLookupTest.class);
093: }
094:
095: protected boolean runInEQ() {
096: return true;
097: }
098:
099: protected int checkAtLeastCount() {
100: return 0;
101: }
102:
103: /** Setup component with lookup.
104: */
105: protected void setUp() {
106: final MVElemTopComponent elem1 = new MVElemTopComponent();
107: final MVElemTopComponent elem2 = new MVElemTopComponent();
108: final MVElemTopComponent elem3 = new MVElemTopComponent();
109: desc1 = new MVDesc("desc1", null, 0, elem1);
110: desc2 = new MVDesc("desc2", null, 0, elem2);
111: desc3 = new MVDesc("desc3", null, 0, elem3);
112: MultiViewDescription[] descs = new MultiViewDescription[] {
113: desc1, desc2, desc3 };
114: TopComponent mvtop = MultiViewFactory.createMultiView(descs,
115: desc1);
116: top = (TopComponent) elem1;
117: get = top;
118: top2 = (TopComponent) elem2;
119: top3 = (TopComponent) elem3;
120: lookup = mvtop.getLookup();
121: mvtop.open();
122: mvtop.requestActive();
123: mvtc = mvtop;
124: }
125:
126: public void testMVTCActivatedNodes() throws Exception {
127: ProChange change = new ProChange();
128:
129: TopComponent.getRegistry().addPropertyChangeListener(change);
130: Node[] nodes = new Node[] { new N("one"), new N("two") };
131:
132: assertEquals(TopComponent.getRegistry().getActivated(), mvtc);
133: top.setActivatedNodes(nodes);
134: Node[] ret = mvtc.getActivatedNodes();
135: assertNotNull(ret);
136: assertEquals(ret.length, 2);
137:
138: assertTrue(ret[0] == nodes[1] || ret[0] == nodes[0]);
139: assertTrue(ret[1] == nodes[0] || ret[1] == nodes[1]);
140: Node[] activ = TopComponent.getRegistry().getActivatedNodes();
141: assertEquals(activ.length, 2);
142: assertTrue(activ[0] == nodes[1] || activ[0] == nodes[0]);
143: assertTrue(activ[1] == nodes[0] || activ[1] == nodes[1]);
144: // assertEquals(1, change.count);
145:
146: Node[] nodes2 = new Node[] { new N("three") };
147: top.setActivatedNodes(nodes2);
148: ret = mvtc.getActivatedNodes();
149: assertNotNull(ret);
150: assertEquals(ret.length, 1);
151: assertEquals(ret[0], nodes2[0]);
152: activ = TopComponent.getRegistry().getActivatedNodes();
153: assertEquals(activ.length, 1);
154: assertEquals(activ[0], nodes2[0]);
155: // assertEquals(2, change.count);
156:
157: }
158:
159: public void testMVTCActivatedNodesOnElementChange()
160: throws Exception {
161: Node[] nodes1 = new Node[] { new N("one"), new N("two") };
162: Node[] nodes2 = new Node[] { new N("three"), new N("four"),
163: new N("five") };
164: Node[] nodes3 = new Node[] { new N("six") };
165: top.setActivatedNodes(nodes1);
166: top2.setActivatedNodes(nodes2);
167: top3.setActivatedNodes(nodes3);
168:
169: assertEquals(TopComponent.getRegistry().getActivated(), mvtc);
170: // first element selected now..
171: Node[] ret = mvtc.getActivatedNodes();
172: assertNotNull(ret);
173: assertEquals(ret.length, 2);
174:
175: MultiViewHandler handler = MultiViews
176: .findMultiViewHandler(mvtc);
177: // test related hack, easy establishing a connection from Desc->perspective
178: handler
179: .requestActive(Accessor.DEFAULT
180: .createPerspective(desc2));
181: ret = mvtc.getActivatedNodes();
182: assertNotNull(ret);
183: assertEquals(ret.length, 3);
184: handler
185: .requestActive(Accessor.DEFAULT
186: .createPerspective(desc3));
187: ret = mvtc.getActivatedNodes();
188: assertNotNull(ret);
189: assertEquals(ret.length, 1);
190: handler
191: .requestActive(Accessor.DEFAULT
192: .createPerspective(desc1));
193: ret = mvtc.getActivatedNodes();
194: assertNotNull(ret);
195: assertEquals(ret.length, 2);
196:
197: }
198:
199: private class ProChange implements PropertyChangeListener {
200: public int count = 0;
201:
202: public void propertyChange(java.beans.PropertyChangeEvent evt) {
203: if ("activatedNodes".equals(evt.getPropertyName())) {
204: count = count + 1;
205: }
206: }
207:
208: }
209:
210: }
|