Package net.sf.jwan.jsf.renderer

Source Code of net.sf.jwan.jsf.renderer.TabbedPaneRenderer

package net.sf.jwan.jsf.renderer;

import java.io.IOException;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.faces.render.Renderer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import net.sf.jwan.jsf.ui.UITabbedPane;
import net.sf.jwan.jsf.util.Renderers;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TabbedPaneRenderer extends Renderer
{
  static Log logger = LogFactory.getLog(TabbedPaneRenderer.class);
 
  public boolean getRendersChildren(){return true;}
 
  public void decode(FacesContext context, UIComponent component)
  {
    logger.debug("decode");
    Map<String,String> requestMap = context.getExternalContext().getRequestParameterMap();
    String clientId = component.getClientId(context);
   
    String content = (String)(requestMap.get(clientId));
    if(content!=null && !content.equals(""))
    {
      UITabbedPane tabbedPane = (UITabbedPane)component;
      tabbedPane.setContent(content);
    }
    component.queueEvent(new ActionEvent(component));
  }
 
  public void encodeBegin(FacesContext context, UIComponent component) throws IOException
  {
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("table", component);
   
    String styleClass = (String)component.getAttributes().get("styleClass");
    if(styleClass!=null){writer.writeAttribute("class", styleClass, null);}
   
    writer.write("\n");
  }
 
  public void encodeEnd(FacesContext context, UIComponent component) throws IOException
  {
    ResponseWriter writer = context.getResponseWriter();
    UITabbedPane tabbedPane = (UITabbedPane)component;
    String content = tabbedPane.getContent();
   
    writer.startElement("tbody", component);
    writer.startElement("tr", component);
    writer.startElement("td", component);
   
    if(content!=null)
    {
      UIComponent facet = component.getFacet(content);
      if(facet!=null)
      {
        if(facet.isRendered())
        {
          facet.encodeBegin(context);
          if(facet.getRendersChildren()){facet.encodeChildren(context);}
          facet.encodeEnd(context);
        }
      }
      else {includePage(context,component);}
    }
   
    writer.endElement("td");
    writer.endElement("tr");
    writer.endElement("tbody");
    writer.endElement("table");
   
    encodeHiddenField(context,writer,component);
  }
 
  public void encodeChildren(FacesContext context, UIComponent component) throws IOException
  {
    if(component.getChildCount()==0){return;}
    ResponseWriter writer = context.getResponseWriter();
   
    writer.startElement("thead", component);
    writer.startElement("tr", component);
    writer.startElement("th", component);
   
    writer.startElement("table", component);
    writer.startElement("tbody", component);
    writer.startElement("tr", component);
   
    for(SelectItem item : Renderers.getSelectItems(component))
    {
      encodeTab(context,writer,item,component);
    }
   
    writer.endElement("tr");
    writer.endElement("tbody");
    writer.endElement("table");
   
    writer.endElement("th");
    writer.endElement("tr");
    writer.endElement("thead");
    writer.write("\n");
  }
 
  private void encodeHiddenField(FacesContext context, ResponseWriter writer, UIComponent component) throws IOException
  {
    writer.startElement("input", component);
    writer.writeAttribute("type", "hidden", null);
    writer.writeAttribute("name", component.getClientId(context),null);
    writer.endElement("input");
  }
 
  private void encodeTab(FacesContext context, ResponseWriter writer, SelectItem item, UIComponent component) throws IOException
  {
    String content = (String)item.getValue();
   
    writer.startElement("td", component);
    writer.startElement("a", component);
    writer.writeAttribute("href", "#", "href");
   
    String clientId = component.getClientId(context);
    String formId = Renderers.getFormId(context,component);
   
    writer.writeAttribute("onclick",
                 "document.forms['"+formId+"']['"+clientId+"'].value='"+content+"';"+
                 "document.forms['"+formId+"'].submit();",null);
    UITabbedPane tabbedPane = (UITabbedPane)component;
    String selectedContent= tabbedPane.getContent();
   
    String tabClass=null;
    if(content.equals(selectedContent)){tabClass = (String)component.getAttributes().get("selectedTabClass");}
    else{tabClass = (String)component.getAttributes().get("tabClass");}
    if(tabClass!=null){writer.writeAttribute("class", tabClass, null);}
   
    writer.write(item.getLabel());
   
    writer.endElement("a");
    writer.endElement("td");
    writer.write("\n");
  }
 
  private void includePage(FacesContext fc, UIComponent component)
  {
    ExternalContext ec = fc.getExternalContext();
    ServletContext sc = (ServletContext)ec.getContext();
    UITabbedPane tabbedPane = (UITabbedPane)component;
    String content = tabbedPane.getContent();
   
    ServletRequest request = (ServletRequest)ec.getRequest();
    ServletResponse response = (ServletResponse)ec.getResponse();
    try
    {
      sc.getRequestDispatcher(content).include(request, response);
    }
    catch (ServletException e){logger.warn("Couldn't load page: "+content,e);}
    catch (IOException e){logger.warn("Couldn't load page: "+content,e);}
  }
}
TOP

Related Classes of net.sf.jwan.jsf.renderer.TabbedPaneRenderer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.