Listing Two

package mybeans;

import java.awt.Image;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;

//
//
// CaseAwareTextDisplayBeanInfo
//
//
public class CaseAwareTextDisplayBeanInfo extends SimpleBeanInfo
{
  public static final String BEAN_CLASS_NAME = "mybeans.CaseAwareTextDisplay";

  //------------------------------------------------------------
  // Define an icon for the bean. The image must be in the
  // bean's subdirectory in the jar, but the subdirectory
  // is not referred to here since its known from the
  // package (fully qualified class name).
  public Image getIcon(int iconKind) {
    if (iconKind == BeanInfo.ICON_COLOR_32x32)
      return loadImage(getBeanIconString());
    else
      return null;
  }

  //------------------------------------------------------------
  // Return the property descriptors
  public PropertyDescriptor[] getPropertyDescriptors() {

    PropertyDescriptor[] pd = new PropertyDescriptor[6];
    PropertyDescriptor[] superPd;
    PropertyDescriptor[] finalPd;

    try {
      // Build the property descriptor for the text property
      pd[0] =
        new PropertyDescriptor( CaseAwareTextDisplay.TEXT,
                                Class.forName(BEAN_CLASS_NAME),
                                "getText", "setText");
      pd[0].setBound(true);
      pd[0].setConstrained(false);
      pd[0].setDisplayName("text");
      pd[0].setExpert(false);
      pd[0].setHidden(false);
      pd[0].setShortDescription("The text to be displayed.");

      // Build the property descriptor for the topMargin property
      pd[1] =
        new PropertyDescriptor( CaseAwareTextDisplay.TOP_MARGIN,
                                Class.forName(BEAN_CLASS_NAME),
                                "getTopMargin", "setTopMargin");
      pd[1].setBound(true);
      pd[1].setConstrained(false);
      pd[1].setDisplayName("top margin");
      pd[1].setExpert(false);
      pd[1].setHidden(false);
      pd[1].setShortDescription("The top margin in pixels.");

      // Build the property descriptor for the leftMargin property
      pd[2] =
        new PropertyDescriptor( CaseAwareTextDisplay.LEFT_MARGIN,
                                Class.forName(BEAN_CLASS_NAME),
                                "getLeftMargin", "setLeftMargin");
      pd[2].setBound(true);
      pd[2].setConstrained(false);
      pd[2].setDisplayName("left margin");
      pd[2].setExpert(false);
      pd[2].setHidden(false);
      pd[2].setShortDescription("The left margin in pixels.");

      // Build the property descriptor for the foreground property
      pd[3] =
        new PropertyDescriptor( CaseAwareTextDisplay.FOREGROUND,
                                Class.forName(BEAN_CLASS_NAME),
                                "getForeground", "setForeground");
      pd[3].setBound(true);
      pd[3].setConstrained(true);
      pd[3].setDisplayName("foreground");
      pd[3].setExpert(false);
      pd[3].setHidden(false);
      pd[3].setShortDescription("The foreground color for the text.");

      // Build the property descriptor for the background property
      pd[4] =
        new PropertyDescriptor( CaseAwareTextDisplay.BACKGROUND,
                                Class.forName(BEAN_CLASS_NAME),
                                "getBackground", "setBackground");
      pd[4].setBound(true);
      pd[4].setConstrained(true);
      pd[4].setDisplayName("background");
      pd[4].setExpert(false);
      pd[4].setHidden(false);
      pd[4].setShortDescription("The background color for the text.");

      // Build the property descriptor for the textCase property
      pd[5] = getTextCasePropertyDescriptor();
    }
    catch (Throwable t) {
      t.printStackTrace();
      return null;      // use default design patterns
    }

    // Get the property descriptors of the superclass.
    // The font property is implemented by Canvas.
    // This logic is needed because some tools do not call getAdditionalBeanInfo.
    try {
      BeanInfo superBeanInfo = Introspector.getBeanInfo(Class.forName(BEAN_CLASS_NAME).getSuperclass());
      superPd = superBeanInfo.getPropertyDescriptors();
    }
    catch (Throwable t) {
      t.printStackTrace();
      return null;      // use default design patterns
    }

    // Add the two sets of property descriptors to a single array
    finalPd = new PropertyDescriptor[superPd.length + pd.length];
    for (int i = 0; i < superPd.length; i++)
      finalPd[i] = superPd[i];
    for (int i = superPd.length; i < (superPd.length + pd.length); i++)
      finalPd[i] = pd[i - superPd.length];

    // Return the array of property descriptors
    return finalPd;
  }

  //---------------------------------------------------------------------------
  // Return the bean info for the superclasses, excluding the Object class.
  // This handles the font property which is implemented in the Canvas class.
  public BeanInfo[] getAdditionalBeanInfo() {
    try {
      BeanInfo[] bi = new BeanInfo[1];
      bi[0] = Introspector.getBeanInfo(Class.forName(BEAN_CLASS_NAME).getSuperclass());
      return bi;
    }
    catch (Throwable t) {
    }
    return null;
  }

  //---------------------------------------------------------------------------
  // Returns a property descriptor for the textCase property. This is
  // implemented as a separate method solely for convenience in subclassing.
  protected PropertyDescriptor getTextCasePropertyDescriptor()
  throws IntrospectionException, ClassNotFoundException {
    PropertyDescriptor pd;
    pd = new PropertyDescriptor(CaseAwareTextDisplay.TEXT_CASE,
                                Class.forName(BEAN_CLASS_NAME),
                                "getTextCase", "setTextCase");
    pd.setBound(true);
    pd.setConstrained(false);
    pd.setDisplayName("case");
    pd.setExpert(false);
    pd.setHidden(false);
    pd.setShortDescription("Case to use for displaying the text.");
    return pd;
  }

  //---------------------------------------------------------------------------
  // Returns the name of the file containing the beans icon. This is
  // implemented as a separate method solely for convenience in subclassing.
  protected String getBeanIconString() {
    return "Earth.gif";
  }
}
