KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > navigation > MenuTree


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.navigation;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29
30 import org.apache.struts.action.ActionForward;
31
32 import com.sslexplorer.core.AvailableMenuItem;
33 import com.sslexplorer.core.CoreUtil;
34 import com.sslexplorer.core.MenuItem;
35 import com.sslexplorer.security.LogonControllerFactory;
36 import com.sslexplorer.security.SessionInfo;
37
38 public class MenuTree {
39
40     /**
41      *
42      */

43     protected List JavaDoc menus;
44     protected HashMap JavaDoc menuMap;
45     protected String JavaDoc name;
46     
47     public MenuTree(String JavaDoc name) {
48         this.name = name;
49         menus = new ArrayList JavaDoc();
50         menuMap = new HashMap JavaDoc();
51     }
52     
53     /**
54      * Get the name of this menu tree
55      *
56      * @return name
57      */

58     public String JavaDoc getName() {
59         return name;
60     }
61
62     /**
63      * Add a menu item to the menu tree.
64      *
65      * @param parentId parent to add menu item to (or <code>null</code> for top level items)
66      * @param menuItem menuItem object
67      */

68     public void addMenuItem(String JavaDoc parentId, MenuItem menuItem) {
69         MenuItem parent = parentId == null ? null : (MenuItem) menuMap.get(parentId);
70         if (parent != null) {
71             menuItem.setParent(parent);
72             parent.addChild(menuItem);
73         } else {
74             menus.add(menuItem);
75         }
76         menuMap.put(menuItem.getId(), menuItem);
77     }
78
79
80     /**
81      * Remove a menu item from the menu tree
82      *
83      * @param parentId parent to remove menu item from (or <code>null</code> for top level items)
84      * @param itemId menu item ID
85      */

86     public void removeMenuItem(String JavaDoc parentId, String JavaDoc itemId) {
87         MenuItem parent = parentId == null ? null : (MenuItem) menuMap.get(parentId);
88         if (parent != null) {
89             parent.removeChild(parent.getChild(itemId));
90         }
91         menuMap.remove(itemId);
92     }
93
94
95     /**
96      * @param name
97      * @return
98      */

99     public MenuItem getMenuItem(String JavaDoc id) {
100         return (MenuItem) menuMap.get(id);
101     }
102
103
104     /**
105      * Rebuild the navigation menu structure, checking each menu item to see if
106      * is available for the current state.
107      *
108      * @param menuItem root menu item
109      * @param checkNavigationContext navigation context
110      * @param info info
111      * @param request request
112      * @param referer referer
113      * @return available root menus
114      */

115     public List JavaDoc rebuildMenus(MenuItem menuItem, int checkNavigationContext, SessionInfo info, HttpServletRequest JavaDoc request, String JavaDoc referer) {
116         List JavaDoc availableMenus = new ArrayList JavaDoc();
117         for (Iterator JavaDoc i = menuItem == null ? menus.iterator() : menuItem.availableChildren(checkNavigationContext, info, request).iterator(); i.hasNext();) {
118             MenuItem it = (MenuItem) i.next();
119             if (it.isAvailable(checkNavigationContext, info, request)) {
120                 AvailableMenuItem nm = createAvailableMenuItem(it, null, request, referer, checkNavigationContext, info);
121                 if (it.isLeaf() || (!it.isLeaf() && !nm.isEmpty())) {
122                     availableMenus.add(nm);
123                 }
124             }
125         }
126         Collections.sort(availableMenus);
127         return availableMenus;
128     }
129     
130     /**
131      * Called when rebuilding the menus, create an {@link AvailableMenuItem} for
132      * the provided {@link MenuItem}.
133      *
134      * @param item
135      * @param parent
136      * @param request
137      * @param referer
138      * @param checkNavigationContext
139      * @param info
140      * @return available menu item
141      */

142     public AvailableMenuItem createAvailableMenuItem(MenuItem item, AvailableMenuItem parent, HttpServletRequest JavaDoc request, String JavaDoc referer, int checkNavigationContext, SessionInfo info) {
143         return new AvailableMenuItem(item, parent, request, referer, checkNavigationContext, info);
144     }
145
146     /**
147      * Rebuild the navigation menu structure, checking each menu item to see if
148      * is available for the current state.
149      *
150      * @param checkNavigationContext navigation context
151      * @param info info
152      * @param request request
153      * @param referer referer
154      * @return available menus
155      */

156     public List JavaDoc rebuildMenus(int checkNavigationContext, SessionInfo info, HttpServletRequest JavaDoc request, String JavaDoc referer) {
157         return rebuildMenus(null, checkNavigationContext, info, request, referer);
158     }
159
160     /**
161      * Rebuild the navigation menu structure, checking each menu item to see if
162      * is available for the current state.
163      *
164      * @param request request
165      * @return available root menus
166      */

167     public List JavaDoc rebuildMenus(HttpServletRequest JavaDoc request) {
168         return rebuildMenus(null, request);
169         
170     }
171
172     /**
173      * Rebuild the navigation menu structure, checking each menu item to see if
174      * is available for the current state.
175      *
176      * @param menuItem root menu item
177      * @param request request
178      * @return available root menus
179      */

180     public List JavaDoc rebuildMenus(MenuItem menuItem, HttpServletRequest JavaDoc request) {
181         SessionInfo info = LogonControllerFactory.getInstance().getSessionInfo(request);
182             return rebuildMenus(menuItem, info == null ?0 :
183                 info.getNavigationContext(), info, request,
184                                 CoreUtil.getReferer(request));
185     }
186
187     public ActionForward getFirstAvailableActionForward(List JavaDoc availableMenuItems) {
188         for(Iterator JavaDoc i = availableMenuItems.iterator(); i.hasNext(); ) {
189             AvailableMenuItem it = getFirstAvailableMenuLeaf((AvailableMenuItem)i.next());
190             if(it != null) {
191                 return new ActionForward(it.getPath(), true);
192             }
193         }
194         return null;
195     }
196
197     AvailableMenuItem getFirstAvailableMenuLeaf(AvailableMenuItem item) {
198         if(item.getMenuItem().isLeaf()) {
199             return item;
200         }
201         else for(Iterator JavaDoc i = item.iterator(); i.hasNext(); ) {
202             AvailableMenuItem it = getFirstAvailableMenuLeaf((AvailableMenuItem)i.next());
203             if(it != null) {
204                 return it;
205             }
206         }
207         return null;
208     }
209 }
Popular Tags