KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > swing > support > DyanBeanTableModel


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 JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.List JavaDoc;
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 JavaDoc[] columns = {};
36
37    private String JavaDoc[] properties = {};
38
39    private Class JavaDoc[] classes = {};
40
41    public void addColumn(String JavaDoc title, String JavaDoc property)
42    {
43       addColumn(title, property, Object JavaDoc.class);
44    }
45
46    public void addColumn(String JavaDoc title, String JavaDoc property, Class JavaDoc klass)
47    {
48       List JavaDoc columns = new ArrayList JavaDoc(Arrays.asList(this.columns));
49       columns.add(title);
50       this.columns = (String JavaDoc[]) columns.toArray(new String JavaDoc[] {});
51
52       List JavaDoc properties = new ArrayList JavaDoc(Arrays.asList(this.properties));
53       properties.add(property);
54       this.properties = (String JavaDoc[]) properties.toArray(new String JavaDoc[] {});
55
56       List JavaDoc classes = new ArrayList JavaDoc(Arrays.asList(this.classes));
57       classes.add(klass);
58       this.classes = (Class JavaDoc[]) classes.toArray(new Class JavaDoc[] {});
59    }
60
61    public String JavaDoc getSortPropertyName(int i)
62    {
63       return properties[i];
64    }
65
66    /**
67     * @see javax.swing.table.TableModel#getColumnName(int)
68     */

69    public String JavaDoc 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 JavaDoc getValueAt(int row, int column)
86    {
87       try
88       {
89          Object JavaDoc bean = list.get(row);
90          if (bean == null)
91          {
92             return null;
93          }
94          return PropertyUtils.getProperty(bean, properties[column]);
95       }
96       catch (Exception JavaDoc e)
97       {
98          return e;
99       }
100    }
101
102    /**
103     * @see javax.swing.table.TableModel#getColumnClass(int)
104     */

105    public Class JavaDoc getColumnClass(int index)
106    {
107       return classes[index];
108    }
109
110 }
Popular Tags