Previous Page
Next Page

13.3. Displaying Properties in the Properties View

Another place that properties can be displayed and edited is in the Properties view. The Properties view examines the workbench selection to determine whether the selected objects support the org.eclipse.ui.views.properties.IPropertySource interface. An object can support the IPropertySource interface by either directly implementing IPropertySource or by implementing the getAdapter() method to return an object that implements the IPropertySource interface (see Figure 13-4).

Figure 13-4. From selection to the Properties view.


13.3.1. Properties view API

The IPropertySource interface provides a descriptor for each property to be displayed in the Properties view, as well as methods for getting and setting property values. The id argument in the methods that follow is the identifier associated with the descriptor for that property.

getPropertyDescriptors() Returns an array of descriptors, one for each property to be displayed in the Properties view.

getPropertyValue(Object) Returns the value of the property that has the specified identifier.

isPropertySet(Object) Returns true if the property specified by the identifier has a value different than its default value.

resetPropertyValue(Object) Sets the value of the property specified by the identifier to its default value.

setPropertyValue(Object, Object) Sets the value of the property specified by the identifier to the specified value.

Optionally, objects can implement the IPropertySource2 interface to allow an easier indication of properties that have a default value and can be reset.

isPropertyResettable(Object) Returns whether the value of the property with the specified id can be reset to a default value.

isPropertySet(Object) Very similar to the identical method in IPropertySource except, if an object implements IPropertySource2, then this method should return true rather than false if the referenced property does not have a meaningful default value.

Property descriptorsobjects that implement the IPropertyDescriptor interfacecontain a property identifier and create a property editor as necessary for the Properties view. Eclipse provides some implementations of the IPropertyDescriptor interface (see Figure 13-5).

Figure 13-5. IPropertyDescriptor hierarchy.


Instances of PropertyDescriptor are constructed with a property identifier and a display name for the property. If an object has many properties, then it's useful to group similar properties visually by calling setCategory() on each descriptor in the group.

Other useful methods include:

setAlwaysIncompatible(boolean) Sets a flag indicating whether the property descriptor is to be considered always incompatible with any other property descriptor. Setting this flag prevents a property from displaying during multiple selections.

setCategory(String) Sets the name of the category to which the property belongs. Properties belonging to the same category are grouped together visually. This localized string is shown to the user. If the category is not set on any of the descriptors, the property will appear at the top level in the Properties view without being grouped. If the category is set on at least one descriptor, then any descriptors with an unset category will appear in a miscellaneous category.

setDescription(String) Sets a brief description of the property. This localized string is shown to the user when the property is selected.

setFilterFlags(String[]) Sets a list of filter types to which the property belongs. The user is able to toggle the filters to show/hide properties belonging to a filter type. Currently, the only valid value for these flags is IPropertySheetEntry.FILTER_ID_EXPERT.

setHelpContextIds(Object) Sets the help context ID for the property. Even though the method name is plural, only a string can be specified, indicating the singular associated help context (see Section 15.3.1, Associating context IDs with items, on page 553).

setLabelProvider(ILabelProvider) Sets the label provider for the property. The label provider is used to obtain the text (and possible image) for displaying the value of this property.

setValidator(ICellEditorValidator) Sets the input validator for the cell editor for this property descriptor.

13.3.2. Favorite properties in the Properties view

For the Favorites product, you want to display the Color property and a Hash Code property. Using setAlwaysIncompatible(true), you specify that the Hash Code property should appear in the Properties view only when the Show Advanced Properties option is turned on. The Favorites view is already a workbench selection provider (see Section 7.4.1, Selection provider, on page 305), so the Properties view is already examining the selected Favorites items. All that's left is for BasicFavoriteItem to implement the IPropertySource2 interface.

private static final String COLOR_ID = "favorite.color";
private static final ColorPropertyDescriptor
COLOR_PROPERTY_DESCRIPTOR =
   new ColorPropertyDescriptor(COLOR_ID, "Color");

private static final String HASH_ID = "favorite.hash";
private static final TextPropertyDescriptor HASH_PROPERTY_DESCRIPTOR
  = new TextPropertyDescriptor(HASH_ID, "Hash Code");
static {
   HASH_PROPERTY_DESCRIPTOR.setCategory("Other");
   HASH_PROPERTY_DESCRIPTOR.setFilterFlags(
      new String[] {IPropertySheetEntry.FILTER_ID_EXPERT });
   HASH_PROPERTY_DESCRIPTOR.setAlwaysIncompatible(true);
}

private static final IPropertyDescriptor[] DESCRIPTORS =
   { COLOR_PROPERTY_DESCRIPTOR, HASH_PROPERTY_DESCRIPTOR };

public Object getEditableValue() {
   return this;
}

public IPropertyDescriptor[] getPropertyDescriptors() {
   return DESCRIPTORS;
}

public Object getPropertyValue(Object id) {
   if (COLOR_ID.equals(id))
      return getColor().getRGB();
   if (HASH_ID.equals(id))
      return new Integer(hashCode());
   return null;
}

public boolean isPropertyResettable(Object id) {
   if (COLOR_ID.equals(id))
      return true;
   return false;
}

public boolean isPropertySet(Object id) {
   if (COLOR_ID.equals(id))
       return getColor() != getDefaultColor();
   if (HASH_ID.equals(id)) {
      // Return true for indicating that hash
      // does not have a meaningful default value.
      return true;
   }
   return false;
}

public void resetPropertyValue(Object id) {
   if (COLOR_ID.equals(id))
      setColor(null);
}

public void setPropertyValue(Object id, Object value) {
   if (COLOR_ID.equals(id))
      setColor(getColor((RGB) value));
}

Now, when an item is selected in the Favorites view, the Properties view displays the Color property for that item. When the Show Advanced Properties option is turned on, the Hash Code property appears (see Figure 13-6).

Figure 13-6. Properties view showing expert properties.



Previous Page
Next Page