| 1 |
/* |
| 2 |
* @(#)LazyActionMap.java 8/19/2006 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2006 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.plaf.basic; |
| 8 |
|
| 9 |
import com.jidesoft.plaf.UIDefaultsLookup; |
| 10 |
|
| 11 |
import javax.swing.*; |
| 12 |
import javax.swing.plaf.ActionMapUIResource; |
| 13 |
import java.lang.reflect.Method; |
| 14 |
|
| 15 |
/** |
| 16 |
* An ActionMap that populates its contents as necessary. The |
| 17 |
* contents are populated by invoking the <code>loadActionMap</code> |
| 18 |
* method on the passed in Object. |
| 19 |
* |
| 20 |
* @author Scott Violet |
| 21 |
* @version 1.5, 12/19/03 |
| 22 |
*/ |
| 23 |
public class LazyActionMap extends ActionMapUIResource { |
| 24 |
/** |
| 25 |
* Object to invoke <code>loadActionMap</code> on. This may be |
| 26 |
* a Class object. |
| 27 |
*/ |
| 28 |
private transient Object _loader; |
| 29 |
|
| 30 |
/** |
| 31 |
* Installs an ActionMap that will be populated by invoking the |
| 32 |
* <code>loadActionMap</code> method on the specified Class |
| 33 |
* when necessary. |
| 34 |
* <p/> |
| 35 |
* This should be used if the ActionMap can be shared. |
| 36 |
* |
| 37 |
* @param c JComponent to install the ActionMap on. |
| 38 |
* @param loaderClass Class object that gets loadActionMap invoked |
| 39 |
* on. |
| 40 |
* @param defaultsKey Key to use to defaults table to check for |
| 41 |
* existing map and what resulting Map will be registered on. |
| 42 |
*/ |
| 43 |
public static void installLazyActionMap(JComponent c, Class loaderClass, |
| 44 |
String defaultsKey) { |
| 45 |
ActionMap map = (ActionMap) UIDefaultsLookup.get(defaultsKey); |
| 46 |
if (map == null) { |
| 47 |
map = new LazyActionMap(loaderClass); |
| 48 |
UIManager.getLookAndFeelDefaults().put(defaultsKey, map); |
| 49 |
} |
| 50 |
SwingUtilities.replaceUIActionMap(c, map); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns an ActionMap that will be populated by invoking the |
| 55 |
* <code>loadActionMap</code> method on the specified Class |
| 56 |
* when necessary. |
| 57 |
* <p/> |
| 58 |
* This should be used if the ActionMap can be shared. |
| 59 |
* |
| 60 |
* @param loaderClass Class object that gets loadActionMap invoked |
| 61 |
* on. |
| 62 |
* @param defaultsKey Key to use to defaults table to check for |
| 63 |
* existing map and what resulting Map will be registered on. |
| 64 |
*/ |
| 65 |
static ActionMap getActionMap(Class loaderClass, |
| 66 |
String defaultsKey) { |
| 67 |
ActionMap map = (ActionMap) UIDefaultsLookup.get(defaultsKey); |
| 68 |
if (map == null) { |
| 69 |
map = new LazyActionMap(loaderClass); |
| 70 |
UIManager.getLookAndFeelDefaults().put(defaultsKey, map); |
| 71 |
} |
| 72 |
return map; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
private LazyActionMap(Class loader) { |
| 77 |
_loader = loader; |
| 78 |
} |
| 79 |
|
| 80 |
public void put(Action action) { |
| 81 |
put(action.getValue(Action.NAME), action); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void put(Object key, Action action) { |
| 86 |
loadIfNecessary(); |
| 87 |
super.put(key, action); |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public Action get(Object key) { |
| 92 |
loadIfNecessary(); |
| 93 |
return super.get(key); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void remove(Object key) { |
| 98 |
loadIfNecessary(); |
| 99 |
super.remove(key); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void clear() { |
| 104 |
loadIfNecessary(); |
| 105 |
super.clear(); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public Object[] keys() { |
| 110 |
loadIfNecessary(); |
| 111 |
return super.keys(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public int size() { |
| 116 |
loadIfNecessary(); |
| 117 |
return super.size(); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public Object[] allKeys() { |
| 122 |
loadIfNecessary(); |
| 123 |
return super.allKeys(); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void setParent(ActionMap map) { |
| 128 |
loadIfNecessary(); |
| 129 |
super.setParent(map); |
| 130 |
} |
| 131 |
|
| 132 |
private void loadIfNecessary() { |
| 133 |
if (_loader != null) { |
| 134 |
Object loader = _loader; |
| 135 |
|
| 136 |
_loader = null; |
| 137 |
Class klass = (Class) loader; |
| 138 |
try { |
| 139 |
Method method = klass.getDeclaredMethod("loadActionMap", |
| 140 |
new Class[]{LazyActionMap.class}); |
| 141 |
method.invoke(klass, this); |
| 142 |
} |
| 143 |
catch (Exception nsme) { |
| 144 |
System.out.println("LazyActionMap unable to load actions " + klass); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |