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-2006 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.openide.windows;
043:
044: import org.openide.util.Lookup;
045: import org.openide.util.ContextGlobalProvider;
046: import org.openide.util.lookup.Lookups;
047: import org.openide.util.lookup.ProxyLookup;
048: import org.openide.windows.TopComponent;
049:
050: /** An interface that can be registered in a lookup by subsystems
051: * wish to provide a global context actions should react to.
052: *
053: * @author Jaroslav Tulach
054: */
055: public final class GlobalActionContextImpl extends Object implements
056: ContextGlobalProvider, Lookup.Provider,
057: java.beans.PropertyChangeListener {
058: /** registry to work with */
059: private TopComponent.Registry registry;
060:
061: public GlobalActionContextImpl() {
062: this (TopComponent.getRegistry());
063: }
064:
065: public GlobalActionContextImpl(TopComponent.Registry r) {
066: this .registry = r;
067: }
068:
069: /** the lookup to temporarily use */
070: private static volatile Lookup temporary;
071:
072: /** Temporarily provides different action map in the lookup.
073: */
074: public static void blickActionMap(javax.swing.ActionMap map) {
075: Object obj = Lookup.getDefault().lookup(
076: ContextGlobalProvider.class);
077: if (obj instanceof GlobalActionContextImpl) {
078: GlobalActionContextImpl g = (GlobalActionContextImpl) obj;
079:
080: Lookup[] arr = {
081: Lookups.singleton(map),
082: Lookups
083: .exclude(
084: g.getLookup(),
085: new Class[] { javax.swing.ActionMap.class }), };
086:
087: Lookup prev = temporary;
088: try {
089: temporary = new ProxyLookup(arr);
090: Object q = org.openide.util.Utilities
091: .actionsGlobalContext().lookup(
092: javax.swing.ActionMap.class);
093: assert q == map : "We really get map from the lookup. Map: "
094: + map + " returned: " + q; // NOI18N
095: } finally {
096: temporary = prev;
097: // fire the changes about return of the values back
098: org.openide.util.Utilities.actionsGlobalContext()
099: .lookup(javax.swing.ActionMap.class);
100: }
101: }
102: }
103:
104: /** Let's create the proxy listener that delegates to currently
105: * selected top component.
106: */
107: public Lookup createGlobalContext() {
108: registry.addPropertyChangeListener(this );
109: return org.openide.util.lookup.Lookups.proxy(this );
110: }
111:
112: /** The current component lookup */
113: public Lookup getLookup() {
114: Lookup l = temporary;
115: if (l != null) {
116: return l;
117: }
118:
119: TopComponent tc = registry.getActivated();
120: return tc == null ? Lookup.EMPTY : tc.getLookup();
121: }
122:
123: /** Requests refresh of our lookup everytime component is chagned.
124: */
125: public void propertyChange(java.beans.PropertyChangeEvent evt) {
126: if (TopComponent.Registry.PROP_ACTIVATED.equals(evt
127: .getPropertyName())) {
128: org.openide.util.Utilities.actionsGlobalContext().lookup(
129: javax.swing.ActionMap.class);
130: }
131: }
132:
133: }
|