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.tag;
17  
18  import javax.servlet.jsp.JspException;
19  import javax.servlet.jsp.tagext.BodyTagSupport;
20  
21  import org.apache.commons.beanutils.BeanUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
25  import org.extremecomponents.table.ColumnAttributes;
26  import org.extremecomponents.table.bean.Column;
27  import org.extremecomponents.util.ExceptionUtils;
28  
29  
30  
31  /***
32   * The container which holds all the column specific information. A copy
33   * of each column will be fed to the Model.
34   *
35   * @author Jeff Johnston
36   */
37  public class ColumnTag extends BodyTagSupport implements ColumnAttributes
38  {
39  	private static Log logger = LogFactory.getLog(ColumnTag.class);
40  
41  	private String property;
42  	private Object value;
43  	private String title;
44  	private String styleClass;
45  	private String headerClass;
46  	private String cell;
47  	private String format;
48  	private String parse;
49  	private String filter;
50  	private String width;
51  	private String sort;
52  	private String group;
53  	private String style;
54  	private String export;
55  	
56  	public String getProperty()
57  	{
58  		return property;
59  	}
60  
61  	public void setProperty(String property)
62  	{
63  		this.property = property;
64  	}
65  
66  	public Object getValue()
67  	{
68  		return value;
69  	}
70  
71  	public void setValue(Object value)
72  	{
73  		this.value = value;
74  	}
75  
76  	public String getTitle()
77  	{
78  		return title;
79  	}
80  
81  	public void setTitle(String title)
82  	{
83  		this.title = title;
84  	}
85  
86  	public String getStyleClass()
87  	{
88  		return styleClass;
89  	}
90  
91  	public void setStyleClass(String styleClass)
92  	{
93  		this.styleClass = styleClass;
94  	}
95  
96  	public String getHeaderClass()
97  	{
98  		return headerClass;
99  	}
100 
101 	public void setHeaderClass(String headerClass)
102 	{
103 		this.headerClass = headerClass;
104 	}
105 
106 	public String getCell()
107 	{
108 		return cell;
109 	}
110 
111 	public void setCell(String cell)
112 	{
113 		this.cell = cell;
114 	}
115 
116 	public String getFormat()
117 	{
118 		return format;
119 	}
120 
121 	public void setFormat(String format)
122 	{
123 		this.format = format;
124 	}
125 
126 	public String getParse()
127 	{
128 		return parse;
129 	}
130 
131 	public void setParse(String string)
132 	{
133 		parse = string;
134 	}
135 
136 	public String getFilter()
137 	{
138 		return filter;
139 	}
140 
141 	public void setFilter(String filter)
142 	{
143 		this.filter = filter;
144 	}
145 
146 	public String getSort()
147 	{
148 		return sort;
149 	}
150 
151 	public void setSort(String sort)
152 	{
153 		this.sort = sort;
154 	}
155 	
156 	public String getWidth()
157 	{
158 		return width;
159 	}
160 
161 	public void setWidth(String width)
162 	{
163 		this.width = width;
164 	}	
165 	
166 	public String getGroup()
167 	{
168 		return group;
169 	}
170 
171 	public void setGroup(String group)
172 	{
173 		this.group = group;
174 	}
175 	
176 	public String getStyle() 
177 	{
178 		return style;
179 	}
180 	
181 	public void setStyle(String style) 
182 	{
183 		this.style = style;
184 	}
185 
186 	public String getExport() 
187 	{
188 		return export;
189 	}
190 
191 	public void setExport(String export) 
192 	{
193 		this.export = export;
194 	}
195 
196 	/***
197 	 * Get the value for the column. First look to see if it displayed in the body
198 	 * of the column. If it is not in the body, then use the value attribute. If the
199 	 * value attribute is not specified then use the property attribute to find the value in
200 	 * the bean.
201 	 */
202 	public void setColumnValue()
203 		throws JspException
204 	{
205 		TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
206 
207 		if (tableTag.getModelRowsSize() == 0) //not going through the body for content yet...skip the first pass through
208 		{
209 			return;
210 		}
211 
212 		if (this.bodyContent != null) //have a body
213 		{
214 			value = getBodyContent().getString();
215 		}
216 
217 		if (value != null)
218 		{
219 			value = (Object) ExpressionEvaluatorManager.evaluate("value", value.toString(), Object.class, this, pageContext);
220 		}
221 
222 		try
223 		{
224 			if (value == null)
225 			{
226 				Object ojb = pageContext.getAttribute(tableTag.getCollection());
227 				value = BeanUtils.getProperty(ojb, property);
228 			}
229 		}
230 		catch (Exception e)
231 		{
232 			logger.error("could not find the property " + property + " in " + tableTag.getCollection());
233 			throw new JspException("ColumnTag.setColumnValue() Problem: " + "Could not find the property " + property + " in " + tableTag.getCollection());
234 		}
235 	}
236 	
237 	public int doStartTag()
238 		throws JspException
239 	{
240 		TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
241 		if (!tableTag.hasMetaData()) //first pass through skip the body 
242 		{
243 			return SKIP_BODY;
244 		}
245 
246 		return EVAL_BODY_BUFFERED;
247 	}
248 
249 	/***
250 	 * Must make a copy of the column because this tag may be reused. Send the copy up to the Model.
251 	 */
252 	public int doEndTag() throws JspException
253 	{
254 		try
255 		{
256 			TableTag tableTag = (TableTag) findAncestorWithClass(this, TableTag.class);
257 			if (tableTag.hasMetaData())
258 			{
259 				setColumnValue();
260 				tableTag.addColumnValue(property, value);
261 			}
262 			else
263 			{
264 				Column column = new Column(tableTag.getModel()); //must do a copy because this tag will be reused
265 				column.setProperty(property);
266 				column.setValue(value);
267 				column.setTitle(title);
268 				column.setStyleClass(styleClass);
269 				column.setHeaderClass(headerClass);
270 				column.setCell(cell);
271 				column.setFormat(format);
272 				column.setParse(parse);
273 				column.setFilter(filter);
274 				column.setSort(sort);
275 				column.setWidth(width);
276 				column.setGroup(group);
277 				column.setStyle(style);
278 				column.setExport(export);
279 				tableTag.addColumnMetaData(column);
280 			}
281 		}
282 		catch (Exception e)
283 		{
284 			throw new JspException("ColumnTag.doEndTag() Problem: " + ExceptionUtils.formatStackTrace(e));
285 		}
286 
287 		 cleanup();
288 
289 		return EVAL_PAGE;
290 	}
291 	
292 	public void cleanup()
293 	{
294 		property = null;
295 		value = null;
296 		title = null;
297 		styleClass = null;
298 		headerClass = null;
299 		cell = null;
300 		format = null;
301 		parse = null;
302 		filter = null;
303 		sort = null;
304 		width = null;
305 		group = null;
306 		style = null;
307 		export = null;
308 		setBodyContent(null);
309 	}
310 }