10 - Eclipse Plugin Properties Tutorial: Page 2 of 2

Let’s display properties in properties View

Object properties are shown in Properties view if selected object implements org.eclipse.ui.views.properties.IPropertySource interface. In our example we will show Author property in the properties view whenever “.properties” file is selected in the com.myplugin.rmp.views.PropertyManagerView. Since TreeObject is the model object behind “.properties” file – TreeObject needs to implement “org.eclipse.ui.views.properties.IPropertySource” interface. Also, com.myplugin.rmp.views.PropertyManagerView should emit selection changed events so that the Properties View can listen to them and decide whether properties are to be displayed for selected object. Before we get into details it will be good to look at the IPropertySource interface and its methods. Please refer to Eclipse API Specification to know about this interface
 
Property descriptors objects are used to create a property editor in the Properties view. Eclipse provides some implementations of the IPropertyDescriptor interface as shown in figure below.

      implementation of property descriptor.

Confused! Let’s start with real coding
 
If you are not able to understand above mentioned details then don’t worry!! What follows is the step by step approach of integrating properties View with our own Property Manager View (similar names!! Don’t get confused, Property Manager View is our example view where in we are displaying all the property files in workspace whereas Properties View is provided by eclipse to display properties of object selected in workbench). 
 
Step 1: Modify com.myplugin.rmp.views.PropertyManagerView such that it starts broadcasting selection changed events. Remember Property View listens to selection change events in order to display properties of selected object. Modify createPartControl method in com.myplugin.rmp.views.PropertyManagerView as shown below (in bold).

public void createPartControl(Composite parent) {
              viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
              viewer.setContentProvider(new ViewContentProvider());
              viewer.setLabelProvider(new ViewLabelProvider());
              viewer.setInput(getViewSite());
              ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
              getSite().setSelectionProvider(viewer);
              hookContextMenu();
              hookDoubleCLickAction();
}

Step 2: With above step, PropertyManagerView will start broadcasting selection changed events. Whenever user selects/deselects “.properties” file in view an event will be generated. Eclipse Properties View listen to these selection change events and display properties of currently selected object. BUT HOW???? How does it know which properties to display?? What are the values for these properties. Can it display properties for any type of objects?? The short answer is NO… Eclipse Properties View will display properties for only those objects which implement org.eclipse.ui.views.properties.IPropertySource Interface.
In our case “.properties” files are essentially TreeObject. So we need to modify TreeObject class so that it implements IPropertySource Interface. However, in order to use org.eclipse.ui.views.properties.IPropertySource you need to add org.eclipse.ui.views in plug-in dependencies as shown in figure below.

        eclipse plugin dependencies.

Next, add following three lines in com.myplugin.rmp.views.PropertyManagerView class.

private static final String AUTHOR_ID = "RMP.author";
private static final TextPropertyDescriptor AUTHOR_PROP_DESC = new
                    TextPropertyDescriptor(AUTHOR_ID,"author");
private static final IPropertyDescriptor[] DESCRIPTORS =  { AUTHOR_PROP_DESC };
Next, modify com.myplugin.rmp.views.PropertyManagerView$TreeObject as shown below (notice changes in bold):
public class TreeObject implements IAdaptable,IPropertySource {
              private String name;
              private TreeParent parent;
              private IResource resouce;

              public TreeObject(String name) {
                        this.name = name;
              } 
              public String getName() {
                        return name;
              } 
              public void setParent(TreeParent parent) {
                        this.parent = parent;
              } 
              public TreeParent getParent() {
                        return parent;
              } 
              public String toString() {
                        return getName();
              } 
              public Object getAdapter(Class key) {
                        return null;
              } 
              public IResource getResouce() {
                        return resouce;
              } 
   
              public void setResouce(IResource resouce) {
                        this.resouce = resouce;
              } 
                  public Object getEditableValue() {
                              return null;
                  } 
                  public IPropertyDescriptor[] getPropertyDescriptors() {
                              return DESCRIPTORS;
                  } 
                  public Object getPropertyValue(Object id) {
                        try{
                          if(AUTHOR_ID.equals(id)){
                             return resouce.getPersistentProperty(propertyPage.AUTHOR_PROP_KEY);
                          }
                          }catch(Exception e){
                       
                          }
                       return null;
                  } 
                  public boolean isPropertySet(Object id) {
                              return false;
                  } 
                  public void resetPropertyValue(Object id) {
                  }

                  public void setPropertyValue(Object id, Object value) {
                     try{
                       if(AUTHOR_ID.equals(id)){
                         resouce.setPersistentProperty(propertyPage.AUTHOR_PROP_KEY,(String)value);
                       }
                     }catch(Exception e){

                     }
                  } 
     }

Now, whenever a “.properties” file is selected in Property Manager View. Its properties are shown in Eclipse Properties View as shown in figure below.

      Eclipse Plugin Properties in Properties View

Like us on Facebook