KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > interpreter > lib > swing > MenuInterpreter


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

61 public class MenuInterpreter
62      extends AbstractDescriptionInterpreter
63 {
64
65     //==================================================================
66
//
67
// Internal States.
68
//
69
// ==================================================================
70

71     protected final String JavaDoc ICON_INTERPRETER = "icon-interpreter";
72     
73     // ==================================================================
74
//
75
// Constructors.
76
//
77
// ==================================================================
78

79     // ==================================================================
80
//
81
// Internal method.
82
//
83
// ==================================================================
84

85     /**
86      * Provides the icon for the given icon description
87      * applied to the given object.
88      */

89     protected Icon JavaDoc getIcon(IconDescription iconDesc, TreeView treeView){
90         if(iconDesc!=null){
91             IconProvider iconProvider = (IconProvider) getIconInterpreter().interprete(iconDesc, treeView);
92             if(iconProvider!=null){
93                 Object JavaDoc object = iconProvider.newIcon(treeView.getSelectedObject());
94                 if(object instanceof Icon JavaDoc){
95                     return (Icon JavaDoc)object;
96                 } else if(object instanceof String JavaDoc) {
97                     return (Icon JavaDoc)(new IconFileProvider((String JavaDoc)object)).newIcon(treeView.getSelectedObject());
98                 } else if (object instanceof URL JavaDoc) {
99                     return (Icon JavaDoc)(new IconFileProvider((URL JavaDoc)object)).newIcon(treeView.getSelectedObject());
100                 }
101             }
102         }
103         return null;
104     }
105     
106     /**
107      * Provides the Java/Swing action defined by the given item description.
108      */

109     protected Action JavaDoc getAction(ItemDescription itemDesc, MenuItemTreeView treeView){
110         if(itemDesc!=null && !itemDesc.isEmpty()){
111             Code actionCode = getCodeProvider().getCode(itemDesc.getCodeDescription());
112             if(actionCode!=null && actionCode.isInstanceOf(MenuItem.class)){
113                 MenuItem action = (MenuItem)actionCode.createInstance();
114                 int actionStatus = action.getStatus(treeView);
115                 Icon JavaDoc icon = getIcon(itemDesc.getIconDescription(), treeView);
116                 if(actionStatus!=MenuItem.NOT_VISIBLE_STATUS){
117                     return new GenericAction(itemDesc, action, treeView, icon, actionStatus==MenuItem.ENABLED_STATUS);
118                 }
119             } else {
120                 getTrace().warn(itemDesc.getCodeDescription().getCode() + " is not a MenuItem instance!");
121             }
122         }
123         return null;
124     }
125     
126     /**
127      * Provides a Java/Swing menu separator.
128      */

129     protected JSeparator JavaDoc getSeparator(MenuSeparator menuSeparator){
130         return new JSeparator JavaDoc();
131     }
132     
133     /**
134      * Creates the Java/Swing popup menu defined by the given menu description.
135      */

136     protected JPopupMenu JavaDoc createMenu(MenuDescription menuDesc, MenuItemTreeView treeView){
137         JPopupMenu JavaDoc menu = null;
138         if(menuDesc!=null && !menuDesc.isEmpty()){
139             menu = new JPopupMenu JavaDoc();
140             MenuElement[] menuElement = menuDesc.getAllMenuElements();
141             for (int i = 0; i < menuElement.length; i++) {
142                 MenuElement element = menuElement[i];
143                 if(element instanceof ItemDescription){
144                     Action JavaDoc action = getAction((ItemDescription)element, treeView);
145                     if(action != null) {
146                         menu.add(action);
147                     }
148                 } else if(element instanceof MenuSeparator){
149                     menu.add(getSeparator((MenuSeparator)element));
150                 }
151             }
152         }
153         return menu;
154     }
155
156     protected DescriptionInterpreter getIconInterpreter(){
157         try {
158             return (DescriptionInterpreter)lookupFc(ICON_INTERPRETER);
159         } catch (NoSuchInterfaceException e) {
160             getTrace().warn(ICON_INTERPRETER + ": interface not found!");
161             return null;
162         }
163     }
164     
165     // ==================================================================
166
//
167
// Public methods for BindingFeature abstract class.
168
//
169
// ==================================================================
170

171     /* (non-Javadoc)
172      * @see org.objectweb.util.explorer.core.common.lib.BindingFeature#clientFc()
173      */

174     public String JavaDoc[] clientFc() {
175         return new String JavaDoc[]{CodeProvider.CODE_PROVIDER, ICON_INTERPRETER};
176     }
177     
178     // ==================================================================
179
//
180
// Public methods DescriptionInterpreter interface.
181
//
182
// ==================================================================
183

184     /* (non-Javadoc)
185      * @see org.objectweb.util.explorer.interpreter.api.DescriptionInterpreter#interprete(org.objectweb.util.explorer.core.common.api.Description, java.lang.Object)
186      */

187     public Object JavaDoc interprete(Description description, Object JavaDoc params) {
188         MenuDescription menuDesc = (MenuDescription)description;
189         return createMenu(menuDesc, (MenuItemTreeView)params);
190     }
191
192 }
193
194
195    
196
Popular Tags