View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.utils.web;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  /**
23   * @author Marco Ruiz
24   * @since Dec 12, 2008
25   */
26  public class HtmlTable {
27  
28  	private static final String TABLE_START = "\n<table class=\"bodyTable\">\n\t<tbody>\n";
29  	private static final String TABLE_END = "</tbody>\n</table>";
30  
31  	private static final String ROW_A_START = "\t<tr class=\"a\">\n";
32  	private static final String ROW_B_START = "\t<tr class=\"b\">\n";
33  	private static final String ROW_END = "\t</tr>\n\n";
34  	
35  	private List<HtmlTableCell> headers = new ArrayList<HtmlTableCell>();
36  	private List<List<HtmlTableCell>> data = new ArrayList<List<HtmlTableCell>>();
37  	
38  	public HtmlTable(Object... headers) {
39  		for (Object header : headers) {
40  			HtmlTableCell cellData = (header instanceof HtmlTableCell) ? (HtmlTableCell)header : createCell(header);
41  			cellData.setHeader(true);
42  			this.headers.add(cellData);
43  		}
44  	}
45  	
46  	public void addRow(Object... rowData) {
47  		List<HtmlTableCell> row = new ArrayList<HtmlTableCell>();
48  		data.add(row);
49  		for (Object dataEle : rowData) {
50  			HtmlTableCell cellData = null;
51  			if (dataEle != null) {
52  				if (dataEle instanceof HtmlTableCell) cellData = (HtmlTableCell)dataEle;  
53  				if (dataEle instanceof HtmlContent)   cellData = new HtmlTableCell("", (HtmlContent)dataEle);  
54  			}
55  			if (cellData == null) cellData = createCell(dataEle);
56  			row.add(cellData);
57  		}
58  	}
59  
60  	private HtmlTableCell createCell(Object dataEle) {
61  	    return new HtmlTableCell(dataEle == null ? null : dataEle.toString(), "");
62      }
63  	
64  	public boolean isEmpty() {
65  		return data.size() == 0;
66  	}
67  	
68  	public String getHTML() {
69  		return new StringBuffer(TABLE_START).append(getContent()).append(TABLE_END).toString(); 
70  	}
71  
72  	private StringBuffer getContent() {
73  		boolean odd = true;
74  		StringBuffer result = getRow(ROW_A_START, headers);
75  		for (List<HtmlTableCell> rowData : data) {
76  			odd = !odd;
77  	        result.append(getRow(odd ? ROW_A_START : ROW_B_START, rowData));
78          }
79  	    return result;
80      }
81  	
82  	private StringBuffer getRow(String rowStart, List<HtmlTableCell> rowData) {
83  	    StringBuffer result = new StringBuffer(rowStart); 
84  		for (HtmlTableCell cellData : rowData)
85  			result.append(cellData.getHTML());
86  
87  		return result.append(ROW_END);
88      }
89  }