KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > gui > ObjectTableModel


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/gui/ObjectTableModel.java,v 1.5 2004/02/11 23:48:32 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.gui;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.swing.event.TableModelEvent JavaDoc;
29 import javax.swing.table.DefaultTableModel JavaDoc;
30
31 import org.apache.jorphan.logging.LoggingManager;
32 import org.apache.log.Logger;
33
34 /**
35  * @version $Revision: 1.5 $
36  */

37 public class ObjectTableModel extends DefaultTableModel JavaDoc
38 {
39     private static Logger log = LoggingManager.getLoggerForClass();
40     private transient ArrayList JavaDoc objects = new ArrayList JavaDoc();
41     private transient List JavaDoc headers = new ArrayList JavaDoc();
42     private transient ArrayList JavaDoc classes = new ArrayList JavaDoc();
43     private transient Class JavaDoc objectClass;
44
45     private transient ArrayList JavaDoc setMethods = new ArrayList JavaDoc();
46     private transient ArrayList JavaDoc getMethods = new ArrayList JavaDoc();
47
48     public ObjectTableModel(String JavaDoc[] headers, String JavaDoc[] propertyNames, Class JavaDoc[] propertyClasses, Class JavaDoc[] renderClasses, Object JavaDoc sampleObject)
49     {
50         this.headers.addAll(Arrays.asList(headers));
51         this.classes.addAll(Arrays.asList(renderClasses));
52         objectClass = sampleObject.getClass();
53         Class JavaDoc[] emptyClasses = new Class JavaDoc[0];
54         for (int i = 0; i < propertyNames.length; i++)
55         {
56             propertyNames[i] =
57                 propertyNames[i].substring(0, 1).toUpperCase()
58                     + propertyNames[i].substring(1);
59             try
60             {
61                 if (!propertyClasses[i].equals(Boolean JavaDoc.class)
62                     && !propertyClasses[i].equals(boolean.class))
63                 {
64                     getMethods.add(
65                         objectClass.getMethod(
66                             "get" + propertyNames[i],
67                             emptyClasses));
68                 }
69                 else
70                 {
71                     getMethods.add(
72                         objectClass.getMethod(
73                             "is" + propertyNames[i],
74                             emptyClasses));
75                 }
76                 setMethods.add(objectClass.getMethod("set" + propertyNames[i], new Class JavaDoc[] { propertyClasses[i] }));
77             }
78             catch (NoSuchMethodException JavaDoc e)
79             {
80                 log.error("Invalid Method name for class: " + objectClass, e);
81             }
82         }
83     }
84
85     public Iterator JavaDoc iterator()
86     {
87         return objects.iterator();
88     }
89
90     public void clearData()
91     {
92         int size = getRowCount();
93         objects.clear();
94         super.fireTableRowsDeleted(0, size);
95     }
96
97     public void addRow(Object JavaDoc value)
98     {
99         objects.add(value);
100         super.fireTableRowsInserted(objects.size() - 1, objects.size());
101     }
102
103     /**
104      * @see javax.swing.table.TableModel#getColumnCount()
105      */

106     public int getColumnCount()
107     {
108         return headers.size();
109     }
110
111     /**
112      * @see javax.swing.table.TableModel#getColumnName(int)
113      */

114     public String JavaDoc getColumnName(int col)
115     {
116         return (String JavaDoc) headers.get(col);
117     }
118
119     /**
120      * @see javax.swing.table.TableModel#getRowCount()
121      */

122     public int getRowCount()
123     {
124         if (objects == null)
125         {
126             return 0;
127         }
128         return objects.size();
129     }
130
131     /**
132      * @see javax.swing.table.TableModel#getValueAt(int, int)
133      */

134     public Object JavaDoc getValueAt(int row, int col)
135     {
136         Object JavaDoc value = objects.get(row);
137         Method JavaDoc getMethod = (Method JavaDoc) getMethods.get(col);
138         try
139         {
140             return getMethod.invoke(value, new Object JavaDoc[0]);
141         }
142         catch (IllegalAccessException JavaDoc e)
143         {
144             log.error("Illegal method access", e);
145         }
146         catch (InvocationTargetException JavaDoc e)
147         {
148             log.error("incorrect method access", e);
149         }
150         return null;
151     }
152
153     /**
154      * @see javax.swing.table.TableModel#isCellEditable(int, int)
155      */

156     public boolean isCellEditable(int arg0, int arg1)
157     {
158         return true;
159     }
160
161     /**
162      * @see javax.swing.table.DefaultTableModel#moveRow(int, int, int)
163      */

164     public void moveRow(int start, int end, int to)
165     {
166         List JavaDoc subList = objects.subList(start, end);
167         for (int x = end - 1; x >= start; x--)
168         {
169             objects.remove(x);
170         }
171         objects.addAll(to, subList);
172         super.fireTableChanged(new TableModelEvent JavaDoc(this));
173     }
174
175     /**
176      * @see javax.swing.table.DefaultTableModel#removeRow(int)
177      */

178     public void removeRow(int row)
179     {
180         objects.remove(row);
181         super.fireTableRowsDeleted(row, row);
182     }
183
184     /**
185      * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
186      */

187     public void setValueAt(Object JavaDoc cellValue, int row, int col)
188     {
189         if (row < objects.size())
190         {
191             Object JavaDoc value = objects.get(row);
192             if (col < setMethods.size())
193             {
194                 Method JavaDoc setMethod = (Method JavaDoc) setMethods.get(col);
195                 try
196                 {
197                     setMethod.invoke(value, new Object JavaDoc[] { cellValue });
198                 }
199                 catch (IllegalAccessException JavaDoc e)
200                 {
201                     log.error("Illegal method access", e);
202                 }
203                 catch (InvocationTargetException JavaDoc e)
204                 {
205                     log.error("incorrect method access", e);
206                 }
207                 super.fireTableDataChanged();
208             }
209         }
210     }
211
212     /**
213      * @see javax.swing.table.TableModel#getColumnClass(int)
214      */

215     public Class JavaDoc getColumnClass(int arg0)
216     {
217         return (Class JavaDoc) classes.get(arg0);
218     }
219
220 }
221
Popular Tags