KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > interpreter > lib > swt > AbstractMenuInterpreter


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.interpreter.lib.swt;
27
28 import java.net.URL JavaDoc;
29
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.widgets.Menu;
33 import org.eclipse.swt.widgets.MenuItem;
34 import org.objectweb.fractal.api.NoSuchInterfaceException;
35 import org.objectweb.util.explorer.api.IconProvider;
36 import org.objectweb.util.explorer.api.MenuItemTreeView;
37 import org.objectweb.util.explorer.api.TreeView;
38 import org.objectweb.util.explorer.core.code.api.Code;
39 import org.objectweb.util.explorer.core.code.api.CodeProvider;
40 import org.objectweb.util.explorer.core.common.api.Description;
41 import org.objectweb.util.explorer.core.icon.api.IconDescription;
42 import org.objectweb.util.explorer.core.menu.api.AcceleratorDescription;
43 import org.objectweb.util.explorer.core.menu.api.ItemDescription;
44 import org.objectweb.util.explorer.core.menu.api.MenuDescription;
45 import org.objectweb.util.explorer.core.menu.api.MenuElement;
46 import org.objectweb.util.explorer.core.menu.api.MenuSeparator;
47 import org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter;
48 import org.objectweb.util.explorer.interpreter.lib.AbstractDescriptionInterpreter;
49
50 /**
51  *
52  *
53  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
54  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
55  *
56  * @version 0.1
57  */

58 public abstract class AbstractMenuInterpreter
59      extends AbstractDescriptionInterpreter
60 {
61
62     //==================================================================
63
//
64
// Internal States.
65
//
66
// ==================================================================
67

68     protected final String JavaDoc ICON_INTERPRETER = "icon-interpreter";
69         
70     // ==================================================================
71
//
72
// Constructors.
73
//
74
// ==================================================================
75

76     // ==================================================================
77
//
78
// Internal methods.
79
//
80
// ==================================================================
81

82     /**
83      * Provides the icon for the given icon description
84      * applied to the given object.
85      */

86     protected Image getIcon(IconDescription iconDesc, TreeView treeView){
87         if(iconDesc!=null){
88             IconProvider iconProvider = (IconProvider) getIconInterpreter().interprete(iconDesc, treeView);
89             if(iconProvider!=null){
90                 Object JavaDoc object = iconProvider.newIcon(treeView.getSelectedObject());
91                 if(object instanceof Image){
92                     return (Image)object;
93                 } else if(object instanceof String JavaDoc) {
94                     return (Image)(new IconFileProvider((String JavaDoc)object)).newIcon(treeView.getSelectedObject());
95                 } else if (object instanceof URL JavaDoc) {
96                     return (Image)(new IconFileProvider((URL JavaDoc)object)).newIcon(treeView.getSelectedObject());
97                 }
98             }
99         }
100         return null;
101     }
102
103     protected int getAccelerator(AcceleratorDescription accDesc){
104         int acc = 0;
105         if(accDesc!=null && !accDesc.isEmpty()){
106             if (accDesc.getCtrlKey()) acc+=SWT.CTRL;
107             if (accDesc.getAltKey()) acc+=SWT.ALT;
108             if (accDesc.getShiftKey()) acc+=SWT.SHIFT;
109             acc += accDesc.getAcceleratorCharacter();
110         }
111         return acc;
112     }
113     
114     protected String JavaDoc getAcceleratorName(AcceleratorDescription accDesc){
115         if(accDesc!=null && !accDesc.isEmpty()){
116             return accDesc.getName();
117         }
118         return null;
119     }
120     
121     /**
122      * Provides the Java/SWT item defined by the given item description.
123      */

124     protected void addMenuItem(Menu menu, ItemDescription itemDesc, MenuItemTreeView treeView){
125         if(itemDesc!=null && !itemDesc.isEmpty()){
126             Code actionCode = getCodeProvider().getCode(itemDesc.getCodeDescription());
127             if(actionCode!=null && actionCode.isInstanceOf(org.objectweb.util.explorer.api.MenuItem.class)){
128                 org.objectweb.util.explorer.api.MenuItem action = (org.objectweb.util.explorer.api.MenuItem)actionCode.createInstance();
129                 int actionStatus = action.getStatus(treeView);
130                 if(actionStatus!=org.objectweb.util.explorer.api.MenuItem.NOT_VISIBLE_STATUS){
131                     MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
132                     String JavaDoc acceleratorName = getAcceleratorName(itemDesc.getAcceleratorDescription());
133                     if(acceleratorName!=null && !acceleratorName.equals("")){
134                         menuItem.setText(itemDesc.getLabel() + "\t" + acceleratorName);
135                         menuItem.setAccelerator(getAccelerator(itemDesc.getAcceleratorDescription()));
136                     } else {
137                         menuItem.setText(itemDesc.getLabel());
138                     }
139                     menuItem.addListener(SWT.Selection, new GenericAction(action, treeView));
140                     menuItem.setEnabled(actionStatus!=org.objectweb.util.explorer.api.MenuItem.DISABLED_STATUS);
141                     menuItem.setImage(getIcon(itemDesc.getIconDescription(), treeView));
142                 }
143             } else {
144                 getTrace().warn(itemDesc.getCodeDescription().getCode() + " is not a MenuItem instance!");
145             }
146         }
147     }
148     
149     /**
150      * Provides a Java/SWT menu separator
151      */

152     protected void addMenuSeparator(Menu menu){
153         MenuItem separator = new MenuItem(menu, SWT.SEPARATOR);
154     }
155     
156     protected Menu createMenu(MenuDescription menuDesc, MenuItemTreeView treeView){
157         Menu menu = null;
158         if(menuDesc!=null && !menuDesc.isEmpty()){
159             menu = getMenu();
160             MenuElement[] menuElement = menuDesc.getAllMenuElements();
161             for (int i = 0; i < menuElement.length; i++) {
162                 MenuElement element = menuElement[i];
163                 if(element instanceof ItemDescription){
164                     addMenuItem(menu, (ItemDescription)element, treeView);
165                 } else if(element instanceof MenuSeparator){
166                     addMenuSeparator(menu);
167                 }
168             }
169         }
170         return menu;
171     }
172
173     protected DescriptionInterpreter getIconInterpreter(){
174         try {
175             return (DescriptionInterpreter)lookupFc(ICON_INTERPRETER);
176         } catch (NoSuchInterfaceException e) {
177             getTrace().warn(ICON_INTERPRETER + ": interface not found!");
178             return null;
179         }
180     }
181     
182     // ==================================================================
183
//
184
// Public methods for BindingFeature abstract class.
185
//
186
// ==================================================================
187

188     /* (non-Javadoc)
189      * @see org.objectweb.util.explorer.core.common.lib.BindingFeature#clientFc()
190      */

191     public String JavaDoc[] clientFc() {
192         return new String JavaDoc[]{CodeProvider.CODE_PROVIDER, ICON_INTERPRETER};
193     }
194
195     // ==================================================================
196
//
197
// Public methods for DescriptionInterpreter interface.
198
//
199
// ==================================================================
200

201     /* (non-Javadoc)
202      * @see org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter#interprete(org.objectweb.util.explorer.core.common.api.Description, org.objectweb.util.explorer.api.TreeView)
203      */

204     public Object JavaDoc interprete(Description description, TreeView treeView) {
205         MenuDescription menuDesc = (MenuDescription)description;
206         return createMenu(menuDesc, (MenuItemTreeView)treeView);
207     }
208
209     // ==================================================================
210
//
211
// Public abstract methods.
212
//
213
// ==================================================================
214

215     public abstract Menu getMenu();
216 }
217
Popular Tags