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.tree.model;
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.cell.Cell;
23  import org.extremecomponents.table.model.ProcessColumn;
24  import org.extremecomponents.table.model.TableModel;
25  import org.extremecomponents.table.view.View;
26  
27  
28  
29  /***
30   * Take care of the view.
31   *
32   * @author Jeff Johnston
33   */
34  public class ViewHandler extends org.extremecomponents.table.model.ViewHandler
35  {
36  	private static Log logger = LogFactory.getLog(ViewHandler.class);
37  	public final static String HTML = "html";
38  	public final static String VALUE = "value";
39  	private TreeModel model;
40  	private View view;
41  	private ProcessColumn processColumn;
42  	private String valueType;
43  	private int rowcount = 0;
44  
45  	public ViewHandler(TreeModel model)
46  	{
47  		super((TableModel) model);
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.getTreeProperties().getProperty(TreeProperties.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.getTreeProperties().getProperty(TreeProperties.VIEW_HTML));
76  			this.view = ((View) classDefinition.newInstance());
77  			this.view.beforeBody(model);
78  			this.valueType = HTML;
79  		}
80  
81  		String customImpl = model.getTreeProperties().getProperty(TreeProperties.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.getTreeProperties().getProperty(TreeProperties.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 }