JAL-1807 includes ?j2sdebug flag and DebugJS._(msg)
[jalviewjs.git] / site / j2s / swingjs / plaf / LazyActionMap.java
1 /*\r
2  * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.\r
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\r
4  *\r
5  * This code is free software; you can redistribute it and/or modify it\r
6  * under the terms of the GNU General Public License version 2 only, as\r
7  * published by the Free Software Foundation.  Oracle designates this\r
8  * particular file as subject to the "Classpath" exception as provided\r
9  * by Oracle in the LICENSE file that accompanied this code.\r
10  *\r
11  * This code is distributed in the hope that it will be useful, but WITHOUT\r
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
14  * version 2 for more details (a copy is included in the LICENSE file that\r
15  * accompanied this code).\r
16  *\r
17  * You should have received a copy of the GNU General Public License version\r
18  * 2 along with this work; if not, write to the Free Software Foundation,\r
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\r
22  * or visit www.oracle.com if you need additional information or have any\r
23  * questions.\r
24  */\r
25 package swingjs.plaf;\r
26 \r
27 import jsjavax.swing.Action;\r
28 import jsjavax.swing.ActionMap;\r
29 import jsjavax.swing.JComponent;\r
30 import jsjavax.swing.SwingUtilities;\r
31 import jsjavax.swing.UIManager;\r
32 import jsjavax.swing.plaf.ActionMapUIResource;\r
33 \r
34 /**\r
35  * An ActionMap that populates its contents as necessary. The\r
36  * contents are populated by invoking the <code>loadActionMap</code>\r
37  * method on the passed in Object.\r
38  *\r
39  * @author Scott Violet\r
40  */\r
41 class LazyActionMap extends ActionMapUIResource {\r
42     /**\r
43      * Object to invoke <code>loadActionMap</code> on. This may be\r
44      * a Class object.\r
45      */\r
46     private transient Object _loader;\r
47 \r
48     /**\r
49      * Installs an ActionMap that will be populated by invoking the\r
50      * <code>loadActionMap</code> method on the specified Class\r
51      * when necessary.\r
52      * <p>\r
53      * This should be used if the ActionMap can be shared.\r
54      *\r
55      * @param c JComponent to install the ActionMap on.\r
56      * @param loaderClass Class object that gets loadActionMap invoked\r
57      *                    on.\r
58      * @param defaultsKey Key to use to defaults table to check for\r
59      *        existing map and what resulting Map will be registered on.\r
60      */\r
61     static void installLazyActionMap(JComponent c, Class loaderClass,\r
62                                      String defaultsKey) {\r
63         ActionMap map = (ActionMap)UIManager.get(defaultsKey);\r
64         if (map == null) {\r
65             map = new LazyActionMap(loaderClass);\r
66 //            UIManager.getLookAndFeelDefaults().put(defaultsKey, map);\r
67         }\r
68         SwingUtilities.replaceUIActionMap(c, map);\r
69     }\r
70 \r
71     /**\r
72      * Returns an ActionMap that will be populated by invoking the\r
73      * <code>loadActionMap</code> method on the specified Class\r
74      * when necessary.\r
75      * <p>\r
76      * This should be used if the ActionMap can be shared.\r
77      *\r
78      * @param c JComponent to install the ActionMap on.\r
79      * @param loaderClass Class object that gets loadActionMap invoked\r
80      *                    on.\r
81      * @param defaultsKey Key to use to defaults table to check for\r
82      *        existing map and what resulting Map will be registered on.\r
83      */\r
84     static ActionMap getActionMap(Class loaderClass,\r
85                                   String defaultsKey) {\r
86         ActionMap map = (ActionMap)UIManager.get(defaultsKey);\r
87         if (map == null) {\r
88             map = new LazyActionMap(loaderClass);\r
89 //SwingJS temp            UIManager.getLookAndFeelDefaults().put(defaultsKey, map);\r
90         }\r
91         return map;\r
92     }\r
93 \r
94 \r
95     private LazyActionMap(Class loader) {\r
96         _loader = loader;\r
97     }\r
98 \r
99     public void put(Action action) {\r
100         put(action.getValue(Action.NAME), action);\r
101     }\r
102 \r
103     public void put(Object key, Action action) {\r
104         loadIfNecessary();\r
105         super.put(key, action);\r
106     }\r
107 \r
108     public Action get(Object key) {\r
109         loadIfNecessary();\r
110         return super.get(key);\r
111     }\r
112 \r
113     public void remove(Object key) {\r
114         loadIfNecessary();\r
115         super.remove(key);\r
116     }\r
117 \r
118     public void clear() {\r
119         loadIfNecessary();\r
120         super.clear();\r
121     }\r
122 \r
123     public Object[] keys() {\r
124         loadIfNecessary();\r
125         return super.keys();\r
126     }\r
127 \r
128     public int size() {\r
129         loadIfNecessary();\r
130         return super.size();\r
131     }\r
132 \r
133     public Object[] allKeys() {\r
134         loadIfNecessary();\r
135         return super.allKeys();\r
136     }\r
137 \r
138     public void setParent(ActionMap map) {\r
139         loadIfNecessary();\r
140         super.setParent(map);\r
141     }\r
142 \r
143     private void loadIfNecessary() {\r
144         if (_loader != null) {\r
145                 /**\r
146                  * @j2sNative\r
147                  * \r
148                  * this._loader.loadActionMap(this);\r
149                  * this._loader = null;\r
150                  */\r
151                 {}\r
152 //            Object loader = _loader;\r
153 //\r
154 //              _loader = null;\r
155 //            Class klass = (Class)loader;\r
156 //            try {\r
157 //                Method method = klass.getDeclaredMethod("loadActionMap",\r
158 //                                      new Class[] { LazyActionMap.class });\r
159 //                method.invoke(klass, new Object[] { this });\r
160 //            } catch (NoSuchMethodException nsme) {\r
161 //                assert false : "LazyActionMap unable to load actions " +\r
162 //                        klass;\r
163 //            } catch (IllegalAccessException iae) {\r
164 //                assert false : "LazyActionMap unable to load actions " +\r
165 //                        iae;\r
166 //            } catch (InvocationTargetException ite) {\r
167 //                assert false : "LazyActionMap unable to load actions " +\r
168 //                        ite;\r
169 //            } catch (IllegalArgumentException iae) {\r
170 //                assert false : "LazyActionMap unable to load actions " +\r
171 //                        iae;\r
172 //            }\r
173         }\r
174     }\r
175 }\r