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.components.menu;
034:
035: import com.flexive.faces.FxJsfComponentUtils;
036: import com.flexive.faces.FxJsfUtils;
037: import com.flexive.faces.javascript.menu.MenuItem;
038: import com.flexive.faces.javascript.menu.MenuItemContainer;
039:
040: import javax.faces.component.UIOutput;
041: import javax.faces.context.FacesContext;
042: import java.io.IOException;
043: import java.util.ArrayList;
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.Map;
047:
048: /**
049: * Adds a dojo menu item to the enclosing Dojo menu container. DojoMenuItems
050: * can be nested to create submenus, i.e. a DojoMenuItem is an item container
051: * like DojoMenu itself.
052: *
053: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
054: * @version $Rev: 1 $
055: * @see DojoMenu
056: */
057: public class DojoMenuItem extends UIOutput implements MenuItemContainer {
058: private final List<MenuItem> menuItems = new ArrayList<MenuItem>();
059: private String labelKey;
060: private String label;
061: private String icon;
062: private String clickHandler;
063: private String link;
064: private Map<String, Object> itemProperties;
065:
066: public DojoMenuItem() {
067: setRendererType(null);
068: }
069:
070: /**
071: * {@inheritDoc}
072: */
073: @Override
074: public void encodeBegin(FacesContext facesContext)
075: throws IOException {
076: MenuItemContainer container = FxJsfUtils.findAncestor(this ,
077: MenuItemContainer.class);
078: String itemLabel = getLabel() != null ? getLabel() : FxJsfUtils
079: .getLocalizedMessage(getLabelKey());
080: // create an item that also includes nested menu items (added in the body of this component)
081: MenuItem item = new MenuItem(getClientId(facesContext),
082: itemLabel, getIcon(), getClickHandler(),
083: getItemProperties(), getMenuItems());
084: container.addMenuItem(item);
085: }
086:
087: /**
088: * Return the new menu item's properties.
089: *
090: * @return the new menu item's properties
091: */
092: protected Map<String, Object> getItemProperties() {
093: if (itemProperties == null) {
094: itemProperties = new HashMap<String, Object>();
095: }
096: return itemProperties;
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 getLabelKey() {
114: if (labelKey == null) {
115: labelKey = FxJsfComponentUtils.getStringValue(this ,
116: "labelKey");
117: }
118: return labelKey;
119: }
120:
121: public void setLabelKey(String labelKey) {
122: this .labelKey = labelKey;
123: }
124:
125: public String getIcon() {
126: if (icon == null) {
127: icon = FxJsfComponentUtils.getStringValue(this , "icon");
128: }
129: return icon;
130: }
131:
132: public void setIcon(String icon) {
133: this .icon = icon;
134: }
135:
136: public String getClickHandler() {
137: if (clickHandler == null) {
138: clickHandler = FxJsfComponentUtils.getStringValue(this ,
139: "clickHandler");
140: }
141: if (clickHandler == null && getLink() != null) {
142: clickHandler = "function(menuItem) { loadContentPage('"
143: + getLink() + "'); }";
144: }
145: return clickHandler;
146: }
147:
148: public void setClickHandler(String clickHandler) {
149: this .clickHandler = clickHandler;
150: }
151:
152: public String getLabel() {
153: if (label == null) {
154: label = FxJsfComponentUtils.getStringValue(this , "label");
155: }
156: return label;
157: }
158:
159: public void setLabel(String label) {
160: this .label = label;
161: }
162:
163: public String getLink() {
164: if (link == null) {
165: link = FxJsfComponentUtils.getStringValue(this , "link");
166: }
167: return link;
168: }
169:
170: public void setLink(String link) {
171: this .link = link;
172: }
173:
174: /**
175: * {@inheritDoc}
176: */
177: @Override
178: public Object saveState(FacesContext facesContext) {
179: Object[] state = new Object[6];
180: state[0] = super .saveState(facesContext);
181: state[1] = clickHandler;
182: state[2] = icon;
183: state[3] = label;
184: state[4] = labelKey;
185: state[5] = link;
186: return state;
187: }
188:
189: /**
190: * {@inheritDoc}
191: */
192: @Override
193: public void restoreState(FacesContext facesContext, Object o) {
194: Object[] state = (Object[]) o;
195: super .restoreState(facesContext, state[0]);
196: clickHandler = (String) state[1];
197: icon = (String) state[2];
198: label = (String) state[3];
199: labelKey = (String) state[4];
200: link = (String) state[5];
201: }
202: }
|