Package org.xulfaces.renderer.listbox

Source Code of org.xulfaces.renderer.listbox.DataListBoxRenderer

/*
*   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.listbox;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xulfaces.annotation.faces.RENDERER;
import org.xulfaces.component.listbox.DataListBoxComponent;
import org.xulfaces.component.listbox.ListColsComponent;
import org.xulfaces.component.listbox.ListHeadComponent;
import org.xulfaces.component.listbox.ListItemComponent;
import org.xulfaces.renderer.XULRenderer;

/**
*
* @author kito31
* @version $Id$
*/
@RENDERER(kit="XUL_RENDERKIT",family="xul.component.family",type="xul.renderer.DataListBox")
public class DataListBoxRenderer extends XULRenderer {
 
  private static final Log log = LogFactory.getLog(DataListBoxRenderer.class);

  public void decode(FacesContext facesContext, UIComponent component) {

    DataListBoxComponent listBoxComponent = (DataListBoxComponent) component;
    listBoxComponent.setSelectedRows(null);

    if (isSubmitted(facesContext, component)) {

      Map paramMap = facesContext.getExternalContext().getRequestParameterMap();
      String clientId = component.getClientId(facesContext);

      String selection = (String) paramMap.get(clientId);
     
      if(log.isDebugEnabled()){
        log.debug("DataListBoxComponent " + component.getId() + " selection is " + selection);
      }
     
      if ((selection != null) && (selection.length() > 0)) {

        String selections[] = selection.split(" ");
        if (selections != null) {
          List<Integer> selectedRows = new ArrayList<Integer>();
          for(int i= 0; i < selections.length;i++){
            String rowId = selections[i];
            int lastIndex = rowId.lastIndexOf(":");
            if(lastIndex != -1){
              int rowIndex = Integer.parseInt(rowId.substring(lastIndex+1));
              listBoxComponent.setRowIndex(rowIndex);
              if(listBoxComponent.isRowAvailable()){
                selectedRows.add(rowIndex);
              }
            }
          }
          listBoxComponent.setSelectedRows(selectedRows);           
        }
      }     
    }
  }       
 
  public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
    super.encodeBegin(facesContext,component);
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    responseWriter.startElement("listbox", component);
    responseWriter.writeAttribute("id", component.getClientId(facesContext), "id");
    renderAttributes(facesContext, component, (List) component
        .getAttributes().get("annotatedAttributes"));
  }

 
  public void encodeEnd(FacesContext facesContext, UIComponent arg1) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    responseWriter.endElement("listbox");
  }

  public void encodeChildren(FacesContext facesContext, UIComponent component) throws IOException {
    DataListBoxComponent listBoxComponent = (DataListBoxComponent) component;     
    encodeListHead(facesContext,listBoxComponent);
    encodeListCols(facesContext,listBoxComponent);
    encodeListItem(facesContext,listBoxComponent);           
  }

 
  protected void encodeListHead(FacesContext facesContext, DataListBoxComponent listBoxComponent) throws IOException{
    List<UIComponent> children = listBoxComponent.getChildren();
    if(!children.isEmpty()){
      for(UIComponent child:children){
        if(child instanceof ListHeadComponent){
          renderChild(facesContext, child);
        }
      }
    }
  }

  protected void encodeListCols(FacesContext facesContext, DataListBoxComponent listBoxComponent) throws IOException{
    List<UIComponent> children = listBoxComponent.getChildren();
    if(!children.isEmpty()){
      for(UIComponent child:children){
        if(child instanceof ListColsComponent){
          renderChild(facesContext, child);
        }
      }
    }
  }
 
  protected void encodeListItem(FacesContext facesContext, DataListBoxComponent listBoxComponent) throws IOException{

    List<UIComponent> children = listBoxComponent.getChildren();
    if(!children.isEmpty()){
      int first = listBoxComponent.getFirst();
      int rows = listBoxComponent.getRows();
      int rowCount = listBoxComponent.getRowCount();
     
      Object value = listBoxComponent.getValue();
     
      if (value != null) {            
        if (value instanceof Collection) {       
         
          if (rows <= 0) {
            rows = rowCount - first;
          }       
          int last = first + rows;
         
          if (last > rowCount) {
            last = rowCount;
          }
         
          for (int i = first; i < last; i++) {
            listBoxComponent.setRowIndex(i);
            if (!listBoxComponent.isRowAvailable()) {
              return;
            }
            Object object = listBoxComponent.getRowData();         
            for(UIComponent child:children){
              if(child instanceof ListItemComponent){
                child.getAttributes().put("rowId",(new Integer(i)).toString());
                renderChild(facesContext, child);
              }
            }               
          }
        }       
      }     
    }       
  }
 
 
 
 
  protected void encodeRow(FacesContext facesContext, DataListBoxComponent listBoxComponent, Object value)
      throws IOException {
   
    List<UIComponent> children = listBoxComponent.getChildren();
    for (UIComponent child : children) {
      if(child instanceof ListColsComponent){
        List<UIComponent> listcolsChildren = child.getChildren();
        for (UIComponent listcol : listcolsChildren) {
          List<UIComponent> listcolChildren =listcol.getChildren();
          for(UIComponent listcolChild: listcolChildren){
            renderChild(facesContext, listcolChild)
          }         
        } 
      }     
    }
  }

  public boolean getRendersChildren() {
    return true;
  }
}
TOP

Related Classes of org.xulfaces.renderer.listbox.DataListBoxRenderer

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.