1 package fr.improve.struts.taglib.layout; 2 3 import java.util.Iterator ; 4 5 import javax.servlet.jsp.JspException ; 6 import javax.servlet.jsp.tagext.BodyTagSupport ; 7 8 import org.apache.struts.taglib.html.Constants; 9 10 import fr.improve.struts.taglib.layout.util.LayoutUtils; 11 import fr.improve.struts.taglib.layout.util.TagUtils; 12 13 18 public class TableTag extends BodyTagSupport { 19 protected String name = Constants.BEAN_KEY; 20 protected String property; 21 protected String id; 22 protected String indexId; 23 24 protected String width; 25 protected String align; 26 protected int col = 2; 27 28 protected Iterator iterator = null; 29 protected int index = 0; 30 public int doAfterBody() throws JspException { 31 32 if (bodyContent != null) { 34 TagUtils.writePrevious(pageContext, "<td>"); 35 TagUtils.writePrevious(pageContext, bodyContent.getString()); 36 TagUtils.writePrevious(pageContext, "</td>"); 37 if (index % col == 0) TagUtils.writePrevious(pageContext, "</tr><tr>"); 38 bodyContent.clearBody(); 39 } 40 41 if (iterator.hasNext()) { 43 Object element = iterator.next(); 44 if (element == null) 45 pageContext.removeAttribute(id); 46 else 47 pageContext.setAttribute(id, element); 48 index++; 49 if (indexId != null) 50 pageContext.setAttribute(indexId, new Integer (index)); 51 return (EVAL_BODY_TAG); 52 } else 53 return (SKIP_BODY); 54 55 } 56 public int doEndTag() throws JspException { 57 TagUtils.write(pageContext,"</tr></table>"); 58 return EVAL_PAGE; 59 } 60 public int doStartTag() throws JspException { 61 Object collection = LayoutUtils.getBeanFromPageContext(pageContext, name, property); 62 iterator = LayoutUtils.getIterator(collection); 63 64 if (iterator.hasNext()) { 65 Object element = iterator.next(); 66 if (element == null) 67 pageContext.removeAttribute(id); 68 else 69 pageContext.setAttribute(id, element); 70 index++; 71 if (indexId != null) 72 pageContext.setAttribute(indexId, new Integer (index)); 73 74 StringBuffer sb = new StringBuffer (); 75 sb.append("<table border=\"0\""); 76 if (width!=null) { 77 sb.append(" width=\""); 78 sb.append(width); 79 sb.append("\""); 80 } 81 if (align!=null) { 82 sb.append(" align=\""); 83 sb.append(align); 84 sb.append("\""); 85 } 86 sb.append("><tr>"); 87 TagUtils.write(pageContext,sb.toString()); 88 return EVAL_BODY_TAG; 89 } 90 return SKIP_BODY; 91 } 92 96 public java.lang.String getAlign() { 97 return align; 98 } 99 103 public java.lang.String getId() { 104 return id; 105 } 106 110 public java.lang.String getIndexId() { 111 return indexId; 112 } 113 117 public java.lang.String getName() { 118 return name; 119 } 120 124 public java.lang.String getProperty() { 125 return property; 126 } 127 131 public java.lang.String getWidth() { 132 return width; 133 } 134 public void release() { 135 name = Constants.BEAN_KEY; 136 property = null; 137 id = null; 138 indexId = null; 139 140 width = null; 141 align = null; 142 col = 2; 143 144 iterator = null; 145 index = 0; 146 } 147 151 public void setAlign(java.lang.String newAlign) { 152 align = newAlign; 153 } 154 public void setCol(String col) throws JspException { 155 try { 156 this.col = Integer.parseInt(col); 157 if (this.col<1) throw new Exception (); 158 } catch (Exception e) { 159 throw new JspException ("Invalid attribute for TableTag: row most be a positive integer"); 160 } 161 162 } 163 167 public void setId(java.lang.String newId) { 168 id = newId; 169 } 170 174 public void setIndexId(java.lang.String newIndexId) { 175 indexId = newIndexId; 176 } 177 181 public void setName(java.lang.String newName) { 182 name = newName; 183 } 184 188 public void setProperty(java.lang.String newProperty) { 189 property = newProperty; 190 } 191 195 public void setWidth(java.lang.String newWidth) { 196 width = newWidth; 197 } 198 } 199 | Popular Tags |