package com.avantouch.common.web.struts.taglib;
import javax.servlet.jsp.JspException;
import org.apache.struts.util.ResponseUtils;
/**
* <p>Title ListTag.java</p>
* <p>Description </p>
* <p>Copyright: Copyright (c) 2004 AvanTouch,Inc</p>
* <p>Company: AvanTouch,Inc</p>
* @author: david 2004-11-30
* @modifier:
* @version 1.0
*/
public class ListTag extends PaginationTag {
/**
* The name of the scripting variable to be exposed.
*/
private String id = null;
/**
* The number of elements we have already rendered.
*/
protected int lengthCount = 0;
/**
* Has this tag instance been started?
*/
protected boolean started = false;
/**
* The name of the scripting variable to be exposed as the current index.
*/
protected String indexId = null;
/**
* <p>Return the zero-relative index of the current iteration through the
* loop. If you specify an <code>offset</code>, the first iteration
* through the loop will have that value; otherwise, the first iteration
* will return zero.</p>
*
* <p>This property is read-only, and gives nested custom tags access to
* this information. Therefore, it is <strong>only</strong> valid in
* between calls to <code>doStartTag()</code> and <code>doEndTag()</code>.
* </p>
*/
public int getIndex() {
if (started)
return (offsetValue + lengthCount - 1);
else
return (0);
}
/**
* The Java class of each exposed element of the collection.
*/
protected String type = null;
/**
* Make the next collection element available and loop, or
* finish the iterations if there are no more elements.
* p><code>doStartTag</code></p>
* @return
* @throws javax.servlet.jsp.JspException if a JSP exception has occurred
* @author david 2004-11-30
* @Since 1.0
*/
public int doStartTag() throws JspException {
//init all attributes's value
initAllAttribute();
lengthCount = 0;
// Store the first value and evaluate, or skip the body if none
if (iterator.hasNext()) {
Object element = iterator.next();
if (element == null) {
pageContext.removeAttribute(id);
} else {
pageContext.setAttribute(id, element);
}
lengthCount++;
started = true;
if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
}
//write pagination information
writePaginationInTable();
// write table
writeTableHead();
return (EVAL_BODY_BUFFERED);
} else {
return (SKIP_BODY);
}
}
public void writeTableHead() throws JspException {
StringBuffer buf = new StringBuffer("<TABLE");
setT_ID(buf);
setTableCommonAttributes(buf);
prepareEvents_T(buf);
buf.append(">/n");
buf = setTableCaption(buf);
buf = wirteTableHeader(buf);
ResponseUtils.write(pageContext, buf.toString());
}
public StringBuffer wirteTableHeader(StringBuffer buf) {
if (tableheaders == null || tableheaders.length == 0)
return buf;
buf.append(" <TR nowrap>/n");
for(int i=0;i<tableheaders.length;i++){
buf.append(" <TH>"+tableheaders[i]+"</TH>/n");
}
buf.append(" </TR>/n");
return buf;
}
/**
*
* p><code>doAfterBody</code></p>
* @return
* @throws javax.servlet.jsp.JspException
* @author david 2004-11-30
* @Since 1.0
*/
public int doAfterBody() throws JspException {
// Render the output from this iteration to the output stream
if (bodyContent != null) {
ResponseUtils.writePrevious(pageContext, bodyContent.getString());
bodyContent.clearBody();
}
// Decide whether to iterate or quit
if ((getTotalSize() > 0) && (lengthCount >= getPerPageCount())) {
return (SKIP_BODY);
}
if (iterator.hasNext()) {
Object element = iterator.next();
if (element == null) {
pageContext.removeAttribute(id);
} else {
pageContext.setAttribute(id, element);
}
lengthCount++;
if (indexId != null) {
pageContext.setAttribute(indexId, new Integer(getIndex()));
}
return (EVAL_BODY_BUFFERED);
} else {
return (SKIP_BODY);
}
}
/**
*
* p><code>doEndTag</code></p>
* @return
* @throws javax.servlet.jsp.JspException
* @author david 2004-11-30
* @Since 1.0
*/
public int doEndTag() throws JspException {
// Clean up our started state
started = false;
ResponseUtils.write(pageContext, "</TABLE>/n");
pageContext.removeAttribute(id);
if (indexId != null)
pageContext.removeAttribute(indexId);
release();
// Continue processing this page
return (EVAL_PAGE);
}
/**
* Release all allocated resources.
*/
public void release() {
super.release();
id = null;
started = false;
type = null;
lengthCount = 0;
}
public String getId() {
return id;
}
public String getIndexId() {
return indexId;
}
public String getType() {
return type;
}
public void setId(String id) {
this.id = id;
}
public void setIndexId(String string) {
indexId = string;
}
public void setType(String type) {
this.type = type;
}
} // end ListTag