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  
20  /**
21   * @author Marco Ruiz
22   * @since Dec 12, 2008
23   */
24  public class HtmlTableCell {
25  	
26  	private CellAlignment alignment = CellAlignment.LEFT;
27  	private String tooltip = "";
28  	private boolean header = false;
29  	private HtmlContent[] content;
30  	
31  	public HtmlTableCell(String caption, String tooltip, HtmlLink link) {
32  		this(caption, tooltip, link, null);
33  	}
34  	
35  	public HtmlTableCell(String caption, String tooltip, HtmlLink link, WebIcon image) {
36  	    this(tooltip, new HtmlContent(caption, link, image));
37  	    if (link != null || image != null) this.alignment = CellAlignment.CENTER;
38      }
39  	
40  	public HtmlTableCell(String caption, String tooltip) {
41  		this(tooltip, new HtmlContent(caption));
42  		this.alignment = CellAlignment.LEFT;
43      }
44  	
45  	public HtmlTableCell(String tooltip, HtmlContent... content) {
46  		this.tooltip = tooltip;
47  		this.content = content;
48  		this.alignment = CellAlignment.CENTER;
49      }
50  	
51  	public void setHeader(boolean value) {
52      	this.header = value;
53      }
54  
55  	public void setAlignment(CellAlignment alignment) {
56      	this.alignment = alignment;
57      }
58  
59  	public StringBuffer getHTML() {
60  		String htmlTag = header ? "th" : "td";
61  	    StringBuffer result = new StringBuffer();
62  	    
63  	    // Start cell tag
64  	    result.append("\t\t<").append(htmlTag).append(" align=\"").append(alignment).append("\"");
65  	    if (tooltip != null && !tooltip.equals("")) 
66  	    	result.append(" title=\"").append(tooltip).append("\" ");
67  	    result.append(">");
68  
69  	    // cell content
70  	    result.append(getContent());
71  	    
72  	    // cell tag closure
73  		return result.append("</").append(htmlTag).append(">\n");
74  	}
75  
76  	private String getContent() {
77  		if (content == null || content.length == 0) return "";
78  		StringBuffer result = new StringBuffer();
79  		for (HtmlContent cont : content) result.append(cont.getHTML());
80  	    return result.toString();
81      }
82  }