1 7 package javax.swing.plaf.basic; 8 9 import java.lang.reflect.*; 10 import javax.swing.*; 11 import javax.swing.plaf.*; 12 13 21 class LazyActionMap extends ActionMapUIResource { 22 26 private transient Object _loader; 27 28 41 static void installLazyActionMap(JComponent c, Class loaderClass, 42 String defaultsKey) { 43 ActionMap map = (ActionMap)UIManager.get(defaultsKey); 44 if (map == null) { 45 map = new LazyActionMap (loaderClass); 46 UIManager.getLookAndFeelDefaults().put(defaultsKey, map); 47 } 48 SwingUtilities.replaceUIActionMap(c, map); 49 } 50 51 64 static ActionMap getActionMap(Class loaderClass, 65 String defaultsKey) { 66 ActionMap map = (ActionMap)UIManager.get(defaultsKey); 67 if (map == null) { 68 map = new LazyActionMap (loaderClass); 69 UIManager.getLookAndFeelDefaults().put(defaultsKey, map); 70 } 71 return map; 72 } 73 74 75 private LazyActionMap(Class loader) { 76 _loader = loader; 77 } 78 79 public void put(Action action) { 80 put(action.getValue(Action.NAME), action); 81 } 82 83 public void put(Object key, Action action) { 84 loadIfNecessary(); 85 super.put(key, action); 86 } 87 88 public Action get(Object key) { 89 loadIfNecessary(); 90 return super.get(key); 91 } 92 93 public void remove(Object key) { 94 loadIfNecessary(); 95 super.remove(key); 96 } 97 98 public void clear() { 99 loadIfNecessary(); 100 super.clear(); 101 } 102 103 public Object [] keys() { 104 loadIfNecessary(); 105 return super.keys(); 106 } 107 108 public int size() { 109 loadIfNecessary(); 110 return super.size(); 111 } 112 113 public Object [] allKeys() { 114 loadIfNecessary(); 115 return super.allKeys(); 116 } 117 118 public void setParent(ActionMap map) { 119 loadIfNecessary(); 120 super.setParent(map); 121 } 122 123 private void loadIfNecessary() { 124 if (_loader != null) { 125 Object loader = _loader; 126 127 _loader = null; 128 Class klass = (Class )loader; 129 try { 130 Method method = klass.getDeclaredMethod("loadActionMap", 131 new Class [] { LazyActionMap .class }); 132 method.invoke(klass, new Object [] { this }); 133 } catch (NoSuchMethodException nsme) { 134 assert false : "LazyActionMap unable to load actions " + 135 klass; 136 } catch (IllegalAccessException iae) { 137 assert false : "LazyActionMap unable to load actions " + 138 iae; 139 } catch (InvocationTargetException ite) { 140 assert false : "LazyActionMap unable to load actions " + 141 ite; 142 } catch (IllegalArgumentException iae) { 143 assert false : "LazyActionMap unable to load actions " + 144 iae; 145 } 146 } 147 } 148 } 149 | Popular Tags |