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