BeanInfo Class

It is highly recommended that every Java bean have an associated BeanInfo class. If a BeanInfo class is not found, Java will try to ascertain all methods and properties by "reflection" which will lead to a huge list (especially if the bean extends a Java swing component). Such a large list would be undesirable since we would not want to expose every single method and property (that has a getter and setter method) in the class. A BeanInfo class will have to extend SimpleBeanInfo.

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

public class MyClassBeanInfo extends SimpleBeanInfo

protected Class  m_classBeanSrc;

/**
* Constructor
*/
public MyClassBeanInfo ()

m_classBeanSrc = MyClass.class;
}

/**
* Returns the bean descriptor object for this bean.
* @return BeanDescriptor The bean descriptor for this bean
*/
public BeanDescriptor getBeanDescriptor()

BeanDescriptor beanDesc = new BeanDescriptor(m_classBeanSrc);
beanDesc.setDisplayName("My Class");
beanDesc.setShortDescription("My first Bean Class");
return beanDesc;
}

/**
* Returns the Property Descriptor for MyClass bean
* @returns The Property descriptor array, representing the
* properties of the MyClass bean
*/
public PropertyDescriptor[] getPropertyDescriptors()

try

PropertyDescriptor propDesc1 = new PropertyDescriptor(
"My Property", m_classBeanSrc,
"GetMyProperty", "SetMyProperty");
propDesc1.setDisplayName("Text");
propDesc1.setShortDescription("Set label for the control");
propDesc1.setBound(true);
PropertyDescriptor[] arrPropDescs = {propDesc1};
return arrPropDescs;
}
catch (IntrospectionException exceptionIntrospection )

exceptionIntrospection.printStackTrace();
return null;
}
}

/**
* Returns the Method Descriptor for MyClass bean
* @returns The Method descriptor array, representing the
* Methods of the MyClass bean
*/
public MethodDescriptor[] getMethodDescriptors()

try

Method meth1 = m_classBeanSrc.getMethod("DoSomething", null);
MethodDescriptor methDesc1 = new MethodDescriptor(meth1);
MethodDescriptor[] arrmethDescNew = { methDesc1 };
return arrmethDescriptors;
}
catch ( NoSuchMethodException exceptionNoSuchMethod )

exceptionNoSuchMethod.printStackTrace();
return null;
}
}

/**
* Show only the HWMCmdEvent
* @return EventSetDescriptor[] The array of events that the bean
* exposes
*/
public EventSetDescriptor[] getEventSetDescriptors()

 return null;
}
}

Most of our beans will extend Java swing components, such as JPanel, JButton, and so on. Some of the properties that all such beans will want to expose include foreground, background colors, font, visible, and enabled. Similarly, all such beans will want to expose methods such as enable, disable, show, hide, and repaint.

For simplicity in doing the above, Process Manager has provided a utility class called HWMSimpleBeanInfo. The BeanInfo classes can extend this class, define the properties and methods specific to their bean. To expose properties and methods mentioned in BeanInfo class, Process Manager has provided two methods by name "AppendPropertyDescriptors" and "AppendMethodDescriptors" in HWMSimpleBeanInfo.

Instead of every BeanInfo class exposing the above mentioned properties and methods, Process Manager has provided:

import java.beans.*;
import java.lang.reflect.*;
import com.altair.hwm.beans.utils.HWMSimpleBeanInfo;

public class MyClassBeanInfo extends HWMSimpleBeanInfo

/**
* Constructor
*/
public MyClassBeanInfo ()

super(MyClass.class);
}

/**
* Returns the bean descriptor object for this bean.
* @return BeanDescriptor The bean descriptor for this bean
*/
public BeanDescriptor getBeanDescriptor()

BeanDescriptor beanDesc = new BeanDescriptor(m_classBeanSrc);
beanDesc.setDisplayName("My Class");
beanDesc.setShortDescription("My first Bean Class");
return beanDesc;
}

/**
* Returns the Property Descriptor for MyClass bean
* @returns The Property descriptor array, representing the
* properties of the MyClass bean
*/
public PropertyDescriptor[] getPropertyDescriptors()

try

PropertyDescriptor propDesc1 = new PropertyDescriptor(
"My Property", m_classBeanSrc,
"GetMyProperty", "SetMyProperty");
propDesc1.setDisplayName("Text");
propDesc1.setShortDescription("Set label for the control");
propDesc1.setBound(true);
PropertyDescriptor[] arrPropDescs = {propDesc1};
return super.AppendPropertyDescriptors(arrPropDescs);
}
catch (IntrospectionException exceptionIntrospection )

exceptionIntrospection.printStackTrace();
return null;
}
}
 
/**
* Returns the Method Descriptor for MyClass bean
* @returns The Method descriptor array, representing the
* Methods of the MyClass bean
*/
public MethodDescriptor[] getMethodDescriptors()

try

Method meth1 = m_classBeanSrc.getMethod("DoSomething", null);
MethodDescriptor methDesc1 = new MethodDescriptor(meth1);
MethodDescriptor[] arrmethDescNew = { methDesc1 };
return super.AppendMethodDescriptors(arrmethDescriptors);
}
catch ( NoSuchMethodException exceptionNoSuchMethod )

exceptionNoSuchMethod.printStackTrace();
return null;
}
}

/**
*  Show only the HWMCmdEvent
*  @return EventSetDescriptor[] The array of events that the bean
* exposes
*/
public EventSetDescriptor[] getEventSetDescriptors()

 return super.GetEventSetDescriptors();
}
}