001/*
002 * $Id: UIAction.java 4028 2011-06-03 19:32:19Z kschaefe $
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
022package org.jdesktop.swingx.plaf;
023
024import java.beans.PropertyChangeListener;
025
026import javax.swing.Action;
027
028/**
029 * UIAction is the basis of all of basic's action classes that are used in
030 * an ActionMap. Subclasses need to override <code>actionPerformed</code>.
031 * <p>
032 * A typical subclass will look like:
033 * <pre>
034 *    private static class Actions extends UIAction {
035 *        Actions(String name) {
036 *            super(name);
037 *        }
038 *
039 *        public void actionPerformed(ActionEvent ae) {
040 *            if (getName() == "selectAll") {
041 *                selectAll();
042 *            }
043 *            else if (getName() == "cancelEditing") {
044 *                cancelEditing();
045 *            }
046 *        }
047 *    }
048 * </pre>
049 * <p>
050 * Subclasses that wish to conditionalize the enabled state should override
051 * <code>isEnabled(Component)</code>, and be aware that the passed in
052 * <code>Component</code> may be null.
053 * <p>
054 * This is based on sun.swing.UIAction in J2SE 1.5
055 *
056 * @see javax.swing.Action
057 * @author Scott Violet
058 */
059public abstract class UIAction implements Action {
060    private String name;
061
062    public UIAction(String name) {
063        this.name = name;
064    }
065
066    public final String getName() {
067        return name;
068    }
069
070    @Override
071    public Object getValue(String key) {
072        return NAME.equals(key) ? name : null;
073    }
074
075    // UIAction is not mutable, this does nothing.
076    @Override
077    public void putValue(String key, Object value) {
078    }
079
080    // UIAction is not mutable, this does nothing.
081    @Override
082    public void setEnabled(boolean b) {
083    }
084
085    /**
086     * Cover method for <code>isEnabled(null)</code>.
087     */
088    @Override
089    public final boolean isEnabled() {
090        return isEnabled(null);
091    }
092
093    /**
094     * Subclasses that need to conditionalize the enabled state should
095     * override this. Be aware that <code>sender</code> may be null.
096     *
097     * @param sender Widget enabled state is being asked for, may be null.
098     */
099    public boolean isEnabled(Object sender) {
100        return true;
101    }
102
103    // UIAction is not mutable, this does nothing.
104    @Override
105    public void addPropertyChangeListener(PropertyChangeListener listener) {
106    }
107
108    // UIAction is not mutable, this does nothing.
109    @Override
110    public void removePropertyChangeListener(PropertyChangeListener listener) {
111    }
112}