KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > MultiViewActionMap


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.core.multiview;
21
22
23 import javax.swing.Action JavaDoc;
24 import javax.swing.ActionMap JavaDoc;
25 import javax.swing.JComponent JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import org.openide.windows.TopComponent;
28
29
30 /** ActionMap that delegates to current action map of provided component and dynamically also the current element.
31  * Used in <code>MultiViewTopComopnent</code> lookup.
32  *
33  * @author Milos Kleint
34  */

35 final class MultiViewActionMap extends ActionMap JavaDoc {
36     private ActionMap JavaDoc delegate;
37     private ActionMap JavaDoc topComponentMap;
38     private TopComponent component;
39     
40     private boolean preventRecursive = false;
41     private Object JavaDoc LOCK = new Object JavaDoc();
42
43     public MultiViewActionMap(TopComponent tc, ActionMap JavaDoc tcMap) {
44         topComponentMap = tcMap;
45         component = tc;
46     }
47     
48     public void setDelegateMap(ActionMap JavaDoc map) {
49         delegate = map;
50     }
51     
52     public int size() {
53         return keys ().length;
54     }
55
56     public Action JavaDoc get(Object JavaDoc key) {
57         // the multiview's action map first.. for stuff like the closewindow and clonewindow from TopComponent.initActionMap
58
javax.swing.ActionMap JavaDoc m = topComponentMap;
59         if (m != null) {
60             Action JavaDoc a = m.get (key);
61             if (a != null) {
62                 return a;
63             }
64         }
65         // delegate then
66
m = delegate;
67         if (m != null) {
68             //this is needed because of Tc's DelegateActionMap which traverses up the component hierarchy.
69
// .. results in calling this method again and again and again. -> stackoverflow.
70
// this should break the evil cycle.
71
synchronized (LOCK) {
72                 if (preventRecursive) {
73                     preventRecursive = false;
74                     return null;
75                 }
76                 preventRecursive = true;
77                 Action JavaDoc a = m.get (key);
78                 preventRecursive = false;
79                 if (a != null) {
80                     return a;
81                 }
82             }
83         }
84         
85         java.awt.Component JavaDoc owner = java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
86         Action JavaDoc found = null;
87         while (owner != null && owner != component) {
88             if (found == null && (owner instanceof JComponent JavaDoc)) {
89                 m = ((JComponent JavaDoc)owner).getActionMap ();
90                 if (m != null) {
91                     found = m.get (key);
92                 }
93             }
94             owner = owner.getParent ();
95         }
96         
97         return owner == component ? found : null;
98     }
99
100     public Object JavaDoc[] allKeys() {
101         return keys (true);
102     }
103
104     public Object JavaDoc[] keys() {
105         return keys (false);
106     }
107
108
109     private Object JavaDoc[] keys(boolean all) {
110         java.util.Set JavaDoc keys = new java.util.HashSet JavaDoc();
111
112         if (delegate != null) {
113             java.util.List JavaDoc l;
114             if (all) {
115                 l = Arrays.asList(delegate.allKeys());
116             } else {
117                 l = Arrays.asList(delegate.keys());
118             }
119             keys.addAll(l);
120         }
121         
122         if (topComponentMap != null) {
123             java.util.List JavaDoc l;
124
125             if (all) {
126                 l = Arrays.asList (topComponentMap.allKeys ());
127             } else {
128                 l = Arrays.asList (topComponentMap.keys ());
129             }
130
131             keys.addAll (l);
132         }
133
134         return keys.toArray();
135     }
136
137     //
138
// Not implemented
139
//
140
public void remove(Object JavaDoc key) {
141         topComponentMap.remove(key);
142     }
143
144     public void setParent(ActionMap JavaDoc map) {
145         topComponentMap.setParent(map);
146     }
147
148     public void clear() {
149         topComponentMap.clear();
150     }
151
152     public void put(Object JavaDoc key, Action JavaDoc action) {
153         topComponentMap.put (key, action);
154     }
155
156     public ActionMap JavaDoc getParent() {
157         return topComponentMap.getParent();
158     }
159  
160 }
161
Popular Tags