View Javadoc

1   /**
2    * Copyright (c) 2009-2010, Elias Gomes. All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5    * following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8    * disclaimer.
9    * 
10   * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided with the distribution.
12   * 
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
14   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
18   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20   */
21  package net.sourceforge.mystique.infrastructure.xhtml;
22  
23  import java.io.IOException;
24  import java.io.StringWriter;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import javax.servlet.jsp.JspException;
31  import javax.servlet.jsp.tagext.SimpleTagSupport;
32  
33  /**
34   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
35   */
36  public class TableTag extends SimpleTagSupport {
37  
38  	private String var = "item";
39  
40  	private Collection<?> items;
41  
42  	private String noItems = "No items found.";
43  
44  	private String cssClass;
45  
46  	private String cssStyle;
47  
48  	private boolean firstTime = true;
49  
50  	private List<String> labels = new ArrayList<String>();
51  
52  	private List<String> rows = new ArrayList<String>();
53  
54  	@Override
55  	public void doTag() throws JspException, IOException {
56  
57  		Iterator<?> itemIterator = items.iterator();
58  
59  		if (!itemIterator.hasNext()) {
60  			StringBuilder builder = new StringBuilder();
61  			builder.append("<p>").append(noItems).append("</p>\n");
62  			getJspContext().getOut().print(builder.toString());
63  			return;
64  		}
65  
66  		while (itemIterator.hasNext()) {
67  
68  			getJspContext().setAttribute(var, itemIterator.next());
69  
70  			StringWriter rowOut = new StringWriter();
71  			getJspBody().invoke(rowOut);
72  			rowOut.close();
73  
74  			rows.add(rowOut.toString());
75  
76  			if (firstTime) {
77  				firstTime = false;
78  			}
79  
80  		}
81  
82  		StringBuilder builder = new StringBuilder();
83  		builder.append("<table");
84  		if (cssClass != null) {
85  			builder.append(" class=\"").append(cssClass).append("\"");
86  		}
87  		if (cssStyle != null) {
88  			builder.append(" style=\"").append(cssStyle).append("\"");
89  		}
90  		builder.append(">\n");
91  		builder.append("\t<thead>\n");
92  		builder.append("\t\t<tr>\n");
93  
94  		Iterator<String> labelIterator = labels.iterator();
95  		while (labelIterator.hasNext()) {
96  			builder.append("\t\t\t<th>").append(labelIterator.next()).append("</th>\n");
97  		}
98  
99  		builder.append("\t\t</tr>\n");
100 		builder.append("\t</thead>\n");
101 		builder.append("\t<tbody>\n");
102 
103 		Iterator<String> rowIterator = rows.iterator();
104 		int i = 1;
105 		while (rowIterator.hasNext()) {
106 
107 			builder.append("\t\t<tr");
108 			if ((i++ % 2) != 0) {
109 				builder.append(" class=\"odd\"");
110 			}
111 			builder.append(">\n");
112 
113 			builder.append(rowIterator.next());
114 
115 			builder.append("\t\t</tr>\n");
116 
117 		}
118 
119 		builder.append("\t</tbody>\n");
120 		builder.append("</table>\n");
121 
122 		getJspContext().getOut().print(builder.toString());
123 
124 	}
125 
126 	public void setVar(String var) {
127 		this.var = var;
128 	}
129 
130 	public void setItems(Collection<?> items) {
131 		this.items = items;
132 	}
133 
134 	public void setNoItems(String noItems) {
135 		this.noItems = noItems;
136 	}
137 
138 	public void setCssClass(String cssClass) {
139 		this.cssClass = cssClass;
140 	}
141 
142 	public void setCssStyle(String cssStyle) {
143 		this.cssStyle = cssStyle;
144 	}
145 
146 	public void addLabels(String label) {
147 		if (firstTime) {
148 			labels.add(label);
149 		}
150 	}
151 
152 }