KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > popup > MenuPropertyContainer


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2002 USTL - LIFL - GOAL
5 Contact: architecture@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.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.popup;
28
29 /** The console's imports */
30 import org.objectweb.util.browser.api.Context;
31 import org.objectweb.util.browser.api.Entry;
32 import org.objectweb.util.browser.core.api.BrowserProperty;
33 import org.objectweb.util.browser.core.api.MenuFactory;
34 import org.objectweb.util.browser.core.api.MenuFactoryConfiguration;
35 import org.objectweb.util.browser.core.api.Role;
36 import org.objectweb.util.browser.core.api.RoleManagement;
37 import org.objectweb.util.browser.core.api.TypeKey;
38 import org.objectweb.util.browser.core.common.*;
39 import org.objectweb.util.browser.core.common.DefaultPropertyContainer;
40 import org.objectweb.util.browser.core.common.ClassesInheritance;
41 import org.objectweb.util.browser.core.naming.DefaultEntry;
42 import org.objectweb.util.browser.core.naming.DefaultName;
43
44 /** The Java API's imports */
45 import java.util.HashMap JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.Vector JavaDoc;
48 import java.util.List JavaDoc;
49 import java.util.Iterator JavaDoc;
50
51 /**
52  *
53  * It contains a cache memory which allow to have only one factory per class type
54  *
55  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
56  * @version 0.1
57  */

58 public class MenuPropertyContainer
59     extends DefaultPropertyContainer {
60
61     protected Map JavaDoc menuCache_;
62
63     // ==================================================================
64
//
65
// Constructors.
66
//
67
// ==================================================================
68

69     /**
70      * Default contstructor
71      * @param loader The object to use to load new configuration
72      */

73     public MenuPropertyContainer(BrowserProperty loader) {
74         super(loader);
75         menuCache_ = new HashMap JavaDoc();
76     }
77
78     // ==================================================================
79
//
80
// Internal methods.
81
//
82
// ==================================================================
83

84     /**
85      * Tries to load an element for the associated class name.
86      * @param className The name of the class
87      * @return The founded entry or null
88      */

89     protected Entry getElement(String JavaDoc className, Role role, ExtendedBoolean nodeFound) {
90         return loader_.getMenu(className, role, nodeFound);
91     }
92
93     /**
94      * Adds the menuFactory to the container.
95      *
96      * @param id The name of the entry to be retrieved.
97      * @param object The menuFactory to add
98      *
99      */

100     protected void addEntry(String JavaDoc id, MenuFactory object) {
101         super.addEntry(id, object);
102     }
103
104     // ==================================================================
105
//
106
// Surcharging public methods for DefaultContextContainer class.
107
//
108
// ==================================================================
109

110     /**
111      * Tries to load an element for the associated class name.
112      * It calls the loader and adds the given entry into the context.
113      * The result is an entry containing an array of entry (one for each role for which a menu is defined).
114      * @param className The name of the class
115      */

116     protected Entry tryToLoad(String JavaDoc className) {
117         Vector JavaDoc entryList = new Vector JavaDoc();
118         Role[] roles = loader_.getInheritedRoles();
119         ExtendedBoolean nodeFound = new ExtendedBoolean(false);
120         boolean atLeastOneNodeFound = false;
121         for(int i = 0 ; i < roles.length ; i++){
122             Entry entry = getElement(className, roles[i], nodeFound);
123             if (entry != null) {
124                 if(!(roles[i].getId().equals(RoleManagement.DEFAULT_ROLE) && atLeastOneNodeFound))
125                     entryList.add(entry);
126                 if(nodeFound.getValue())
127                     atLeastOneNodeFound = true;
128             }
129         }
130         if(!entryList.isEmpty()){
131             return new DefaultEntry((Entry[])entryList.toArray(new Entry[0]),new DefaultName(className));
132         }
133         return null;
134     }
135
136     /**
137      * Finds the object associated value.
138      * If the key doesn't exist, it looks for in interfaces implemented
139      * by this class and in superclass of the entity
140      * In order to have a string representation of the object,
141      * it takes his name (object.getClass().getName())
142      * @param object The research key
143      * @return The associated value - null is returned if no value exists
144      */

145     public Entry getRecursiveProperty(Object JavaDoc object) {
146         Class JavaDoc c = object.getClass();
147         TypeKey typeKey = loader_.getTypeSystem().getTypeKey(c);
148         String JavaDoc cKey = typeKey.getTypeSystem() + "." + typeKey.getTypeName();
149         // Looks for association into the cache
150
if (cache_.containsKey(cKey)) {
151             return (Entry)menuCache_.get((String JavaDoc) cache_.get(cKey));
152         }
153         // Gets the list of inherit classes
154
ClassesInheritance ci = new ClassesInheritance(c);
155         List JavaDoc list = ci.getInheritClasses();
156         Iterator JavaDoc it = list.iterator();
157         // To put the list of factory
158
Vector JavaDoc inheritTypeMenu = new ContextVector();
159         Vector JavaDoc inheritTreeMenu = new ContextVector();
160         boolean isFirst = true;
161         MenuFactoryConfiguration menuFactory = null;
162         Entry currentMenuEntry = null;
163         Entry entry = null;
164         while (it.hasNext()) {
165             Class JavaDoc class_ = (Class JavaDoc) it.next();
166             typeKey = loader_.getTypeSystem().getTypeKey(class_);
167             String JavaDoc key = typeKey.getTypeSystem() + "." + typeKey.getTypeName();
168             entry = getLocalEntry(key);
169             if (entry == null) {
170                 entry = tryToLoad(key);
171             }
172             if (entry != null) {
173                 Entry[] entryList = (Entry[]) entry.getValue();
174                 for(int i=0; i < entryList.length ; i++){
175                     if(isFirst) {
176                         // First cell of the classes inheritances array and the roles array.
177
isFirst = false;
178                         cache_.put(cKey, key);
179                         menuFactory = (MenuFactoryConfiguration) entryList[i].getValue();
180                         menuFactory.setClassName(class_.getName());
181                         currentMenuEntry = entryList[i];
182                         menuCache_.put(key,currentMenuEntry);
183                     } else {
184                         MenuFactoryConfiguration mfc = (MenuFactoryConfiguration) entryList[i].getValue();
185                         inheritTypeMenu.add(mfc.getItems());
186                         inheritTreeMenu.add(mfc.getChildItems());
187                     }
188                 }
189             }
190         }
191         if (menuFactory != null) {
192             menuFactory.setInheritTypeMenu((Context[]) inheritTypeMenu.toArray(new Context[0]));
193             menuFactory.setInheritTreeMenu((Context[]) inheritTreeMenu.toArray(new Context[0]));
194         }
195         return currentMenuEntry;
196     }
197
198     /**
199      * Adds the object to the container.
200      *
201      * This method does not modify the given name and the target context.
202      *
203      * @param id The name of the entry to be retrieved.
204      * @param object The object to add
205      *
206      */

207     /*public void addEntry(String id, Object object) {
208         if (object != null) {
209             //addEntry(id, (MenuFactory) object);
210             addEntry(id, (Entry[]) object);
211         }
212     }*/

213
214     /**
215      * Defines a subclass of Vector which adds a context only if the given one is not empty.
216      */

217     protected class ContextVector extends Vector JavaDoc {
218
219         /**
220          * Indicates if a context is empty
221          * @return true if the given context is empty
222          */

223         protected boolean isEmpty(Context context) {
224             if (context != null) {
225                 Entry[] entries = context.getEntries();
226                 return entries.length == 0;
227             }
228             return true;
229         }
230
231         /**
232          * Appends the given context to the end of this Vector only if it's not empty.
233          */

234         public boolean add(Context c) {
235             if (!isEmpty(c))
236                 return super.add(c);
237             return false;
238         }
239
240         /**
241          * Appends the specified element to the end of this Vector.
242          */

243         public boolean add(Object JavaDoc o) {
244             if (o != null)
245                 return add((Context) o);
246             return false;
247         }
248
249     }
250
251 }
252
Popular Tags