001package jmri.util.com.sun;
002
003// This class comes from the Java Swing tutorial at
004// http://java.sun.com/docs/books/tutorial/uiswing/examples/dnd/ListCutPasteProject
005/*
006 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
007 *
008 * Redistribution and use in source and binary forms, with or without
009 * modification, are permitted provided that the following conditions
010 * are met:
011 *
012 *   - Redistributions of source code must retain the above copyright
013 *     notice, this list of conditions and the following disclaimer.
014 *
015 *   - Redistributions in binary form must reproduce the above copyright
016 *     notice, this list of conditions and the following disclaimer in the
017 *     documentation and/or other materials provided with the distribution.
018 *
019 *   - Neither the name of Sun Microsystems nor the names of its
020 *     contributors may be used to endorse or promote products derived
021 *     from this software without specific prior written permission.
022 *
023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
024 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
025 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
027 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
028 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
029 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
030 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
031 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
032 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034 */
035
036/*
037 * TransferActionListener.java is used by the ListCutPaste example.
038 */
039import java.awt.KeyboardFocusManager;
040import java.awt.event.ActionEvent;
041import java.awt.event.ActionListener;
042import java.beans.PropertyChangeEvent;
043import java.beans.PropertyChangeListener;
044import javax.swing.Action;
045import javax.swing.JComponent;
046
047/**
048 * A class that tracks the focused component. This is necessary to delegate the
049 * menu cut/copy/paste commands to the right component. An instance of this
050 * class is listening and when the user fires one of these commands, it calls
051 * the appropriate action on the currently focused component.
052 */
053public class TransferActionListener implements ActionListener,
054        PropertyChangeListener {
055
056    private JComponent focusOwner = null;
057
058    public TransferActionListener() {
059        KeyboardFocusManager manager = KeyboardFocusManager.
060                getCurrentKeyboardFocusManager();
061        manager.addPropertyChangeListener("permanentFocusOwner", this);
062    }
063
064    @Override
065    public void propertyChange(PropertyChangeEvent e) {
066        Object o = e.getNewValue();
067        if (o instanceof JComponent) {
068            focusOwner = (JComponent) o;
069        } else {
070            focusOwner = null;
071        }
072    }
073
074    @Override
075    public void actionPerformed(ActionEvent e) {
076        if (focusOwner == null) {
077            return;
078        }
079        String action = e.getActionCommand();
080        Action a = focusOwner.getActionMap().get(action);
081        if (a != null) {
082            a.actionPerformed(new ActionEvent(focusOwner,
083                    ActionEvent.ACTION_PERFORMED,
084                    null));
085        }
086    }
087}