blob: ca3ff418e79dd1a74e90a50772c97b8d3d295e4f [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package javax.swing;
26
27/**
28 * A <code>ComponentInputMap</code> is an <code>InputMap</code>
29 * associated with a particular <code>JComponent</code>.
30 * The component is automatically notified whenever
31 * the <code>ComponentInputMap</code> changes.
32 * <code>ComponentInputMap</code>s are used for
33 * <code>WHEN_IN_FOCUSED_WINDOW</code> bindings.
34 *
35 * @author Scott Violet
36 * @since 1.3
37 */
38public class ComponentInputMap extends InputMap {
39 /** Component binding is created for. */
40 private JComponent component;
41
42 /**
43 * Creates a <code>ComponentInputMap</code> associated with the
44 * specified component.
45 *
46 * @param component a non-null <code>JComponent</code>
47 * @throws IllegalArgumentException if <code>component</code> is null
48 */
49 public ComponentInputMap(JComponent component) {
50 this.component = component;
51 if (component == null) {
52 throw new IllegalArgumentException("ComponentInputMaps must be associated with a non-null JComponent");
53 }
54 }
55
56 /**
57 * Sets the parent, which must be a <code>ComponentInputMap</code>
58 * associated with the same component as this
59 * <code>ComponentInputMap</code>.
60 *
61 * @param map a <code>ComponentInputMap</code>
62 *
63 * @throws IllegalArgumentException if <code>map</code>
64 * is not a <code>ComponentInputMap</code>
65 * or is not associated with the same component
66 */
67 public void setParent(InputMap map) {
68 if (getParent() == map) {
69 return;
70 }
71 if (map != null && (!(map instanceof ComponentInputMap) ||
72 ((ComponentInputMap)map).getComponent() != getComponent())) {
73 throw new IllegalArgumentException("ComponentInputMaps must have a parent ComponentInputMap associated with the same component");
74 }
75 super.setParent(map);
76 getComponent().componentInputMapChanged(this);
77 }
78
79 /**
80 * Returns the component the <code>InputMap</code> was created for.
81 */
82 public JComponent getComponent() {
83 return component;
84 }
85
86 /**
87 * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
88 * If <code>actionMapKey</code> is null, this removes the current binding
89 * for <code>keyStroke</code>.
90 */
91 public void put(KeyStroke keyStroke, Object actionMapKey) {
92 super.put(keyStroke, actionMapKey);
93 if (getComponent() != null) {
94 getComponent().componentInputMapChanged(this);
95 }
96 }
97
98 /**
99 * Removes the binding for <code>key</code> from this object.
100 */
101 public void remove(KeyStroke key) {
102 super.remove(key);
103 if (getComponent() != null) {
104 getComponent().componentInputMapChanged(this);
105 }
106 }
107
108 /**
109 * Removes all the mappings from this object.
110 */
111 public void clear() {
112 int oldSize = size();
113 super.clear();
114 if (oldSize > 0 && getComponent() != null) {
115 getComponent().componentInputMapChanged(this);
116 }
117 }
118}