View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
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  package org.extremecomponents.table.cell;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.extremecomponents.table.bean.Column;
22  import org.extremecomponents.table.model.TableModel;
23  import org.extremecomponents.util.HtmlBuilder;
24  
25  
26  
27  /***
28   * Some of the common things that a cell needs to do.
29   * Most of the time this class should be extended for convenience
30   *
31   * @author Jeff Johnston
32   */
33  public abstract class BaseCell implements Cell
34  {
35  	private static Log logger = LogFactory.getLog(BaseCell.class);
36  
37  	/***
38  	 * An even row in the table.
39  	 */
40  	public final static String EVEN = "even";
41  
42  	/***
43  	 * An odd row in the table.
44  	 */
45  	public final static String ODD = "odd";
46  	protected TableModel model;
47  	protected Column column;
48  	protected Integer rowcount;
49  
50  	public BaseCell()
51  	{
52  	}
53  
54  	/***
55  	 * Set the Model, Column and current rowcount.
56  	 */
57  	public void init(TableModel model, Column column, Integer rowcount)
58  	{
59  		this.model = model;
60  		this.column = column;
61  		this.rowcount = rowcount;
62  	}
63  	
64  	/***
65  	 * Destroy the objects set up in the init() method.
66  	 */
67  	public void destroy()
68  	{
69  		this.model = null;
70  		this.column = null;
71  		this.rowcount = null;
72  	}
73  
74  	/***
75  	 * Find out if the column is sitting on an even row.
76  	 */
77  	public boolean isRowEven()
78  	{
79  		if (rowcount != null && (rowcount.intValue() % 2) == 0)
80  		{
81  			return true;
82  		}
83  
84  		return false;
85  	}
86  
87  	/***
88  	 * Find out if the column is sitting on an odd row.
89  	 */
90  	public boolean isRowOdd()
91  	{
92  		if (rowcount != null && (rowcount.intValue() % 2) == 0)
93  		{
94  			return false;
95  		}
96  
97  		return true;
98  	}
99  
100 	/***
101 	 * Get the HTML for the td <td> tag. If the style is defined then
102 	 * it will try to use that first. If no style is defined then
103 	 * will use the alternating even and odd styles.
104 	 */
105 	public String startTD()
106 	{
107 		HtmlBuilder html = new HtmlBuilder();
108 
109 		html.td(2);
110 
111 		if (StringUtils.isNotEmpty(column.getStyleClass()))
112 		{
113 			html.styleClass(column.getStyleClass());
114 		}
115 		else if (isRowEven())
116 		{
117 			html.styleClass(EVEN);
118 		}
119 		else if (isRowOdd())
120 		{
121 			html.styleClass(ODD);
122 		}
123 		
124 		if (StringUtils.isNotEmpty(column.getWidth()))
125 		{
126 			html.width(column.getWidth());
127 		}
128 
129 		if (StringUtils.isNotEmpty(column.getStyle()))
130 		{
131 			html.style(column.getStyle());
132 		}
133 
134 		html.close();
135 
136 		return html.toString();
137 	}
138 
139 	/***
140 	 * Get the HTML for the end td </td> tag.
141 	 */
142 	public String endTD()
143 	{
144 		return new HtmlBuilder().tdEnd().toString();
145 	}
146 
147 	/***
148 	 * Get the cell specific HTML
149 	 */
150 	public abstract String html();
151 
152 	/***
153 	 * Get the cell specific simple value
154 	 */
155 	public abstract String value();
156 }