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