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