1 22 23 package salsa.debug; 24 25 import java.beans.*; 26 import java.lang.reflect.*; 27 import java.util.*; 28 import javax.swing.*; 29 import javax.swing.table.*; 30 31 public class BeanPropertyTableModel extends AbstractTableModel 32 { 33 34 final static String _columnName[] = new String []{ 35 "Name", "Type", "Access", "Bound" 36 }; 37 38 final static Class _columnType[] = new Class []{ 39 String .class, Class .class, String .class, Boolean .class 40 }; 41 PropertyDescriptor _props[]; 42 43 public BeanPropertyTableModel( Class clazz ) 44 throws IntrospectionException 45 { 46 BeanInfo beanInfo = Introspector.getBeanInfo( clazz ); 47 _props = beanInfo.getPropertyDescriptors(); 48 49 Arrays.sort( _props, 51 new Comparator() 52 { 53 public int compare( Object l, Object r ) 54 { 55 PropertyDescriptor lhs = ( PropertyDescriptor ) l; 56 PropertyDescriptor rhs = ( PropertyDescriptor ) r; 57 58 return lhs.getName().compareToIgnoreCase( rhs.getName() ); 59 } 60 } ); 61 } 62 63 public Class getColumnClass( int column ) 64 { 65 return _columnType[column]; 66 } 67 68 public int getColumnCount() 69 { 70 return _columnName.length; 71 } 72 73 public String getColumnName( int column ) 74 { 75 return _columnName[column]; 76 } 77 78 public int getRowCount() 79 { 80 return _props.length; 81 } 82 83 public Object getValueAt( int row, int column ) 84 { 85 PropertyDescriptor prop = _props[row]; 86 switch ( column ) 87 { 88 case 0: 89 return prop.getName(); 90 case 1: 91 return prop.getPropertyType(); 92 case 2: 93 return getAccessType( prop ); 94 case 3: 95 return new Boolean ( prop.isBound() ); 96 default: 97 return null; 98 } 99 } 100 101 private String getAccessType( PropertyDescriptor prop ) 102 { 103 Method reader = prop.getReadMethod(); 104 Method writer = prop.getWriteMethod(); 106 108 if( ( reader != null ) && ( writer != null ) ) 109 return "Read/Write"; 110 else if( reader != null ) 111 return "Read-Only"; 112 else if( writer != null ) 113 return "Write-Only"; 114 else 115 return "No Access"; 116 } 117 } 118 | Popular Tags |