KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > openide > windows > GlobalActionContextImpl


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

19
20 package org.netbeans.modules.openide.windows;
21
22 import org.openide.util.Lookup;
23 import org.openide.util.ContextGlobalProvider;
24 import org.openide.util.lookup.Lookups;
25 import org.openide.util.lookup.ProxyLookup;
26 import org.openide.windows.TopComponent;
27
28 /** An interface that can be registered in a lookup by subsystems
29  * wish to provide a global context actions should react to.
30  *
31  * @author Jaroslav Tulach
32 */

33 public final class GlobalActionContextImpl extends Object JavaDoc
34 implements ContextGlobalProvider, Lookup.Provider, java.beans.PropertyChangeListener JavaDoc {
35     /** registry to work with */
36     private TopComponent.Registry registry;
37     
38     public GlobalActionContextImpl () {
39         this (TopComponent.getRegistry());
40     }
41     
42     public GlobalActionContextImpl (TopComponent.Registry r) {
43         this.registry = r;
44     }
45     
46     /** the lookup to temporarily use */
47     private static volatile Lookup temporary;
48     /** Temporarily provides different action map in the lookup.
49      */

50     public static void blickActionMap (javax.swing.ActionMap JavaDoc map) {
51         Object JavaDoc obj = Lookup.getDefault ().lookup (ContextGlobalProvider.class);
52         if (obj instanceof GlobalActionContextImpl) {
53             GlobalActionContextImpl g = (GlobalActionContextImpl)obj;
54             
55             Lookup[] arr = {
56                 Lookups.singleton (map),
57                 Lookups.exclude (g.getLookup (), new Class JavaDoc[] { javax.swing.ActionMap JavaDoc.class }),
58             };
59             
60             Lookup prev = temporary;
61             try {
62                 temporary = new ProxyLookup (arr);
63                 Object JavaDoc q = org.openide.util.Utilities.actionsGlobalContext ().lookup (javax.swing.ActionMap JavaDoc.class);
64                 assert q == map : "We really get map from the lookup. Map: " + map + " returned: " + q; // NOI18N
65
} finally {
66                 temporary = prev;
67                 // fire the changes about return of the values back
68
org.openide.util.Utilities.actionsGlobalContext ().lookup (javax.swing.ActionMap JavaDoc.class);
69             }
70         }
71     }
72     
73     /** Let's create the proxy listener that delegates to currently
74      * selected top component.
75      */

76     public Lookup createGlobalContext() {
77         registry.addPropertyChangeListener(this);
78         return org.openide.util.lookup.Lookups.proxy(this);
79     }
80     
81     /** The current component lookup */
82     public Lookup getLookup() {
83         Lookup l = temporary;
84         if (l != null) {
85             return l;
86         }
87         
88         TopComponent tc = registry.getActivated();
89         return tc == null ? Lookup.EMPTY : tc.getLookup();
90     }
91     
92     /** Requests refresh of our lookup everytime component is chagned.
93      */

94     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
95         if (TopComponent.Registry.PROP_ACTIVATED.equals (evt.getPropertyName())) {
96             org.openide.util.Utilities.actionsGlobalContext ().lookup (javax.swing.ActionMap JavaDoc.class);
97         }
98     }
99     
100 }
101
Popular Tags