1 /* 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package 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 */ 38 @SuppressWarnings("serial") // Field data not serializable across versions 39 public class ComponentInputMap extends InputMap { 40 /** Component binding is created for. */ 41 private JComponent component; 42 43 /** 44 * Creates a <code>ComponentInputMap</code> associated with the 45 * specified component. 46 * 47 * @param component a non-null <code>JComponent</code> 48 * @throws IllegalArgumentException if <code>component</code> is null 49 */ ComponentInputMap(JComponent component)50 public ComponentInputMap(JComponent component) { 51 this.component = component; 52 if (component == null) { 53 throw new IllegalArgumentException("ComponentInputMaps must be associated with a non-null JComponent"); 54 } 55 } 56 57 /** 58 * Sets the parent, which must be a <code>ComponentInputMap</code> 59 * associated with the same component as this 60 * <code>ComponentInputMap</code>. 61 * 62 * @param map a <code>ComponentInputMap</code> 63 * 64 * @throws IllegalArgumentException if <code>map</code> 65 * is not a <code>ComponentInputMap</code> 66 * or is not associated with the same component 67 */ setParent(InputMap map)68 public void setParent(InputMap map) { 69 if (getParent() == map) { 70 return; 71 } 72 if (map != null && (!(map instanceof ComponentInputMap) || 73 ((ComponentInputMap)map).getComponent() != getComponent())) { 74 throw new IllegalArgumentException("ComponentInputMaps must have a parent ComponentInputMap associated with the same component"); 75 } 76 super.setParent(map); 77 getComponent().componentInputMapChanged(this); 78 } 79 80 /** 81 * Returns the component the {@code InputMap} was created for. 82 * 83 * @return the component the {@code InputMap} was created for. 84 */ getComponent()85 public JComponent getComponent() { 86 return component; 87 } 88 89 /** 90 * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>. 91 * If <code>actionMapKey</code> is null, this removes the current binding 92 * for <code>keyStroke</code>. 93 */ put(KeyStroke keyStroke, Object actionMapKey)94 public void put(KeyStroke keyStroke, Object actionMapKey) { 95 super.put(keyStroke, actionMapKey); 96 if (getComponent() != null) { 97 getComponent().componentInputMapChanged(this); 98 } 99 } 100 101 /** 102 * Removes the binding for <code>key</code> from this object. 103 */ remove(KeyStroke key)104 public void remove(KeyStroke key) { 105 super.remove(key); 106 if (getComponent() != null) { 107 getComponent().componentInputMapChanged(this); 108 } 109 } 110 111 /** 112 * Removes all the mappings from this object. 113 */ clear()114 public void clear() { 115 int oldSize = size(); 116 super.clear(); 117 if (oldSize > 0 && getComponent() != null) { 118 getComponent().componentInputMapChanged(this); 119 } 120 } 121 } 122