jdk/src/share/classes/javax/swing/InputMap.java
author rriggs
Tue, 03 Jun 2014 11:18:03 -0400
changeset 25122 1ecc464c69d2
parent 23010 6dadb192ad81
child 25386 9ef80c24fd74
permissions -rw-r--r--
8044460: Cleanup new Boolean and single character strings Reviewed-by: pchelko, serb, rriggs Contributed-by: otaviopolianasantana@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 11268
diff changeset
     2
 * Copyright (c) 1999, 2011, 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
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
    55
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class InputMap implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** Handles the mapping between KeyStroke and Action name. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private transient ArrayTable     arrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Parent that handles any bindings we don't contain. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private InputMap                                parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Creates an <code>InputMap</code> with no parent and no mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public InputMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Sets this <code>InputMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param map  the <code>InputMap</code> that is the parent of this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void setParent(InputMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.parent = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Gets this <code>InputMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return map  the <code>InputMap</code> that is the parent of this one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *              or null if this <code>InputMap</code> has no parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public InputMap getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Adds a binding for <code>keyStroke</code> to <code>actionMapKey</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * If <code>actionMapKey</code> is null, this removes the current binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * for <code>keyStroke</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void put(KeyStroke keyStroke, Object actionMapKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (keyStroke == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (actionMapKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            remove(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                arrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            arrayTable.put(keyStroke, actionMapKey);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the binding for <code>keyStroke</code>, messaging the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * parent <code>InputMap</code> if the binding is not locally defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public Object get(KeyStroke keyStroke) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            InputMap    parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return parent.get(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        Object value = arrayTable.get(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            InputMap    parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return parent.get(keyStroke);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Removes the binding for <code>key</code> from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>InputMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public void remove(KeyStroke key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            arrayTable.remove(key);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Removes all the mappings from this <code>InputMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            arrayTable.clear();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns the <code>KeyStroke</code>s that are bound in this <code>InputMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public KeyStroke[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        KeyStroke[] keys = new KeyStroke[arrayTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        arrayTable.getKeys(keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns the number of <code>KeyStroke</code> bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return arrayTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns an array of the <code>KeyStroke</code>s defined in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <code>InputMap</code> and its parent. This differs from <code>keys()</code> in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * this method includes the keys defined in the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public KeyStroke[] allKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int             count = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        InputMap        parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (parent == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        KeyStroke[]    keys = keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        KeyStroke[]    pKeys =  parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (pKeys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // Should only happen if size() != keys.length, which should only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // happen if mutated from multiple threads (or a bogus subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return pKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   204
        HashMap<KeyStroke, KeyStroke> keyMap = new HashMap<KeyStroke, KeyStroke>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int            counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        for (counter = keys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            keyMap.put(keys[counter], keys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        for (counter = pKeys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            keyMap.put(pKeys[counter], pKeys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        KeyStroke[]    allKeys = new KeyStroke[keyMap.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   216
        return keyMap.keySet().toArray(allKeys);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        ArrayTable.writeArrayTable(s, arrayTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    private void readObject(ObjectInputStream s) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                                 IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        for (int counter = s.readInt() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            put((KeyStroke)s.readObject(), s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}