KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > UtilitiesCompositeActionMap


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.util;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 import java.beans.*;
25
26 import java.io.*;
27
28 import javax.swing.*;
29
30
31 /** ActionMap that is composed from all Components up to the ExplorerManager.Provider
32 *
33 * @author Jaroslav Tulach
34 */

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