1 4 package com.tc.admin.common; 5 6 import java.lang.reflect.Method ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 10 import javax.swing.table.AbstractTableModel ; 11 12 15 16 public class PropertyTableModel extends AbstractTableModel { 17 private Class 18 m_type; 19 20 private Object 21 m_instance; 22 23 private String [] 24 m_fieldNames; 25 26 private String [] 27 m_headings; 28 29 protected Method 30 m_getters[]; 31 32 protected Method 33 m_setters[]; 34 35 protected Method 36 m_ops[]; 37 38 protected static final HashMap 39 m_primitiveMap = new HashMap (); 40 41 private static final String FIELD_HEADER = "Field"; 42 private static final String VALUE_HEADER = "Value"; 43 44 public static final int FIELD_COLUMN = 0; 45 public static final int VALUE_COLUMN = 1; 46 public static final int COLUMN_COUNT = 2; 47 48 static { 49 m_primitiveMap.put(Double.TYPE, Double .class); 50 m_primitiveMap.put(Integer.TYPE, Integer .class); 51 m_primitiveMap.put(Boolean.TYPE, Boolean .class); 52 m_primitiveMap.put(Character.TYPE, Character .class); 53 m_primitiveMap.put(Byte.TYPE, Byte .class); 54 m_primitiveMap.put(Float.TYPE, Float .class); 55 m_primitiveMap.put(Long.TYPE, Long .class); 56 } 57 58 public PropertyTableModel() { 59 super(); 60 } 61 62 public PropertyTableModel(Object instance) { 63 this(instance, null, null); 64 } 65 66 public PropertyTableModel(Object instance, String [] fields) { 67 this(instance, fields, null); 68 } 69 70 public PropertyTableModel(Class type) { 71 this(type, null, null); 72 } 73 74 public PropertyTableModel(Class type, String [] fields) { 75 this(type, fields, null); 76 } 77 78 public PropertyTableModel( 79 Object instance, 80 String [] fields, 81 String [] headings) 82 { 83 this(instance.getClass(), fields, headings); 84 setInstance(instance); 85 } 86 87 public PropertyTableModel( 88 Class type, 89 String [] fields, 90 String [] headers) 91 { 92 super(); 93 init(type, fields, headers); 94 } 95 96 public void setInstance(Object instance) { 97 m_instance = instance; 98 fireTableDataChanged(); 99 } 100 101 public Object getInstance() { 102 return m_instance; 103 } 104 105 public void init( 106 Class type, 107 String [] fields, 108 String [] headings) 109 { 110 m_type = type; 111 m_fieldNames = determineFields(fields); 112 m_headings = determineHeadings(headings); 113 114 setup(); 115 } 116 117 public void setup() { 118 if(m_type != null) { 119 int size = m_fieldNames.length; 120 121 m_setters = new Method [size]; 122 m_getters = new Method [size]; 123 m_ops = new Method [size]; 124 125 for(int i = 0; i < m_fieldNames.length; i++) { 126 determineMethods(i, m_fieldNames[i]); 127 } 128 } 129 } 130 131 private Class _mapPrimitive(Class c) { 132 return (Class )m_primitiveMap.get(c); 133 } 134 135 140 private String [] determineFields(String fieldNames[]) { 141 if(fieldNames == null) { 142 if(m_type != null) { 143 Method method; 144 Method [] methods = m_type.getMethods(); 145 ArrayList fieldList = new ArrayList (); 146 String methodName; 147 Class returnType; 148 Class [] paramTypes; 149 150 for(int i = 0; i < methods.length; i++) { 151 method = methods[i]; 152 returnType = method.getReturnType(); 153 paramTypes = method.getParameterTypes(); 154 methodName = method.getName(); 155 156 if(paramTypes.length == 0 && 157 (methodName.startsWith("get") || methodName.startsWith("is")) && 158 (returnType.isPrimitive() || 159 returnType.equals(String .class) || 160 returnType.equals(java.util.Date .class) || 161 hasEditor(returnType))) 162 { 163 int j = 0; 164 165 while(!Character.isUpperCase(methodName.charAt(j))) j++; 166 fieldList.add(methodName.substring(j)); 167 } 168 else if(paramTypes.length == 0 && returnType == Void.TYPE) { 169 fieldList.add(methodName); 170 } 171 } 172 173 fieldNames = (String [])fieldList.toArray(new String []{}); 174 } 175 } 176 177 return fieldNames; 178 } 179 180 184 private static String fieldName2Heading(String fieldName) { 185 StringBuffer sb = new StringBuffer (); 186 int len = fieldName.length(); 187 char c; 188 189 sb.append(Character.toUpperCase(fieldName.charAt(0))); 190 191 for(int i = 1; i < len; i++) { 192 c = fieldName.charAt(i); 193 194 if(Character.isUpperCase(c)) { 195 sb.append(" "); 196 sb.append(Character.toLowerCase(c)); 197 } 198 else { 199 sb.append(c); 200 } 201 } 202 203 return sb.toString(); 204 } 205 206 private String [] determineHeadings(String headings[]) { 207 if(headings == null) { 208 ArrayList headingList = new ArrayList (); 209 210 for(int i = 0; i < m_fieldNames.length; i++) { 211 headingList.add(fieldName2Heading(m_fieldNames[i])); 212 } 213 214 headings = (String [])headingList.toArray(new String []{}); 215 } 216 217 return headings; 218 } 219 220 private void determineMethods(int index, String name) { 221 Method [] methods = m_type.getMethods(); 222 Method method; 223 String methodName; 224 Class returnType; 225 Class [] paramTypes; 226 227 for(int i = 0; i < methods.length; i++) { 228 method = methods[i]; 229 returnType = method.getReturnType(); 230 paramTypes = method.getParameterTypes(); 231 methodName = method.getName(); 232 233 if(("set"+name).equals(methodName) && 234 paramTypes.length == 1 && 235 (paramTypes[0].isPrimitive() || 236 paramTypes[0].equals(String .class) || 237 paramTypes[0].equals(java.util.Date .class) || 238 hasEditor(paramTypes[0]))) 239 { 240 m_setters[index] = method; 241 break; 242 } 243 } 244 245 for(int i = 0; i < methods.length; i++) { 246 method = methods[i]; 247 returnType = method.getReturnType(); 248 paramTypes = method.getParameterTypes(); 249 methodName = method.getName(); 250 251 if((("get"+name).equals(methodName) || 252 ("is"+name).equals(methodName)) && 253 paramTypes.length == 0 && 254 (returnType.isPrimitive() || 255 returnType.equals(String .class) || 256 returnType.equals(java.util.Date .class) || 257 hasEditor(returnType))) 258 { 259 m_getters[index] = method; 260 break; 261 } 262 } 263 264 for(int i = 0; i < methods.length; i++) { 265 method = methods[i]; 266 methodName = method.getName(); 267 268 if(name.equals(methodName)) { 269 m_ops[index] = method; 270 break; 271 } 272 } 273 } 274 275 public int getRowCount() { 276 return m_instance != null ? m_fieldNames.length : 0; 277 } 278 279 public int getColumnCount() { 280 return COLUMN_COUNT; 281 } 282 283 public boolean isCellEditable(int row, int col) { 284 return (m_setters[row] != null) || (m_ops[row] != null); 285 } 286 287 public String getColumnName(int col) { 288 switch(col) { 289 case FIELD_COLUMN: return FIELD_HEADER; 290 case VALUE_COLUMN: return VALUE_HEADER; 291 } 292 293 return "PropertyTableModel: invalid column: "+col; 294 } 295 296 public Class getColumnClass(int col) { 297 return Object .class; 298 } 299 300 public Class getRowClass(int row) { 301 Method method = m_getters[row]; 302 303 if(method != null) { 304 Class rowClass = method.getReturnType(); 305 306 if(rowClass.isPrimitive()) { 307 rowClass = _mapPrimitive(rowClass); 308 } 309 310 return rowClass; 311 } 312 313 if((method = m_ops[row]) != null) { 314 return Method .class; 315 } 316 317 return Object .class; 318 } 319 320 private Object _getFieldValue(int fieldIndex) { 321 try { 322 return m_getters[fieldIndex].invoke(m_instance, new Object [] {}); 323 } 324 catch(Exception e) { 325 return e.getMessage(); 326 } 327 } 328 329 public String getFieldHeading(int row) { 330 return m_headings[row] != null ? 331 m_headings[row] : m_fieldNames[row]; 332 } 333 334 public Object getValueAt(int row, int col) { 335 switch(col) { 336 case FIELD_COLUMN: { 337 return getFieldHeading(row); 338 } 339 case VALUE_COLUMN: { 340 if(m_instance != null) { 341 Method method; 342 343 if((method = m_ops[row]) != null) { 344 return method; 345 } 346 else { 347 return _getFieldValue(row); 348 } 349 } 350 } 351 } 352 353 return ""; 354 } 355 356 public void setValueAt(Object value, int row, int col) { 357 Method setter = m_setters[col]; 358 359 if(setter != null) { 360 try { 361 setter.invoke(getValueAt(row, col), new Object [] {value}); 362 } 363 catch(Exception e) {} 364 } 365 } 366 367 public void clear() { 368 setInstance(null); 369 } 370 371 public boolean hasEditor(Class type) { 372 return false; 373 } 374 } 375 | Popular Tags |