KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > ComponentInputMap


1 /*
2  * @(#)ComponentInputMap.java 1.9 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;
8
9 /**
10  * A <code>ComponentInputMap</code> is an <code>InputMap</code>
11  * associated with a particular <code>JComponent</code>.
12  * The component is automatically notified whenever
13  * the <code>ComponentInputMap</code> changes.
14  * <code>ComponentInputMap</code>s are used for
15  * <code>WHEN_IN_FOCUSED_WINDOW</code> bindings.
16  *
17  * @version 1.9 12/19/03
18  * @author Scott Violet
19  */

20 public class ComponentInputMap extends InputMap JavaDoc {
21     /** Component binding is created for. */
22     private JComponent JavaDoc component;
23
24     /**
25      * Creates a <code>ComponentInputMap</code> associated with the
26      * specified component.
27      *
28      * @param component a non-null <code>JComponent</code>
29      * @throws IllegalArgumentException if <code>component</code> is null
30      */

31     public ComponentInputMap(JComponent JavaDoc component) {
32     this.component = component;
33     if (component == null) {
34         throw new IllegalArgumentException JavaDoc("ComponentInputMaps must be associated with a non-null JComponent");
35     }
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 JavaDoc map) {
50     if (getParent() == map) {
51         return;
52     }
53     if (map != null && (!(map instanceof ComponentInputMap JavaDoc) ||
54          ((ComponentInputMap JavaDoc)map).getComponent() != getComponent())) {
55         throw new IllegalArgumentException JavaDoc("ComponentInputMaps must have a parent ComponentInputMap associated with the same component");
56     }
57     super.setParent(map);
58     getComponent().componentInputMapChanged(this);
59     }
60
61     /**
62      * Returns the component the <code>InputMap</code> was created for.
63      */

64     public JComponent JavaDoc getComponent() {
65     return component;
66     }
67
68     /**
69      * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
70      * If <code>actionMapKey</code> is null, this removes the current binding
71      * for <code>keyStroke</code>.
72      */

73     public void put(KeyStroke JavaDoc keyStroke, Object JavaDoc actionMapKey) {
74     super.put(keyStroke, actionMapKey);
75     if (getComponent() != null) {
76         getComponent().componentInputMapChanged(this);
77     }
78     }
79
80     /**
81      * Removes the binding for <code>key</code> from this object.
82      */

83     public void remove(KeyStroke JavaDoc key) {
84     super.remove(key);
85     if (getComponent() != null) {
86         getComponent().componentInputMapChanged(this);
87     }
88     }
89
90     /**
91      * Removes all the mappings from this object.
92      */

93     public void clear() {
94     int oldSize = size();
95     super.clear();
96     if (oldSize > 0 && getComponent() != null) {
97         getComponent().componentInputMapChanged(this);
98     }
99     }
100 }
101
Popular Tags