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.model;
17  
18  import org.apache.commons.lang.StringUtils;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  import org.extremecomponents.table.bean.Column;
24  
25  import org.extremecomponents.table.cell.Cell;
26  
27  import org.extremecomponents.table.view.View;
28  
29  
30  /***
31   * Take care of the view.
32   *
33   * @author Jeff Johnston
34   */
35  public class ViewHandler
36  {
37  	private static Log logger = LogFactory.getLog(ViewHandler.class);
38  	public final static String HTML = "html";
39  	public final static String VALUE = "value";
40  	private TableModel model;
41  	private View view;
42  	private ProcessColumn processColumn;
43  	private String valueType;
44  	private int rowcount = 0;
45  
46  	public ViewHandler(TableModel model)
47  	{
48  		this.model = model;
49  	}
50  
51  	public View getView()
52  	{
53  		return view;
54  	}
55  
56  	public void setView()
57  		throws Exception
58  	{
59  		boolean invokeExport = model.getExportHandler().invokeExport();
60  
61  		if (invokeExport)
62  		{
63  			String view = model.getExportHandler().getCurrentExport().getView();
64  
65  			if (StringUtils.isNotBlank(view))
66  			{
67  				Class classDefinition = Class.forName(model.getProperties().getProperty(TableProperties.VIEW + view.toLowerCase()));
68  				this.view = ((View) classDefinition.newInstance());
69  				this.view.beforeBody(model);
70  				this.valueType = VALUE;
71  			}
72  		}
73  		else
74  		{
75  			Class classDefinition = Class.forName(model.getProperties().getProperty(TableProperties.VIEW_HTML));
76  			this.view = ((View) classDefinition.newInstance());
77  			this.view.beforeBody(model);
78  			this.valueType = HTML;
79  		}
80  
81  		String customImpl = model.getProperties().getProperty(TableProperties.PROCESS_COLUMN);
82  
83  		if (StringUtils.isNotBlank(customImpl))
84  		{
85  			Class classDefinition = Class.forName(customImpl);
86  			processColumn = (ProcessColumn) classDefinition.newInstance();
87  		}
88  	}
89  
90  	public void addRow()
91  	{
92  		rowcount++;
93  	}
94  
95  	public void addColumnValue(String property, Object value)
96  	{
97  		Column column = model.getColumnMetaData().getColumn(property);
98  		if (column != null)
99  		{
100 			Cell cell = buildCell(column, value);
101 			String cellvalue = processColumn.process(model, column, cell, valueType);
102 			cell.destroy();
103 
104 			boolean isFirstColumn = model.getColumnMetaData().isFirstColumn(property);
105 			boolean isLastColumn = model.getColumnMetaData().isLastColumn(property);
106 			view.body(model, cellvalue, isFirstColumn, isLastColumn);
107 		}
108 	}
109 
110 	/***
111 	 * The current row count. 
112 	 */
113 	public int rowcount()
114 	{
115 		return rowcount;
116 	}
117 
118 	/***
119 	 * Build up the cells in the row. Will either load up a known cell
120 	 * type, or just instantiate a new Cell using the fully qualified
121 	 * package name.
122 	 */
123 	private Cell buildCell(Column column, Object value)
124 	{
125 		Cell result = null;
126 
127 		String cell = column.getCell();
128 		String className = model.getProperties().getProperty(TableProperties.CELL + cell);
129 
130 		if (className == null) //a custom cell
131 		{
132 			className = cell;
133 		}
134 
135 		try
136 		{
137 			result = model.getCachedCell(Class.forName(className));
138 			column.setValue(value); //set the value in the column
139 			result.init(model, column, new Integer(rowcount));
140 		}
141 		catch (Exception e)
142 		{
143 			logger.error("ViewHandler.buildCell()", e);
144 		}
145 
146 		return result;
147 	}
148 }