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.openide.windows;
043:
044: import java.awt.KeyboardFocusManager;
045: import java.util.ArrayList;
046: import java.util.Arrays;
047: import java.util.logging.Logger;
048: import javax.swing.ActionMap;
049: import javax.swing.text.DefaultEditorKit;
050:
051: import junit.framework.*;
052:
053: import org.netbeans.junit.*;
054: import org.openide.explorer.*;
055: import org.openide.nodes.AbstractNode;
056: import org.openide.nodes.Children;
057: import org.openide.nodes.FilterNode;
058: import org.openide.nodes.Node;
059: import org.openide.util.Lookup;
060: import org.openide.util.lookup.AbstractLookup;
061: import org.openide.util.lookup.InstanceContent;
062:
063: /** Tests behaviour of GlobalContextProviderImpl
064: * and its cooperation with activated and current nodes when TopComponent is
065: * using its own lookup as in examples of ExplorerUtils...
066: *
067: * @author Jaroslav Tulach
068: */
069: public class TopComponentGetLookupOverridenTest extends
070: TopComponentGetLookupTest {
071: private Logger LOG = Logger
072: .getLogger(TopComponentGetLookupOverridenTest.class
073: + ".TEST-" + getName());
074:
075: public TopComponentGetLookupOverridenTest(java.lang.String testName) {
076: super (testName);
077: }
078:
079: /** Setup component with lookup.
080: */
081: protected void setUp() {
082: ListingYourComponent tc = new ListingYourComponent(LOG);
083: top = tc;
084: get = tc.delegate;
085: lookup = tc.delegate.getLookup();
086: }
087:
088: private static class ListingYourComponent extends TopComponent
089: implements java.beans.PropertyChangeListener {
090: YourComponent delegate;
091: private Logger LOG;
092:
093: public ListingYourComponent(Logger l) {
094: delegate = new YourComponent();
095: LOG = l;
096:
097: addPropertyChangeListener(this );
098: delegate.getExplorerManager().setRootContext(
099: new AbstractNode(new Children.Array()));
100: java.lang.ref.SoftReference ref = new java.lang.ref.SoftReference(
101: new Object());
102: assertGC(
103: "Trying to simulate issue 40842, to GC TopComponent$SynchronizeNodes",
104: ref);
105:
106: delegate.getExplorerManager().addPropertyChangeListener(
107: this );
108: }
109:
110: private ThreadLocal callbacks = new ThreadLocal();
111:
112: public void propertyChange(java.beans.PropertyChangeEvent ev) {
113: ExplorerManager manager = delegate.getExplorerManager();
114:
115: LOG.info("propertyChange: " + ev.getPropertyName());
116:
117: if ("activatedNodes".equals(ev.getPropertyName())) {
118: if (Boolean.TRUE.equals(callbacks.get())) {
119: LOG.info(" it was callback");
120: return;
121: }
122: try {
123: callbacks.set(Boolean.TRUE);
124: Node[] arr = getActivatedNodes();
125:
126: LOG.info("settings ndoes to zero");
127: // first of all clear the previous values otherwise
128: // we will not test SynchronizeNodes (associateLookup (..., true))
129: setActivatedNodes(ownNode());
130:
131: Children.Array ch = (Children.Array) manager
132: .getRootContext().getChildren();
133: for (int i = 0; i < arr.length; i++) {
134: if (arr[i].getParentNode() != manager
135: .getRootContext()) {
136: assertTrue(
137: "If this fails we are in troubles",
138: ch.add(new Node[] { arr[i] }));
139: }
140: }
141: LOG.info("em setSelectedNodes: "
142: + Arrays.asList(arr));
143: manager.setSelectedNodes(arr);
144: LOG.info("em setSelectedNodes done: "
145: + Arrays.asList(arr));
146: } catch (java.beans.PropertyVetoException ex) {
147: ex.printStackTrace();
148: fail(ex.getMessage());
149: } finally {
150: callbacks.set(null);
151: }
152: }
153:
154: }
155:
156: public String toString() {
157: return "ListingYourComponent";
158: }
159:
160: private static Node[] ownNode() {
161: AbstractNode a = new AbstractNode(Children.LEAF);
162: a.setName("ownNode");
163: return new Node[] { a };
164: }
165: } // end of ListingYourComponent
166:
167: // The following class is copied from example in ExplorerUtils:
168: //
169: public static class YourComponent extends TopComponent implements
170: ExplorerManager.Provider, Lookup.Provider {
171: private ExplorerManager manager;
172:
173: public YourComponent() {
174: this .manager = new ExplorerManager();
175: ActionMap map = getActionMap();
176: map.put(DefaultEditorKit.copyAction, ExplorerUtils
177: .actionCopy(manager));
178: map.put(DefaultEditorKit.cutAction, ExplorerUtils
179: .actionCut(manager));
180: map.put(DefaultEditorKit.pasteAction, ExplorerUtils
181: .actionPaste(manager));
182: map
183: .put("delete", ExplorerUtils.actionDelete(manager,
184: true)); // or false
185:
186: associateLookup(ExplorerUtils.createLookup(manager, map));
187: }
188:
189: public ExplorerManager getExplorerManager() {
190: return manager;
191: }
192:
193: // It is good idea to switch all listeners on and off when the
194: // component is shown or hidden. In the case of TopComponent use:
195: protected void componentActivated() {
196: ExplorerUtils.activateActions(manager, true);
197: }
198:
199: protected void componentDeactivated() {
200: ExplorerUtils.activateActions(manager, false);
201: }
202:
203: public String toString() {
204: return "YourComponent";
205: }
206: } // end of YourComponent
207: }
|