KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tags > menu > MenuImpl


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.core.tags.menu;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25 import javax.servlet.ServletContext JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpSession JavaDoc;
29 import javax.servlet.jsp.PageContext JavaDoc;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.config.ForwardConfig;
33 import org.apache.struts.config.ModuleConfig;
34 import org.apache.struts.util.RequestUtils;
35
36 import org.apache.roller.RollerException;
37 import org.apache.roller.ui.core.RollerContext;
38
39 /////////////////////////////////////////////////////////////////////////
40

41 /** MenuImpl model has collection of menus */
42 public class MenuImpl extends BaseRollerMenu implements Menu {
43     private String JavaDoc mMenuId = null;
44     
45     /** Vector of MenuItemImpl objects */
46     Vector JavaDoc mMenuItems = new Vector JavaDoc();
47     
48     /** Is this the default menu? */
49     boolean mDefault = false;
50     
51     public MenuImpl() {}
52     
53     /** Construct with name */
54     public MenuImpl(String JavaDoc n) { super(n, null); }
55     
56     /** Add MenuItemImpl to MenuImpl */
57     public void addItem( MenuItemImpl item ) { mMenuItems.addElement(item); };
58     
59     /** Parent menu's ID */
60     public void setMenuId( String JavaDoc v ) { mMenuId = v; }
61     
62     /** Parent menu's ID */
63     public String JavaDoc getMenuId() { return mMenuId; }
64     
65     /** Collection of MenuItemImpl objects */
66     public Vector JavaDoc getMenuItems() { return mMenuItems; }
67     
68     /** Get currently selected menu item in this menu
69      * @throws RollerException*/

70     public MenuItem getSelectedMenuItem( HttpServletRequest JavaDoc req ) throws RollerException {
71         return getSelectedMenuItem( req, true ) ;
72     }
73     
74     /**
75      * Get currently selected menu item in this menu
76      * @throws RollerException
77      */

78     public MenuItem getSelectedMenuItem( HttpServletRequest JavaDoc req,
79             boolean returnDefault ) throws RollerException {
80         MenuItemImpl def = null;
81         MenuItemImpl selected = null;
82         for ( int i=0; i<mMenuItems.size(); i++ ) {
83             MenuItemImpl item = (MenuItemImpl)mMenuItems.elementAt(i);
84             if ( item.isSelected( req ) ) {
85                 selected = item;
86                 break;
87             }
88             // Set first permitted and enabled menu item in each menu as default
89
if ( item.isPermitted(req) && def == null) {
90                 def = item;
91             }
92         }
93         if ( selected != null ) {
94             return selected;
95         } else if ( returnDefault ) {
96             return def;
97         } else {
98             return null;
99         }
100     }
101     
102     /**
103      * Get default menu item (first one that is permitted)
104      * @throws RollerException
105      */

106     public MenuItem getDefaultMenuItem( HttpServletRequest JavaDoc req )
107     throws RollerException {
108         MenuItemImpl def = null;
109         MenuItemImpl selected = null;
110         for ( int i=0; i<mMenuItems.size(); i++ ) {
111             // Set first permitted and enabled menu item in each menu as default
112
MenuItemImpl item = (MenuItemImpl)mMenuItems.elementAt(i);
113             if (item.isPermitted(req) && def == null) {
114                 def = item;
115             }
116         }
117         return def;
118     }
119     
120     /**
121      * Is this menu selected?
122      * @throws RollerException
123      */

124     public boolean isSelected( HttpServletRequest JavaDoc req ) throws RollerException {
125         boolean selected = false;
126         HttpSession JavaDoc ses = req.getSession(false);
127         
128         // try to get state from request param then attribute
129
String JavaDoc menuKey = req.getParameter(RollerMenuModel.MENU_KEY );
130         if (null == menuKey) {
131             menuKey = (String JavaDoc)req.getAttribute(RollerMenuModel.MENU_KEY);
132         }
133         if (menuKey != null && menuKey.equals(mName)) {
134             selected = true;
135         }
136         // next, if submenu is selected, then we're selected
137
else if (getSelectedMenuItem(req, false) != null) {
138             selected = true;
139         }
140         // next, try to use Struts forward to determine state
141
else if (mForward != null) {
142             ServletContext JavaDoc ctx = RollerContext.getServletContext();
143             ModuleConfig mConfig = RequestUtils.getModuleConfig(req, ctx);
144             ActionMapping amapping = (ActionMapping)req.getAttribute(Globals.MAPPING_KEY);
145             List JavaDoc fconfigs = new ArrayList JavaDoc();
146             fconfigs.add(mConfig.findForwardConfig(mForward));
147             if (mSubforwards != null) {
148                 String JavaDoc[] subforwards = mSubforwards.split(",");
149                 for (int i=0; i<subforwards.length; i++) {
150                     fconfigs.add(mConfig.findForwardConfig(subforwards[i]));
151                 }
152             }
153             for (Iterator JavaDoc iter = fconfigs.iterator(); iter.hasNext();) {
154                 ForwardConfig fconfig = (ForwardConfig)iter.next();
155                 String JavaDoc fwdPath = fconfig.getPath();
156                 int end = fwdPath.indexOf(".do");
157                 fwdPath = (end == -1) ? fwdPath : fwdPath.substring(0, end);
158                 if (fwdPath.equals(amapping.getPath())) {
159                     selected = true;
160                     break;
161                 }
162             }
163         }
164         return selected;
165     }
166     
167     /** Name of Struts forward menu item should link to */
168     public String JavaDoc getUrl(PageContext JavaDoc pctx) {
169         String JavaDoc url = null;
170         try {
171             // If no forward specified, use default submenu URL
172
if (mForward == null && mMenuItems != null && mMenuItems.size() > 0) {
173                 HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc)pctx.getRequest();
174                 String JavaDoc surl = getDefaultMenuItem( req ).getUrl( pctx );
175                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc( surl );
176                 if ( surl.indexOf("?") == -1 ) {
177                     sb.append( "?" );
178                 } else {
179                     sb.append( "&amp;" );
180                 }
181                 sb.append( RollerMenuModel.MENU_KEY );
182                 sb.append( "=" );
183                 sb.append( getName() );
184                 url = sb.toString();
185             } else {
186                 return super.getUrl(pctx);
187             }
188         } catch (Exception JavaDoc e) {
189             pctx.getServletContext().log(
190                     "ERROR in menu creating URL",e);
191         }
192         return url;
193     }
194     
195 }
196
197
Popular Tags