jdk/src/share/classes/javax/swing/AbstractAction.java
author ant
Thu, 17 May 2012 21:48:57 +0400
changeset 12661 6cf8b7116579
parent 5506 202f599c92aa
child 20458 f2423fb3fd19
permissions -rw-r--r--
7125044: [macosx] Test failure because Component.transferFocus() works differently in applet and application. Summary: forward port from 7u4 Reviewed-by: art
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: 2
diff changeset
     2
 * Copyright (c) 1997, 2007, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.event.SwingPropertyChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class provides default implementations for the JFC <code>Action</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * interface. Standard behaviors like the get and set methods for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>Action</code> object properties (icon, text, and enabled) are defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * here. The developer need only subclass this abstract class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * define the <code>actionPerformed</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Georges Saab
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see Action
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public abstract class AbstractAction implements Action, Cloneable, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Whether or not actions should reconfigure all properties on null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static Boolean RECONFIGURE_ON_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Specifies whether action is enabled; the default is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected boolean enabled = true;
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
     * Contains the array of key bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private transient ArrayTable arrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Whether or not to reconfigure all action properties from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * specified event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static boolean shouldReconfigure(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (e.getPropertyName() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            synchronized(AbstractAction.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                if (RECONFIGURE_ON_NULL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    RECONFIGURE_ON_NULL = Boolean.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        AccessController.doPrivileged(new GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        "swing.actions.reconfigureOnNull", "false")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                return RECONFIGURE_ON_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Sets the enabled state of a component from an Action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param c the Component to set the enabled state on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param a the Action to set the enabled state from, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void setEnabledFromAction(JComponent c, Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        c.setEnabled((a != null) ? a.isEnabled() : true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Sets the tooltip text of a component from an Action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param c the Component to set the tooltip text on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param a the Action to set the tooltip text from, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static void setToolTipTextFromAction(JComponent c, Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        c.setToolTipText(a != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                         (String)a.getValue(Action.SHORT_DESCRIPTION) : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static boolean hasSelectedKey(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return (a != null && a.getValue(Action.SELECTED_KEY) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    static boolean isSelected(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return Boolean.TRUE.equals(a.getValue(Action.SELECTED_KEY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Creates an {@code Action}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public AbstractAction() {
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
     * Creates an {@code Action} with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param name the name ({@code Action.NAME}) for the action; a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *        value of {@code null} is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public AbstractAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        putValue(Action.NAME, name);
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
     * Creates an {@code Action} with the specified name and small icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param name the name ({@code Action.NAME}) for the action; a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *        value of {@code null} is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param icon the small icon ({@code Action.SMALL_ICON}) for the action; a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *        value of {@code null} is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public AbstractAction(String name, Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        this(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        putValue(Action.SMALL_ICON, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Gets the <code>Object</code> associated with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param key a string containing the specified <code>key</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return the binding <code>Object</code> stored with this key; if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          are no keys, it will return <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see Action#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public Object getValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (key == "enabled") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return arrayTable.get(key);
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
     * Sets the <code>Value</code> associated with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param key  the <code>String</code> that identifies the stored object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param newValue the <code>Object</code> to store using this key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see Action#putValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void putValue(String key, Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        Object oldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (key == "enabled") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // Treat putValue("enabled") the same way as a call to setEnabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // If we don't do this it means the two may get out of sync, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // bogus property change notification would be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // To avoid dependencies between putValue & setEnabled this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // directly changes enabled. If we instead called setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // to change enabled, it would be possible for stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // overflow in the case where a developer implemented setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            // in terms of putValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (newValue == null || !(newValue instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                newValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            oldValue = enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            enabled = (Boolean)newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                arrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (arrayTable.containsKey(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                oldValue = arrayTable.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // Remove the entry for key if newValue is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // else put in the newValue for key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (newValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                arrayTable.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                arrayTable.put(key,newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        firePropertyChange(key, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns true if the action is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return true if the action is enabled, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @see Action#isEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Sets whether the {@code Action} is enabled. The default is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param newValue  {@code true} to enable the action, {@code false} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *                  disable it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see Action#setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public void setEnabled(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        boolean oldValue = this.enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (oldValue != newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.enabled = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            firePropertyChange("enabled",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                               Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Returns an array of <code>Object</code>s which are keys for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * which values have been set for this <code>AbstractAction</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * or <code>null</code> if no keys have values set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return an array of key objects, or <code>null</code> if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *                  keys have values set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public Object[] getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        Object[] keys = new Object[arrayTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        arrayTable.getKeys(keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * If any <code>PropertyChangeListeners</code> have been registered, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <code>changeSupport</code> field describes them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    protected SwingPropertyChangeSupport changeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Supports reporting bound property changes.  This method can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * when a bound property has changed and it will send the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>PropertyChangeEvent</code> to any registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>PropertyChangeListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (changeSupport == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            (oldValue != null && newValue != null && oldValue.equals(newValue))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        changeSupport.firePropertyChange(propertyName, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Adds a <code>PropertyChangeListener</code> to the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * The listener is registered for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * A <code>PropertyChangeEvent</code> will get fired in response to setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * a bound property, e.g. <code>setFont</code>, <code>setBackground</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * or <code>setForeground</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Note that if the current component is inheriting its foreground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * background, or font from its container, then no event will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * fired in response to a change in the inherited property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param listener  The <code>PropertyChangeListener</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @see Action#addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            changeSupport = new SwingPropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        changeSupport.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Removes a <code>PropertyChangeListener</code> from the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * This removes a <code>PropertyChangeListener</code> that was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param listener  the <code>PropertyChangeListener</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @see Action#removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        changeSupport.removePropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Returns an array of all the <code>PropertyChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * to this AbstractAction with addPropertyChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return all of the <code>PropertyChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return new PropertyChangeListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return changeSupport.getPropertyChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Clones the abstract action. This gives the clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * its own copy of the key/value list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * which is not handled for you by <code>Object.clone()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    protected Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        AbstractAction newAction = (AbstractAction)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                newAction.arrayTable = (ArrayTable)arrayTable.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return newAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // Store the default fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // And the keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        ArrayTable.writeArrayTable(s, arrayTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private void readObject(ObjectInputStream s) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        for (int counter = s.readInt() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            putValue((String)s.readObject(), s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}