KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > AvailableMenuItem


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.core;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import com.sslexplorer.boot.Util;
29 import com.sslexplorer.security.SessionInfo;
30
31
32 /**
33  * Wraps {@link com.sslexplorer.core.MenuItem} providing a tree of
34  * menu items (which contains all possible menu items)
35  * that are available for the current user, configuration and navigation
36  * context.
37  * <p>
38  * Each users session contains a tree of these objects that are used
39  * to build up the various navigation components such as side menu bar,
40  * top navigation page, page tasks etc.
41  *
42  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
43  */

44 public class AvailableMenuItem extends ArrayList JavaDoc implements Comparable JavaDoc {
45     
46     private static final long serialVersionUID = -5447780375295861982L;
47     
48     // Private instance variables
49

50     private MenuItem menuItem;
51     private AvailableMenuItem parent;
52     private String JavaDoc path;
53     private HttpServletRequest JavaDoc request;
54     private SessionInfo sessionInfo;
55     
56     /**
57      * Constructor.
58      *
59      * @param menuItem menu item this object wraps
60      * @param parent parent or <code>null</code> if root
61      * @param request request
62      * @param referer referer
63      * @param checkNavigationContext current navigation context to check against
64      * @param sessionInfo sesion
65      */

66     public AvailableMenuItem(MenuItem menuItem, AvailableMenuItem parent, HttpServletRequest JavaDoc request, String JavaDoc referer, int checkNavigationContext, SessionInfo sessionInfo) {
67         this.request = request;
68         this.sessionInfo = sessionInfo;
69         for(Iterator JavaDoc i = menuItem.availableChildren(checkNavigationContext, sessionInfo, request).iterator(); i.hasNext(); ) {
70             MenuItem it = (MenuItem)i.next();
71             if(it.isLeaf() || ( !it.isLeaf() && !it.isEmpty())) {
72                 add(new AvailableMenuItem(it, this, request, referer, checkNavigationContext, sessionInfo));
73             }
74         }
75         Collections.sort(this);
76         this.menuItem = menuItem;
77         this.parent = parent;
78
79         path = menuItem.getPath();
80         if(path != null) {
81             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
82             buf.append(path);
83             if(referer != null && !path.startsWith("javascript:")) {
84                 // The referer may already be in the path, strip it out
85
while(true) {
86                     int idx = path.indexOf("referer=");
87                     if(idx != -1) {
88                         int end = path.indexOf('&', idx);
89                         path = path.substring(0, idx) + ( end == -1 ? "" : path.substring(end) );
90                     }
91                     else {
92                         break;
93                     }
94                 }
95                 if(path.indexOf("?") != -1) {
96                     buf.append("&");
97                 }
98                 else {
99                     buf.append("?");
100                 }
101                 buf.append("referer=");
102                 buf.append(Util.urlEncode(referer));
103             }
104             path = buf.toString();
105         }
106     }
107     
108     /**
109      * Get if this menu it empty.
110      *
111      * @return empty
112      */

113     public boolean getEmpty() {
114         return size() == 0;
115     }
116     
117     /**
118      * Get the parent available menu item
119      *
120      * @return parent available menu item
121      */

122     public AvailableMenuItem getParent() {
123         return parent;
124     }
125     
126     /**
127      * Get the {@link MenuItem} this object wraps.
128      *
129      * @return wrapped menu item
130      */

131     public MenuItem getMenuItem() {
132         return menuItem;
133     }
134     
135     /**
136      * Get the first available child that is available from the node
137      * in the tree.
138      *
139      * @return first available menu item
140      */

141     public AvailableMenuItem getFirstAvailableChild() {
142         if(!menuItem.isLeaf() && !menuItem.isEmpty()) {
143             return null;
144         }
145         return (AvailableMenuItem)get(0);
146     }
147     
148     /**
149      * Get the path activating the menu item should direct the browser to.
150      *
151      * @return path to direct browser to
152      */

153     public String JavaDoc getPath() {
154         return path;
155     }
156     
157     /* (non-Javadoc)
158      * @see java.lang.Object#toString()
159      */

160     public String JavaDoc toString() {
161         return menuItem == null ? "<no menu item>" : ( menuItem.getId() + " [" + menuItem.getPath() + "] empty = " + menuItem.isEmpty());
162     }
163
164     /* (non-Javadoc)
165      * @see java.lang.Comparable#compareTo(java.lang.Object)
166      */

167     public int compareTo(Object JavaDoc arg0) {
168         return getMenuItem().compareTo(((AvailableMenuItem)arg0).getMenuItem());
169     }
170     
171     /**
172      * Get the request this available menu item was created with
173      *
174      * @return request
175      */

176     public HttpServletRequest JavaDoc getRequest() {
177         return request;
178     }
179     
180     /**
181      * Get the session this available menu item was created with
182      *
183      * @return session
184      */

185     public SessionInfo getSessionInfo() {
186         return sessionInfo;
187     }
188 }
Popular Tags