KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > salsa > debug > BeanPropertyTableModel


1 /*
2 ** Salsa - Swing Add-On Suite
3 ** Copyright (c) 2001, 2002 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

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 JavaDoc _columnName[] = new String JavaDoc[]{
35          "Name", "Type", "Access", "Bound"
36          };
37
38    final static Class JavaDoc _columnType[] = new Class JavaDoc[]{
39          String JavaDoc.class, Class JavaDoc.class, String JavaDoc.class, Boolean JavaDoc.class
40          };
41    PropertyDescriptor _props[];
42
43    public BeanPropertyTableModel( Class JavaDoc clazz )
44           throws IntrospectionException
45    {
46       BeanInfo beanInfo = Introspector.getBeanInfo( clazz );
47       _props = beanInfo.getPropertyDescriptors();
48
49       // do a case-insensitive sort by property name
50
Arrays.sort( _props,
51          new Comparator()
52          {
53             public int compare( Object JavaDoc l, Object JavaDoc 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 JavaDoc getColumnClass( int column )
64    {
65       return _columnType[column];
66    }
67
68    public int getColumnCount()
69    {
70       return _columnName.length;
71    }
72
73    public String JavaDoc getColumnName( int column )
74    {
75       return _columnName[column];
76    }
77
78    public int getRowCount()
79    {
80       return _props.length;
81    }
82
83    public Object JavaDoc 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 JavaDoc( prop.isBound() );
96          default:
97             return null;
98       }
99    }
100
101    private String JavaDoc getAccessType( PropertyDescriptor prop )
102    {
103       Method reader = prop.getReadMethod();
104       // aka getter
105
Method writer = prop.getWriteMethod();
106       // aka setter
107

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