You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2006/08/25 19:10:51 UTC

svn commit: r436858 - in /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom: dojo/DojoUtils.java fisheye/HtmlFishEyeNavigationMenuRenderer.java

Author: werpu
Date: Fri Aug 25 10:10:50 2006
New Revision: 436858

URL: http://svn.apache.org/viewvc?rev=436858&view=rev
Log:
partial fixup for jira-624 the renderer for the fisheye now triggers upon programmatic generation, the 
dojo utils have 3 new methods for that kind of thing

Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/fisheye/HtmlFishEyeNavigationMenuRenderer.java

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java?rev=436858&r1=436857&r2=436858&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/dojo/DojoUtils.java Fri Aug 25 10:10:50 2006
@@ -19,6 +19,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
+
 import org.apache.myfaces.renderkit.html.util.AddResource;
 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
@@ -28,8 +29,11 @@
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 
+import java.util.Iterator;
+import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.Map.Entry;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
@@ -315,6 +319,94 @@
         return true;
     }
 
+
+    /**
+     * please, instead of using standard
+     * dojo taglib mechanisms use this code for initialisation
+     * it will render a clean and proper javascript initialisation
+     * instead. There are issues with ADF and the dojo taglib mechanisms
+     * and also (Alex Russel wont like to hear this) the 
+     * dojo taglib initialisation fails on W3C validations.
+     * returns the name of the javascript var for further processing
+     * @param facesContext standard faces context
+     * @param component standard component
+     * @param dojoType the dojo type of this component
+     * @param paramMap
+     */  
+    public static String renderWidgetInitializationCode(FacesContext facesContext, UIComponent component, String dojoType, Map paramMap) throws IOException {
+        ResponseWriter writer = facesContext.getResponseWriter();
+        String clientId = component.getClientId(facesContext);
+        return renderWidgetInitializationCode(writer,component, dojoType, paramMap,  clientId, true);
+    }
+    
+    /**
+     * same for a given neutral id...
+     * @param dojoType
+     * @param paramMap
+     * @param clientId the referencing id which the widget has to render to (note the id is enforced the uicomponent does nothing in this case!!!!)
+     * @param refId if true the refid is set in the dojo javascript init code if false no ref is set the false often is needed
+     * for containers which dynamically generated widgets with no referencing div
+     * @return a string with the name of the javascript variable
+     */
+    public static String renderWidgetInitializationCode(ResponseWriter writer, UIComponent component,  String dojoType, Map paramMap, String clientId, boolean refId) throws IOException {
+        
+        writer.startElement(HTML.SCRIPT_ELEM, component);
+        writer.writeAttribute(HTML.TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
+        String javascriptVar = calculateWidgetVarName(clientId);
+        
+        Iterator it = paramMap.entrySet().iterator();
+        
+        writer.write("var ");
+        writer.write(javascriptVar);
+        writer.write(" = ");
+        
+        writer.write("dojo.widget.createWidget(\"");
+        writer.write(dojoType);
+        writer.write("\",");
+        
+        writer.write("{");
+        boolean first = true;
+        while(it.hasNext()) {
+        	Entry entry = (Entry) it.next();
+        	if(entry.getValue() != null) {
+        		if(!first)
+            		writer.write(",");
+	        	writer.write(entry.getKey().toString());
+	        	writer.write(":"); //only real string values should be within ambersants, dojo req
+	        	boolean isString = entry.getValue() instanceof String;
+	        	if(isString)
+	        		writer.write("'");
+	        	writer.write(entry.getValue().toString());
+	          	if(isString)
+	        		writer.write("'");
+	    	}
+        	first = false;
+        }
+        writer.write("}");
+        if(refId) {
+	        writer.write(",dojo.byId('");
+	        writer.write(clientId);
+	        writer.write("')");
+        }
+        writer.write(");");
+             
+        writer.endElement(HTML.SCRIPT_ELEM);
+        return javascriptVar;
+    }
+
+    /**
+     * helper method to centralize the widget
+     * variable name calculation for our dojo
+     * javascript widget init code
+     * @param clientId the client id upon which the var name
+     * has to be generated
+     * @return the javascript widget var name for the given client id
+     */
+	public static String calculateWidgetVarName(String clientId) {
+		return clientId.replaceAll("\\:", "_") + "_dojoControl";
+	}
+
+    
     /**
      * helper to merge in an external dojo config instance
      * the merge algorithm is that an existing entry is overwritten
@@ -451,5 +543,7 @@
         writer.write(script);
         writer.endElement(HTML.SCRIPT_ELEM);
     }
+    
+    
 
 }

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/fisheye/HtmlFishEyeNavigationMenuRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/fisheye/HtmlFishEyeNavigationMenuRenderer.java?rev=436858&r1=436857&r2=436858&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/fisheye/HtmlFishEyeNavigationMenuRenderer.java (original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/fisheye/HtmlFishEyeNavigationMenuRenderer.java Fri Aug 25 10:10:50 2006
@@ -16,14 +16,19 @@
 package org.apache.myfaces.custom.fisheye;
 
 import java.io.IOException;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
+import java.util.Stack;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
 import javax.faces.event.ActionEvent;
+import javax.servlet.http.HttpServletRequest;
 
+import org.apache.commons.collections.map.HashedMap;
 import org.apache.myfaces.custom.dojo.DojoUtils;
 import org.apache.myfaces.custom.navmenu.UINavigationMenuItem;
 import org.apache.myfaces.renderkit.html.ext.HtmlLinkRenderer;
@@ -35,32 +40,48 @@
 
 /**
  * Renderer for the FishEyeList component
- *
+ * 
  * @author Jurgen Lust (latest modification by $Author$)
  * @version $Revision$ $Date$
  */
 public class HtmlFishEyeNavigationMenuRenderer extends HtmlLinkRenderer {
-    public static final String ATTACH_EDGE_ATTR = "dojo:attachEdge";
+	private static final String ON_CLICK_ATTR = "onClick";
 
-    public static final String CAPTION_ATTR = "caption";
-    public static final String DOJO_ITEM_STYLE_CLASS = "dojo-FisheyeListItem";
-    public static final String DOJO_STYLE_CLASS = "dojo-FisheyeList";
-    public static final String EFFECT_UNITS_ATTR = "dojo:effectUnits";
-    public static final String ICON_SRC_ATTR = "dojo:iconsrc";
-    public static final String ITEM_HEIGHT_ATTR = "dojo:itemHeight";
-    public static final String ITEM_MAX_HEIGHT_ATTR = "dojo:itemMaxHeight";
-    public static final String ITEM_MAX_WIDTH_ATTR = "dojo:itemMaxWidth";
-    public static final String ITEM_PADDING_ATTR = "dojo:itemPadding";
-    public static final String ITEM_WIDTH_ATTR = "dojo:itemWidth";
-    public static final String LABEL_EDGE_ATTR = "dojo:labelEdge";
-    public static final String ORIENTATION_ATTR = "dojo:orientation";
-    public static final String CONSERVATIVE_TRIGGER_ATTR = "dojo:conservativeTrigger";
-    public static final String RENDERER_TYPE = "org.apache.myfaces.FishEyeList";
-
-    /**
-     * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
-     */
-    public void decode(FacesContext context, UIComponent component) {
+	private static final String DOJO_COMPONENT_TYPE = "FisheyeList";
+
+	private static final String DOJO_ITEM_TYPE = "FisheyeListItem";
+
+	public static final String ATTACH_EDGE_ATTR = "attachEdge";
+
+	public static final String CAPTION_ATTR = "caption";
+
+	public static final String EFFECT_UNITS_ATTR = "effectUnits";
+
+	public static final String ICON_SRC_ATTR = "iconSrc";
+
+	public static final String ITEM_HEIGHT_ATTR = "itemHeight";
+
+	public static final String ITEM_MAX_HEIGHT_ATTR = "itemMaxHeight";
+
+	public static final String ITEM_MAX_WIDTH_ATTR = "itemMaxWidth";
+
+	public static final String ITEM_PADDING_ATTR = "itemPadding";
+
+	public static final String ITEM_WIDTH_ATTR = "itemWidth";
+
+	public static final String LABEL_EDGE_ATTR = "labelEdge";
+
+	public static final String ORIENTATION_ATTR = "orientation";
+
+	public static final String CONSERVATIVE_TRIGGER_ATTR = "conservativeTrigger";
+
+	public static final String RENDERER_TYPE = "org.apache.myfaces.FishEyeList";
+
+	/**
+	 * @see javax.faces.render.Renderer#decode(javax.faces.context.FacesContext,
+	 *      javax.faces.component.UIComponent)
+	 */
+ 	public void decode(FacesContext context, UIComponent component) {
         FormInfo nestingForm = findNestingForm(component, context);
         if (nestingForm != null) {
             String fieldName = HtmlRendererUtils.getHiddenCommandLinkFieldName(nestingForm);
@@ -75,84 +96,122 @@
 
     }
 
-    /**
-     * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
-     */
-    public void encodeBegin(FacesContext context, UIComponent component)
-        throws IOException {
-        if (component.isRendered()) {
-            HtmlFishEyeNavigationMenu fisheye = (HtmlFishEyeNavigationMenu) component;
-            ResponseWriter writer = context.getResponseWriter();
-            //initialize DOJO
-            String javascriptLocation = (String) component.getAttributes().get(
-                JSFAttr.JAVASCRIPT_LOCATION);
-            DojoUtils.addMainInclude(context, component, javascriptLocation,
-                                     DojoUtils.getDjConfigInstance(context));
-            DojoUtils.addRequire(context, component, "dojo.widget.FisheyeList");
-
-            writer.startElement(HTML.DIV_ELEM, fisheye);
-            writer.writeAttribute(HTML.CLASS_ATTR, DOJO_STYLE_CLASS, null);
-            writeAttribute(writer, fisheye, ITEM_WIDTH_ATTR, fisheye
-                .getItemWidth());
-            writeAttribute(writer, fisheye, ITEM_HEIGHT_ATTR, fisheye
-                .getItemHeight());
-            writeAttribute(writer, fisheye, ITEM_MAX_WIDTH_ATTR, fisheye
-                .getItemMaxWidth());
-            writeAttribute(writer, fisheye, ITEM_MAX_HEIGHT_ATTR, fisheye
-                .getItemMaxHeight());
-            writeAttribute(writer, fisheye, ORIENTATION_ATTR, fisheye
-                .getOrientation());
-            writeAttribute(writer, fisheye, EFFECT_UNITS_ATTR, fisheye
-                .getEffectUnits());
-            writeAttribute(writer, fisheye, ITEM_PADDING_ATTR, fisheye
-                .getItemPadding());
-            writeAttribute(writer, fisheye, ATTACH_EDGE_ATTR, fisheye
-                .getAttachEdge());
-            writeAttribute(writer, fisheye, LABEL_EDGE_ATTR, fisheye
-                .getLabelEdge());
-            writeAttribute(writer, fisheye, CONSERVATIVE_TRIGGER_ATTR, fisheye
-                .getConservativeTrigger());
-        }
-    }
-
-    /**
-     * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
-     */
-    public void encodeChildren(FacesContext context, UIComponent component)
-        throws IOException {
-        ResponseWriter writer = context.getResponseWriter();
-        List children = component.getChildren();
-        for (Iterator cit = children.iterator(); cit.hasNext();) {
-            UIComponent child = (UIComponent) cit.next();
-            if (!child.isRendered())
-                continue;
-            if (child instanceof UINavigationMenuItem) {
-                renderMenuItem(context, writer, (UINavigationMenuItem) child);
-            }
-        }
-    }
-
-    /**
-     * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
-     */
-    public void encodeEnd(FacesContext context, UIComponent component)
-        throws IOException {
-        if (component.isRendered()) {
-            ResponseWriter writer = context.getResponseWriter();
-            writer.endElement(HTML.DIV_ELEM);
-        }
-    }
-
-    /**
-     * @see javax.faces.render.Renderer#getRendersChildren()
-     */
-    public boolean getRendersChildren() {
-        //always render the menu items
-        return true;
-    }
-
-    protected void renderMenuItem(FacesContext context, ResponseWriter writer,
-                                  UINavigationMenuItem item) throws IOException {
+	/**
+	 * @see javax.faces.render.Renderer#encodeBegin(javax.faces.context.FacesContext,
+	 *      javax.faces.component.UIComponent)
+	 */
+	public void encodeBegin(FacesContext context, UIComponent component)
+			throws IOException {
+		if (component.isRendered()) {
+			HtmlFishEyeNavigationMenu fisheye = (HtmlFishEyeNavigationMenu) component;
+			ResponseWriter writer = context.getResponseWriter();
+			// initialize DOJO
+			String javascriptLocation = (String) component.getAttributes().get(
+					JSFAttr.JAVASCRIPT_LOCATION);
+			DojoUtils.addMainInclude(context, component, javascriptLocation,
+					DojoUtils.getDjConfigInstance(context));
+			DojoUtils.addRequire(context, component, "dojo.widget.FisheyeList");
+
+			writer.startElement(HTML.DIV_ELEM, fisheye);
+			writer.writeAttribute(HTML.ID_ATTR, component.getClientId(context),
+					null);
+
+			writer.endElement(HTML.DIV_ELEM);
+
+			Map paramMap = new HashedMap();
+			paramMap.put(ITEM_WIDTH_ATTR, fisheye.getItemWidth());
+			paramMap.put(ITEM_HEIGHT_ATTR, fisheye.getItemHeight());
+			paramMap.put(ITEM_MAX_WIDTH_ATTR, fisheye.getItemMaxWidth());
+			paramMap.put(ITEM_MAX_HEIGHT_ATTR, fisheye.getItemMaxHeight());
+			paramMap.put(ORIENTATION_ATTR, fisheye.getOrientation());
+			paramMap.put(EFFECT_UNITS_ATTR, fisheye.getEffectUnits());
+			paramMap.put(ITEM_PADDING_ATTR, fisheye.getItemPadding());
+			paramMap.put(ATTACH_EDGE_ATTR, fisheye.getAttachEdge());
+			paramMap.put(LABEL_EDGE_ATTR, fisheye.getLabelEdge());
+			paramMap.put(CONSERVATIVE_TRIGGER_ATTR, fisheye
+					.getConservativeTrigger());
+
+			String menu = DojoUtils.renderWidgetInitializationCode(context,
+					component, DOJO_COMPONENT_TYPE, paramMap);
+			getChildsMenuStack(context, component).push(menu);
+
+		}
+
+	}
+
+	private Stack getChildsMenuStack(FacesContext context, UIComponent component) {
+		Stack menuStack = (Stack) ((HttpServletRequest) context
+				.getExternalContext().getRequest()).getAttribute(component
+				.getClientId(context)
+				+ "_FishEyeMenuAttr");
+		if (menuStack != null)
+			return menuStack;
+
+		menuStack = new Stack();
+		((HttpServletRequest) context.getExternalContext().getRequest())
+				.setAttribute(component.getClientId(context)
+						+ "_FishEyeMenuAttr", menuStack);
+		return menuStack;
+	}
+
+	/**
+	 * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext,
+	 *      javax.faces.component.UIComponent)
+	 */
+	public void encodeChildren(FacesContext context, UIComponent component)
+			throws IOException {
+		ResponseWriter writer = context.getResponseWriter();
+		List children = component.getChildren();
+		Stack menuStack = (Stack) getChildsMenuStack(context, component);
+		for (Iterator cit = children.iterator(); cit.hasNext();) {
+			UIComponent child = (UIComponent) cit.next();
+			if (!child.isRendered())
+				continue;
+			if (child instanceof UINavigationMenuItem) {
+				renderMenuItem(context, writer, component,
+						(UINavigationMenuItem) child, menuStack);
+			}
+		}
+	}
+
+	/**
+	 * @see javax.faces.render.Renderer#encodeEnd(javax.faces.context.FacesContext,
+	 *      javax.faces.component.UIComponent)
+	 */
+	public void encodeEnd(FacesContext context, UIComponent component)
+			throws IOException {
+		if (component.isRendered()) {
+			ResponseWriter writer = context.getResponseWriter();
+			Stack menuStack = getChildsMenuStack(context, component);
+			String jsMenuVar = (String) menuStack.remove(0);// TODO change this
+															// to a separate
+			// location this is evil
+
+			writer.startElement(HTML.SCRIPT_ELEM, component);
+			writer.writeAttribute(HTML.TYPE_ATTR,
+					HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
+			while (!menuStack.isEmpty()) {
+				String item = (String) menuStack.pop();
+				writer.write(jsMenuVar);
+				writer.write(".addChild(");
+				writer.write(item);
+				writer.write(");\n");
+			}
+			writer.endElement(HTML.SCRIPT_ELEM);
+		}
+	}
+
+	/**
+	 * @see javax.faces.render.Renderer#getRendersChildren()
+	 */
+	public boolean getRendersChildren() {
+		// always render the menu items
+		return true;
+	}
+
+	protected void renderMenuItem(FacesContext context, ResponseWriter writer,
+			UIComponent menu, UINavigationMenuItem item, Stack childsMenuStack)
+			throws IOException {
         //find the enclosing form
         FormInfo formInfo = findNestingForm(item, context);
         String clientId = item.getClientId(context);
@@ -205,21 +264,27 @@
         onClick.append(jsForm);
         onClick.append(".submit();}return false;"); //return false, so that browser does not handle the click
 
-        writer.startElement(HTML.DIV_ELEM, item);
-        writer.writeAttribute(HTML.CLASS_ATTR, DOJO_ITEM_STYLE_CLASS, null);
-        writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
-        writer.writeAttribute(CAPTION_ATTR, item.getItemLabel(), null);
-        writer.writeAttribute(ICON_SRC_ATTR, item.getIcon(), null);
-        writer.writeAttribute(HTML.ID_ATTR, clientId, null);
-        writer.endElement(HTML.DIV_ELEM);
-    }
-
-    protected void writeAttribute(ResponseWriter writer,
-                                  HtmlFishEyeNavigationMenu fisheye, String name, Object value)
-        throws IOException {
-        if (name != null && value != null) {
-            writer.writeAttribute(name, value, null);
-        }
-    }
+		Map paramMap = new HashMap();
+		paramMap.put(CAPTION_ATTR, item.getItemLabel());
+		paramMap.put(ICON_SRC_ATTR, item.getIcon());
+		paramMap.put(ON_CLICK_ATTR, new StringBuffer("function () {").append(
+				onClick).append("}"));
+		// push the onclick as lambda and use a stringbuffer so that we do not
+		// get enclosing quotes
+		String menuItemId = DojoUtils.renderWidgetInitializationCode(writer,
+				item, DOJO_ITEM_TYPE, paramMap, item.getClientId(context),
+				false);
+		childsMenuStack.push(menuItemId); 
+		// we have to revert the elements,
+		// hence a stack
+	}
+
+	protected void writeAttribute(ResponseWriter writer,
+			HtmlFishEyeNavigationMenu fisheye, String name, Object value)
+			throws IOException {
+		if (name != null && value != null) {
+			writer.writeAttribute(name, value, null);
+		}
+	}
 
 }