001: /*
002: * @(#)LazyActionMap.java 8/19/2006
003: *
004: * Copyright 2002 - 2006 JIDE Software Inc. All rights reserved.
005: */
006:
007: package com.jidesoft.plaf.basic;
008:
009: import com.jidesoft.plaf.UIDefaultsLookup;
010:
011: import javax.swing.*;
012: import javax.swing.plaf.ActionMapUIResource;
013: import java.lang.reflect.Method;
014:
015: /**
016: * An ActionMap that populates its contents as necessary. The
017: * contents are populated by invoking the <code>loadActionMap</code>
018: * method on the passed in Object.
019: *
020: * @author Scott Violet
021: * @version 1.5, 12/19/03
022: */
023: public class LazyActionMap extends ActionMapUIResource {
024: /**
025: * Object to invoke <code>loadActionMap</code> on. This may be
026: * a Class object.
027: */
028: private transient Object _loader;
029:
030: /**
031: * Installs an ActionMap that will be populated by invoking the
032: * <code>loadActionMap</code> method on the specified Class
033: * when necessary.
034: * <p/>
035: * This should be used if the ActionMap can be shared.
036: *
037: * @param c JComponent to install the ActionMap on.
038: * @param loaderClass Class object that gets loadActionMap invoked
039: * on.
040: * @param defaultsKey Key to use to defaults table to check for
041: * existing map and what resulting Map will be registered on.
042: */
043: public static void installLazyActionMap(JComponent c,
044: Class loaderClass, String defaultsKey) {
045: ActionMap map = (ActionMap) UIDefaultsLookup.get(defaultsKey);
046: if (map == null) {
047: map = new LazyActionMap(loaderClass);
048: UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
049: }
050: SwingUtilities.replaceUIActionMap(c, map);
051: }
052:
053: /**
054: * Returns an ActionMap that will be populated by invoking the
055: * <code>loadActionMap</code> method on the specified Class
056: * when necessary.
057: * <p/>
058: * This should be used if the ActionMap can be shared.
059: *
060: * @param loaderClass Class object that gets loadActionMap invoked
061: * on.
062: * @param defaultsKey Key to use to defaults table to check for
063: * existing map and what resulting Map will be registered on.
064: */
065: static ActionMap getActionMap(Class loaderClass, String defaultsKey) {
066: ActionMap map = (ActionMap) UIDefaultsLookup.get(defaultsKey);
067: if (map == null) {
068: map = new LazyActionMap(loaderClass);
069: UIManager.getLookAndFeelDefaults().put(defaultsKey, map);
070: }
071: return map;
072: }
073:
074: private LazyActionMap(Class loader) {
075: _loader = loader;
076: }
077:
078: public void put(Action action) {
079: put(action.getValue(Action.NAME), action);
080: }
081:
082: @Override
083: public void put(Object key, Action action) {
084: loadIfNecessary();
085: super .put(key, action);
086: }
087:
088: @Override
089: public Action get(Object key) {
090: loadIfNecessary();
091: return super .get(key);
092: }
093:
094: @Override
095: public void remove(Object key) {
096: loadIfNecessary();
097: super .remove(key);
098: }
099:
100: @Override
101: public void clear() {
102: loadIfNecessary();
103: super .clear();
104: }
105:
106: @Override
107: public Object[] keys() {
108: loadIfNecessary();
109: return super .keys();
110: }
111:
112: @Override
113: public int size() {
114: loadIfNecessary();
115: return super .size();
116: }
117:
118: @Override
119: public Object[] allKeys() {
120: loadIfNecessary();
121: return super .allKeys();
122: }
123:
124: @Override
125: public void setParent(ActionMap map) {
126: loadIfNecessary();
127: super .setParent(map);
128: }
129:
130: private void loadIfNecessary() {
131: if (_loader != null) {
132: Object loader = _loader;
133:
134: _loader = null;
135: Class klass = (Class) loader;
136: try {
137: Method method = klass.getDeclaredMethod(
138: "loadActionMap",
139: new Class[] { LazyActionMap.class });
140: method.invoke(klass, this );
141: } catch (Exception nsme) {
142: System.out
143: .println("LazyActionMap unable to load actions "
144: + klass);
145: }
146: }
147: }
148: }
|