001    /*
002     * $Id: UIAction.java 1846 2007-03-16 21:38:13Z rbair $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    
022    package org.jdesktop.swingx;
023    
024    import javax.swing.*;
025    import java.beans.PropertyChangeListener;
026    
027    /**
028     * UIAction is the basis of all of basic's action classes that are used in
029     * an ActionMap. Subclasses need to override <code>actionPerformed</code>.
030     * <p>
031     * A typical subclass will look like:
032     * <pre>
033     *    private static class Actions extends UIAction {
034     *        Actions(String name) {
035     *            super(name);
036     *        }
037     *
038     *        public void actionPerformed(ActionEvent ae) {
039     *            if (getName() == "selectAll") {
040     *                selectAll();
041     *            }
042     *            else if (getName() == "cancelEditing") {
043     *                cancelEditing();
044     *            }
045     *        }
046     *    }
047     * </pre>
048     * <p>
049     * Subclasses that wish to conditionalize the enabled state should override
050     * <code>isEnabled(Component)</code>, and be aware that the passed in
051     * <code>Component</code> may be null.
052     * <p>
053     * This is based on sun.swing.UIAction in J2SE 1.5
054     *
055     * @see javax.swing.Action
056     * @author Scott Violet
057     */
058    public abstract class UIAction implements Action {
059        private String name;
060    
061        public UIAction(String name) {
062            this.name = name;
063        }
064    
065        public final String getName() {
066            return name;
067        }
068    
069        public Object getValue(String key) {
070            return NAME.equals(key) ? name : null;
071        }
072    
073        // UIAction is not mutable, this does nothing.
074        public void putValue(String key, Object value) {
075        }
076    
077        // UIAction is not mutable, this does nothing.
078        public void setEnabled(boolean b) {
079        }
080    
081        /**
082         * Cover method for <code>isEnabled(null)</code>.
083         */
084        public final boolean isEnabled() {
085            return isEnabled(null);
086        }
087    
088        /**
089         * Subclasses that need to conditionalize the enabled state should
090         * override this. Be aware that <code>sender</code> may be null.
091         *
092         * @param sender Widget enabled state is being asked for, may be null.
093         */
094        public boolean isEnabled(Object sender) {
095            return true;
096        }
097    
098        // UIAction is not mutable, this does nothing.
099        public void addPropertyChangeListener(PropertyChangeListener listener) {
100        }
101    
102        // UIAction is not mutable, this does nothing.
103        public void removePropertyChangeListener(PropertyChangeListener listener) {
104        }
105    }