KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import javax.servlet.ServletContext JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.struts.Globals;
31 import org.apache.struts.action.ActionMapping;
32
33 import org.apache.struts.config.ForwardConfig;
34 import org.apache.struts.config.ModuleConfig;
35 import org.apache.struts.util.RequestUtils;
36 import org.apache.roller.ui.core.RollerContext;
37
38
39 /////////////////////////////////////////////////////////////////////////
40

41 /**
42  * MenuImpls has collection of menu items. Multiple menus can be used
43  * in one session, but they must have unique names.
44  */

45 public class MenuItemImpl extends BaseRollerMenu implements MenuItem {
46     private String JavaDoc mMenuId = null;
47     private static Log log = LogFactory.getFactory().getInstance(MenuItemImpl.class);
48     
49     //private Vector mMenuItems = new Vector();
50

51     /** Is this the default menu? */
52     boolean mDefault = false;
53     
54     //---------------------------------------------------
55

56     public MenuItemImpl() {}
57     
58     /** Construct with name and Struts forward */
59     public MenuItemImpl(String JavaDoc n, String JavaDoc f) {
60         super(n, f);
61     }
62     
63     /** Parent menu's ID */
64     public void setMenuId( String JavaDoc v ) { mMenuId = v; }
65     
66     /** Parent menu's ID */
67     public String JavaDoc getMenuId() { return mMenuId; }
68     
69     /** Given a request, tells if menu item is selected */
70     public boolean isSelected( HttpServletRequest JavaDoc req ) {
71         boolean selected = false;
72         HttpSession JavaDoc ses = req.getSession(false);
73         
74         // first look for menu state in request params, then attributes
75
String JavaDoc itemKey = req.getParameter(RollerMenuModel.MENU_ITEM_KEY );
76         if (null == itemKey) {
77             itemKey = (String JavaDoc)req.getAttribute(RollerMenuModel.MENU_ITEM_KEY);
78         }
79         
80         ActionMapping amapping = (ActionMapping)req.getAttribute(Globals.MAPPING_KEY);
81         if (itemKey != null && itemKey.equals(mName)) {
82             selected = true;
83         } else if (mForward != null && amapping != null) {
84             ServletContext JavaDoc ctx = RollerContext.getServletContext();
85             ModuleConfig mConfig = RequestUtils.getModuleConfig(req, ctx);
86             List JavaDoc fconfigs = new ArrayList JavaDoc();
87             fconfigs.add(mConfig.findForwardConfig(mForward));
88             if (mSubforwards != null) {
89                 String JavaDoc[] subforwards = mSubforwards.split(",");
90                 for (int i=0; i<subforwards.length; i++) {
91                     ForwardConfig fconfig = mConfig.findForwardConfig(subforwards[i]);
92                     if (fconfig != null) {
93                         fconfigs.add(fconfig);
94                     } else {
95                         log.error("ERROR: subforward specified in XML menu file not found: " + subforwards[i]);
96                     }
97                 }
98             }
99             for (Iterator JavaDoc iter = fconfigs.iterator(); iter.hasNext();) {
100                 ForwardConfig fconfig = (ForwardConfig)iter.next();
101                 String JavaDoc fwdPath = fconfig.getPath();
102                 int end = fwdPath.indexOf(".do");
103                 fwdPath = (end == -1) ? fwdPath : fwdPath.substring(0, end);
104                 if (fwdPath.equals(amapping.getPath())) {
105                     selected = true;
106                     break;
107                 }
108             }
109         }
110         
111         // still not found, look for menu state in session attributes
112
if (ses != null && selected) {
113             ses.setAttribute(mMenuId+"_"+RollerMenuModel.MENU_ITEM_KEY, mName);
114         }
115         return selected;
116     }
117     
118     /*
119     public Vector getMenuItems()
120     {
121         return mMenuItems;
122     }
123      
124     public addMenuItem(MenuItem item)
125     {
126         mMenuItems.add(item);
127     }
128      */

129 }
130
131
132
133 //// get menu key from request param or from cookie
134
//String menuKeyName = mMenuId+"rmk";
135
//String menuKey = req.getParameter("rmk");
136
//if (menuKey == null)
137
//{
138
// Cookie menuCookie = RequestUtil.getCookie(req, menuKeyName);
139
// if (menuCookie != null)
140
// {
141
// menuKey = menuCookie.getValue();
142
// req.setAttribute("rmk", menuKey);
143
// }
144
//}
145
//// save menu key in cookie
146
//RequestUtil.setCookie(res, menuKeyName, menuKey, req.getContextPath());
147
//
148
//// get menu item key from request param or from cookie
149
//String itemKeyName = mMenuId+"rmik";
150
//String itemKey = req.getParameter("rmik");
151
//if (itemKey == null)
152
//{
153
// Cookie itemCookie = RequestUtil.getCookie(req, itemKeyName);
154
// if (itemCookie != null)
155
// {
156
// itemKey = itemCookie.getValue();
157
// req.setAttribute("rmik", itemKey);
158
// }
159
//}
160
//// save menu item key in cookie
161
//RequestUtil.setCookie(res, itemKeyName, itemKey, req.getContextPath());
162

163
164
Popular Tags