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      */
  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      */
  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</code> was created for.
  82      */
  83     public JComponent getComponent() {
  84         return component;
  85     }
  86 
  87     /**
  88      * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
  89      * If <code>actionMapKey</code> is null, this removes the current binding
  90      * for <code>keyStroke</code>.
  91      */
  92     public void put(KeyStroke keyStroke, Object actionMapKey) {
  93         super.put(keyStroke, actionMapKey);
  94         if (getComponent() != null) {
  95             getComponent().componentInputMapChanged(this);
  96         }
  97     }
  98 
  99     /**
 100      * Removes the binding for <code>key</code> from this object.
 101      */
 102     public void remove(KeyStroke key) {
 103         super.remove(key);
 104         if (getComponent() != null) {
 105             getComponent().componentInputMapChanged(this);
 106         }
 107     }
 108 
 109     /**
 110      * Removes all the mappings from this object.
 111      */
 112     public void clear() {
 113         int oldSize = size();
 114         super.clear();
 115         if (oldSize > 0 && getComponent() != null) {
 116             getComponent().componentInputMapChanged(this);
 117         }
 118     }
 119 }