1 package org.lcsim.plugin.browser;
2
3 import java.lang.reflect.Method;
4
5 import javax.swing.table.AbstractTableModel;
6 import java.util.List;
7 import org.lcsim.event.EventHeader.LCMetaData;
8
9
10
11
12
13 abstract class GenericTableModel extends AbstractTableModel implements EventBrowserTableModel
14 {
15 private List data;
16 private String[] columns;
17 private Class klass;
18
19 GenericTableModel(Class beanClass, String[] columns)
20 {
21 this.columns =columns;
22 this.klass = beanClass;
23 }
24 public boolean canDisplay(Class c)
25 {
26 return klass.isAssignableFrom(c);
27 }
28 public void setData(LCMetaData meta, List data)
29 {
30 this.data = data;
31 fireTableDataChanged();
32 }
33 public int getRowCount()
34 {
35 return data == null ? 0 : data.size();
36 }
37 public int getColumnCount()
38 {
39 return columns.length;
40 }
41 public String getColumnName(int index)
42 {
43 return columns[index];
44 }
45 public Class getColumnClass(int index)
46 {
47 try
48 {
49 String name = columns[index];
50 Method m = klass.getMethod("get"+name,(Class[]) null);
51 Class returnType = m.getReturnType();
52 if (returnType.isPrimitive())
53 {
54 if (returnType == Integer.TYPE) return Integer.class;
55 if (returnType == Short.TYPE) return Short.class;
56 if (returnType == Long.TYPE) return Long.class;
57 if (returnType == Double.TYPE) return Double.class;
58 if (returnType == Float.TYPE) return Float.class;
59 if (returnType == Byte.TYPE) return Byte.class;
60 if (returnType == Character.TYPE) return Character.class;
61 if (returnType == Boolean.TYPE) return Boolean.class;
62 }
63 return returnType;
64 }
65 catch (Throwable t)
66 {
67 return Throwable.class;
68 }
69 }
70 protected Object getData(int row)
71 {
72 return data.get(row);
73 }
74 public Object getValueAt(int row, int column)
75 {
76 try
77 {
78 Object bean = getData(row);
79 String name = columns[column];
80 Method m = klass.getMethod("get"+name,(Class[]) null);
81 return m.invoke(bean,(Object[]) null);
82 }
83 catch (Throwable t)
84 {
85 return t;
86 }
87 }
88 }