jdk/src/share/classes/javax/swing/InputMap.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 1639 a97859015238
child 11268 f0e59c4852de
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <code>InputMap</code> provides a binding between an input event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * (currently only <code>KeyStroke</code>s are used)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and an <code>Object</code>. <code>InputMap</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * are usually used with an <code>ActionMap</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * to determine an <code>Action</code> to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * when a key is pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An <code>InputMap</code> can have a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * that is searched for bindings not defined in the <code>InputMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>As with <code>ActionMap</code> if you create a cycle, eg:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   InputMap am = new InputMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   InputMap bm = new InputMap():
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   am.setParent(bm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   bm.setParent(am);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * some of the methods will cause a StackOverflowError to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class InputMap implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** Handles the mapping between KeyStroke and Action name. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private transient ArrayTable     arrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** Parent that handles any bindings we don't contain. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private InputMap                                parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Creates an <code>InputMap</code> with no parent and no mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public InputMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Sets this <code>InputMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param map  the <code>InputMap</code> that is the parent of this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public void setParent(InputMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.parent = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Gets this <code>InputMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return map  the <code>InputMap</code> that is the parent of this one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *              or null if this <code>InputMap</code> has no parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public InputMap getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * If <code>actionMapKey</code> is null, this removes the current binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * for <code>keyStroke</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void put(KeyStroke keyStroke, Object actionMapKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (keyStroke == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (actionMapKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            remove(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                arrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            arrayTable.put(keyStroke, actionMapKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Returns the binding for <code>keyStroke</code>, messaging the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * parent <code>InputMap</code> if the binding is not locally defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public Object get(KeyStroke keyStroke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            InputMap    parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                return parent.get(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Object value = arrayTable.get(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            InputMap    parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return parent.get(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Removes the binding for <code>key</code> from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>InputMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public void remove(KeyStroke key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            arrayTable.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Removes all the mappings from this <code>InputMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            arrayTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns the <code>KeyStroke</code>s that are bound in this <code>InputMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public KeyStroke[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        KeyStroke[] keys = new KeyStroke[arrayTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        arrayTable.getKeys(keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns the number of <code>KeyStroke</code> bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return arrayTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns an array of the <code>KeyStroke</code>s defined in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <code>InputMap</code> and its parent. This differs from <code>keys()</code> in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * this method includes the keys defined in the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public KeyStroke[] allKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int             count = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        InputMap        parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                return parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        KeyStroke[]    keys = keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        KeyStroke[]    pKeys =  parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (pKeys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // Should only happen if size() != keys.length, which should only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // happen if mutated from multiple threads (or a bogus subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return pKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   203
        HashMap<KeyStroke, KeyStroke> keyMap = new HashMap<KeyStroke, KeyStroke>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int            counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        for (counter = keys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            keyMap.put(keys[counter], keys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (counter = pKeys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            keyMap.put(pKeys[counter], pKeys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        KeyStroke[]    allKeys = new KeyStroke[keyMap.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   215
        return keyMap.keySet().toArray(allKeys);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        ArrayTable.writeArrayTable(s, arrayTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private void readObject(ObjectInputStream s) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                 IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        for (int counter = s.readInt() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            put((KeyStroke)s.readObject(), s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
}