001/*
002 * $Id: TargetableSupport.java 3972 2011-03-17 20:31:58Z 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.action;
023import java.awt.event.ActionEvent;
024
025import javax.swing.Action;
026import javax.swing.ActionMap;
027import javax.swing.JComponent;
028
029/**
030 *
031 * @author rbair
032 */
033public class TargetableSupport {
034    private JComponent component;
035    
036    /** Creates a new instance of TargetableSupport */
037    public TargetableSupport(JComponent component) {
038        this.component = component;
039    }
040    
041    public boolean doCommand(Object command, Object value) {
042        // Look at the internal component first.
043        ActionMap map = component.getActionMap();
044        Action action = map.get(command);
045
046        if (action != null) {
047            if (value instanceof ActionEvent) {
048                action.actionPerformed( (ActionEvent) value);
049            }
050            else {
051                // XXX should the value represent the event source?
052                action.actionPerformed(new ActionEvent(value, 0,
053                    command.toString()));
054            }
055            return true;
056        }
057        return false;
058    }
059
060    public Object[] getCommands() {
061        ActionMap map = component.getActionMap();
062        return map.allKeys();
063    }
064
065    public boolean hasCommand(Object command) {
066        Object[] commands = getCommands();
067        for (int i = 0; i < commands.length; i++) {
068            if (commands[i].equals(command)) {
069                return true;
070            }
071        }
072        return false;
073    }
074}