001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.ui.action;
020    
021    import javax.swing.*;
022    import java.awt.event.ActionEvent;
023    import java.awt.event.ActionListener;
024    
025    /**
026     * AWTActionProxy acts as a proxy between a given <code>java.awt.event.ActionListener</code> and
027     * <code>java.swing.Action</code>, transferring received action events to the Action's <code>actionPerformed</code>
028     * method.
029     * This class provides an easy way to use <code>java.swing.Action</code> instances in AWT components.
030     *
031     * <p>
032     * Usage: after creating an <code>AWTActionProxy</code> instance, the <code>addActionListener</oode> method must be
033     * called on the AWT component which action events are to be proxied, using the <code>AWTActionProxy</code> instance as
034     * a parameter.
035     * </p>
036     *
037     * @author Maxence Bernard
038     */
039    public class AWTActionProxy implements ActionListener {
040    
041        /** Proxied Action */
042        private Action proxiedAction;
043    
044        /**
045         * Creates a new AWTActionProxy instance that will transfer ActionEvents caught by {@link #actionPerformed(java.awt.event.ActionEvent)}
046         * to the specified <code>Action</code>.
047         *
048         * @param action the Action instance to transfer the ActionEvents to.
049         */
050        public AWTActionProxy(Action action) {
051            this.proxiedAction = action;
052        }
053    
054        /**
055         * Returns the <code>Action</code> instance to which the ActionEvents received by {@link #actionPerformed(java.awt.event.ActionEvent)}
056         * are transferred. 
057         */
058        public Action getProxiedAction() {
059            return proxiedAction;
060        }
061    
062        /**
063         * Forwards the specified ActionEvent to the proxied Action.
064         */
065        public void actionPerformed(ActionEvent actionEvent) {
066            proxiedAction.actionPerformed(actionEvent);
067        }
068    }