KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > LazyActionMap


1 /*
2  * @(#)LazyActionMap.java 1.5 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.basic;
8
9 import java.lang.reflect.*;
10 import javax.swing.*;
11 import javax.swing.plaf.*;
12
13 /**
14  * An ActionMap that populates its contents as necessary. The
15  * contents are populated by invoking the <code>loadActionMap</code>
16  * method on the passed in Object.
17  *
18  * @version 1.5, 12/19/03
19  * @author Scott Violet
20  */

21 class LazyActionMap extends ActionMapUIResource {
22     /**
23      * Object to invoke <code>loadActionMap</code> on. This may be
24      * a Class object.
25      */

26     private transient Object JavaDoc _loader;
27
28     /**
29      * Installs an ActionMap that will be populated by invoking the
30      * <code>loadActionMap</code> method on the specified Class
31      * when necessary.
32      * <p>
33      * This should be used if the ActionMap can be shared.
34      *
35      * @param c JComponent to install the ActionMap on.
36      * @param loaderClass Class object that gets loadActionMap invoked
37      * on.
38      * @param defaultsKey Key to use to defaults table to check for
39      * existing map and what resulting Map will be registered on.
40      */

41     static void installLazyActionMap(JComponent c, Class JavaDoc loaderClass,
42                                      String JavaDoc defaultsKey) {
43         ActionMap map = (ActionMap)UIManager.get(defaultsKey);
44         if (map == null) {
45             map = new LazyActionMap JavaDoc(loaderClass);
46             UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
47         }
48         SwingUtilities.replaceUIActionMap(c, map);
49     }
50
51     /**
52      * Returns an ActionMap that will be populated by invoking the
53      * <code>loadActionMap</code> method on the specified Class
54      * when necessary.
55      * <p>
56      * This should be used if the ActionMap can be shared.
57      *
58      * @param c JComponent to install the ActionMap on.
59      * @param loaderClass Class object that gets loadActionMap invoked
60      * on.
61      * @param defaultsKey Key to use to defaults table to check for
62      * existing map and what resulting Map will be registered on.
63      */

64     static ActionMap getActionMap(Class JavaDoc loaderClass,
65                                   String JavaDoc defaultsKey) {
66         ActionMap map = (ActionMap)UIManager.get(defaultsKey);
67         if (map == null) {
68             map = new LazyActionMap JavaDoc(loaderClass);
69             UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
70         }
71         return map;
72     }
73
74
75     private LazyActionMap(Class JavaDoc loader) {
76         _loader = loader;
77     }
78
79     public void put(Action action) {
80         put(action.getValue(Action.NAME), action);
81     }
82
83     public void put(Object JavaDoc key, Action action) {
84         loadIfNecessary();
85         super.put(key, action);
86     }
87
88     public Action get(Object JavaDoc key) {
89         loadIfNecessary();
90         return super.get(key);
91     }
92
93     public void remove(Object JavaDoc key) {
94         loadIfNecessary();
95         super.remove(key);
96     }
97
98     public void clear() {
99         loadIfNecessary();
100         super.clear();
101     }
102
103     public Object JavaDoc[] keys() {
104         loadIfNecessary();
105         return super.keys();
106     }
107
108     public int size() {
109         loadIfNecessary();
110         return super.size();
111     }
112
113     public Object JavaDoc[] allKeys() {
114         loadIfNecessary();
115         return super.allKeys();
116     }
117
118     public void setParent(ActionMap map) {
119         loadIfNecessary();
120         super.setParent(map);
121     }
122
123     private void loadIfNecessary() {
124         if (_loader != null) {
125             Object JavaDoc loader = _loader;
126
127             _loader = null;
128             Class JavaDoc klass = (Class JavaDoc)loader;
129             try {
130                 Method method = klass.getDeclaredMethod("loadActionMap",
131                                       new Class JavaDoc[] { LazyActionMap JavaDoc.class });
132                 method.invoke(klass, new Object JavaDoc[] { this });
133             } catch (NoSuchMethodException JavaDoc nsme) {
134                 assert false : "LazyActionMap unable to load actions " +
135                         klass;
136             } catch (IllegalAccessException JavaDoc iae) {
137                 assert false : "LazyActionMap unable to load actions " +
138                         iae;
139             } catch (InvocationTargetException ite) {
140                 assert false : "LazyActionMap unable to load actions " +
141                         ite;
142             } catch (IllegalArgumentException JavaDoc iae) {
143                 assert false : "LazyActionMap unable to load actions " +
144                         iae;
145             }
146         }
147     }
148 }
149
Popular Tags