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