KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > multiview > CookieProxyLookup


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui.multiview;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import org.openide.nodes.Node;
25 import org.openide.util.Lookup;
26 import org.openide.util.lookup.ProxyLookup;
27
28 /**
29  * A ProxyLookup that changes the last delegate lookup to not return instances
30  * of Node.class depending on changes to the "activatedNodes" property. The
31  * value of this is that the delegate lookup comes from a DataObject Node
32  * delegate, which presumably provides cookies such as SaveCookie. But we
33  * do not want the Node delegate to return instances of Node via its Lookup
34  * (specifically the Node delegate itself).
35  *
36  * <p>Usage, from within a <code>TopComponent</code> constructor:</p>
37  *
38  * <pre>
39  * CookieProxyLookup cpl = new CookieProxyLookup(new Lookup[] {
40  * lookup1,
41  * lookup2,
42  * // The Node delegate Lookup must be the last one in the list
43  * // for the CookieProxyLookup to work properly.
44  * delegate.getLookup(),
45  * }, delegate);
46  * associateLookup(cpl);
47  * addPropertyChangeListener("activatedNodes", cpl);
48  * </pre>
49  *
50  * @author Nathan Fiedler
51  */

52 public class CookieProxyLookup extends ProxyLookup
53         implements PropertyChangeListener JavaDoc {
54     /** The Node to which we delegate lookup for cookies. */
55     private Node delegate;
56     /** Signal that we are processing a property change event. */
57     private volatile boolean propertyChanging;
58
59     /**
60      * Creates a new instance of CookieProxyLookup.
61      *
62      * @param lookups the Lookup instances to which we proxy.
63      * @param delegate the Node delegate from which cookies come.
64      */

65     public CookieProxyLookup(Lookup[] lookups, Node delegate) {
66         super(lookups);
67         this.delegate = delegate;
68     }
69
70     public synchronized void propertyChange(PropertyChangeEvent JavaDoc event) {
71         if (propertyChanging) {
72             // Avoid an infinite loop whereby changing the lookup contents
73
// causes the activated nodes to change, which calls us again.
74
return;
75         }
76         propertyChanging = true;
77         try {
78             Lookup[] lookups = getLookups();
79             Node[] oldNodes = (Node[]) event.getOldValue();
80             Node[] newNodes = (Node[]) event.getNewValue();
81             Lookup lastLookup = lookups[lookups.length - 1];
82             if (!(lastLookup instanceof NoNodeLookup) &&
83                     (oldNodes.length >= 1) &&
84                     (!oldNodes[0].equals(delegate))) {
85                 switchLookup();
86             } else if ((lastLookup instanceof NoNodeLookup) &&
87                     (newNodes.length == 0)) {
88                 switchLookup();
89             }
90         } finally {
91             propertyChanging = false;
92         }
93     }
94
95     /**
96      * Switch out the last lookup in the proxy with a special NoNodeLookup
97      * that delegates to our Node delegate's lookup.
98      */

99     private void switchLookup() {
100         Lookup[] lookups = getLookups();
101         Lookup nodeLookup = delegate.getLookup();
102         int index = lookups.length - 1;
103         if (lookups[index] instanceof NoNodeLookup) {
104             lookups[index] = nodeLookup;
105         } else {
106             lookups[index] = new NoNodeLookup(nodeLookup);
107         }
108         setLookups(lookups);
109     }
110
111     /*
112      * Lookup that excludes nodes. Needed for use with instanceof in the
113      * property change listener.
114      */

115     private static class NoNodeLookup extends Lookup {
116         private final Lookup delegate;
117
118         public NoNodeLookup(Lookup delegate) {
119             this.delegate = delegate;
120         }
121
122         public Object JavaDoc lookup(Class JavaDoc clazz) {
123             return (clazz == Node.class) ? null : delegate.lookup(clazz);
124         }
125
126         public Lookup.Result lookup(Lookup.Template template) {
127             if (template.getType() == Node.class) {
128                 return Lookup.EMPTY.lookup(new Lookup.Template(Node.class));
129             } else {
130                 return delegate.lookup(template);
131             }
132         }
133     }
134 }
135
Popular Tags