KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > core > multiview > SourceCookieProxyLookup


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

54 public class SourceCookieProxyLookup extends ProxyLookup
55         implements PropertyChangeListener JavaDoc {
56     /** Lookups excluding activated nodes and their lookups . */
57     private Lookup[] lookups;
58     /** The Node to which we delegate lookup for cookies. */
59     private Node delegate;
60     /** Signal that we are processing a property change event. */
61     private boolean propertyChanging;
62
63     /**
64      * Creates a new instance of CookieProxyLookup.
65      *
66      * @param lookups the Lookup instances to which we proxy.
67      * @param delegate the Node delegate from which cookies come.
68      */

69     public SourceCookieProxyLookup(Lookup[] lookups, Node delegate) {
70         super();
71         this.lookups = lookups;
72         this.delegate = delegate;
73         setLookups(new Lookup[] {
74             new ProxyLookup(lookups),
75             new NoNodeLookup(delegate.getLookup()),
76             Lookups.singleton(delegate),
77         });
78     }
79
80     public synchronized void propertyChange(PropertyChangeEvent JavaDoc event) {
81         if (propertyChanging)
82         {
83             // Avoid an infinite loop whereby changing the lookup contents
84
// causes the activated nodes to change, which calls us again.
85
return;
86         }
87         propertyChanging = true;
88         try
89         {
90             Node[] oldNodes = (Node[]) event.getOldValue();
91             Node[] newNodes = (Node[]) event.getNewValue();
92             if(newNodes==null || newNodes.length==0)
93             {
94                 setLookups(new Lookup[] {
95                     new ProxyLookup(lookups),
96                     new NoNodeLookup(delegate.getLookup()),
97                     Lookups.singleton(delegate),
98                 });
99             }
100             else
101             {
102                 Lookup[] newNodeLookups = new Lookup[newNodes.length];
103                 for (int i=0;i<newNodes.length;i++)
104                 {
105                     newNodeLookups[i]=new NoNodeLookup(newNodes[i].getLookup());
106                 }
107                 setLookups(new Lookup[] {
108                     new ProxyLookup(lookups),
109                     new ProxyLookup(newNodeLookups),
110                     Lookups.fixed(newNodes),
111                 });
112             }
113         }
114         finally
115         {
116             propertyChanging = false;
117         }
118    }
119
120     /*
121      * Lookup that excludes nodes. Needed for use with instanceof in the
122      * property change listener.
123      */

124     private static class NoNodeLookup extends Lookup {
125         private final Lookup delegate;
126
127         public NoNodeLookup(Lookup delegate) {
128             this.delegate = delegate;
129         }
130
131         public Object JavaDoc lookup(Class JavaDoc clazz) {
132             return (clazz == Node.class) ? null : delegate.lookup(clazz);
133         }
134
135         public Lookup.Result lookup(Lookup.Template template) {
136             if (template.getType() == Node.class) {
137                 return Lookup.EMPTY.lookup(new Lookup.Template(Node.class));
138             } else {
139                 return delegate.lookup(template);
140             }
141         }
142     }
143 }
144
Popular Tags