Path: blob/master/src/java.desktop/share/classes/javax/swing/ComponentInputMap.java
41153 views
/*1* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.swing;2526/**27* A <code>ComponentInputMap</code> is an <code>InputMap</code>28* associated with a particular <code>JComponent</code>.29* The component is automatically notified whenever30* the <code>ComponentInputMap</code> changes.31* <code>ComponentInputMap</code>s are used for32* <code>WHEN_IN_FOCUSED_WINDOW</code> bindings.33*34* @author Scott Violet35* @since 1.336*/37@SuppressWarnings("serial") // Field data not serializable across versions38public class ComponentInputMap extends InputMap {39/** Component binding is created for. */40private JComponent component;4142/**43* Creates a <code>ComponentInputMap</code> associated with the44* specified component.45*46* @param component a non-null <code>JComponent</code>47* @throws IllegalArgumentException if <code>component</code> is null48*/49public ComponentInputMap(JComponent component) {50this.component = component;51if (component == null) {52throw new IllegalArgumentException("ComponentInputMaps must be associated with a non-null JComponent");53}54}5556/**57* Sets the parent, which must be a <code>ComponentInputMap</code>58* associated with the same component as this59* <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 component66*/67public void setParent(InputMap map) {68if (getParent() == map) {69return;70}71if (map != null && (!(map instanceof ComponentInputMap) ||72((ComponentInputMap)map).getComponent() != getComponent())) {73throw new IllegalArgumentException("ComponentInputMaps must have a parent ComponentInputMap associated with the same component");74}75super.setParent(map);76getComponent().componentInputMapChanged(this);77}7879/**80* Returns the component the {@code InputMap} was created for.81*82* @return the component the {@code InputMap} was created for.83*/84public JComponent getComponent() {85return component;86}8788/**89* Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.90* If <code>actionMapKey</code> is null, this removes the current binding91* for <code>keyStroke</code>.92*/93public void put(KeyStroke keyStroke, Object actionMapKey) {94super.put(keyStroke, actionMapKey);95if (getComponent() != null) {96getComponent().componentInputMapChanged(this);97}98}99100/**101* Removes the binding for <code>key</code> from this object.102*/103public void remove(KeyStroke key) {104super.remove(key);105if (getComponent() != null) {106getComponent().componentInputMapChanged(this);107}108}109110/**111* Removes all the mappings from this object.112*/113public void clear() {114int oldSize = size();115super.clear();116if (oldSize > 0 && getComponent() != null) {117getComponent().componentInputMapChanged(this);118}119}120}121122123