FileDocCategorySizeDatePackage
TemperatureBeanInfo.javaAPI DocExample3130Sun Jan 04 21:18:34 GMT 1998BeansBook.Simulator

TemperatureBeanInfo.java

// This example is from the book Developing Java Beans by Robert Englander. 
// Copyright (c) 1997 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

// Chapter 9 -- The TemperatureBeanInfo class

package BeansBook.Simulator;

import java.beans.*;
import java.lang.reflect.*;

public class TemperatureBeanInfo extends SimpleBeanInfo
{
   // return a bean descriptor for the Temperature object
   public BeanDescriptor getBeanDescriptor() 
   {
      // create an instance of BeanDescriptor
      BeanDescriptor bd = new BeanDescriptor(Temperature.class);
       
      // set the display name
      bd.setDisplayName("Temperature Source");
      
      // return the descriptor
      return bd;
   }
   
   // return the property descriptors
   public PropertyDescriptor[] getPropertyDescriptors()
   {
      try
      {
         // get the Temperature class
         Class c = Temperature.class;

         // get the get method for the Temperature property         
         Method getter = c.getMethod("returnTemperature", null);
         
         // create the parameters array for the set method of the
         // Temperature property
         Class[] params = { java.lang.Double.TYPE };
         
         // get the set method
         Method setter = c.getMethod("assignTemperature", params);
         
         // create a property descriptor for the Temperature property
         PropertyDescriptor pd = new PropertyDescriptor("Temperature",
                      getter, setter);
                      
         // the Temperature property is bound
         pd.setBound(true);
         
         // the Temperature property is not constrained
         pd.setConstrained(false);
         
         // create the property descriptor array and return it
         PropertyDescriptor[] pda = { pd };
         return pda;
      }
      catch (IntrospectionException e)
      {
         return null;
      }
      catch (NoSuchMethodException e)
      {
         return null;
      }
      catch (SecurityException e)
      {
         return null;
      }
   }
   
   // return the event set descriptors
   public EventSetDescriptor[] getEventSetDescriptors()
   {
      try
      {
         // the method names for the listener interface
         String[] names = { "propertyChange" } ;
         
         // create the event set descriptor
         EventSetDescriptor ed = new EventSetDescriptor(Temperature.class,
                            "Property Change Event",
                            PropertyChangeListener.class,
                            names,
                            "addPropertyChangeListener",
                            "removePropertyChangeListener");
         
         // create the descriptor array and return it
         EventSetDescriptor[] eda = { ed } ;
         return eda;
      }
      catch (IntrospectionException e)
      {
         return null;
      }
   }
}