KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > windows > DelegateActionMap


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 package org.openide.windows;
20
21 import javax.swing.Action JavaDoc;
22 import javax.swing.ActionMap JavaDoc;
23 import javax.swing.JComponent JavaDoc;
24
25
26 // This is almost copy of org.openide.util.UtilitiesCompositeActionMap.
27

28 /** ActionMap that delegates to current action map of provided component.
29  * Used in <code>TopComopnent</code> lookup.
30  * <p><b>Note: This action map is 'passive', i.e putting new mappings
31  * into it makes no effect. Could be changed later.</b>
32  *
33  * @author Peter Zavadsky
34  */

35 final class DelegateActionMap extends ActionMap JavaDoc {
36     private JComponent JavaDoc component;
37     private ActionMap JavaDoc delegate;
38
39     public DelegateActionMap(JComponent JavaDoc c) {
40         this.component = c;
41     }
42
43     public DelegateActionMap(TopComponent c, ActionMap JavaDoc delegate) {
44         this.component = c;
45         this.delegate = delegate;
46     }
47
48     public int size() {
49         return keys().length;
50     }
51
52     public Action JavaDoc get(Object JavaDoc key) {
53         javax.swing.ActionMap JavaDoc m = (delegate == null) ? component.getActionMap() : delegate;
54
55         if (m != null) {
56             Action JavaDoc a = m.get(key);
57
58             if (a != null) {
59                 return a;
60             }
61         }
62
63         java.awt.Component JavaDoc owner = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
64         Action JavaDoc found = null;
65
66         while ((owner != null) && (owner != component)) {
67             if ((found == null) && (owner instanceof JComponent JavaDoc)) {
68                 m = ((JComponent JavaDoc) owner).getActionMap();
69
70                 if (m != null) {
71                     found = m.get(key);
72                 }
73             }
74
75             owner = owner.getParent();
76         }
77
78         return (owner == component) ? found : null;
79     }
80
81     public Object JavaDoc[] allKeys() {
82         return keys(true);
83     }
84
85     public Object JavaDoc[] keys() {
86         return keys(false);
87     }
88
89     private Object JavaDoc[] keys(boolean all) {
90         java.util.Set JavaDoc<Object JavaDoc> keys = new java.util.HashSet JavaDoc<Object JavaDoc>();
91
92         javax.swing.ActionMap JavaDoc m = (delegate == null) ? component.getActionMap() : delegate;
93
94         if (m != null) {
95             java.util.List JavaDoc<Object JavaDoc> l;
96
97             if (all) {
98                 l = java.util.Arrays.asList(m.allKeys());
99             } else {
100                 l = java.util.Arrays.asList(m.keys());
101             }
102
103             keys.addAll(l);
104         }
105
106         return keys.toArray();
107     }
108
109     //
110
// Not implemented
111
//
112
public void remove(Object JavaDoc key) {
113         if (delegate != null) {
114             delegate.remove(key);
115         }
116     }
117
118     public void setParent(ActionMap JavaDoc map) {
119         if (delegate != null) {
120             delegate.setParent(map);
121         }
122     }
123
124     public void clear() {
125         if (delegate != null) {
126             delegate.clear();
127         }
128     }
129
130     public void put(Object JavaDoc key, Action JavaDoc action) {
131         if (delegate != null) {
132             delegate.put(key, action);
133         }
134     }
135
136     public ActionMap JavaDoc getParent() {
137         return (delegate == null) ? null : delegate.getParent();
138     }
139
140     public String JavaDoc toString() {
141         return super.toString() + " for " + this.component;
142     }
143 }
144
Popular Tags