/* * based off of HSTable */ package net.toodarkpark.taglib.html; import java.util.*; // import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.struts.util.PropertyUtils; import org.apache.struts.util.RequestUtils; import org.apache.struts.util.ResponseUtils; public class HSTableTag extends BodyTagSupport { protected Iterator iterator = null; protected int maxColumns=0; protected Collection collection=null; protected String id=null; protected String type=null; protected String name=null; protected String scope=null; protected String property=null; // alter the appearance of the table protected String backgroundColor=null; protected String oddRowBackgroundColor=null; protected String width=null; protected String height=null; protected String cellspacing=null; protected String cellpadding=null; protected String border=null; protected String collspan=null; protected String cellwidth=null; protected String cellalign=null; protected String cellvalign=null; // keeping track of the Collection protected int currentItemIndex=-1; protected int maxItems=-1; // keeping track of where we are in the table protected int currentColumn=-1; protected int currentRow=-1; // the collection we're iterating through public Collection getCollection() { return collection; } public void setCollection( Collection value ) { collection = value; maxItems = collection.size(); iterator = collection.iterator(); } public int getMaxColumns() { return maxColumns; } public void setMaxColumns( int value ) { maxColumns = value; } public String getId() { return id; } public void setId( String value ) { id = value; } public String getType() { return type; } public void setType( String value ) { type = value; } public String getName() { return name; } public void setName( String value ) { name = value; } public String getScope() { return scope; } public void setScope( String value ) { scope = value; } public String getProperty() { return property; } public void setProperty( String value ) { property = value; } public String getBackgroundColor() { return backgroundColor; } public void setBackgroundColor( String value ) { backgroundColor = value; } public String getOddRowBackgroundColor() { return oddRowBackgroundColor; } public void setOddRowBackgroundColor( String value ) { oddRowBackgroundColor = value; } public String getWidth() { return width; } public void setWidth( String value ) { width = value; } public String getHeight() { return height; } public void setHeight( String value ) { height = value; } public String getBorder() { return border; } public void setBorder( String value ) { border = value; } public String getCellspacing() { return cellspacing; } public void setCellspacing( String value ) { cellspacing = value; } public String getCellpadding() { return cellpadding; } public void setCellpadding( String value ) { cellpadding = value; } public String getCollspan() { return collspan; } public void setCollspan( String value ) { collspan = value; } public String getCellwidth() { return cellwidth; } public void setCellwidth( String value ) { cellwidth = value; } public String getCellalign() { return cellalign; } public void setCellalign( String value ) { cellalign = value; } public String getCellvalign() { return cellvalign; } public void setCellvalign( String value ) { cellvalign = value; } // reset everything public void release() { super.release(); collection = null; iterator = null; name = null; property = null; scope = null; type = null; maxItems = -1; id = null; currentItemIndex = -1; maxColumns = -1; } /** methods that actually do something important **/ public int doStartTag() throws JspException { StringBuffer sb = new StringBuffer(); // get the collection we're iterating through if( this.getCollection() == null ) { Collection coll = (Collection)RequestUtils.lookup( pageContext, this.getName(), this.getProperty(), this.getScope() ); if( coll == null ) { JspException e = new JspException( "Couldn't find a collection to iterate through." ); RequestUtils.saveException( pageContext, e ); throw e; } else { this.setCollection( coll ); } } // begin our table sb.append( "\n" ); // write the begin table ResponseUtils.writePrevious( pageContext, sb.toString() ); // start off a new row this.printStartRow(); // start off with an item if( iterator.hasNext() ) { Object currentObject = iterator.next(); if( currentObject == null ) { pageContext.removeAttribute( this.getId() ); } else { // print out a cell this.printStartCell(); // push things back to the parent pageContext.setAttribute( this.getId(), currentObject ); currentRow = 1; currentColumn = 1; currentItemIndex = 0; } return( EVAL_BODY_TAG ); } else { // collection has no elements; skip outputting things. return( SKIP_BODY ); } } private void printStartRow() throws JspException { StringBuffer sb = new StringBuffer(); boolean rowIsOdd = currentRow % 2 > 0; // start off a new row sb.append( " \n" ); currentRow++; ResponseUtils.writePrevious( pageContext, sb.toString() ); } private void printStartCell() throws JspException { StringBuffer sb = new StringBuffer(); sb.append( " \n" ); currentColumn++; ResponseUtils.writePrevious( pageContext, sb.toString() ); } private String printAttribute( String attribute, String value ) { StringBuffer sb = new StringBuffer(); if( value != null ) { sb.append( " " ); sb.append( attribute ); sb.append( "=\"" ); sb.append( value ); sb.append( "\"" ); } return( sb.toString() ); } private void printEndCell() throws JspException { ResponseUtils.writePrevious( pageContext, " \n" ); if( currentColumn >= maxColumns ) { // end previous row ResponseUtils.writePrevious( pageContext, " \n" ); // start off a new row this.printStartRow(); // reset our column count to 0 currentColumn = 0; } } // this method is called after the body of this tag has been processed public int doAfterBody() throws JspException { // print out our body content if( bodyContent != null ) { ResponseUtils.writePrevious( pageContext, bodyContent.getString() ); bodyContent.clearBody(); } // end our cell this.printEndCell(); // if we have more items to iterate through, then do so. if( iterator.hasNext() ) { Object currentObject = iterator.next(); if( currentObject == null ) { pageContext.removeAttribute( this.getId() ); } else { pageContext.setAttribute( this.getId(), currentObject ); } this.printStartCell(); return( EVAL_BODY_TAG ); } else { // if we have NO MORE items but we're in the middle of a table, spit out blank cells for the // remainder. int i=0; for( i=currentColumn; i>maxColumns; i++ ) { this.printStartCell(); ResponseUtils.write( pageContext, "   " ); this.printEndCell(); } return( SKIP_BODY ); } } public int doEndTag() throws JspException { StringBuffer sb = new StringBuffer(); // end our table ResponseUtils.writePrevious( pageContext, " \n" ); // continue processing the page return( EVAL_PAGE ); } }