001: package org.contineo.web.navigation;
002:
003: import org.apache.commons.lang.StringUtils;
004: import org.apache.commons.logging.Log;
005: import org.apache.commons.logging.LogFactory;
006:
007: import org.contineo.core.security.Menu;
008: import org.contineo.core.security.dao.MenuDAO;
009:
010: import org.contineo.util.Context;
011:
012: import org.contineo.web.SessionManagement;
013: import org.contineo.web.StyleBean;
014: import org.contineo.web.i18n.Messages;
015:
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.List;
019:
020: import javax.faces.context.FacesContext;
021: import javax.faces.el.MethodBinding;
022: import javax.faces.event.ActionEvent;
023:
024: /**
025: * <p>
026: * The MenuBarBean class determines which menu item fired the ActionEvent and
027: * stores the modified id information in a String. MenuBarBean also controls the
028: * orientation of the Menu Bar.
029: * </p>
030: *
031: * @author Marco Meschieri
032: * @version $Id: MenuBarBean.java,v 1.9 2007/10/11 16:33:56 marco Exp $
033: * @since 3.0
034: */
035: public class MenuBarBean {
036: protected static Log logger = LogFactory.getLog(MenuBarBean.class);
037:
038: // records which menu item fired the event
039: private String actionFired;
040:
041: // records the param value for the menu item which fired the event
042: private String param;
043:
044: // orientation of the menubar ("horizontal" or "vertical")
045: private String orientation = "horizontal";
046:
047: // list for the dynamic menus w/ getter & setter
048: protected List<MenuItem> model = new ArrayList<MenuItem>();
049: private NavigationBean navigation;
050:
051: /**
052: * Get the param value for the menu item which fired the event.
053: *
054: * @return the param value
055: */
056: public String getParam() {
057: return param;
058: }
059:
060: /**
061: * Set the param value.
062: */
063: public void setParam(String param) {
064: this .param = param;
065: }
066:
067: /**
068: * Get the modified ID of the fired action.
069: *
070: * @return the modified ID
071: */
072: public String getActionFired() {
073: return actionFired;
074: }
075:
076: public List<MenuItem> getModel() {
077: if (model.isEmpty()) {
078: createMenuItems();
079: }
080:
081: return model;
082: }
083:
084: public void setModel(List<MenuItem> model) {
085: this .model = model;
086: }
087:
088: /**
089: * Identify the ID of the element that fired the event and return it in a
090: * form suitable for display.
091: *
092: * @param e the event that fired the listener
093: */
094: public void primaryListener(ActionEvent e) {
095: MenuItem menu = (MenuItem) e.getSource();
096:
097: if (StringUtils.isNotEmpty(menu.getContent().getTemplate())) {
098: navigation.setSelectedPanel(menu.getContent());
099: }
100: }
101:
102: /**
103: * Get the orientation of the menu ("horizontal" or "vertical")
104: *
105: * @return the orientation of the menu
106: */
107: public String getOrientation() {
108: return orientation;
109: }
110:
111: /**
112: * Set the orientation of the menu ("horizontal" or "vertical").
113: *
114: * @param orientation the new orientation of the menu
115: */
116: public void setOrientation(String orientation) {
117: this .orientation = orientation;
118: }
119:
120: /**
121: * Finds all first-level menus accessible by the current user
122: *
123: * @return
124: */
125: protected void createMenuItems() {
126: model.clear();
127:
128: String username = SessionManagement.getUsername();
129: PageContentBean page = new PageContentBean("home", "home");
130: page.setContentTitle("Home");
131: page.setDisplayText("Home");
132: page.setIcon(StyleBean.getImagePath("home.png"));
133:
134: MenuItem item = createMenuItem("home", null,
135: "#{menuBar.primaryListener}", null, null, StyleBean
136: .getImagePath("home.png"), false, null, page);
137: model.add(item);
138:
139: try {
140: if (username != null) {
141: MenuDAO menuDao = (MenuDAO) Context.getInstance()
142: .getBean(MenuDAO.class);
143: Collection<Menu> menus = menuDao.findByUserName(
144: username, 1);
145:
146: for (Menu menu : menus) {
147: createMenuStructure(menu, null);
148: }
149: }
150: } catch (Throwable t) {
151: logger.error(t.getMessage(), t);
152: }
153:
154: MenuItem helpMenu = createMenuItem("Help", "m-help",
155: "#{menuBar.primaryListener}", null, null, StyleBean
156: .getImagePath("help.png"), false, null, null);
157:
158: PageContentBean infoPage = new PageContentBean("info", "info");
159: infoPage.setContentTitle(Messages.getMessage("msg.jsp.info"));
160: helpMenu.getChildren().add(
161: createMenuItem("About Contineo", "m-about",
162: "#{menuBar.primaryListener}", null, null,
163: StyleBean.getImagePath("about.png"), false,
164: null, infoPage));
165: model.add(helpMenu);
166: }
167:
168: private void createMenuStructure(Menu menu, MenuItem parent) {
169: PageContentBean page;
170: MenuItem item;
171: page = new PageContentBean("m-"
172: + Integer.toString(menu.getMenuId()));
173:
174: if (StringUtils.isNotEmpty(menu.getMenuRef())) {
175: page.setTemplate(menu.getMenuRef());
176: }
177:
178: page.setContentTitle(Messages.getMessage(menu.getMenuText()));
179: page.setIcon(StyleBean.getImagePath(menu.getMenuIcon()));
180:
181: item = createMenuItem(Messages.getMessage(menu.getMenuText()),
182: "m-" + Integer.toString(menu.getMenuId()),
183: "#{menuBar.primaryListener}", null, null, (menu
184: .getMenuIcon() != null) ? StyleBean
185: .getImagePath(menu.getMenuIcon()) : null,
186: false, null, page);
187:
188: if (parent == null) {
189: model.add(item);
190: } else {
191: parent.getChildren().add(item);
192: }
193:
194: // For 'Documents' menu, skip children
195: if (menu.getMenuId() != Menu.MENUID_DOCUMENTS) {
196: MenuDAO menuDao = (MenuDAO) Context.getInstance().getBean(
197: MenuDAO.class);
198: Collection<Menu> children = menuDao.findByUserName(
199: SessionManagement.getUsername(), menu.getMenuId());
200:
201: for (Menu child : children) {
202: createMenuStructure(child, item);
203: }
204: }
205: }
206:
207: protected MenuItem createMenuItem(String label, String id,
208: String actionListener, String action, String link,
209: String icon, boolean immediate, String target,
210: PageContentBean content) {
211: MenuItem menuItem = new MenuItem();
212: menuItem.setValue(label);
213:
214: if (id != null) {
215: menuItem.setId(id);
216: }
217:
218: if (actionListener != null) {
219: menuItem
220: .setActionListener(createActionListenerMethodBinding(actionListener));
221: }
222:
223: if (action != null) {
224: menuItem.setAction(createActionMethodBinding(action));
225: }
226:
227: if (link != null) {
228: menuItem.setLink(link);
229: }
230:
231: if (icon != null) {
232: menuItem.setIcon(icon);
233: }
234:
235: menuItem.setImmediate(immediate);
236:
237: if (target != null) {
238: menuItem.setTarget(target);
239: }
240:
241: if (content != null) {
242: menuItem.setContent(content);
243: }
244:
245: return menuItem;
246: }
247:
248: protected MethodBinding createActionListenerMethodBinding(
249: String actionListenerString) {
250: Class[] args = { ActionEvent.class };
251: MethodBinding methodBinding = null;
252:
253: methodBinding = FacesContext.getCurrentInstance()
254: .getApplication().createMethodBinding(
255: actionListenerString, args);
256:
257: return methodBinding;
258: }
259:
260: protected MethodBinding createActionMethodBinding(String action) {
261: Class[] args = {};
262: MethodBinding methodBinding = null;
263:
264: methodBinding = FacesContext.getCurrentInstance()
265: .getApplication().createMethodBinding(action, args);
266:
267: return methodBinding;
268: }
269:
270: public void setNavigation(NavigationBean navigation) {
271: this.navigation = navigation;
272: }
273: }
|