Implementing a property page

When the workbench creates and launches a properties page, it sets the selected resource into the page. The page can use the getElement() method to obtain its element, an IAdaptable.

The pattern for creating property pages is similar to that of preference pages, so we will only focus on what is different. Property pages show information about their element. This information can be obtained by accessing the element in order to query or compute the relevant information.  The information can also be stored and retrieved from the resource's properties.

The ReadmeFilePropertyPage computes most of its information using its element. The following snippet shows how the number of sections is computed and displayed in a label.

   ...
   IResource resource = (IResource) getElement();
   ...
   IAdaptable sections = getSections(resource);
   if (sections instanceof AdaptableList) {
      AdaptableList list = (AdaptableList)sections;
      label = createLabel(panel, String.valueOf(list.size()));
   ...

When a property is computed, there is no need for corresponding logic to save the value, since the user cannot update this value.

Properties pages are commonly used for viewing and for setting the application-specific  properties of a resource. (See Resource properties for a discussion of session and persistent properties.)  Since the property page knows its resource, the resources API can be used in the page to initialize control values or to set new property values based on user selections in the properties page.

The following snippet shows a checkbox value being initialized from a property on a property page's element.

   private void initializeValues() {
      ...
      IResource resource = (IResource) getElement();
      label.setText(resource.getPersistentProperty("MyProperty"));
      ...
   }

The corresponding code for saving the checkbox value back into the property looks like this:

   private void storeValues() {
      ...
      IResource resource = (IResource) getElement();
      resource.setPersistentProperty("MyProperty", label.getText());
      ...
   }