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.JsfRelativeUriMapper;
037: import com.flexive.faces.javascript.FxJavascriptUtils;
038: import com.flexive.faces.javascript.menu.MenuItem;
039: import com.flexive.faces.javascript.menu.MenuItemContainer;
040: import com.flexive.faces.javascript.menu.MenuWriter;
041:
042: import javax.faces.component.NamingContainer;
043: import javax.faces.component.UIOutput;
044: import javax.faces.context.FacesContext;
045: import javax.faces.context.ResponseWriter;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.List;
049:
050: /**
051: * Renders a Dojo menu component. Renders a stand-alone popup menu for a Dojo Menu2 widget
052: * using javascript (i.e. no markup needs to be processed, and it does not work without javascript).
053: *
054: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
055: * @version $Rev: 1 $
056: */
057: public class DojoMenu extends UIOutput implements MenuItemContainer,
058: NamingContainer {
059: private static final String DOJO_MENU = "PopupMenu2";
060: private static final String DOJO_ITEM = "MenuItem2";
061:
062: private final List<MenuItem> menuItems = new ArrayList<MenuItem>();
063: private String name;
064: private String contextMenuTarget;
065: private String showHandler;
066:
067: public DojoMenu() {
068: setRendererType(null);
069: }
070:
071: /**
072: * {@inheritDoc }
073: */
074: @Override
075: public String getClientId(FacesContext facesContext) {
076: if (getName() != null) {
077: setId(getName()); // use the js component name as our id
078: }
079: return super .getClientId(facesContext);
080: }
081:
082: /**
083: * {@inheritDoc }
084: */
085: @Override
086: public void encodeEnd(FacesContext facesContext) throws IOException {
087: final ResponseWriter writer = facesContext.getResponseWriter();
088: FxJavascriptUtils.beginJavascript(writer);
089: FxJavascriptUtils
090: .writeDojoRequires(writer, "dojo.widget.Menu2");
091: writer.write("dojo.addOnLoad(function() {\n");
092: // addOnLoad start --->
093: MenuWriter.writeMenu(writer, getName(), getName(), DOJO_MENU,
094: DOJO_ITEM, this , new JsfRelativeUriMapper(),
095: getContextMenuTarget(), getShowHandler());
096: writer.write("document.getElementById('" + getName()
097: + "').appendChild(" + getName() + ".domNode);\n");
098: // <--- addOnLoad end
099: writer.write("});\n");
100: FxJavascriptUtils.endJavascript(writer);
101: writer.write("<div id=\"" + getName() + "\"> </div>\n");
102: }
103:
104: /**
105: * {@inheritDoc}
106: */
107: public List<MenuItem> getMenuItems() {
108: return menuItems;
109: }
110:
111: public void addMenuItem(MenuItem menuItem) {
112: menuItems.add(menuItem);
113: }
114:
115: public String getName() {
116: if (name == null) {
117: name = FxJsfComponentUtils.getStringValue(this , "name");
118: }
119: return name;
120: }
121:
122: public void setName(String name) {
123: this .name = name;
124: }
125:
126: public String getContextMenuTarget() {
127: if (contextMenuTarget == null) {
128: contextMenuTarget = FxJsfComponentUtils.getStringValue(
129: this , "contextMenuTarget");
130: }
131: return contextMenuTarget;
132: }
133:
134: public void setContextMenuTarget(String contextMenuTarget) {
135: this .contextMenuTarget = contextMenuTarget;
136: }
137:
138: public String getShowHandler() {
139: if (showHandler == null) {
140: showHandler = FxJsfComponentUtils.getStringValue(this ,
141: "showHandler");
142: }
143: return showHandler;
144: }
145:
146: public void setShowHandler(String showHandler) {
147: this .showHandler = showHandler;
148: }
149:
150: /**
151: * {@inheritDoc}
152: */
153: @Override
154: public Object saveState(FacesContext facesContext) {
155: Object[] state = new Object[4];
156: state[0] = super .saveState(facesContext);
157: state[1] = getName();
158: state[2] = getContextMenuTarget();
159: state[3] = getShowHandler();
160: return state;
161: }
162:
163: /**
164: * {@inheritDoc}
165: */
166: @Override
167: public void restoreState(FacesContext facesContext, Object o) {
168: Object[] state = (Object[]) o;
169: super .restoreState(facesContext, state[0]);
170: setName((String) state[1]);
171: setContextMenuTarget((String) state[2]);
172: setShowHandler((String) state[3]);
173: }
174: }
|