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-2007 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.modules.xml.xam.ui.multiview;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import org.openide.nodes.Node;
047: import org.openide.util.Lookup;
048: import org.openide.util.lookup.ProxyLookup;
049:
050: /**
051: * A ProxyLookup that changes the last delegate lookup to not return instances
052: * of Node.class depending on changes to the "activatedNodes" property. The
053: * value of this is that the delegate lookup comes from a DataObject Node
054: * delegate, which presumably provides cookies such as SaveCookie. But we
055: * do not want the Node delegate to return instances of Node via its Lookup
056: * (specifically the Node delegate itself).
057: *
058: * <p>Usage, from within a <code>TopComponent</code> constructor:</p>
059: *
060: * <pre>
061: * CookieProxyLookup cpl = new CookieProxyLookup(new Lookup[] {
062: * lookup1,
063: * lookup2,
064: * // The Node delegate Lookup must be the last one in the list
065: * // for the CookieProxyLookup to work properly.
066: * delegate.getLookup(),
067: * }, delegate);
068: * associateLookup(cpl);
069: * addPropertyChangeListener("activatedNodes", cpl);
070: * </pre>
071: *
072: * @author Nathan Fiedler
073: */
074: public class CookieProxyLookup extends ProxyLookup implements
075: PropertyChangeListener {
076: /** The Node to which we delegate lookup for cookies. */
077: private Node delegate;
078: /** Signal that we are processing a property change event. */
079: private volatile boolean propertyChanging;
080:
081: /**
082: * Creates a new instance of CookieProxyLookup.
083: *
084: * @param lookups the Lookup instances to which we proxy.
085: * @param delegate the Node delegate from which cookies come.
086: */
087: public CookieProxyLookup(Lookup[] lookups, Node delegate) {
088: super (lookups);
089: this .delegate = delegate;
090: }
091:
092: public synchronized void propertyChange(PropertyChangeEvent event) {
093: if (propertyChanging) {
094: // Avoid an infinite loop whereby changing the lookup contents
095: // causes the activated nodes to change, which calls us again.
096: return;
097: }
098: propertyChanging = true;
099: try {
100: Lookup[] lookups = getLookups();
101: Node[] oldNodes = (Node[]) event.getOldValue();
102: Node[] newNodes = (Node[]) event.getNewValue();
103: Lookup lastLookup = lookups[lookups.length - 1];
104: if (!(lastLookup instanceof NoNodeLookup)
105: && (oldNodes.length >= 1)
106: && (!oldNodes[0].equals(delegate))) {
107: switchLookup();
108: } else if ((lastLookup instanceof NoNodeLookup)
109: && (newNodes.length == 0)) {
110: switchLookup();
111: }
112: } finally {
113: propertyChanging = false;
114: }
115: }
116:
117: /**
118: * Switch out the last lookup in the proxy with a special NoNodeLookup
119: * that delegates to our Node delegate's lookup.
120: */
121: private void switchLookup() {
122: Lookup[] lookups = getLookups();
123: Lookup nodeLookup = delegate.getLookup();
124: int index = lookups.length - 1;
125: if (lookups[index] instanceof NoNodeLookup) {
126: lookups[index] = nodeLookup;
127: } else {
128: lookups[index] = new NoNodeLookup(nodeLookup);
129: }
130: setLookups(lookups);
131: }
132:
133: /*
134: * Lookup that excludes nodes. Needed for use with instanceof in the
135: * property change listener.
136: */
137: private static class NoNodeLookup extends Lookup {
138: private final Lookup delegate;
139:
140: public NoNodeLookup(Lookup delegate) {
141: this .delegate = delegate;
142: }
143:
144: public Object lookup(Class clazz) {
145: return (clazz == Node.class) ? null : delegate
146: .lookup(clazz);
147: }
148:
149: public Lookup.Result lookup(Lookup.Template template) {
150: if (template.getType() == Node.class) {
151: return Lookup.EMPTY.lookup(new Lookup.Template(
152: Node.class));
153: } else {
154: return delegate.lookup(template);
155: }
156: }
157: }
158: }
|