KickJava   Java API By Example, From Geeks To Geeks.

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


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): Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.popup;
28
29 /** The administration 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.api.MenuItemTreeView;
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.common.DynamicTree;
36
37 /**
38  * Basic implementation for Menu factories.
39  *
40  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
41  * @version 0.1
42  *
43  */

44 public class DefaultMenuFactory
45     implements MenuFactory, MenuFactoryConfiguration {
46
47     /** The name of the class (or interface) from which this menu has to be instanciate */
48     protected String JavaDoc className_;
49
50     /** Specifies if this menu has to inherit menus from parents at the tree level. */
51     protected boolean inheritTreeMenu_;
52
53     /** Specifies if this menu has to inherit menus from parents at the java type inheritance level. */
54     protected boolean inheritTypeMenu_;
55
56     /** The menu to create. */
57     protected Context items_;
58
59     /** The context containing the tree child configuration */
60     protected Context childItems_;
61
62     /** List of context corresponding to the java type inheritance per level */
63     protected Context[] inheritTypeMenuFactory_;
64
65     /** The list */
66     protected Context[] inheritTreeMenuFactory_;
67
68     /**
69      * Add the correxponding item containing in the given Context
70      */

71     protected void addItems(AdvancedJPopupMenu menu,Context itemList,DynamicTree tree,boolean hisMenu) {
72         if (itemList != null) {
73             Entry[] entries = itemList.getEntries();
74             for (int i = 0; i < entries.length; i++) {
75                 Entry entry = entries[i];
76                 ItemProperty ip = (ItemProperty) entry.getValue();
77                 if (hisMenu || (!hisMenu && ip.isTypeChildVisible()) || (!hisMenu && ip.isTreeChildVisible()))
78                     menu.addMenuItem(entry.getName().toString(), ip, tree);
79             }
80         }
81     }
82
83     /**
84      * Indicates if a context is empty
85      * @return true if the given context is empty
86      */

87     protected boolean isEmpty(Context context) {
88         if (context != null) {
89             Entry[] entries = context.getEntries();
90             return entries.length == 0;
91         }
92         return true;
93     }
94
95     /**
96      * Return a JPopupMenu associated to the given object.
97      *
98      * @param object The object you want to create the associated menu
99      * @param tree The tree on which the popup menu will be displayed
100      * @return The associated menu.
101      */

102     public AdvancedJPopupMenu newMenu(Object JavaDoc object, DynamicTree tree, MenuItemTreeView treeView) {
103
104         AdvancedJPopupMenu popupMenu =
105             new AdvancedJPopupMenu(tree, treeView, inheritTreeMenu_);
106         boolean isFirst = true; //To fix correctly the menu separator
107

108         // Adds its items
109
// object.getClass().getName().equals(className_) has boolean to consider the type-child-visible attribute
110
addItems(popupMenu,items_,tree,object.getClass().getName().equals(className_));
111
112         // Adds java type inherit items
113
if (inheritTypeMenu_ && inheritTypeMenuFactory_ != null) {
114             for (int i = 0; i < inheritTypeMenuFactory_.length; i++) {
115                 if ((isFirst && !isEmpty(items_)) || !isFirst) {
116                     popupMenu.addSeparator();
117                     isFirst = false;
118                 }
119                 addItems(popupMenu, inheritTypeMenuFactory_[i], tree, false);
120             }
121         }
122
123         return popupMenu;
124     }
125
126     /**
127      * Return a JPopupMenu associated to the param's object.
128      *
129      * @param tree The tree on which the popup menu will be displayed
130      * @return The parent menu.
131      */

132     public AdvancedJPopupMenu newParentMenu(DynamicTree tree) {
133
134         AdvancedJPopupMenu popupMenu = new AdvancedJPopupMenu(tree);
135         boolean isFirst = true; //To fix correctly the menu separator
136

137         // Adds its child items
138
addItems(popupMenu, childItems_, tree, false);
139
140         // Adds java type inherit child items
141
if (inheritTreeMenuFactory_ != null) {
142             for (int i = 0; i < inheritTreeMenuFactory_.length; i++) {
143                 if ((isFirst && !isEmpty(childItems_)) || !isFirst) {
144                     popupMenu.addSeparator();
145                     isFirst = false;
146                 }
147                 addItems(popupMenu, inheritTreeMenuFactory_[i], tree, false);
148             }
149         }
150
151         return popupMenu;
152     }
153
154     /**
155      * Fixes the inheritTreeMenu value.
156      * @param inheritTreeMenu true if this menu has to inherit menus from parents at the tree level
157      */

158     public void setInheritTreeMenu(boolean inheritTreeMenu) {
159         inheritTreeMenu_ = inheritTreeMenu;
160     }
161
162     /**
163      * Fixes the inheritTypeMenu value.
164      * @param inheritTypeMenu true if this menu has to inherit menus from parents at the java type inheritance level
165      */

166     public void setInheritTypeMenu(boolean inheritTypeMenu) {
167         inheritTypeMenu_ = inheritTypeMenu;
168     }
169
170     /**
171      * Fixes the list of MenuFactory from which this menu have to inherit.
172      * @param inheritMenu The list of MenuFactory
173      */

174     public void setInheritTypeMenu(Context[] inheritMenu) {
175         inheritTypeMenuFactory_ = inheritMenu;
176     }
177
178     /**
179      * Fixes the list of items for tree child.
180      * @param inheritMenu The list of context
181      */

182     public void setInheritTreeMenu(Context[] inheritMenu) {
183         inheritTreeMenuFactory_ = inheritMenu;
184     }
185
186     /**
187      * Fixes the context containing the popup menu configuration
188      *
189      * @param items The context containing the popup menu configuration
190      */

191     public void setItems(Context items) {
192         items_ = items;
193     }
194
195     /**
196      * Gives the context containing the items for this object
197      * @return The context containing this menu configuration
198      */

199     public Context getItems() {
200         return items_;
201     }
202
203     /**
204      * Fixes the context containing the items for tree child
205      * @param childItems The context containing the popup menu configuration for tree child
206      */

207     public void setChildItems(Context childItems) {
208         childItems_ = childItems;
209     }
210
211     /**
212      * Gives the context containing the items for tree child
213      * @return The context containing the tree child configuration
214      */

215     public Context getChildItems() {
216         return childItems_;
217     }
218
219     /**
220      * Fixes the name of the class from which this menu has to be instanciate.
221      * @param className The name of the class
222      */

223     public void setClassName(String JavaDoc className) {
224         className_ = className;
225     }
226
227 }
228
Popular Tags