KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > util > PowerTableModel


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/util/PowerTableModel.java,v 1.10 2004/02/14 03:34:29 sebb Exp $
2
/*
3  * Copyright 2001-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.jmeter.gui.util;
20
21 import java.lang.reflect.Constructor JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.swing.table.DefaultTableModel JavaDoc;
25
26 import org.apache.jorphan.collections.Data;
27 import org.apache.jorphan.logging.LoggingManager;
28 import org.apache.log.Logger;
29
30 /**
31  * @author mstover
32  * @version $Revision: 1.10 $
33  */

34 public class PowerTableModel extends DefaultTableModel JavaDoc
35 {
36     private static Logger log = LoggingManager.getLoggerForClass();
37     Data model = new Data();
38     Class JavaDoc[] columnClasses;
39
40     public PowerTableModel(String JavaDoc[] headers, Class JavaDoc[] cc)
41     {
42         model.setHeaders(headers);
43         columnClasses = cc;
44     }
45
46     public PowerTableModel()
47     {
48     }
49
50     public void setRowValues(int row, Object JavaDoc[] values)
51     {
52         model.setCurrentPos(row);
53         for (int i = 0; i < values.length; i++)
54         {
55             model.addColumnValue(model.getHeaders()[i], values[i]);
56         }
57     }
58
59     public Data getData()
60     {
61         return model;
62     }
63
64     public void addNewColumn(String JavaDoc colName, Class JavaDoc colClass)
65     {
66         model.addHeader(colName);
67         Class JavaDoc[] newClasses = new Class JavaDoc[columnClasses.length + 1];
68         System.arraycopy(columnClasses, 0, newClasses, 0, columnClasses.length);
69         newClasses[newClasses.length - 1] = colClass;
70         columnClasses = newClasses;
71         Object JavaDoc defaultValue = createDefaultValue(columnClasses.length - 1);
72         model.setColumnData(colName, defaultValue);
73         this.fireTableStructureChanged();
74     }
75
76     public void removeRow(int row)
77     {
78         log.debug("remove row: " + row);
79         if (model.size() > row)
80         {
81             log.debug("Calling remove row on Data");
82             model.removeRow(row);
83         }
84     }
85
86     public void removeColumn(int col)
87     {
88         model.removeColumn(col);
89         this.fireTableStructureChanged();
90     }
91
92     public void setColumnData(int col, List JavaDoc data)
93     {
94         model.setColumnData(col, data);
95     }
96
97     public List JavaDoc getColumnData(String JavaDoc colName)
98     {
99         return model.getColumnAsObjectArray(colName);
100     }
101
102     public void clearData()
103     {
104         String JavaDoc[] headers = model.getHeaders();
105         model = new Data();
106         model.setHeaders(headers);
107         this.fireTableDataChanged();
108     }
109
110     public void addRow(Object JavaDoc data[])
111     {
112         model.setCurrentPos(model.size());
113         for (int i = 0; i < data.length; i++)
114         {
115             model.addColumnValue(model.getHeaders()[i], data[i]);
116         }
117     }
118
119     public void addNewRow()
120     {
121         addRow(createDefaultRow());
122     }
123
124     private Object JavaDoc[] createDefaultRow()
125     {
126         Object JavaDoc[] rowData = new Object JavaDoc[getColumnCount()];
127         for (int i = 0; i < rowData.length; i++)
128         {
129             rowData[i] = createDefaultValue(i);
130         }
131         return rowData;
132     }
133
134     public Object JavaDoc[] getRowData(int row)
135     {
136         Object JavaDoc[] rowData = new Object JavaDoc[getColumnCount()];
137         for (int i = 0; i < rowData.length; i++)
138         {
139             rowData[i] = model.getColumnValue(i, row);
140         }
141         return rowData;
142     }
143
144     private Object JavaDoc createDefaultValue(int i)
145     {
146         Class JavaDoc colClass = getColumnClass(i);
147         try
148         {
149             return colClass.newInstance();
150         }
151         catch (Exception JavaDoc e)
152         {
153             try
154             {
155                 Constructor JavaDoc constr =
156                     colClass.getConstructor(new Class JavaDoc[] { String JavaDoc.class });
157                 return constr.newInstance(new Object JavaDoc[] { "" });
158             }
159             catch (Exception JavaDoc err)
160             {
161             }
162             try
163             {
164                 Constructor JavaDoc constr =
165                     colClass.getConstructor(new Class JavaDoc[] { Integer.TYPE });
166                 return constr.newInstance(new Object JavaDoc[] { new Integer JavaDoc(0)});
167             }
168             catch (Exception JavaDoc err)
169             {
170             }
171             try
172             {
173                 Constructor JavaDoc constr =
174                     colClass.getConstructor(new Class JavaDoc[] { Long.TYPE });
175                 return constr.newInstance(new Object JavaDoc[] { new Long JavaDoc(0L)});
176             }
177             catch (Exception JavaDoc err)
178             {
179             }
180             try
181             {
182                 Constructor JavaDoc constr =
183                     colClass.getConstructor(new Class JavaDoc[] { Boolean.TYPE });
184                 return constr.newInstance(new Object JavaDoc[] { Boolean.FALSE});
185             }
186             catch (Exception JavaDoc err)
187             {
188             }
189             try
190             {
191                 Constructor JavaDoc constr =
192                     colClass.getConstructor(new Class JavaDoc[] { Float.TYPE });
193                 return constr.newInstance(new Object JavaDoc[] { new Float JavaDoc(0F)});
194             }
195             catch (Exception JavaDoc err)
196             {
197             }
198             try
199             {
200                 Constructor JavaDoc constr =
201                     colClass.getConstructor(new Class JavaDoc[] { Double.TYPE });
202                 return constr.newInstance(new Object JavaDoc[] { new Double JavaDoc(0D)});
203             }
204             catch (Exception JavaDoc err)
205             {
206             }
207             try
208             {
209                 Constructor JavaDoc constr =
210                     colClass.getConstructor(new Class JavaDoc[] { Character.TYPE });
211                 return constr.newInstance(new Object JavaDoc[] { new Character JavaDoc(' ')});
212             }
213             catch (Exception JavaDoc err)
214             {
215             }
216             try
217             {
218                 Constructor JavaDoc constr =
219                     colClass.getConstructor(new Class JavaDoc[] { Byte.TYPE });
220                 return constr.newInstance(
221                     new Object JavaDoc[] { new Byte JavaDoc(Byte.MIN_VALUE)});
222             }
223             catch (Exception JavaDoc err)
224             {
225             }
226             try
227             {
228                 Constructor JavaDoc constr =
229                     colClass.getConstructor(new Class JavaDoc[] { Short.TYPE });
230                 return constr.newInstance(
231                     new Object JavaDoc[] { new Short JavaDoc(Short.MIN_VALUE)});
232             }
233             catch (Exception JavaDoc err)
234             {
235             }
236         }
237         return "";
238     }
239
240     /**
241      * Required by table model interface.
242      *
243      * @return the RowCount value
244      */

245     public int getRowCount()
246     {
247         if (model == null)
248         {
249             return 0;
250         }
251         return model.size();
252     }
253
254     /**
255      * Required by table model interface.
256      *
257      * @return the ColumnCount value
258      */

259     public int getColumnCount()
260     {
261         return model.getHeaders().length;
262     }
263
264     /**
265      * Required by table model interface.
266      *
267      * @return the ColumnName value
268      */

269     public String JavaDoc getColumnName(int column)
270     {
271         return model.getHeaders()[column];
272     }
273
274     public boolean isCellEditable(int row, int column)
275     {
276         // all table cells are editable
277
return true;
278     }
279
280     public Class JavaDoc getColumnClass(int column)
281     {
282         return columnClasses[column];
283     }
284
285     /**
286      * Required by table model interface.
287      * return the ValueAt value
288      */

289     public Object JavaDoc getValueAt(int row, int column)
290     {
291         return model.getColumnValue(column, row);
292     }
293
294     /**
295      * Sets the ValueAt attribute of the Arguments object.
296      *
297      * @param value the new ValueAt value
298      */

299     public void setValueAt(Object JavaDoc value, int row, int column)
300     {
301         if (row < model.size())
302         {
303             model.setCurrentPos(row);
304             model.addColumnValue(model.getHeaders()[column], value);
305         }
306     }
307 }
Popular Tags