View Javadoc

1   /*
2    * Created on Oct 11, 2004
3    *
4    */
5   package org.extremecomponents.tree.view;
6   
7   import java.util.Iterator;
8   import java.util.List;
9   
10  import org.apache.commons.lang.StringUtils;
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.extremecomponents.table.bean.Column;
14  import org.extremecomponents.table.bean.Export;
15  import org.extremecomponents.table.bean.Form;
16  import org.extremecomponents.table.bean.Table;
17  import org.extremecomponents.table.model.ExportHandler;
18  import org.extremecomponents.table.model.FilterHandler;
19  import org.extremecomponents.table.model.ParameterRegistry;
20  import org.extremecomponents.table.model.TableModel;
21  import org.extremecomponents.table.model.TableModelUtils;
22  import org.extremecomponents.table.model.ViewHandler;
23  import org.extremecomponents.table.view.View;
24  import org.extremecomponents.tree.model.TreeModel;
25  import org.extremecomponents.util.HtmlBuilder;
26  
27  /***
28   * org.extremecomponents.tree.view.HtmlView.java - 
29   * 
30   * @author Paul Horn
31   */
32  public class HtmlView implements View
33  {
34  	private static Log logger = LogFactory.getLog(HtmlView.class);
35  	public final static String EXPORT_REGION = "exportRegion";
36  	public final static String EXPORT_TOOLBAR = "exportToolBar";
37  	public final static String FORM_BUTTONS = "formButtons";
38  	public final static String FILTER = "filter";
39  	public final static String TABLE_TITLE = "tableTitle";
40  	private HtmlBuilder html = new HtmlBuilder();
41  
42  	public void beforeBody(TableModel model)
43  	{
44  		paginationExport(model);
45  		tableStart(model);
46  		header(model);
47  		filter(model);
48  		formStart(model);
49  	}
50  
51  	public void body(TableModel model, String value, boolean startRow, boolean endRow)
52  	{
53  		if (startRow)
54  		{
55  			html.tr(1).close();
56  		}
57  
58  		html.append(value);
59  
60  		if (endRow)
61  		{
62  			html.trEnd(1);
63  		}
64  	}
65  
66  	public Object afterBody(TableModel model)
67  	{
68  		tableEnd(model);
69  
70  		return html.toString();
71  	}
72  
73  	/***
74  	 * The pagination and export will display together.
75  	 */
76  	public void paginationExport(TableModel model)
77  	{
78  		html.table(0).styleClass(EXPORT_REGION).width(model.getTable().getWidth()).close();
79  		html.tr(1).close();
80  		html.td(2).close();
81  
82  		pagination(model);
83  
84  		html.tdEnd();
85  
86  		html.td(2).align("right").valign("bottom").style("padding-right:15px").close();
87  
88  		export(model);
89  
90  		html.tdEnd();
91  		html.trEnd(1);
92  		html.tableEnd(0);
93  	}
94  
95  	/***
96  	 * The pagination that will hover over the table.
97  	 */
98  	public void pagination(TableModel model)
99  	{
100 		return;
101 	}
102 
103 	public void export(TableModel model)
104 	{
105 		if (!model.getExportHandler().hasExports())
106 		{
107 			return;
108 		}
109 
110 		html.table(2).styleClass(EXPORT_TOOLBAR).close();
111 		html.tr(3).close();
112 
113 		for (Iterator iter = model.getExportHandler().getExports().iterator(); iter.hasNext();)
114 		{
115 			html.td(4).close();
116 
117 			Export export = (Export) iter.next();
118 			exportHtml(model, export);
119 			html.tdEnd();
120 		}
121 
122 		html.trEnd(3);
123 		html.tableEnd(2);
124 	}
125 
126 	private void exportHtml(TableModel model, Export export)
127 	{
128 		html.a();
129 		html.quote();
130 
131 		String action = model.getTable().getAction();
132 
133 		if (StringUtils.isNotEmpty(action))
134 		{
135 			html.append(action);
136 		}
137 
138 		html.question().append(Table.EXTREME_TABLE).equals().append(model.getTableKey()).ampersand().append(model.getTableKey() + ParameterRegistry.EXPORT).equals().append(export.getView()).ampersand().append(model.getTableKey() + ExportHandler.EXPORT_NAME).equals().append(export.getName());
139 		html.append(model.getRegistry().getURLParameterString(true, true, true));
140 		html.quote();
141 		html.close();
142 
143 		if (StringUtils.isNotEmpty(export.getImage()))
144 		{
145 			String title = (export.getTitle() == null) ? "Export" : export.getTitle();
146 			html.img(export.getImage(), title);
147 		}
148 		else
149 		{
150 			html.append(" " + export.getView() + " ");
151 		}
152 
153 		html.aEnd();
154 	}
155 
156 	/***
157 	 * The opening table tag along with all the attributes of the table.
158 	 */
159 	public void tableStart(TableModel model)
160 	{
161 		Table table = model.getTable();
162 
163 		html.table(0);
164 		html.id(table.getId());
165 		html.border(table.getBorder());
166 		html.cellSpacing(table.getCellspacing());
167 		html.cellPadding(table.getCellpadding());
168 		html.width(table.getWidth());
169 
170 		String styleClass = table.getStyleClass();
171 
172 		if (StringUtils.isNotBlank(styleClass))
173 		{
174 			html.styleClass(styleClass);
175 		}
176 
177 		String style = table.getStyle();
178 
179 		if (StringUtils.isNotBlank(style))
180 		{
181 			html.style(style);
182 		}
183 
184 		html.close();
185 
186 		if (!model.getTable().doPagination() && StringUtils.isNotBlank(table.getTitle())) //not doing pagination and have a title
187 		{
188 			html.tr(1).close();
189 			html.th(2).styleClass(TABLE_TITLE).colSpan("" + model.getColumnMetaData().columnCount()).close();
190 			html.append(table.getTitle());
191 			html.thEnd();
192 			html.trEnd(1);
193 		}
194 	}
195 
196 	/***
197 	 * Closes the table.
198 	 */
199 	public void tableEnd(TableModel model)
200 	{
201 		html.tableEnd(0);
202 		formButtons(model);
203 	}
204 
205 	/***
206 	 * The header row of the table. This is typically the first row of the table.
207 	 */
208 	public void header(TableModel model)
209 	{
210 		html.tr(1).close();
211 		
212 		for (Iterator iter = TableModelUtils.getHeader(model, ViewHandler.HTML).iterator();
213 				iter.hasNext();)
214 		{
215 			html.append((String) iter.next());
216 		}
217 
218 		html.trEnd(1);
219 	}
220 
221 	/***
222 	 * The filter row of the table. Typically sits below the header (if enabled).
223 	 * Will contain input or select type fields to allow the user to filter the result set.
224 	 */
225 	public void filter(TableModel model)
226 	{
227 		List filter = model.getFilterHandler().getFilter(model);
228 
229 		if (!model.getTable().doFilter())
230 		{
231 			return;
232 		}
233 
234 		filterFormStart(model);
235 
236 		html.tr(1).styleClass(FILTER).close();
237 
238 		for (Iterator iter = filter.iterator(); iter.hasNext();)
239 		{
240 			String value = (String) iter.next();
241 
242 			html.td(2).close();
243 
244 			if (iter.hasNext())
245 			{
246 				html.append(value);
247 			}
248 			else
249 			{
250 				html.table(2).cellPadding("0").cellSpacing("0").border("0").width("100%").close();
251 
252 				html.tr(3).close();
253 
254 				html.td(4).width("100%").close();
255 				html.append(value);
256 				html.tdEnd();
257 
258 				html.td(4).align("right").close();
259 				filterFomButtons(model);
260 				html.tdEnd();
261 
262 				html.trEnd(3);
263 				html.tableEnd(2);
264 			}
265 
266 			html.tdEnd();
267 		}
268 
269 		html.trEnd(1);
270 		filterFormEnd();
271 	}
272 
273 	/***
274 	 * Get the HTML for the start of the form <form> tag. The filter will need to be
275 	 * wrapped in a form tag so that it can be submitted.
276 	 */
277 	private void filterFormStart(TableModel model)
278 	{
279 		html.form();
280 		html.name(model.getTableKey() + "filter");
281 
282 		String action = model.getTable().getAction();
283 
284 		if (StringUtils.isNotEmpty(action))
285 		{
286 			html.action(action);
287 		}
288 
289 		html.close();
290 
291 		String hiddenFields = model.getRegistry().getFormHiddenFields(false, true, false);
292 
293 		if (StringUtils.isNotEmpty(hiddenFields))
294 		{
295 			html.append(hiddenFields);
296 		}
297 	}
298 
299 	/***
300 	 * The HTML for the filter search and clear buttons.
301 	 */
302 	private void filterFomButtons(TableModel model)
303 	{
304 		html.table(4).cellPadding("0").cellSpacing("0").border("0").close();
305 		html.tr(5).close();
306 
307 		html.input("hidden").name(model.getTableKey() + "filter_button").close();
308 
309 		html.td(6).valign("center").close();
310 
311 		html.a().quote().append("javascript:document." + model.getTableKey() + "filter." + model.getTableKey() + "filter_button.value='" + FilterHandler.DO_SEARCH + "';document." + model.getTableKey() + "filter.submit()").quote().close();
312 
313 		String imageSearch = model.getTable().getImage(FilterHandler.SEARCH_IMAGE);
314 
315 		if (StringUtils.isNotEmpty(imageSearch))
316 		{
317 			html.img(imageSearch, "Search");
318 		}
319 		else
320 		{
321 			html.append("&nbsp;Filter&nbsp;");
322 		}
323 
324 		html.aEnd();
325 
326 		html.tdEnd();
327 		html.td(6).valign("center").close();
328 
329 		html.a().quote().append("javascript:document." + model.getTableKey() + "filter." + model.getTableKey() + "filter_button.value='" + FilterHandler.DO_CLEAR + "';document." + model.getTableKey() + "filter.submit()").quote().close();
330 
331 		String imageClear = model.getTable().getImage(FilterHandler.CLEAR_IMAGE);
332 
333 		if (StringUtils.isNotEmpty(imageClear))
334 		{
335 			html.img(imageClear, "Clear");
336 		}
337 		else
338 		{
339 			html.append("&nbsp;Clear&nbsp;");
340 		}
341 
342 		html.aEnd();
343 
344 		html.tdEnd();
345 		html.trEnd(5);
346 		html.tableEnd(4);
347 		html.format(4, 1);
348 		html.newline();
349 	}
350 
351 	/***
352 	 * Get the HTML for the end of the form <form> tag.
353 	 */
354 	private void filterFormEnd()
355 	{
356 		html.formEnd();
357 	}
358 
359 	/***
360 	 * The form will come right before the body section.
361 	 */
362 	private void formStart(TableModel model)
363 	{
364 		Form form = model.getFormHandler().getForm();
365 
366 		if (form != null)
367 		{
368 			String name = form.getName();
369 			String action = form.getAction();
370 			String method = form.getMethod();
371 
372 			html.form().name(name).action(action).method(method);
373 
374 			String onsubmit = form.getOnsubmit();
375 
376 			if (StringUtils.isNotEmpty(onsubmit))
377 			{
378 				html.onsubmit(onsubmit);
379 			}
380 
381 			html.close();
382 
383 			//make up the hiddens
384 			html.append(model.getRegistry().getFormHiddenFields(true, true, true));
385 			html.append(model.getFormHandler().getHiddenFields());
386 		}
387 	}
388 
389 	public void formButtons(TableModel model)
390 	{
391 		String formButtons = model.getFormHandler().getFormButtons();
392 
393 		if (StringUtils.isNotEmpty(formButtons))
394 		{
395 			html.newline();
396 			html.span().styleClass(FORM_BUTTONS).close();
397 			html.append(formButtons);
398 			html.spanEnd();
399 			html.formEnd();
400 		}
401 	}
402 }