jdk/src/share/classes/javax/swing/AbstractAction.java
author henryjen
Wed, 18 Jun 2014 09:13:46 -0700
changeset 25201 4adc75e0c4e5
parent 22574 7f8ce0c8c20a
child 25760 7955db32d6b0
permissions -rw-r--r--
8046485: Add missing @since tag under javax.swing.* Reviewed-by: alexsch, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, 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
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    52
 * of all JavaBeans&trade;
2
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
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 22574
diff changeset
    58
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    60
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public abstract class AbstractAction implements Action, Cloneable, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Whether or not actions should reconfigure all properties on null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static Boolean RECONFIGURE_ON_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Specifies whether action is enabled; the default is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected boolean enabled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Contains the array of key bindings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private transient ArrayTable arrayTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Whether or not to reconfigure all action properties from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * specified event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static boolean shouldReconfigure(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (e.getPropertyName() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            synchronized(AbstractAction.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (RECONFIGURE_ON_NULL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    RECONFIGURE_ON_NULL = Boolean.valueOf(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        AccessController.doPrivileged(new GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        "swing.actions.reconfigureOnNull", "false")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                return RECONFIGURE_ON_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Sets the enabled state of a component from an Action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param c the Component to set the enabled state on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param a the Action to set the enabled state from, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static void setEnabledFromAction(JComponent c, Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        c.setEnabled((a != null) ? a.isEnabled() : true);
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
     * Sets the tooltip text of a component from an Action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param c the Component to set the tooltip text on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param a the Action to set the tooltip text from, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    static void setToolTipTextFromAction(JComponent c, Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        c.setToolTipText(a != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                         (String)a.getValue(Action.SHORT_DESCRIPTION) : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static boolean hasSelectedKey(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return (a != null && a.getValue(Action.SELECTED_KEY) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static boolean isSelected(Action a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return Boolean.TRUE.equals(a.getValue(Action.SELECTED_KEY));
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Creates an {@code Action}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public AbstractAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Creates an {@code Action} with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param name the name ({@code Action.NAME}) for the action; a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *        value of {@code null} is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public AbstractAction(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        putValue(Action.NAME, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Creates an {@code Action} with the specified name and small icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param name the name ({@code Action.NAME}) 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
     * @param icon the small icon ({@code Action.SMALL_ICON}) for the action; a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *        value of {@code null} is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public AbstractAction(String name, Icon icon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        putValue(Action.SMALL_ICON, icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Gets the <code>Object</code> associated with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param key a string containing the specified <code>key</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the binding <code>Object</code> stored with this key; if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          are no keys, it will return <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see Action#getValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public Object getValue(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (key == "enabled") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return arrayTable.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Sets the <code>Value</code> associated with the specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param key  the <code>String</code> that identifies the stored object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param newValue the <code>Object</code> to store using this key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see Action#putValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public void putValue(String key, Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Object oldValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (key == "enabled") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            // Treat putValue("enabled") the same way as a call to setEnabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // If we don't do this it means the two may get out of sync, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // bogus property change notification would be sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // To avoid dependencies between putValue & setEnabled this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // directly changes enabled. If we instead called setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            // to change enabled, it would be possible for stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // overflow in the case where a developer implemented setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // in terms of putValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (newValue == null || !(newValue instanceof Boolean)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                newValue = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            oldValue = enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            enabled = (Boolean)newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                arrayTable = new ArrayTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            if (arrayTable.containsKey(key))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                oldValue = arrayTable.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // Remove the entry for key if newValue is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // else put in the newValue for key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (newValue == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                arrayTable.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                arrayTable.put(key,newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        firePropertyChange(key, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Returns true if the action is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return true if the action is enabled, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @see Action#isEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Sets whether the {@code Action} is enabled. The default is {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param newValue  {@code true} to enable the action, {@code false} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *                  disable it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @see Action#setEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void setEnabled(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        boolean oldValue = this.enabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (oldValue != newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            this.enabled = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            firePropertyChange("enabled",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                               Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns an array of <code>Object</code>s which are keys for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * which values have been set for this <code>AbstractAction</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * or <code>null</code> if no keys have values set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return an array of key objects, or <code>null</code> if no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *                  keys have values set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public Object[] getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (arrayTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        Object[] keys = new Object[arrayTable.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        arrayTable.getKeys(keys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * If any <code>PropertyChangeListeners</code> have been registered, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <code>changeSupport</code> field describes them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    protected SwingPropertyChangeSupport changeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Supports reporting bound property changes.  This method can be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * when a bound property has changed and it will send the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <code>PropertyChangeEvent</code> to any registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <code>PropertyChangeListeners</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (changeSupport == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            (oldValue != null && newValue != null && oldValue.equals(newValue))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        changeSupport.firePropertyChange(propertyName, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Adds a <code>PropertyChangeListener</code> to the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * The listener is registered for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * A <code>PropertyChangeEvent</code> will get fired in response to setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * a bound property, e.g. <code>setFont</code>, <code>setBackground</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * or <code>setForeground</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Note that if the current component is inheriting its foreground,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * background, or font from its container, then no event will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * fired in response to a change in the inherited property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param listener  The <code>PropertyChangeListener</code> to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @see Action#addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            changeSupport = new SwingPropertyChangeSupport(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        changeSupport.addPropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Removes a <code>PropertyChangeListener</code> from the listener list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * This removes a <code>PropertyChangeListener</code> that was registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * for all properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param listener  the <code>PropertyChangeListener</code> to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see Action#removePropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        changeSupport.removePropertyChangeListener(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Returns an array of all the <code>PropertyChangeListener</code>s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * to this AbstractAction with addPropertyChangeListener().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return all of the <code>PropertyChangeListener</code>s added or an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *         array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (changeSupport == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return new PropertyChangeListener[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return changeSupport.getPropertyChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Clones the abstract action. This gives the clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * its own copy of the key/value list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * which is not handled for you by <code>Object.clone()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     **/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    protected Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        AbstractAction newAction = (AbstractAction)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (arrayTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                newAction.arrayTable = (ArrayTable)arrayTable.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return newAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // Store the default fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // And the keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        ArrayTable.writeArrayTable(s, arrayTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private void readObject(ObjectInputStream s) throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        for (int counter = s.readInt() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            putValue((String)s.readObject(), s.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
}