Author: nbelaevski
Date: 2009-03-20 16:07:56 -0400 (Fri, 20 Mar 2009)
New Revision: 13085
Added:
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBaseChildrenHolder.java
Modified:
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBase.java
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/html/PanelMenuRenderer.java
trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuGroup.jspx
trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuItem.jspx
Log:
https://jira.jboss.org/jira/browse/RF-6119
Modified:
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBase.java
===================================================================
---
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBase.java 2009-03-20
19:01:25 UTC (rev 13084)
+++
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBase.java 2009-03-20
20:07:56 UTC (rev 13085)
@@ -295,5 +295,25 @@
}
}
+
+ public void registerComponent(FacesContext context, UIComponent component) {
+ if (component instanceof UIPanelMenuGroup || component instanceof UIPanelMenuItem) {
+ int level = 0;
+ UIComponent c = component;
+ while ((c = c.getParent()) != null) {
+ if (c instanceof UIPanelMenuGroup) {
+ level++;
+ } else if (c instanceof UIPanelMenu) {
+ UIPanelMenu parentMenu = (UIPanelMenu) c;
+
+ PanelMenuRendererBaseChildrenHolder childrenHolder =
PanelMenuRendererBaseChildrenHolder.getChildrenHolder(parentMenu);
+ if (childrenHolder != null) {
+ childrenHolder.registerChild(component, level);
+ }
+ break;
+ }
+ }
+ }
+ }
}
Added:
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBaseChildrenHolder.java
===================================================================
---
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBaseChildrenHolder.java
(rev 0)
+++
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/PanelMenuRendererBaseChildrenHolder.java 2009-03-20
20:07:56 UTC (rev 13085)
@@ -0,0 +1,90 @@
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces - Ajax4jsf Component Library
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.richfaces.renderkit;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.component.UIComponent;
+
+import org.richfaces.component.UIPanelMenu;
+
+public final class PanelMenuRendererBaseChildrenHolder {
+
+ private static final String CHILDREN_HOLDER_ATTRIBUTE =
"_panelMenuRendererBaseChildrenHolder";
+
+ public static final class Item {
+ private UIComponent component;
+ private int level;
+
+ private Item(UIComponent component, int level) {
+ super();
+ this.component = component;
+ this.level = level;
+ }
+
+ public UIComponent getComponent() {
+ return component;
+ }
+
+ public int getLevel() {
+ return level;
+ }
+ }
+
+ private List<PanelMenuRendererBaseChildrenHolder.Item> children = new
ArrayList<PanelMenuRendererBaseChildrenHolder.Item>();
+
+ private Object initialAttributeValue;
+
+ private UIComponent component;
+
+ public PanelMenuRendererBaseChildrenHolder(UIComponent component) {
+ super();
+
+ this.component = component;
+ this.initialAttributeValue = component.getAttributes().put(CHILDREN_HOLDER_ATTRIBUTE,
this);
+ }
+
+ public void unregister() {
+ Map<String, Object> attributes = component.getAttributes();
+
+ if (this.initialAttributeValue != null) {
+ attributes.put(CHILDREN_HOLDER_ATTRIBUTE, initialAttributeValue);
+ } else {
+ attributes.remove(CHILDREN_HOLDER_ATTRIBUTE);
+ }
+ }
+
+ public static PanelMenuRendererBaseChildrenHolder getChildrenHolder(UIPanelMenu
panelMenu) {
+ return (PanelMenuRendererBaseChildrenHolder)
+ panelMenu.getAttributes().get(CHILDREN_HOLDER_ATTRIBUTE);
+ }
+
+ void registerChild(UIComponent child, int level) {
+ this.children.add(new Item(child, level));
+ }
+
+ public List<PanelMenuRendererBaseChildrenHolder.Item> getChildren() {
+ return children;
+ }
+}
\ No newline at end of file
Modified:
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/html/PanelMenuRenderer.java
===================================================================
---
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/html/PanelMenuRenderer.java 2009-03-20
19:01:25 UTC (rev 13084)
+++
trunk/ui/panelmenu/src/main/java/org/richfaces/renderkit/html/PanelMenuRenderer.java 2009-03-20
20:07:56 UTC (rev 13085)
@@ -23,10 +23,8 @@
import java.io.IOException;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -39,20 +37,24 @@
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.ScriptUtils;
import org.ajax4jsf.renderkit.AjaxRendererUtils;
+import org.ajax4jsf.renderkit.ComponentVariables;
+import org.ajax4jsf.renderkit.ComponentsVariableResolver;
import org.ajax4jsf.renderkit.RendererUtils.HTML;
import org.richfaces.component.UIPanelMenu;
import org.richfaces.component.UIPanelMenuGroup;
import org.richfaces.component.UIPanelMenuItem;
+import org.richfaces.renderkit.PanelMenuRendererBaseChildrenHolder;
import org.richfaces.renderkit.PanelMenuRendererBase;
public class PanelMenuRenderer extends PanelMenuRendererBase {
- /* (non-Javadoc)
- * @see org.ajax4jsf.framework.renderer.RendererBase#getComponentClass()
- */
+ private static final String CHILDREN_HOLDER = "childrenHolder";
private static final String FIRST_EXPANDED_ENCODED = "firstExpandedEncoded";
+ /* (non-Javadoc)
+ * @see org.ajax4jsf.framework.renderer.RendererBase#getComponentClass()
+ */
protected Class<? extends UIComponent> getComponentClass() {
return UIComponent.class;
}
@@ -104,94 +106,91 @@
public void insertScript(FacesContext context, UIComponent component)
throws IOException {
-
StringBuffer buffer = new StringBuffer();
Set<String> itemNames = new HashSet<String>();
UIPanelMenu panelMenu = (UIPanelMenu)component;
- Map<String, Integer> levels = new HashMap<String, Integer>();
- List<UIComponent> flatList = new LinkedList<UIComponent>();
- flatten(component.getChildren(), flatList, levels, 0);
-
- for (UIComponent child : flatList) {
- if (!(child instanceof UIPanelMenuItem || child instanceof UIPanelMenuGroup)) {
- continue;
- }
-
- boolean parentRendered = child.getParent().isRendered();
- if (!parentRendered) {
- child.getAttributes().put("rendered",Boolean.FALSE);
- }
-
- boolean childRendered = child.isRendered();
- if (!childRendered || !parentRendered || isParentDisabled(child)/* ||
!isParentExpended(child)*/) {
- continue;
- }
-
- boolean childDisabled = panelMenu.isDisabled()
- || child instanceof UIPanelMenuGroup ? ((UIPanelMenuGroup)child).isDisabled() :
((UIPanelMenuItem)child).isDisabled();
-
-
- String childName;
- if(child instanceof UIPanelMenuGroup){
- childName = ((UIPanelMenuGroup)child).getName();
- } else {
- childName = ((UIPanelMenuItem)child).getName();
- }
-
- if(itemNames.contains(childName)){
- throw new RuntimeException("Attibute \"name\" with value \""
+ childName + "\" is already used in PanelMenu. It must be unique for every
group/item.");
- } else {
- itemNames.add(childName);
- }
-
- buffer.append("var params = new Object();");
+ ComponentVariables variables = ComponentsVariableResolver.getVariables(this,
component);
+ List<PanelMenuRendererBaseChildrenHolder.Item> childrenItems =
(List<PanelMenuRendererBaseChildrenHolder.Item>)
variables.getVariable(CHILDREN_HOLDER);
+ if (childrenItems != null) {
+ for (PanelMenuRendererBaseChildrenHolder.Item childItem : childrenItems) {
+ UIComponent child = childItem.getComponent();
+
+ boolean parentRendered = child.getParent().isRendered();
+ if (!parentRendered) {
+ child.getAttributes().put("rendered",Boolean.FALSE);
+ }
+
+ boolean childRendered = child.isRendered();
+ if (!childRendered || !parentRendered || isParentDisabled(child)/* ||
!isParentExpended(child)*/) {
+ continue;
+ }
+
+ boolean childDisabled = panelMenu.isDisabled()
+ || child instanceof UIPanelMenuGroup ? ((UIPanelMenuGroup)child).isDisabled() :
((UIPanelMenuItem)child).isDisabled();
+
+
+ String childName;
+ if(child instanceof UIPanelMenuGroup){
+ childName = ((UIPanelMenuGroup)child).getName();
+ } else {
+ childName = ((UIPanelMenuItem)child).getName();
+ }
+
+ if(itemNames.contains(childName)){
+ throw new RuntimeException("Attibute \"name\" with value
\"" + childName + "\" is already used in PanelMenu. It must be unique
for every group/item.");
+ } else {
+ itemNames.add(childName);
+ }
+
+ buffer.append("var params = new Object();");
- if (child instanceof UIPanelMenuItem) {
- for (String param : encodeParams(context, (UIPanelMenuItem)child)) {
- buffer.append(param);
+ if (child instanceof UIPanelMenuItem) {
+ for (String param : encodeParams(context, (UIPanelMenuItem)child)) {
+ buffer.append(param);
+ }
}
+
+ buffer.append("new PanelMenuItem(ids, params,")
+
.append("{myId:'").append(child.getClientId(context)).append("',")
+
.append("parentId:'").append(child.getParent().getClientId(context)).append("'},")
+ .append("{type:").append(child instanceof UIPanelMenuItem ?
"\"item\"":"\"node\"").append(",")
+ .append("onopen:").append(getOnOpen(panelMenu,
child)).append(",")
+ .append("onclose:").append(getNoClose(panelMenu,
child)).append(",")
+
.append("event:\"").append(getEvent(panelMenu)).append("\",")
+
.append("mode:\"").append(getItemMode(child)).append("\",")
+ .append("disabled:").append(childDisabled).append(",")
+
.append("target:\"").append(getTarget(child)).append("\",")
+ .append("name:\"").append(childName).append("\"")
+ .append("},{").append(getHoveredStyle(panelMenu,
child)).append("},")
+ .append(getHoverClass(panelMenu, child))
+ .append(childItem.getLevel())
+ .append(switchOnImagesIfNeeded(context,child));
+
+ addActionIfNeeded(context,child,buffer);
+
+ setExpandedIfNeeded(context,child,buffer);
+
+ addAjaxFunction(context,child,buffer);
+
+ addOnItemHover(panelMenu.getOnitemhover(), child, buffer);
+
+ String iconPos;
+ boolean isTopLevel = isTopLevel(child);
+ if(child instanceof UIPanelMenuGroup){
+ iconPos = isTopLevel ? panelMenu.getIconGroupTopPosition() :
panelMenu.getIconGroupPosition();
+ } else {
+ iconPos = isTopLevel ? panelMenu.getIconItemTopPosition() :
panelMenu.getIconItemPosition();
+ }
+
+ buffer.append(",\""+iconPos+'"');
+
+ addImages(buffer, context, child, component.getClientId(context).toString());
+
+ buffer.append(");\n");
}
-
- buffer.append("new PanelMenuItem(ids, params,")
-
.append("{myId:'").append(child.getClientId(context)).append("',")
-
.append("parentId:'").append(child.getParent().getClientId(context)).append("'},")
- .append("{type:").append(child instanceof UIPanelMenuItem ?
"\"item\"":"\"node\"").append(",")
- .append("onopen:").append(getOnOpen(panelMenu,
child)).append(",")
- .append("onclose:").append(getNoClose(panelMenu,
child)).append(",")
-
.append("event:\"").append(getEvent(panelMenu)).append("\",")
-
.append("mode:\"").append(getItemMode(child)).append("\",")
- .append("disabled:").append(childDisabled).append(",")
-
.append("target:\"").append(getTarget(child)).append("\",")
- .append("name:\"").append(childName).append("\"")
- .append("},{").append(getHoveredStyle(panelMenu,
child)).append("},")
- .append(getHoverClass(panelMenu, child))
- .append(levels.get(child.getClientId(context)))
- .append(switchOnImagesIfNeeded(context,child));
-
- addActionIfNeeded(context,child,buffer);
-
- setExpandedIfNeeded(context,child,buffer);
-
- addAjaxFunction(context,child,buffer);
-
- addOnItemHover(panelMenu.getOnitemhover(), child, buffer);
-
- String iconPos;
- boolean isTopLevel = isTopLevel(child);
- if(child instanceof UIPanelMenuGroup){
- iconPos = isTopLevel ? panelMenu.getIconGroupTopPosition() :
panelMenu.getIconGroupPosition();
- } else {
- iconPos = isTopLevel ? panelMenu.getIconItemTopPosition() :
panelMenu.getIconItemPosition();
- }
-
- buffer.append(",\""+iconPos+'"');
-
- addImages(buffer, context, child, component.getClientId(context).toString());
-
- buffer.append(");\n");
}
ResponseWriter writer = context.getResponseWriter();
@@ -287,27 +286,6 @@
return panelMenu;
}
- public void flatten(List<UIComponent> children, List<UIComponent> flatList,
- Map<String, Integer> levels, int initialLevel) {
-
- FacesContext context = FacesContext.getCurrentInstance();
- if (children == null) {
- return;
- }
-
- for (Iterator<UIComponent> iter = children.iterator(); iter.hasNext();) {
- UIComponent child = iter.next();
- if (child instanceof UIPanelMenu){
- continue;
- }
-
- flatList.add(child);
- levels.put(child.getClientId(context), initialLevel);
-
- flatten(child.getChildren(), flatList, levels, initialLevel + 1);
- }
- }
-
private String switchOnImagesIfNeeded(FacesContext context, UIComponent child)throws
IOException {
boolean isToplevel = isTopLevel(child);
String customIconOpened = "";
@@ -524,33 +502,42 @@
}
buffer.append("\"");
}
-
+
public void renderChildren(FacesContext facesContext, UIComponent component)throws
IOException {
if(!(component instanceof UIPanelMenu)) {
return;
}
-
+
UIPanelMenu panelMenu = (UIPanelMenu)component;
- for (Iterator<UIComponent> it = component.getChildren().iterator();
it.hasNext();) {
- UIComponent child = (UIComponent) it.next();
- if (child instanceof UIPanelMenuGroup) {
- UIPanelMenuGroup group = (UIPanelMenuGroup)child;
+ PanelMenuRendererBaseChildrenHolder childrenHolder = new
PanelMenuRendererBaseChildrenHolder(panelMenu);
+
+ try {
+ for (Iterator<UIComponent> it = component.getChildren().iterator();
it.hasNext();) {
+ UIComponent child = (UIComponent) it.next();
+ if (child instanceof UIPanelMenuGroup) {
+ UIPanelMenuGroup group = (UIPanelMenuGroup)child;
- if (panelMenu.isExpandSingle()) {
- if (!(Boolean)panelMenu.getAttributes().get(FIRST_EXPANDED_ENCODED)) {
- if (group.isExpanded()) {
- panelMenu.getAttributes().put(FIRST_EXPANDED_ENCODED, true);
+ if (panelMenu.isExpandSingle()) {
+ if (!(Boolean)panelMenu.getAttributes().get(FIRST_EXPANDED_ENCODED)) {
+ if (group.isExpanded()) {
+ panelMenu.getAttributes().put(FIRST_EXPANDED_ENCODED, true);
+ }
+ } else {
+ group.setExpanded(false);
+ if ((Boolean)group.getValue()) {
+ group.setValue(null);
+ }
}
- } else {
- group.setExpanded(false);
- if ((Boolean)group.getValue()) {
- group.setValue(null);
- }
- }
- }
+ }
+ }
+ renderChild(facesContext, child);
}
- renderChild(facesContext, child);
- }
+ } finally {
+ childrenHolder.unregister();
+ }
+
+ ComponentVariables variables = ComponentsVariableResolver.getVariables(this,
component);
+ variables.setVariable(CHILDREN_HOLDER, childrenHolder.getChildren());
}
public void doDecode(FacesContext context, UIComponent component) {
Modified: trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuGroup.jspx
===================================================================
--- trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuGroup.jspx 2009-03-20
19:01:25 UTC (rev 13084)
+++ trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuGroup.jspx 2009-03-20
20:07:56 UTC (rev 13085)
@@ -13,6 +13,8 @@
<f:clientid var="clientId"/>
+ <f:call name="registerComponent" />
+
<div id="#{clientId}" style="#{this:getHideStyle(context,
component)}"
class="#{this:getDivClass(context, component)}" >
<jsp:scriptlet>
Modified: trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuItem.jspx
===================================================================
--- trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuItem.jspx 2009-03-20
19:01:25 UTC (rev 13084)
+++ trunk/ui/panelmenu/src/main/templates/org/richfaces/htmlPanelMenuItem.jspx 2009-03-20
20:07:56 UTC (rev 13085)
@@ -13,6 +13,8 @@
<f:clientid var="clientId"/>
+ <f:call name="registerComponent" />
+
<div id="#{clientId}" style="#{this:getHideStyle(context,
component)}" >
<table cellspacing="0" cellpadding="0" border="0"
id="tablehide#{clientId}"