1 18 19 package org.apache.jorphan.gui; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.swing.event.TableModelEvent ; 29 import javax.swing.table.DefaultTableModel ; 30 31 import org.apache.jorphan.logging.LoggingManager; 32 import org.apache.log.Logger; 33 34 37 public class ObjectTableModel extends DefaultTableModel 38 { 39 private static Logger log = LoggingManager.getLoggerForClass(); 40 private transient ArrayList objects = new ArrayList (); 41 private transient List headers = new ArrayList (); 42 private transient ArrayList classes = new ArrayList (); 43 private transient Class objectClass; 44 45 private transient ArrayList setMethods = new ArrayList (); 46 private transient ArrayList getMethods = new ArrayList (); 47 48 public ObjectTableModel(String [] headers, String [] propertyNames, Class [] propertyClasses, Class [] renderClasses, Object sampleObject) 49 { 50 this.headers.addAll(Arrays.asList(headers)); 51 this.classes.addAll(Arrays.asList(renderClasses)); 52 objectClass = sampleObject.getClass(); 53 Class [] emptyClasses = new Class [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 .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 [] { propertyClasses[i] })); 77 } 78 catch (NoSuchMethodException e) 79 { 80 log.error("Invalid Method name for class: " + objectClass, e); 81 } 82 } 83 } 84 85 public Iterator 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 value) 98 { 99 objects.add(value); 100 super.fireTableRowsInserted(objects.size() - 1, objects.size()); 101 } 102 103 106 public int getColumnCount() 107 { 108 return headers.size(); 109 } 110 111 114 public String getColumnName(int col) 115 { 116 return (String ) headers.get(col); 117 } 118 119 122 public int getRowCount() 123 { 124 if (objects == null) 125 { 126 return 0; 127 } 128 return objects.size(); 129 } 130 131 134 public Object getValueAt(int row, int col) 135 { 136 Object value = objects.get(row); 137 Method getMethod = (Method ) getMethods.get(col); 138 try 139 { 140 return getMethod.invoke(value, new Object [0]); 141 } 142 catch (IllegalAccessException e) 143 { 144 log.error("Illegal method access", e); 145 } 146 catch (InvocationTargetException e) 147 { 148 log.error("incorrect method access", e); 149 } 150 return null; 151 } 152 153 156 public boolean isCellEditable(int arg0, int arg1) 157 { 158 return true; 159 } 160 161 164 public void moveRow(int start, int end, int to) 165 { 166 List 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 (this)); 173 } 174 175 178 public void removeRow(int row) 179 { 180 objects.remove(row); 181 super.fireTableRowsDeleted(row, row); 182 } 183 184 187 public void setValueAt(Object cellValue, int row, int col) 188 { 189 if (row < objects.size()) 190 { 191 Object value = objects.get(row); 192 if (col < setMethods.size()) 193 { 194 Method setMethod = (Method ) setMethods.get(col); 195 try 196 { 197 setMethod.invoke(value, new Object [] { cellValue }); 198 } 199 catch (IllegalAccessException e) 200 { 201 log.error("Illegal method access", e); 202 } 203 catch (InvocationTargetException e) 204 { 205 log.error("incorrect method access", e); 206 } 207 super.fireTableDataChanged(); 208 } 209 } 210 } 211 212 215 public Class getColumnClass(int arg0) 216 { 217 return (Class ) classes.get(arg0); 218 } 219 220 } 221 | Popular Tags |