KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > swing > menu > GenericAction


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 package org.objectweb.util.explorer.swing.menu;
27
28 import java.awt.event.ActionEvent JavaDoc;
29
30 import javax.swing.AbstractAction JavaDoc;
31 import javax.swing.Icon JavaDoc;
32 import javax.swing.JOptionPane JavaDoc;
33 import javax.swing.KeyStroke JavaDoc;
34
35 import org.objectweb.util.explorer.api.MenuItem;
36 import org.objectweb.util.explorer.api.MenuItemTreeView;
37 import org.objectweb.util.explorer.core.menu.api.AcceleratorDescription;
38 import org.objectweb.util.explorer.core.menu.api.ItemDescription;
39 import org.objectweb.util.explorer.core.menu.api.MenuItemTreeViewConfiguration;
40 import org.objectweb.util.explorer.core.menu.api.MnemonicDescription;
41 import org.objectweb.util.explorer.swing.icon.EmptyIconProvider;
42 import org.objectweb.util.trace.TraceSystem;
43
44 /**
45  * Defines the default action wich wraps the MenuItem.
46  *
47  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
48  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
49  *
50  * @version 0.1
51  */

52 public class GenericAction
53      extends AbstractAction JavaDoc
54 {
55     
56     //==================================================================
57
//
58
// Internal States.
59
//
60
// ==================================================================
61

62     protected MenuItem delegate_;
63     protected MenuItemTreeView treeView_;
64     protected boolean isUserIcon_ = true;
65     
66     // ==================================================================
67
//
68
// Constructors.
69
//
70
// ==================================================================
71

72     /**
73      * Default constructor
74      */

75     public GenericAction(ItemDescription itemDesc, MenuItem action, MenuItemTreeView treeView, Icon JavaDoc icon, boolean enabled){
76         super(itemDesc.getLabel(),icon);
77         
78         delegate_ = action;
79         treeView_ = treeView;
80         
81         if(icon==null){
82             putValue(SMALL_ICON, new EmptyIconProvider().newIcon(null));
83             isUserIcon_ = false;
84         }
85             
86         // Fixes action properties
87
putValue(SHORT_DESCRIPTION, itemDesc.getLabel());
88         MnemonicDescription md = itemDesc.getMnemonicDescription();
89         if(md!=null && !md.isEmpty()){
90             putValue(MNEMONIC_KEY,new Integer JavaDoc(Character.toUpperCase(md.getMnemonicCharacter())));
91         }
92         KeyStroke JavaDoc key = getKeyStroke(itemDesc.getAcceleratorDescription());
93         if(key!=null){
94             putValue(ACCELERATOR_KEY,key);
95         }
96         setEnabled(enabled);
97     }
98     
99     // ==================================================================
100
//
101
// Internal method.
102
//
103
// ==================================================================
104

105     /**
106      * Provides the KeyStroke associates to the given Mnemonic.
107      * @param mnemonic The object containing the definition of the keyStroke
108      * @return The associated KeyStroke.
109      */

110     protected KeyStroke JavaDoc getKeyStroke(AcceleratorDescription acceleratorDesc){
111         if(acceleratorDesc!=null && !acceleratorDesc.isEmpty()){
112             int modifier = 0;
113             modifier = (acceleratorDesc.getAltKey()?java.awt.Event.ALT_MASK:0)
114                 |(acceleratorDesc.getCtrlKey()?java.awt.Event.CTRL_MASK:0)
115                 |(acceleratorDesc.getShiftKey()?java.awt.Event.SHIFT_MASK:0)
116                 |(acceleratorDesc.getMetaKey()?java.awt.Event.META_MASK:0);
117             if(modifier==0)
118                 return KeyStroke.getKeyStroke(Character.toUpperCase(acceleratorDesc.getAcceleratorCharacter()));
119             else
120                 return KeyStroke.getKeyStroke(Character.toUpperCase(acceleratorDesc.getAcceleratorCharacter()), modifier);
121         }
122         return null;
123     }
124     
125     // ==================================================================
126
//
127
// Public methods.
128
//
129
// ==================================================================
130

131     /* (non-Javadoc)
132      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
133      */

134     public void actionPerformed(ActionEvent JavaDoc actionEvent) {
135         try{
136             ((MenuItemTreeViewConfiguration)treeView_).setEvent(actionEvent);
137             delegate_.actionPerformed(treeView_);
138         } catch (Exception JavaDoc e) {
139             TraceSystem.get("explorer").warn(getClass().getName() + " exception: " + e.getMessage());
140             JOptionPane.showMessageDialog(null, e.getClass().getName() + ":\n" + e.getMessage(), "Exception (" + actionEvent.getActionCommand() + ")", JOptionPane.ERROR_MESSAGE);
141         }
142         treeView_.getTree().refreshAll();
143     }
144
145     public boolean isUserIcon(){
146         return isUserIcon_;
147     }
148 }
149
Popular Tags