KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > globalactions > CopyAction


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.globalactions;
19
20 import java.awt.KeyboardFocusManager JavaDoc;
21 import java.awt.Toolkit JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26
27 import javax.swing.Action JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.KeyStroke JavaDoc;
30 import javax.swing.TransferHandler JavaDoc;
31
32 import org.columba.api.gui.frame.IFrameMediator;
33 import org.columba.core.gui.action.AbstractColumbaAction;
34 import org.columba.core.resourceloader.GlobalResourceLoader;
35 import org.columba.core.resourceloader.IconKeys;
36 import org.columba.core.resourceloader.ImageLoader;
37
38 public class CopyAction extends AbstractColumbaAction implements
39         PropertyChangeListener JavaDoc {
40
41     private JComponent JavaDoc focusOwner = null;
42
43     public CopyAction(IFrameMediator controller) {
44         super(controller, GlobalResourceLoader.getString(null, null,
45                 "menu_edit_copy"));
46
47         // tooltip text
48
putValue(SHORT_DESCRIPTION, GlobalResourceLoader.getString(null, null,
49                 "menu_edit_copy_tooltip").replaceAll("&", ""));
50
51         // small icon for menu
52
putValue(SMALL_ICON, ImageLoader.getSmallIcon(IconKeys.EDIT_COPY));
53
54         // large icon for toolbar
55
putValue(LARGE_ICON, ImageLoader.getIcon(IconKeys.EDIT_COPY));
56
57         // short cut key
58
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C,
59                 Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
60
61         // disable toolbar text
62
setShowToolBarText(false);
63
64         setEnabled(true);
65
66         putValue(Action.ACTION_COMMAND_KEY, (String JavaDoc) TransferHandler
67                 .getCopyAction().getValue(Action.NAME));
68
69         KeyboardFocusManager JavaDoc manager = KeyboardFocusManager
70                 .getCurrentKeyboardFocusManager();
71         manager.addPropertyChangeListener("permanentFocusOwner", this);
72
73     }
74
75     public void propertyChange(PropertyChangeEvent JavaDoc e) {
76         Object JavaDoc o = e.getNewValue();
77         if (o instanceof JComponent JavaDoc) {
78             focusOwner = (JComponent JavaDoc) o;
79             
80             setEnabled(isEnabled());
81         }
82         else
83             focusOwner = null;
84
85     }
86
87     /**
88      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
89      */

90     public void actionPerformed(ActionEvent JavaDoc e) {
91
92         if (focusOwner == null)
93             return;
94         String JavaDoc action = (String JavaDoc) e.getActionCommand();
95         Action JavaDoc a = focusOwner.getActionMap().get(action);
96         if (a != null)
97             a.actionPerformed(new ActionEvent JavaDoc(focusOwner,
98                     ActionEvent.ACTION_PERFORMED, null));
99
100     }
101
102     /**
103      * @see org.columba.core.gui.action.AbstractColumbaAction#isSingleton()
104      */

105     public boolean isSingleton() {
106         return true;
107     }
108
109     /**
110      * @see javax.swing.AbstractAction#isEnabled()
111      */

112 // public boolean isEnabled() {
113
//
114
// if (focusOwner == null)
115
// return false;
116
//
117
// if (focusOwner instanceof JTextComponent) {
118
// return ((JTextComponent) focusOwner).getSelectedText() != null ? true:false;
119
// } else if (focusOwner instanceof JList) {
120
// return ((JList) focusOwner).getSelectedIndex() != -1 ? true:false;
121
// } else if (focusOwner instanceof JTable) {
122
// return ((JTable) focusOwner).getSelectedRow() != -1 ? true:false;
123
// } else if (focusOwner instanceof JTree) {
124
// return ((JTree) focusOwner).getSelectionPath() != null ? true:false;
125
// }
126
//
127
// return false;
128
// }
129
}
Popular Tags