13.3. Displaying Properties in the Properties ViewAnother 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 APIThe 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.
Optionally, objects can implement the IPropertySource2 interface to allow an easier indication of properties that have a default value and can be reset.
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:
13.3.2. Favorite properties in the Properties viewFor 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. |