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

package salsa.debug;

import java.beans.*;
import java.lang.reflect.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class BeanPropertyTableModel extends AbstractTableModel
{

   final static String _columnName[] = new String[]{
         "Name", "Type", "Access", "Bound"
         };

   final static Class _columnType[] = new Class[]{
         String.class, Class.class, String.class, Boolean.class
         };
   PropertyDescriptor _props[];

   public BeanPropertyTableModel( Class clazz )
          throws IntrospectionException
   {
      BeanInfo beanInfo = Introspector.getBeanInfo( clazz );
      _props = beanInfo.getPropertyDescriptors();

      // do a case-insensitive sort by property name
      Arrays.sort( _props,
         new Comparator()
         {
            public int compare( Object l, Object r )
            {
               PropertyDescriptor lhs = ( PropertyDescriptor ) l;
               PropertyDescriptor rhs = ( PropertyDescriptor ) r;

               return lhs.getName().compareToIgnoreCase( rhs.getName() );
            }
         } );
   }

   public Class getColumnClass( int column )
   {
      return _columnType[column];
   }

   public int getColumnCount()
   {
      return _columnName.length;
   }

   public String getColumnName( int column )
   {
      return _columnName[column];
   }

   public int getRowCount()
   {
      return _props.length;
   }

   public Object getValueAt( int row, int column )
   {
      PropertyDescriptor prop = _props[row];
      switch ( column )
      {
         case 0:
            return prop.getName();
         case 1:
            return prop.getPropertyType();
         case 2:
            return getAccessType( prop );
         case 3:
            return new Boolean( prop.isBound() );
         default:
            return null;
      }
   }

   private String getAccessType( PropertyDescriptor prop )
   {
      Method reader = prop.getReadMethod();
      // aka getter
      Method writer = prop.getWriteMethod();
      // aka setter

      if( ( reader != null ) && ( writer != null ) )
         return "Read/Write";
      else if( reader != null )
         return "Read-Only";
      else if( writer != null )
         return "Write-Only";
      else
         return "No Access";
   }
}