001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.javascript.menu;
034:
035: import com.flexive.faces.javascript.RelativeUriMapper;
036: import com.flexive.war.JsonWriter;
037: import org.apache.commons.lang.StringUtils;
038:
039: import java.io.IOException;
040: import java.util.ArrayList;
041: import java.util.HashMap;
042: import java.util.List;
043: import java.util.Map;
044:
045: /**
046: * A menu item.
047: *
048: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
049: * @version $Rev: 1 $
050: */
051: public class MenuItem implements MenuItemContainer {
052: private final static Map<String, Object> EMPTY_MAP = new HashMap<String, Object>();
053: private final static List<MenuItem> EMPTY_LIST = new ArrayList<MenuItem>();
054:
055: private final String id;
056: private final String title;
057: private final String icon;
058: private final String onClick;
059: private final Map<String, Object> properties;
060: private final List<MenuItem> menuItems;
061:
062: public MenuItem(String title) {
063: this (null, title, null, null, EMPTY_MAP, EMPTY_LIST);
064: }
065:
066: public MenuItem(String id, String title, String icon,
067: String onClick, Map<String, Object> properties,
068: List<MenuItem> menuItems) {
069: this .id = id;
070: this .title = title;
071: this .icon = icon;
072: this .onClick = onClick;
073: this .properties = properties != null ? properties : EMPTY_MAP;
074: this .menuItems = menuItems;
075: }
076:
077: public void renderItemAttributes(JsonWriter out,
078: RelativeUriMapper uriMapper,
079: Map<String, String> engageSubscriptions, String widgetId)
080: throws IOException {
081: final String caption = "<span id=\"" + widgetId + "\">" + title
082: + "</span>"; // add an unique ID for browser tests
083: out.writeAttribute("widgetId", widgetId);
084: out.writeAttribute("caption", caption);
085: if (StringUtils.isNotBlank(icon)) {
086: out.writeAttribute("iconSrc", uriMapper
087: .getAbsoluteUri(MenuWriter.ICON_PATH + "/" + icon
088: + ".png"));
089: }
090: for (Map.Entry<String, Object> entry : properties.entrySet()) {
091: out.writeAttribute(entry.getKey(), entry.getValue());
092: }
093: if (StringUtils.isNotBlank(onClick)) {
094: // event subscriptions will be written after the menu was rendered
095: engageSubscriptions.put(widgetId, onClick);
096: }
097: }
098:
099: /**
100: * {@inheritDoc}
101: */
102: public void addMenuItem(MenuItem menuItem) {
103: menuItems.add(menuItem);
104: }
105:
106: /**
107: * {@inheritDoc}
108: */
109: public List<MenuItem> getMenuItems() {
110: return menuItems;
111: }
112:
113: public String getId() {
114: return id;
115: }
116:
117: public String getTitle() {
118: return title;
119: }
120:
121: public String getIcon() {
122: return icon;
123: }
124:
125: public String getOnClick() {
126: return onClick;
127: }
128:
129: public Map<String, Object> getProperties() {
130: return properties;
131: }
132: }
|