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