/*
* Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package javax.swing;
/**
* A ComponentInputMap is an InputMap
* associated with a particular JComponent.
* The component is automatically notified whenever
* the ComponentInputMap changes.
* ComponentInputMaps are used for
* WHEN_IN_FOCUSED_WINDOW bindings.
*
* @author Scott Violet
* @since 1.3
*/
public class ComponentInputMap extends InputMap {
/** Component binding is created for. */
private JComponent component;
/**
* Creates a ComponentInputMap associated with the
* specified component.
*
* @param component a non-null JComponent
* @throws IllegalArgumentException if component is null
*/
public ComponentInputMap(JComponent component) {
this.component = component;
if (component == null) {
throw new IllegalArgumentException("ComponentInputMaps must be associated with a non-null JComponent");
}
}
/**
* Sets the parent, which must be a ComponentInputMap
* associated with the same component as this
* ComponentInputMap.
*
* @param map a ComponentInputMap
*
* @throws IllegalArgumentException if map
* is not a ComponentInputMap
* or is not associated with the same component
*/
public void setParent(InputMap map) {
if (getParent() == map) {
return;
}
if (map != null && (!(map instanceof ComponentInputMap) ||
((ComponentInputMap)map).getComponent() != getComponent())) {
throw new IllegalArgumentException("ComponentInputMaps must have a parent ComponentInputMap associated with the same component");
}
super.setParent(map);
getComponent().componentInputMapChanged(this);
}
/**
* Returns the component the InputMap was created for.
*/
public JComponent getComponent() {
return component;
}
/**
* Adds a binding for keyStroke to actionMapKey.
* If actionMapKey is null, this removes the current binding
* for keyStroke.
*/
public void put(KeyStroke keyStroke, Object actionMapKey) {
super.put(keyStroke, actionMapKey);
if (getComponent() != null) {
getComponent().componentInputMapChanged(this);
}
}
/**
* Removes the binding for key from this object.
*/
public void remove(KeyStroke key) {
super.remove(key);
if (getComponent() != null) {
getComponent().componentInputMapChanged(this);
}
}
/**
* Removes all the mappings from this object.
*/
public void clear() {
int oldSize = size();
super.clear();
if (oldSize > 0 && getComponent() != null) {
getComponent().componentInputMapChanged(this);
}
}
}