1 Reply Latest reply on Dec 14, 2007 5:53 AM by Siarhei Kudzelia

    a4j help

    Phil Brown Newbie

      I'm trying to create a jsf custom table component extending HtmlDataTable. I need to implement pagination so I want to incorporate command links for first, last, next, and prev. How do I get the commandLinks to fire the methods in the component to update its attributes and reRender the entire component. I was moving in the direction of using A4J, but could not get the reRendering to occur. Here is what I have so far.

      
      /*
       * Created on Jul 19, 2004
       *
       */
      package net.em.com.components;
      
      import java.util.ArrayList;
      import javax.el.ValueExpression;
      import javax.faces.context.FacesContext;
      import javax.faces.component.html.HtmlDataTable;
      import javax.faces.component.html.HtmlOutputText;
      import net.em.com.tags.table.ListAlgorithm;
      import org.ajax4jsf.ajax.html.HtmlAjaxCommandLink;
      import org.ajax4jsf.framework.ajax.AjaxEvent;
      import org.ajax4jsf.framework.ajax.AjaxListener;
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      
      public class UITable extends HtmlDataTable implements AjaxListener {
      
       private static final Log log = LogFactory.getLog(UITable.class);
      
       // To support commands on the data prior to sorting and pagination
       private Class algorithm = null;
       private String algorithmCriteria = "";
       private boolean algorithmApplied = false ;
      
       // To Support pagination
       private boolean paginate = false ;
       private int pageSize = 10 ;
       private int currentPage = 1 ;
       private String firstTxt = "First" ;
       private String lastTxt = "Last" ;
      
       //To Support AJAX
       private String reRender ;
       private String eventsQueue = "dummy" ;
       private int requestDelay = 10 ;
      
       public void processAjax(AjaxEvent event) {
       Object j = event ;
       }
      
       public Object saveState(FacesContext context) {
       return super.saveState(context);
       }
       public void restoreState(FacesContext context, Object state) {
       super.setRendered(true) ;
       this.currentPage = 3 ;
       super.restoreState(context, state);
      
       }
       public String getFamily(){
       return "em.EmTable";
       }
       public String getRendererType(){
       return "javax.faces.Table";
       }
      
       public String getAlgorithmCriteria() {
       return algorithmCriteria;
       }
      
       public void setAlgorithmCriteria(String algorithmCriteria) {
       this.algorithmCriteria = algorithmCriteria;
       }
      
       public String getAlgorithmClass() {
       return (this.algorithm != null) ? this.algorithm.toString() : "";
       }
      
       public void setAlgorithmClass(String val) {
       if(val.length() > 0) {
       try {
       this.algorithm = Class.forName(val);
       if(!IterableAlgorithm.class.isAssignableFrom(this.algorithm)) {
       this.algorithm = null;
       }
       } catch (Exception e) {
       this.algorithm = null;
       }
       } else {
       this.algorithm = null;
       }
       if(this.algorithm == null) {
       log.info(this.getClass().toString() + ":setAlgorithm(String className)");
       log.info(val + "not of type: " + ListAlgorithm.class.toString());
       } else {
       this.algorithmApplied = false ;
       }
       }
      
       public void setValue(Object value) {
       this.algorithmApplied = false ;
       if(!Iterable.class.isAssignableFrom(value.getClass())) {
       ArrayList list = new ArrayList() ;
       list.add(value) ;
       super.setValue(list) ;
       } else {
       super.setValue(value) ;
       }
       }
      
       public Object getValue() {
       if(this.algorithmApplied == false && this.algorithm != null) {
       this.algorithmApplied = true ;
       setValue(applyAlgorithm((Iterable)super.getValue())) ;
       }
       if(isPaginate()){
       setRows(this.getPageSize());
       }
       //return retVal ;
       return super.getValue();
       }
      
       public void next() {
       setFirst(getFirst() + getPageSize());
       setCurrentPage(getCurrentPage() + 1);
       //enable prev
       if((getFirst() + getPageSize()) >= getRows()){
       //no more rows to display, disable "next" button?
       }
       }
       public void prev() {
      
      
       if((getFirst() - getPageSize()) < 0){
       setFirst(0);
       setCurrentPage(1);
       //hit start of table, diable "prev" button?
       }else{
       setFirst(getFirst() - getPageSize());
       setCurrentPage(getCurrentPage() - 1);
       //enable next
       }
       }
       public void last() {
       this.setFirst((getRows()/getPageSize()) * getPageSize());
       this.setCurrentPage(getRows()/getPageSize());
       //disable next, enable prev
      
       }
       public void first() {
       setFirst(0);
       setCurrentPage(1);
      
       //disable prev, enable next
       }
       public int getCurrentPage() {
       return (Integer)getProperty("currentPage", this.currentPage) ;
       }
      
       public void setCurrentPage(int currentPage) {
       this.currentPage = currentPage;
       }
      
       private Iterable applyAlgorithm(Iterable original) {
       this.algorithmApplied = true;
       Iterable results;
       try {
       IterableAlgorithm a = (IterableAlgorithm)this.algorithm.newInstance();
       a.setCriteria(this.getAlgorithmCriteria());
       results = a.Manipulate(original);
       } catch (Exception e) {
       e.printStackTrace();
       log.info(this.getClass().toString() + ":applyAlgorithm()");
       log.info(this.algorithm.toString() + " must support non parametized constructor" );
       log.info("algorithm not applied");
      
       results = original;
       }
       return results;
       }
      
       public boolean isPaginate() {
       return (Boolean)getProperty("paginate", this.paginate) ;
       }
      
       public void setPaginate(boolean paginate) {
       this.paginate = paginate;
       }
      
       public int getPageSize() {
       return (Integer)getProperty("pageSize", this.pageSize) ;
       }
      
       public void setPageSize(int pageSize) {
       this.pageSize = pageSize;
       }
      
       private Object getProperty(String att, Object field) {
       if (field != null) return field;
       ValueExpression ve = this.getValueExpression(att);
       if (ve != null) return (String) ve.getValue(this.getFacesContext().getELContext());
       return null;
       }
      
       public HtmlAjaxCommandLink getCmdLnkLast(FacesContext facesCtx) {
      
       HtmlAjaxCommandLink cmdLnkLast = (HtmlAjaxCommandLink)getComponent(facesCtx, HtmlAjaxCommandLink.COMPONENT_TYPE) ;
       cmdLnkLast.setParent(this) ;
       cmdLnkLast.setId(this.getId() + "_LAST");
       cmdLnkLast.setReRender(this.getId()) ;
       cmdLnkLast.setRequestDelay(this.requestDelay) ;
       cmdLnkLast.setEventsQueue(this.eventsQueue) ;
      
       HtmlOutputText text = (HtmlOutputText)getComponent(facesCtx, HtmlOutputText.COMPONENT_TYPE) ;
       text.setValue(this.lastTxt) ;
      
       cmdLnkLast.getChildren().add(text) ;
      
       return cmdLnkLast;
       }
      
       private Object getComponent(FacesContext facesCtx, String name) {
       return facesCtx.getApplication().createComponent(name) ;
       }
      
       public void setReRender(String reRender) {
       this.reRender = reRender;
       }
      }
      
      
      /*
       * Created on Jul 19, 2004
       *
       */
      package net.em.com.components;
      
      import com.sun.faces.renderkit.html_basic.TableRenderer;
      import java.io.IOException;
      import javax.faces.component.UIComponent;
      import javax.faces.context.FacesContext;
      import javax.faces.render.Renderer;
      
      public class UITableRenderer extends TableRenderer {
      
      
       public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
       UITable table = (UITable)component ;
      
       render(context, table.getCmdLnkLast(context)) ;
      
       super.encodeBegin(context, component) ;
       }
       public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
       super.encodeEnd(context, component) ;
       }
       public void decode(FacesContext context, UIComponent component) {
       super.decode(context, component) ;
       }
      
       private void render(FacesContext context, UIComponent component) throws IOException {
       Renderer renderer = getRendererFor(context, component) ;
       renderer.encodeBegin(context, component) ;
       renderer.encodeChildren(context, component) ;
       renderer.encodeEnd(context, component) ;
       }
      
       private Renderer getRendererFor(FacesContext context, UIComponent c) {
       return context.getRenderKit().getRenderer(c.getFamily(), c.getRendererType());
       }
      
      }