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: package org.openide.windows;
042:
043: import java.lang.ref.Reference;
044: import java.lang.ref.WeakReference;
045: import javax.swing.Action;
046: import javax.swing.ActionMap;
047: import javax.swing.JComponent;
048:
049: // This is almost copy of org.openide.util.UtilitiesCompositeActionMap.
050:
051: /** ActionMap that delegates to current action map of provided component.
052: * Used in <code>TopComopnent</code> lookup.
053: * <p><b>Note: This action map is 'passive', i.e putting new mappings
054: * into it makes no effect. Could be changed later.</b>
055: *
056: * @author Peter Zavadsky
057: */
058: final class DelegateActionMap extends ActionMap {
059: private Reference<JComponent> component;
060: private ActionMap delegate;
061:
062: public DelegateActionMap(JComponent c) {
063: setComponent(c);
064: }
065:
066: public DelegateActionMap(TopComponent c, ActionMap delegate) {
067: setComponent(c);
068: this .delegate = delegate;
069: }
070:
071: public int size() {
072: return keys().length;
073: }
074:
075: public Action get(Object key) {
076: javax.swing.ActionMap m;
077:
078: if (delegate == null) {
079: JComponent comp = getComponent();
080: if (comp == null) {
081: m = null;
082: } else {
083: m = comp.getActionMap();
084: }
085: } else {
086: m = delegate;
087: }
088:
089: if (m != null) {
090: Action a = m.get(key);
091:
092: if (a != null) {
093: return a;
094: }
095: }
096:
097: java.awt.Component owner = java.awt.KeyboardFocusManager
098: .getCurrentKeyboardFocusManager().getFocusOwner();
099: Action found = null;
100:
101: while ((owner != null) && (owner != getComponent())) {
102: if ((found == null) && (owner instanceof JComponent)) {
103: m = ((JComponent) owner).getActionMap();
104:
105: if (m != null) {
106: found = m.get(key);
107: }
108: }
109:
110: owner = owner.getParent();
111: }
112:
113: return (owner == getComponent()) ? found : null;
114: }
115:
116: public Object[] allKeys() {
117: return keys(true);
118: }
119:
120: public Object[] keys() {
121: return keys(false);
122: }
123:
124: private Object[] keys(boolean all) {
125: java.util.Set<Object> keys = new java.util.HashSet<Object>();
126:
127: javax.swing.ActionMap m;
128:
129: if (delegate == null) {
130: JComponent comp = getComponent();
131: if (comp == null) {
132: m = null;
133: } else {
134: m = comp.getActionMap();
135: }
136: } else {
137: m = delegate;
138: }
139:
140: if (m != null) {
141: java.util.List<Object> l;
142:
143: if (all) {
144: l = java.util.Arrays.asList(m.allKeys());
145: } else {
146: l = java.util.Arrays.asList(m.keys());
147: }
148:
149: keys.addAll(l);
150: }
151:
152: return keys.toArray();
153: }
154:
155: //
156: // Not implemented
157: //
158: public void remove(Object key) {
159: if (delegate != null) {
160: delegate.remove(key);
161: }
162: }
163:
164: public void setParent(ActionMap map) {
165: if (delegate != null) {
166: delegate.setParent(map);
167: }
168: }
169:
170: public void clear() {
171: if (delegate != null) {
172: delegate.clear();
173: }
174: }
175:
176: public void put(Object key, Action action) {
177: if (delegate != null) {
178: delegate.put(key, action);
179: }
180: }
181:
182: public ActionMap getParent() {
183: return (delegate == null) ? null : delegate.getParent();
184: }
185:
186: public String toString() {
187: return super .toString() + " for " + this .getComponent();
188: }
189:
190: JComponent getComponent() {
191: return component.get();
192: }
193:
194: private void setComponent(JComponent component) {
195: this .component = new WeakReference<JComponent>(component);
196: }
197: }
|