KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > resolver > lib > MenuResolver


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.resolver.lib;
27
28 import org.objectweb.util.explorer.api.Entry;
29 import org.objectweb.util.explorer.core.common.api.BadParamException;
30 import org.objectweb.util.explorer.core.common.api.Description;
31 import org.objectweb.util.explorer.core.common.api.ExplorerConstants;
32 import org.objectweb.util.explorer.core.common.lib.ClassesInheritance;
33 import org.objectweb.util.explorer.core.menu.api.MenuDescription;
34 import org.objectweb.util.explorer.core.menu.lib.BasicMenuSeparator;
35
36 /**
37  *
38  *
39  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>,
40  * <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>.
41  *
42  * @version 0.1
43  */

44 public class MenuResolver
45      extends AbstractPropertyResolver
46 {
47
48     //==================================================================
49
//
50
// Internal States.
51
//
52
// ==================================================================
53

54     // ==================================================================
55
//
56
// Constructors.
57
//
58
// ==================================================================
59

60     // ==================================================================
61
//
62
// Internal method.
63
//
64
// ==================================================================
65

66     /* (non-Javadoc)
67      * @see org.objectweb.util.explorer.resolver.lib.AbstractPropertyResolver#getPropertyName()
68      */

69     protected String JavaDoc getPropertyType() {
70         return ExplorerConstants.MENU_PROPERTY;
71     }
72         
73     /**
74      * Merges the two menu definition
75      * This method adds the content of the target menu description into
76      * the content of the source menu description.
77      * The target description is not modified whereas the source description is modified.
78      */

79     protected MenuDescription mergeMenuDescription(MenuDescription source, MenuDescription target) {
80         if(target!=null){
81             source.addMenuElements(target.getAllMenuElements());
82         }
83         return source;
84     }
85     
86     /**
87      * Tries to find a menu description for the given key.
88      * @param key The storage key
89      * @return The associated description (or null if no description is found).
90      */

91     protected MenuDescription getMenuDesc(Object JavaDoc key){
92         return (MenuDescription)getPropertyProvider().getPropertyDescription(getPropertyType(), key);
93     }
94     
95     /**
96      * Provides a menu for the given entry
97      * @param entry The entry for which a menu needs to be computed.
98      * @return The associated menu description.
99      */

100     protected MenuDescription getMenuDescription(Entry entry, boolean childrenMenuElement){
101         ExtendedMenuDescription menuDesc = new ExtendedMenuDescription();
102         if(entry!=null && entry.getValue()!=null){
103             Class JavaDoc[] javaTypes = ClassesInheritance.getInheritedClasses(entry.getValue().getClass());
104             String JavaDoc[] roleIds = getRoleProvider().getInheritedRoleIds(getRoleManager().getCurrentRoleIds());
105             boolean isFirst = true;
106             for (int i = 0; i < javaTypes.length; i++) {
107                 for (int j = 0; j < roleIds.length; j++) {
108                     Object JavaDoc key = null;
109                     try {
110                         key = getKeyProvider().computeKey(new String JavaDoc[]{getTypeSystemId(),javaTypes[i].getName(),roleIds[j]});
111                     } catch (BadParamException e) {
112                         getTrace().warn("Bad key param: " + e.getMessage());
113                     }
114                     if(key!=null){
115                         MenuDescription newMenuDesc = getMenuDesc(key);
116                         if(newMenuDesc!=null && !newMenuDesc.isEmpty()){
117                             if(childrenMenuElement) {
118                                 menuDesc.addChildrenMenuElements(newMenuDesc);
119                             } else {
120                                 if(isFirst){
121                                     isFirst = false;
122                                 } else {
123                                     menuDesc.addMenuElement(new BasicMenuSeparator());
124                                 }
125                                 menuDesc.addMenuElements(newMenuDesc,i==0);
126                             }
127                         }
128                     }
129                 }
130             }
131         }
132         return menuDesc;
133     }
134     
135     /**
136      * Computes the menu for the given entries.
137      * @param currentEntry The current entry.
138      * @param parentEntry The parent of the current entry (may be null);
139      * @return The description of the computed menu.
140      */

141     protected MenuDescription getMenuDescription(Entry currentEntry, Entry parentEntry){
142         MenuDescription currentMenu = getMenuDescription(currentEntry,false);
143         if(parentEntry!=null){
144             return mergeMenuDescription(currentMenu, getMenuDescription(parentEntry,true));
145         } else {
146             return currentMenu;
147         }
148     }
149
150     // ==================================================================
151
//
152
// Public methods for BindingFeature abstract class.
153
//
154
// ==================================================================
155

156     /* (non-Javadoc)
157      * @see org.objectweb.util.explorer.resolver.api.PropertyResolver#resolve(java.lang.String, org.objectweb.util.explorer.api.Entry, org.objectweb.util.explorer.api.Entry)
158      */

159     public Description resolve(String JavaDoc propertyType, Entry currentEntry, Entry parentEntry) {
160         return getMenuDescription(currentEntry, parentEntry);
161     }
162
163 }
164
Popular Tags