001 /*
002 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.plaf.basic;
026
027 import java.lang.reflect.*;
028 import javax.swing.*;
029 import javax.swing.plaf.*;
030
031 /**
032 * An ActionMap that populates its contents as necessary. The
033 * contents are populated by invoking the <code>loadActionMap</code>
034 * method on the passed in Object.
035 *
036 * @version 1.12, 05/05/07
037 * @author Scott Violet
038 */
039 class LazyActionMap extends ActionMapUIResource {
040 /**
041 * Object to invoke <code>loadActionMap</code> on. This may be
042 * a Class object.
043 */
044 private transient Object _loader;
045
046 /**
047 * Installs an ActionMap that will be populated by invoking the
048 * <code>loadActionMap</code> method on the specified Class
049 * when necessary.
050 * <p>
051 * This should be used if the ActionMap can be shared.
052 *
053 * @param c JComponent to install the ActionMap on.
054 * @param loaderClass Class object that gets loadActionMap invoked
055 * on.
056 * @param defaultsKey Key to use to defaults table to check for
057 * existing map and what resulting Map will be registered on.
058 */
059 static void installLazyActionMap(JComponent c, Class loaderClass,
060 String defaultsKey) {
061 ActionMap map = (ActionMap) UIManager.get(defaultsKey);
062 if (map == null) {
063 map = new LazyActionMap(loaderClass);
064 UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
065 }
066 SwingUtilities.replaceUIActionMap(c, map);
067 }
068
069 /**
070 * Returns an ActionMap that will be populated by invoking the
071 * <code>loadActionMap</code> method on the specified Class
072 * when necessary.
073 * <p>
074 * This should be used if the ActionMap can be shared.
075 *
076 * @param c JComponent to install the ActionMap on.
077 * @param loaderClass Class object that gets loadActionMap invoked
078 * on.
079 * @param defaultsKey Key to use to defaults table to check for
080 * existing map and what resulting Map will be registered on.
081 */
082 static ActionMap getActionMap(Class loaderClass, String defaultsKey) {
083 ActionMap map = (ActionMap) UIManager.get(defaultsKey);
084 if (map == null) {
085 map = new LazyActionMap(loaderClass);
086 UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
087 }
088 return map;
089 }
090
091 private LazyActionMap(Class loader) {
092 _loader = loader;
093 }
094
095 public void put(Action action) {
096 put(action.getValue(Action.NAME), action);
097 }
098
099 public void put(Object key, Action action) {
100 loadIfNecessary();
101 super .put(key, action);
102 }
103
104 public Action get(Object key) {
105 loadIfNecessary();
106 return super .get(key);
107 }
108
109 public void remove(Object key) {
110 loadIfNecessary();
111 super .remove(key);
112 }
113
114 public void clear() {
115 loadIfNecessary();
116 super .clear();
117 }
118
119 public Object[] keys() {
120 loadIfNecessary();
121 return super .keys();
122 }
123
124 public int size() {
125 loadIfNecessary();
126 return super .size();
127 }
128
129 public Object[] allKeys() {
130 loadIfNecessary();
131 return super .allKeys();
132 }
133
134 public void setParent(ActionMap map) {
135 loadIfNecessary();
136 super .setParent(map);
137 }
138
139 private void loadIfNecessary() {
140 if (_loader != null) {
141 Object loader = _loader;
142
143 _loader = null;
144 Class klass = (Class) loader;
145 try {
146 Method method = klass.getDeclaredMethod(
147 "loadActionMap",
148 new Class[] { LazyActionMap.class });
149 method.invoke(klass, new Object[] { this });
150 } catch (NoSuchMethodException nsme) {
151 assert false : "LazyActionMap unable to load actions "
152 + klass;
153 } catch (IllegalAccessException iae) {
154 assert false : "LazyActionMap unable to load actions "
155 + iae;
156 } catch (InvocationTargetException ite) {
157 assert false : "LazyActionMap unable to load actions "
158 + ite;
159 } catch (IllegalArgumentException iae) {
160 assert false : "LazyActionMap unable to load actions "
161 + iae;
162 }
163 }
164 }
165 }
|