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 java.util.Iterator;
19  import java.util.List;
20  
21  import org.apache.commons.beanutils.PropertyUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.extremecomponents.table.bean.Column;
25  import org.extremecomponents.table.model.TableModelUtils;
26  
27  
28  
29  /***
30   * Helper methods for the tag classes. Trying to keep the logic out of the tags so 
31   * can move to the JSP2.0 container easier.
32   *
33   * @author $author$
34   */
35  public class TagUtils
36  {
37  	private static Log logger = LogFactory.getLog(TableTag.class);
38  
39  	/***
40  	 * If no columns are defined then set the columns automatically.
41  	 * Note: the filtering will not work because the metadata is not set.
42  	 */
43  	public static void autoSetColumns(TableTag tableTag, List rows)
44  		throws Exception
45  	{
46  		logger.info("no columns defined so set them automatically");
47  
48  		//set column meta data
49  		for (Iterator iter = rows.iterator(); iter.hasNext();)
50  		{
51  			Object bean = iter.next();
52  			List properties = TableModelUtils.beanProperties(bean);
53  			autoAddColumns(tableTag, bean, properties);
54  
55  			break;
56  		}
57  
58  		//set column values
59  		for (Iterator iter = rows.iterator(); iter.hasNext();)
60  		{
61  			Object bean = iter.next();
62  			List properties = TableModelUtils.beanProperties(bean);
63  			tableTag.getModel().getViewHandler().addRow();
64  			autoAddColumns(tableTag, bean, properties);
65  		}
66  	}
67  
68  	public static void autoAddColumns(TableTag tableTag, Object bean, List properties)
69  		throws Exception
70  	{
71  		for (Iterator iter = properties.iterator(); iter.hasNext();)
72  		{
73  			String property = (String) iter.next();
74  
75  			if (property.equals("class"))
76  			{
77  				continue;
78  			}
79  
80  			Object value = PropertyUtils.getProperty(bean, property);
81  			Column column = new Column(tableTag.getModel());
82  			column.setProperty(property);
83  			column.setValue(value);
84  			
85  			if (tableTag.hasMetaData())
86  			{
87  				tableTag.addColumnValue(property, value);
88  			}
89  			else
90  			{
91  				tableTag.addColumnMetaData(column);
92  			}
93  		}
94  	}
95  }