001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.core.gui.globalactions;
019:
020: import java.awt.KeyboardFocusManager;
021: import java.awt.Toolkit;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.KeyEvent;
024: import java.beans.PropertyChangeEvent;
025: import java.beans.PropertyChangeListener;
026:
027: import javax.swing.Action;
028: import javax.swing.JComponent;
029: import javax.swing.KeyStroke;
030: import javax.swing.TransferHandler;
031:
032: import org.columba.api.gui.frame.IFrameMediator;
033: import org.columba.core.gui.action.AbstractColumbaAction;
034: import org.columba.core.resourceloader.GlobalResourceLoader;
035: import org.columba.core.resourceloader.IconKeys;
036: import org.columba.core.resourceloader.ImageLoader;
037:
038: public class CopyAction extends AbstractColumbaAction implements
039: PropertyChangeListener {
040:
041: private JComponent focusOwner = null;
042:
043: public CopyAction(IFrameMediator controller) {
044: super (controller, GlobalResourceLoader.getString(null, null,
045: "menu_edit_copy"));
046:
047: // tooltip text
048: putValue(SHORT_DESCRIPTION, GlobalResourceLoader.getString(
049: null, null, "menu_edit_copy_tooltip").replaceAll("&",
050: ""));
051:
052: // small icon for menu
053: putValue(SMALL_ICON, ImageLoader
054: .getSmallIcon(IconKeys.EDIT_COPY));
055:
056: // large icon for toolbar
057: putValue(LARGE_ICON, ImageLoader.getIcon(IconKeys.EDIT_COPY));
058:
059: // short cut key
060: putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C,
061: Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
062:
063: // disable toolbar text
064: setShowToolBarText(false);
065:
066: setEnabled(true);
067:
068: putValue(Action.ACTION_COMMAND_KEY, (String) TransferHandler
069: .getCopyAction().getValue(Action.NAME));
070:
071: KeyboardFocusManager manager = KeyboardFocusManager
072: .getCurrentKeyboardFocusManager();
073: manager.addPropertyChangeListener("permanentFocusOwner", this );
074:
075: }
076:
077: public void propertyChange(PropertyChangeEvent e) {
078: Object o = e.getNewValue();
079: if (o instanceof JComponent) {
080: focusOwner = (JComponent) o;
081:
082: setEnabled(isEnabled());
083: } else
084: focusOwner = null;
085:
086: }
087:
088: /**
089: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
090: */
091: public void actionPerformed(ActionEvent e) {
092:
093: if (focusOwner == null)
094: return;
095: String action = (String) e.getActionCommand();
096: Action a = focusOwner.getActionMap().get(action);
097: if (a != null)
098: a.actionPerformed(new ActionEvent(focusOwner,
099: ActionEvent.ACTION_PERFORMED, null));
100:
101: }
102:
103: /**
104: * @see org.columba.core.gui.action.AbstractColumbaAction#isSingleton()
105: */
106: public boolean isSingleton() {
107: return true;
108: }
109:
110: /**
111: * @see javax.swing.AbstractAction#isEnabled()
112: */
113: // public boolean isEnabled() {
114: //
115: // if (focusOwner == null)
116: // return false;
117: //
118: // if (focusOwner instanceof JTextComponent) {
119: // return ((JTextComponent) focusOwner).getSelectedText() != null ? true:false;
120: // } else if (focusOwner instanceof JList) {
121: // return ((JList) focusOwner).getSelectedIndex() != -1 ? true:false;
122: // } else if (focusOwner instanceof JTable) {
123: // return ((JTable) focusOwner).getSelectedRow() != -1 ? true:false;
124: // } else if (focusOwner instanceof JTree) {
125: // return ((JTree) focusOwner).getSelectionPath() != null ? true:false;
126: // }
127: //
128: // return false;
129: // }
130: }
|