jdk/src/share/classes/javax/swing/ActionMap.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23010 6dadb192ad81
child 24983 f5a6e2ed8c7d
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
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>ActionMap</code> provides mappings from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>Object</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * (called <em>keys</em> or <em><code>Action</code> names</em>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * to <code>Action</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * An <code>ActionMap</code> is usually used with an <code>InputMap</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to locate a particular action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * when a key is pressed. As with <code>InputMap</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * an <code>ActionMap</code> can have a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * that is searched for keys not defined in the <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>As with <code>InputMap</code> if you create a cycle, eg:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   ActionMap am = new ActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   ActionMap bm = new ActionMap():
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   am.setParent(bm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   bm.setParent(am);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * some of the methods will cause a StackOverflowError to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @see InputMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
11268
f0e59c4852de 7116950: Reduce number of warnings in swing
alexsch
parents: 5506
diff changeset
    58
@SuppressWarnings("serial")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class ActionMap implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /** Handles the mapping between Action name and Action. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private transient ArrayTable     arrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** Parent that handles any bindings we don't contain. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private ActionMap                               parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates an <code>ActionMap</code> with no parent and no mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public ActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Sets this <code>ActionMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param map  the <code>ActionMap</code> that is the parent of this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public void setParent(ActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.parent = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Returns this <code>ActionMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @return the <code>ActionMap</code> that is the parent of this one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *         or null if this <code>ActionMap</code> has no parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public ActionMap getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Adds a binding for <code>key</code> to <code>action</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * If <code>action</code> is null, this removes the current binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * for <code>key</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>In most instances, <code>key</code> will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <code>action.getValue(NAME)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public void put(Object key, Action action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                arrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            arrayTable.put(key, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns the binding for <code>key</code>, messaging the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * parent <code>ActionMap</code> if the binding is not locally defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public Action get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Action value = (arrayTable == null) ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                       (Action)arrayTable.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ActionMap    parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                return parent.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Removes the binding for <code>key</code> from this <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            arrayTable.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Removes all the mappings from this <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            arrayTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public Object[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return arrayTable.getKeys(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the number of bindings in this {@code ActionMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the number of bindings in this {@code ActionMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return arrayTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns an array of the keys defined in this <code>ActionMap</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * its parent. This method differs from <code>keys()</code> in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * this method includes the keys defined in the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public Object[] allKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        int           count = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ActionMap     parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                return parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (parent == null) {
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
        Object[]    keys = keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Object[]    pKeys =  parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (pKeys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // Should only happen if size() != keys.length, which should only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // happen if mutated from multiple threads (or a bogus subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return pKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   201
        HashMap<Object, Object> keyMap = new HashMap<Object, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int            counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (counter = keys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            keyMap.put(keys[counter], keys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        for (counter = pKeys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            keyMap.put(pKeys[counter], pKeys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return keyMap.keySet().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        ArrayTable.writeArrayTable(s, arrayTable);
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 readObject(ObjectInputStream s) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                                 IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        for (int counter = s.readInt() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            put(s.readObject(), (Action)s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
}