Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


The Action interface is presented in the following code. As can be seen, constants are defined for the keys used when adding the standard descriptions to an Action. The setEnabled method can be used to enable or disable the Action, similar to how a Component can be enabled or disabled. Finally, the interface defines methods for adding and removing property change listeners. An Action-aware control listens for PropertyChangeEvents fired by the Action and updates its state accordingly. For example, when an Action is added to a menu or toolbar and the Action is disabled, the menu and toolbar will display the control associated with the Action as disabled.

public interface Action extends ActionListener {
    /**
     * Useful constants that can be used as the storage-retrieval key
     * when setting or getting one of this object’s properties (text
     * or icon).
     */
    public static final String DEFAULT = “Default”;
    public static final String NAME = “Name”;
    public static final String SHORT_DESCRIPTION = “ShortDescription”;
    public static final String LONG_DESCRIPTION = “LongDescription”;
    public static final String SMALL_ICON = “SmallIcon”;

    /**
     * Puts/gets one of this object’s properties
     * using the associated key.  If the value has
     * changed, a PropertyChangeEvent will be sent
     * to listeners.
     */
    public Object getValue(String key);
    public void putValue(String key, Object value);

    /**
     * Sets/tests the enabled state of the Action. When enabled,
     * any component associated with this object is active and
     * able to fire this object’s <code>actionPerformed</code> method.
     */
    public void setEnabled(boolean b);
    public boolean isEnabled();

    /**
     * Add or remove a PropertyChange listener. Containers and attached
     * components use these methods to register interest in this Action
     * object. When its enabled state or other property changes,
     * the registered listeners are informed of the change.
     */
    public void addPropertyChangeListener(
                   PropertyChangeListener listener);
    public void removePropertyChangeListener(
                   PropertyChangeListener listener);
}

To ease the burden of writing Actions, the AbstractAction class is provided by the JFC. The AbstractAction class, as its name implies, is an abstract class. It manages property change listeners and fires PropertyChangeEvents when a value is changed in the Action, or when the enabled state of the Action changes. An annoying characteristic of the AbstractAction implementation is that a null value cannot be added to the Action by using the putValue method. If this is attempted, a NullPointerException is thrown. Classes that extend the AbstactAction class must provide an implementation for the actionPerformed method. This is where the functionality contained in the Action lives.

Perhaps the most trivial Action that can be written is shown next. This is an Action that does nothing. From the NoOpAction class, it is seen how easily an Action can be written. When the AbstractAction class is extended, the subclass must simply provide an actionPerformed method. The NoOpAction class also supplies a name for the Action in its constructor. The AbstractAction class provides the implementation for the methods defined in the Action interface. The only method left to implement is the method defined in the ActionListener interface, actionPerformed.

package com.foley.utility;

import java.awt.event.*;

import javax.swing.*;

/**
 * Class NoOpAction
 * <p>
 * An action which does nothing.
 * <p>
 * @author Mike Foley
 * @version 1.2
 **/
public class NoOpAction extends AbstractAction {

    /**
     * The default name used in a menu.
     **/
    private static final String DEFAULT_NAME = “NoOp”;

    public NoOpAction() {
        super( DEFAULT_NAME );
    } // NoOpAction

    /**
     * actionPerformed, from ActionListener
     * <p>
     * Do nothing.
     * <p>
     * @param event The event causing the action to fire.
     **/
    public void actionPerformed( ActionEvent event ) {
    } // actionPerformed

} // NoOpAction


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.