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: import java.util.Vector;
025: import javax.servlet.ServletContext;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpSession;
029: import javax.servlet.jsp.PageContext;
030: import org.apache.struts.Globals;
031: import org.apache.struts.action.ActionMapping;
032: import org.apache.struts.config.ForwardConfig;
033: import org.apache.struts.config.ModuleConfig;
034: import org.apache.struts.util.RequestUtils;
035:
036: import org.apache.roller.RollerException;
037: import org.apache.roller.ui.core.RollerContext;
038:
039: /////////////////////////////////////////////////////////////////////////
040:
041: /** MenuImpl model has collection of menus */
042: public class MenuImpl extends BaseRollerMenu implements Menu {
043: private String mMenuId = null;
044:
045: /** Vector of MenuItemImpl objects */
046: Vector mMenuItems = new Vector();
047:
048: /** Is this the default menu? */
049: boolean mDefault = false;
050:
051: public MenuImpl() {
052: }
053:
054: /** Construct with name */
055: public MenuImpl(String n) {
056: super (n, null);
057: }
058:
059: /** Add MenuItemImpl to MenuImpl */
060: public void addItem(MenuItemImpl item) {
061: mMenuItems.addElement(item);
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: /** Collection of MenuItemImpl objects */
075: public Vector getMenuItems() {
076: return mMenuItems;
077: }
078:
079: /** Get currently selected menu item in this menu
080: * @throws RollerException*/
081: public MenuItem getSelectedMenuItem(HttpServletRequest req)
082: throws RollerException {
083: return getSelectedMenuItem(req, true);
084: }
085:
086: /**
087: * Get currently selected menu item in this menu
088: * @throws RollerException
089: */
090: public MenuItem getSelectedMenuItem(HttpServletRequest req,
091: boolean returnDefault) throws RollerException {
092: MenuItemImpl def = null;
093: MenuItemImpl selected = null;
094: for (int i = 0; i < mMenuItems.size(); i++) {
095: MenuItemImpl item = (MenuItemImpl) mMenuItems.elementAt(i);
096: if (item.isSelected(req)) {
097: selected = item;
098: break;
099: }
100: // Set first permitted and enabled menu item in each menu as default
101: if (item.isPermitted(req) && def == null) {
102: def = item;
103: }
104: }
105: if (selected != null) {
106: return selected;
107: } else if (returnDefault) {
108: return def;
109: } else {
110: return null;
111: }
112: }
113:
114: /**
115: * Get default menu item (first one that is permitted)
116: * @throws RollerException
117: */
118: public MenuItem getDefaultMenuItem(HttpServletRequest req)
119: throws RollerException {
120: MenuItemImpl def = null;
121: MenuItemImpl selected = null;
122: for (int i = 0; i < mMenuItems.size(); i++) {
123: // Set first permitted and enabled menu item in each menu as default
124: MenuItemImpl item = (MenuItemImpl) mMenuItems.elementAt(i);
125: if (item.isPermitted(req) && def == null) {
126: def = item;
127: }
128: }
129: return def;
130: }
131:
132: /**
133: * Is this menu selected?
134: * @throws RollerException
135: */
136: public boolean isSelected(HttpServletRequest req)
137: throws RollerException {
138: boolean selected = false;
139: HttpSession ses = req.getSession(false);
140:
141: // try to get state from request param then attribute
142: String menuKey = req.getParameter(RollerMenuModel.MENU_KEY);
143: if (null == menuKey) {
144: menuKey = (String) req
145: .getAttribute(RollerMenuModel.MENU_KEY);
146: }
147: if (menuKey != null && menuKey.equals(mName)) {
148: selected = true;
149: }
150: // next, if submenu is selected, then we're selected
151: else if (getSelectedMenuItem(req, false) != null) {
152: selected = true;
153: }
154: // next, try to use Struts forward to determine state
155: else if (mForward != null) {
156: ServletContext ctx = RollerContext.getServletContext();
157: ModuleConfig mConfig = RequestUtils.getModuleConfig(req,
158: ctx);
159: ActionMapping amapping = (ActionMapping) req
160: .getAttribute(Globals.MAPPING_KEY);
161: List fconfigs = new ArrayList();
162: fconfigs.add(mConfig.findForwardConfig(mForward));
163: if (mSubforwards != null) {
164: String[] subforwards = mSubforwards.split(",");
165: for (int i = 0; i < subforwards.length; i++) {
166: fconfigs.add(mConfig
167: .findForwardConfig(subforwards[i]));
168: }
169: }
170: for (Iterator iter = fconfigs.iterator(); iter.hasNext();) {
171: ForwardConfig fconfig = (ForwardConfig) iter.next();
172: String fwdPath = fconfig.getPath();
173: int end = fwdPath.indexOf(".do");
174: fwdPath = (end == -1) ? fwdPath : fwdPath.substring(0,
175: end);
176: if (fwdPath.equals(amapping.getPath())) {
177: selected = true;
178: break;
179: }
180: }
181: }
182: return selected;
183: }
184:
185: /** Name of Struts forward menu item should link to */
186: public String getUrl(PageContext pctx) {
187: String url = null;
188: try {
189: // If no forward specified, use default submenu URL
190: if (mForward == null && mMenuItems != null
191: && mMenuItems.size() > 0) {
192: HttpServletRequest req = (HttpServletRequest) pctx
193: .getRequest();
194: String surl = getDefaultMenuItem(req).getUrl(pctx);
195: StringBuffer sb = new StringBuffer(surl);
196: if (surl.indexOf("?") == -1) {
197: sb.append("?");
198: } else {
199: sb.append("&");
200: }
201: sb.append(RollerMenuModel.MENU_KEY);
202: sb.append("=");
203: sb.append(getName());
204: url = sb.toString();
205: } else {
206: return super .getUrl(pctx);
207: }
208: } catch (Exception e) {
209: pctx.getServletContext().log("ERROR in menu creating URL",
210: e);
211: }
212: return url;
213: }
214:
215: }
|