001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.ui.core.tags.menu;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import javax.servlet.ServletContext;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.struts.Globals;
031: import org.apache.struts.action.ActionMapping;
032:
033: import org.apache.struts.config.ForwardConfig;
034: import org.apache.struts.config.ModuleConfig;
035: import org.apache.struts.util.RequestUtils;
036: import org.apache.roller.ui.core.RollerContext;
037:
038: /////////////////////////////////////////////////////////////////////////
039:
040: /**
041: * MenuImpls has collection of menu items. Multiple menus can be used
042: * in one session, but they must have unique names.
043: */
044: public class MenuItemImpl extends BaseRollerMenu implements MenuItem {
045: private String mMenuId = null;
046: private static Log log = LogFactory.getFactory().getInstance(
047: MenuItemImpl.class);
048:
049: //private Vector mMenuItems = new Vector();
050:
051: /** Is this the default menu? */
052: boolean mDefault = false;
053:
054: //---------------------------------------------------
055:
056: public MenuItemImpl() {
057: }
058:
059: /** Construct with name and Struts forward */
060: public MenuItemImpl(String n, String f) {
061: super (n, f);
062: }
063:
064: /** Parent menu's ID */
065: public void setMenuId(String v) {
066: mMenuId = v;
067: }
068:
069: /** Parent menu's ID */
070: public String getMenuId() {
071: return mMenuId;
072: }
073:
074: /** Given a request, tells if menu item is selected */
075: public boolean isSelected(HttpServletRequest req) {
076: boolean selected = false;
077: HttpSession ses = req.getSession(false);
078:
079: // first look for menu state in request params, then attributes
080: String itemKey = req
081: .getParameter(RollerMenuModel.MENU_ITEM_KEY);
082: if (null == itemKey) {
083: itemKey = (String) req
084: .getAttribute(RollerMenuModel.MENU_ITEM_KEY);
085: }
086:
087: ActionMapping amapping = (ActionMapping) req
088: .getAttribute(Globals.MAPPING_KEY);
089: if (itemKey != null && itemKey.equals(mName)) {
090: selected = true;
091: } else if (mForward != null && amapping != null) {
092: ServletContext ctx = RollerContext.getServletContext();
093: ModuleConfig mConfig = RequestUtils.getModuleConfig(req,
094: ctx);
095: List fconfigs = new ArrayList();
096: fconfigs.add(mConfig.findForwardConfig(mForward));
097: if (mSubforwards != null) {
098: String[] subforwards = mSubforwards.split(",");
099: for (int i = 0; i < subforwards.length; i++) {
100: ForwardConfig fconfig = mConfig
101: .findForwardConfig(subforwards[i]);
102: if (fconfig != null) {
103: fconfigs.add(fconfig);
104: } else {
105: log
106: .error("ERROR: subforward specified in XML menu file not found: "
107: + subforwards[i]);
108: }
109: }
110: }
111: for (Iterator iter = fconfigs.iterator(); iter.hasNext();) {
112: ForwardConfig fconfig = (ForwardConfig) iter.next();
113: String fwdPath = fconfig.getPath();
114: int end = fwdPath.indexOf(".do");
115: fwdPath = (end == -1) ? fwdPath : fwdPath.substring(0,
116: end);
117: if (fwdPath.equals(amapping.getPath())) {
118: selected = true;
119: break;
120: }
121: }
122: }
123:
124: // still not found, look for menu state in session attributes
125: if (ses != null && selected) {
126: ses.setAttribute(mMenuId + "_"
127: + RollerMenuModel.MENU_ITEM_KEY, mName);
128: }
129: return selected;
130: }
131:
132: /*
133: public Vector getMenuItems()
134: {
135: return mMenuItems;
136: }
137:
138: public addMenuItem(MenuItem item)
139: {
140: mMenuItems.add(item);
141: }
142: */
143: }
144:
145: //// get menu key from request param or from cookie
146: //String menuKeyName = mMenuId+"rmk";
147: //String menuKey = req.getParameter("rmk");
148: //if (menuKey == null)
149: //{
150: // Cookie menuCookie = RequestUtil.getCookie(req, menuKeyName);
151: // if (menuCookie != null)
152: // {
153: // menuKey = menuCookie.getValue();
154: // req.setAttribute("rmk", menuKey);
155: // }
156: //}
157: //// save menu key in cookie
158: //RequestUtil.setCookie(res, menuKeyName, menuKey, req.getContextPath());
159: //
160: //// get menu item key from request param or from cookie
161: //String itemKeyName = mMenuId+"rmik";
162: //String itemKey = req.getParameter("rmik");
163: //if (itemKey == null)
164: //{
165: // Cookie itemCookie = RequestUtil.getCookie(req, itemKeyName);
166: // if (itemCookie != null)
167: // {
168: // itemKey = itemCookie.getValue();
169: // req.setAttribute("rmik", itemKey);
170: // }
171: //}
172: //// save menu item key in cookie
173: //RequestUtil.setCookie(res, itemKeyName, itemKey, req.getContextPath());
|