jdk/src/share/classes/javax/swing/ActionMap.java
author idk
Mon, 23 Jun 2008 15:21:37 -0400
changeset 1273 3b2eba521268
parent 2 90ce3da70b43
child 1301 15e81207e1f2
permissions -rw-r--r--
6623943: javax.swing.TimerQueue's thread occasionally fails to start Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class ActionMap implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Handles the mapping between Action name and Action. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private transient ArrayTable     arrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** Parent that handles any bindings we don't contain. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private ActionMap                               parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates an <code>ActionMap</code> with no parent and no mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public ActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Sets this <code>ActionMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param map  the <code>ActionMap</code> that is the parent of this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void setParent(ActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.parent = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Returns this <code>ActionMap</code>'s parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return the <code>ActionMap</code> that is the parent of this one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *         or null if this <code>ActionMap</code> has no parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public ActionMap getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Adds a binding for <code>key</code> to <code>action</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * If <code>action</code> is null, this removes the current binding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * for <code>key</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <p>In most instances, <code>key</code> will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <code>action.getValue(NAME)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void put(Object key, Action action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                arrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            arrayTable.put(key, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
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
     * Returns the binding for <code>key</code>, messaging the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * parent <code>ActionMap</code> if the binding is not locally defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public Action get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Action value = (arrayTable == null) ? null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                       (Action)arrayTable.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            ActionMap    parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                return parent.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Removes the binding for <code>key</code> from this <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public void remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            arrayTable.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
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
     * Removes all the mappings from this <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            arrayTable.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
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
     * Returns the <code>Action</code> names that are bound in this <code>ActionMap</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Object[] keys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return arrayTable.getKeys(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Returns the number of bindings in this {@code ActionMap}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the number of bindings in this {@code ActionMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return arrayTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Returns an array of the keys defined in this <code>ActionMap</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * its parent. This method differs from <code>keys()</code> in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * this method includes the keys defined in the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public Object[] allKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int           count = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ActionMap     parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (parent == null) {
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
        Object[]    keys = keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Object[]    pKeys =  parent.allKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (pKeys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // Should only happen if size() != keys.length, which should only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // happen if mutated from multiple threads (or a bogus subclass).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return pKeys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        HashMap        keyMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int            counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        for (counter = keys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            keyMap.put(keys[counter], keys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        for (counter = pKeys.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            keyMap.put(pKeys[counter], pKeys[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return keyMap.keySet().toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        ArrayTable.writeArrayTable(s, arrayTable);
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 readObject(ObjectInputStream s) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                                 IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        for (int counter = s.readInt() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            put(s.readObject(), (Action)s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}