1. /*
  2. * @(#)ComponentInputMap.java 1.8 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. /**
  9. * A <code>ComponentInputMap</code> is an <code>InputMap</code>
  10. * associated with a particular <code>JComponent</code>.
  11. * The component is automatically notified whenever
  12. * the <code>ComponentInputMap</code> changes.
  13. * <code>ComponentInputMap</code>s are used for
  14. * <code>WHEN_IN_FOCUSED_WINDOW</code> bindings.
  15. *
  16. * @version 1.8 01/23/03
  17. * @author Scott Violet
  18. */
  19. public class ComponentInputMap extends InputMap {
  20. /** Component binding is created for. */
  21. private JComponent component;
  22. /**
  23. * Creates a <code>ComponentInputMap</code> associated with the
  24. * specified component.
  25. *
  26. * @param component a non-null <code>JComponent</code>
  27. * @throws IllegalArgumentException if <code>component</code> is null
  28. */
  29. public ComponentInputMap(JComponent component) {
  30. this.component = component;
  31. if (component == null) {
  32. throw new IllegalArgumentException("ComponentInputMaps must be associated with a non-null JComponent");
  33. }
  34. }
  35. /**
  36. * Sets the parent, which must be a <code>ComponentInputMap</code>
  37. * associated with the same component as this
  38. * <code>ComponentInputMap</code>.
  39. *
  40. * @param map a <code>ComponentInputMap</code>
  41. *
  42. * @throws IllegalArgumentException if <code>map</code>
  43. * is not a <code>ComponentInputMap</code>
  44. * or is not associated with the same component
  45. */
  46. public void setParent(InputMap map) {
  47. if (getParent() == map) {
  48. return;
  49. }
  50. if (map != null && (!(map instanceof ComponentInputMap) ||
  51. ((ComponentInputMap)map).getComponent() != getComponent())) {
  52. throw new IllegalArgumentException("ComponentInputMaps must have a parent ComponentInputMap associated with the same component");
  53. }
  54. super.setParent(map);
  55. getComponent().componentInputMapChanged(this);
  56. }
  57. /**
  58. * Returns the component the <code>InputMap</code> was created for.
  59. */
  60. public JComponent getComponent() {
  61. return component;
  62. }
  63. /**
  64. * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
  65. * If <code>actionMapKey</code> is null, this removes the current binding
  66. * for <code>keyStroke</code>.
  67. */
  68. public void put(KeyStroke keyStroke, Object actionMapKey) {
  69. super.put(keyStroke, actionMapKey);
  70. if (getComponent() != null) {
  71. getComponent().componentInputMapChanged(this);
  72. }
  73. }
  74. /**
  75. * Removes the binding for <code>key</code> from this object.
  76. */
  77. public void remove(KeyStroke key) {
  78. super.remove(key);
  79. if (getComponent() != null) {
  80. getComponent().componentInputMapChanged(this);
  81. }
  82. }
  83. /**
  84. * Removes all the mappings from this object.
  85. */
  86. public void clear() {
  87. int oldSize = size();
  88. super.clear();
  89. if (oldSize > 0 && getComponent() != null) {
  90. getComponent().componentInputMapChanged(this);
  91. }
  92. }
  93. }