View Javadoc

1   /***
2    * Copyright (c) 2003 held jointly by the individual authors.            
3    *                                                                          
4    * This library is free software; you can redistribute it and/or modify it    
5    * under the terms of the GNU Lesser General Public License as published      
6    * by the Free Software Foundation; either version 2.1 of the License, or 
7    * (at your option) any later version.                                            
8    *                                                                            
9    * This library is distributed in the hope that it will be useful, but 
10   * WITHOUT ANY WARRANTY; with out even the implied warranty of 
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12   * GNU Lesser General Public License for more details.                                                  
13   *                                                                           
14   * You should have received a copy of the GNU Lesser General Public License   
15   * along with this library;  if not, write to the Free Software Foundation,   
16   * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.              
17   *                                                                            
18   * > http://www.gnu.org/copyleft/lesser.html                                  
19   * > http://www.opensource.org/licenses/lgpl-license.php
20   */
21  package net.mlw.vlh.swing.support;
22  
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.List;
26  
27  import org.apache.commons.beanutils.PropertyUtils;
28  
29  /***
30   * @author Matthew L. Wilson
31   * @version $Revision: 1.2 $ $Date: 2005/04/01 20:21:47 $
32   */
33  public class DyanBeanTableModel extends AbstractValueListTableModel
34  {
35     private String[] columns = {};
36  
37     private String[] properties = {};
38  
39     private Class[] classes = {};
40  
41     public void addColumn(String title, String property)
42     {
43        addColumn(title, property, Object.class);
44     }
45  
46     public void addColumn(String title, String property, Class klass)
47     {
48        List columns = new ArrayList(Arrays.asList(this.columns));
49        columns.add(title);
50        this.columns = (String[]) columns.toArray(new String[] {});
51  
52        List properties = new ArrayList(Arrays.asList(this.properties));
53        properties.add(property);
54        this.properties = (String[]) properties.toArray(new String[] {});
55  
56        List classes = new ArrayList(Arrays.asList(this.classes));
57        classes.add(klass);
58        this.classes = (Class[]) classes.toArray(new Class[] {});
59     }
60  
61     public String getSortPropertyName(int i)
62     {
63        return properties[i];
64     }
65  
66     /***
67      * @see javax.swing.table.TableModel#getColumnName(int)
68      */
69     public String getColumnName(int i)
70     {
71        return columns[i];
72     }
73  
74     /***
75      * @see javax.swing.table.TableModel#getColumnCount()
76      */
77     public int getColumnCount()
78     {
79        return columns.length;
80     }
81  
82     /***
83      * @see javax.swing.table.TableModel#getValueAt(int, int)
84      */
85     public Object getValueAt(int row, int column)
86     {
87        try
88        {
89           Object bean = list.get(row);
90           if (bean == null)
91           {
92              return null;
93           }
94           return PropertyUtils.getProperty(bean, properties[column]);
95        }
96        catch (Exception e)
97        {
98           return e;
99        }
100    }
101 
102    /***
103     * @see javax.swing.table.TableModel#getColumnClass(int)
104     */
105    public Class getColumnClass(int index)
106    {
107       return classes[index];
108    }
109 
110 }