Specifying Custom Property Attributes

When you create a property and create a PropertyInfo object to set its attributes, you can specify a variety of options to customize the property.

Note   More details about using the PropertyInfo class are provided in Defining Control Properties.

The basic syntax for creating a PropertyInfo object is:

new PropertyInfo(Class owner, String name, Class dataType)

For example, the following is a PropertyInfo object for a property called myProp in the MyControl control:

public static final PropertyInfo myProp = 
   new PropertyInfo(MyControl.class, "myProp", int.class);

You can customize the property by providing any number of additional member attributes for the PropertyInfo object. A member attribute is defined as any class that derives from com.ms.wfc.core.MemberAttribute. A certain number of attributes are available by default in the PropertyInfo class, or you can create and add your own attributes by deriving them from MemberAttribute. The following table lists pre-defined attributes in the PropertyInfo class.

Attribute Description
BrowsableAttribute Specifies whether the property is visible in the Properties window. The default is YES. This attribute can be used in conjunction with the PersistableAttribute attribute (see below) to define properties whose value should be saved in the control but should not be editable in the Properties window.
CategoryAttribute Specifies what category the current property fits into in the Properties window, such as Appearance, Behavior, and so on. The default is Misc. You can either place the property in a predefined category by using one of the static members in CategoryAttribute or create a new category by constructing a new CategoryAttribute and passing in the string name that you want the category to be called.
DataBindableAttribute Specifies that the property is a candidate for data binding. The default is .NO. When this is set to .YES, the DataBinder component lists this property in its drop-down list.
DefaultValueAttribute The default value for simple properties. This value is used when the user resets the property value in the Properties window. The Forms Designer also compares the default value against the current value. If they match, the Forms Designer does not generate code to set the property value, which reduces the size of code generated for the form.

Note   You can also create a default value by creating a reset<property> method. For details, see Specifying Property Persistence and Default Values.

The most common values for various data types are predefined as static values (for example, for boolean values, the values TRUE and FALSE are already defined).

DescriptionAttribute Defines text displayed in the bottom of the Properties window when the user selects the current property. The default is "".
LocalizableAttribute Specifies that the property value is saved in a resource file if the user chooses to localize a form. The default is NO. The user can then localize the resource file and not have to modify the code.
PersistableAttribute Specifies whether the value of the property is saved when its container is saved at design time. The default value is YES. This attribute is primarily useful when you do not want to save a property value, for example, transient property values whose values depend on state or are calculated. Normally, properties with this attribute set to NO are also not browsable (BrowsableAttribute is NO).
ValueEditorAttribute Specifies a custom editor used by the Properties window for the current property. If no value editor is specified, the Properties window uses an editor associated with the property's data type. For more details, see Creating a Custom Properties Value Editor.

To specify an attribute, you create an instance of the attribute you want or use one of the predefined instances (such as BrowsableAttribute.NO), and then add it to the PropertyInfo definition, using syntax such as:

new PropertyInfo(Class owner, String name, Class dataType, 
   [MemberAttribute, [MemberAttribute, ...]])

The following example shows how you can define a description and default value for the myProp property using predefined attributes:

public static final PropertyInfo myProp = 
   new PropertyInfo(MyControl.class, "myProp", int.class, 
   new DescriptionAttribute("Test property"), 
   new DefaultValueAttribute(DefaultValueAttribute.ONE);

You can add up to six attributes by simply including them as parameters to the PropertyInfo constructor. If you want to specify more than five member attributes, you can use syntax for the PropertyInfo constructor that accepts an array of member attributes. For details, see PropertyInfo.