/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.renderer.tree;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.swing.tree.DefaultMutableTreeNode;
import org.xulfaces.SmoothlyUpdateable;
import org.xulfaces.annotation.faces.RENDERER;
import org.xulfaces.bridge.Bridge;
import org.xulfaces.bridge.Zone;
import org.xulfaces.bridge.command.UpdateZoneCommand;
import org.xulfaces.component.tree.TreeItemComponent;
import org.xulfaces.component.tree.TreeRowComponent;
import org.xulfaces.renderer.XULRenderer;
import org.xulfaces.utils.XulUtils;
/**
*
* @author kito31
* @version $Id: TreeItemRenderer.java,v 1.12 2007/04/09 20:53:52 kito31 Exp $
*/
@RENDERER(kit="XUL_RENDERKIT",family="xul.component.family",type="xul.renderer.TreeItem")
public class TreeItemRenderer extends XULRenderer {
public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
ResponseWriter responseWriter = facesContext.getResponseWriter();
responseWriter.startElement("treeitem",component);
TreeItemComponent treeItemComponent = (TreeItemComponent) component;
DefaultMutableTreeNode treeNode = treeItemComponent.getCurrentTreeNode();
String nodeId = computeId(facesContext,treeItemComponent);
treeItemComponent.setNodeId(nodeId);
if(!isParentSmoothlyUpdateable(component)){
if (treeItemComponent instanceof SmoothlyUpdateable) {
SmoothlyUpdateable smoothlyUpdateable = (SmoothlyUpdateable) treeItemComponent;
if (smoothlyUpdateable.needsUpdate()) {
Bridge bridge = XulUtils.getBridge();
Zone zone = new Zone(nodeId);
bridge.addCommand(new UpdateZoneCommand(zone,buildTargetName(component)));
}
}
}
responseWriter.writeAttribute("id",nodeId, "id");
if(treeNode.getChildCount() > 0){
responseWriter.writeAttribute("container","true","container");
}
renderAttributes(facesContext,component,(List) component.getAttributes().get("annotatedAttributes"));
// Render open attribute
Map paramMap = facesContext.getExternalContext().getRequestParameterMap();
if (paramMap.containsKey(nodeId)) {
String value = (String) paramMap.get(nodeId);
Boolean booleanValue = treeItemComponent.getOpen();
if(booleanValue != null){
if(booleanValue.toString(booleanValue.booleanValue()).equals(value)){
renderAttribute(facesContext,component,"open");
}
}
}
else {
renderAttribute(facesContext,component,"open");
}
}
private String computeId(FacesContext facesContext, TreeItemComponent treeItemComponent) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(treeItemComponent.getParent().getClientId(facesContext));
stringBuffer.append(":");
stringBuffer.append("ti_");
stringBuffer.append(treeItemComponent.getPath());
return stringBuffer.toString();
}
public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
ResponseWriter responseWriter = facesContext.getResponseWriter();
responseWriter.endElement("treeitem");
}
public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException {
List children = component.getChildren();
TreeRowComponent treeRowComponent = (TreeRowComponent) children.get(0);
if (treeRowComponent.isRendered()) {
renderChild(facesContext,treeRowComponent);
}
}
@Override
public boolean getRendersChildren() {
return true;
}
}