10 - Eclipse Plugin Properties Tutorial

Eclipse plugin Properties are used to assign additional information to resources and objects. Additional information could be anything. For Example, we may want to attach author name property with the java file resource in eclipse. There are two ways to access resource property. First, right click on resource and select properties option from context menu (as shown in figure below)

       Eclipse Plugin Properties

Another way to access properties for a resource is to open up the properties view from Window > Show View > Properties (as shown in figure below)

      Eclipse Viewing Properties

In this chapter we will learn how to create property on a particular resource and display it's properties in properties dialog as well as properties view. For the purpose of our example we will associate properties with property files displayed in Property Manager View. We will create a context menu option to open properties dialog. Later on we will see how to display properties in property view when ever user selects a property file in Property Manager View.

Let’s create a property dialog

We will first create a property page through plug-in manifest editor. This preference page will be shown in properties dialog. In order to create property page open plugin.xml in manifest editor. Navigate to extension page. Click on Add… button. Select org.eclipse.ui.propertyPages and click finish.


      Eclipse Creating Property Page

Next, in the extensions page right click on org.eclipse.ui.propertyPages and select new > page. Click on new page declaration and edit its properties as shown in figure below. We need to provide TreeObject class in object Class because we will be showing property dialog on selection of property files in Property Manager View and model class for property files in our view is TreeObject class.

Note: In order to access TreeObject class in below mentioned properties change access modifier from default to public. Also change the getResource() method in TreeObject to public from protected. It will be accessed from outside in coming sections.

      Eclipse Update the Properties

Next, generate the com.myplugin.rmp.propertyPage class by clicking on class link in manifest editor(extensions page) with the help of java attribute dialog. Modify the com.myplugin.rmp.propertyPage as shown in the listing below.

1.  package com.myplugin.rmp;
2.  import org.eclipse.core.resources.IResource;
3.  import org.eclipse.core.runtime.CoreException;
4.  import org.eclipse.core.runtime.QualifiedName;
5.  import org.eclipse.swt.SWT;
6.  import org.eclipse.swt.layout.GridData;
7.  import org.eclipse.swt.layout.GridLayout;
8.  import org.eclipse.swt.widgets.Composite;
9.  import org.eclipse.swt.widgets.Control;
10. import org.eclipse.swt.widgets.Label;
11. import org.eclipse.swt.widgets.Text;
12. import org.eclipse.ui.IWorkbenchPropertyPage;
13. import org.eclipse.ui.dialogs.PropertyPage;
14. import com.myplugin.rmp.views.PropertyManagerView.TreeObject;

15. public class propertyPage extends PropertyPage implements
16.                    IWorkbenchPropertyPage {

17.          private Text textField;
18.          public static QualifiedName  AUTHOR_PROP_KEY = new QualifiedName("Author", "Author");

19.          public propertyPage() {
20.                    super();
21.          }

22.          protected Control createContents(Composite parent) {
23.                       Composite myComposite = new Composite(parent, SWT.NONE);
24.                       GridLayout mylayout = new GridLayout();
25.                       mylayout.marginHeight = 1;
26.                       mylayout.marginWidth = 1;
27.                       myComposite.setLayout(mylayout);
28.                       Label mylabel = new Label(myComposite, SWT.NONE);
29.                       mylabel.setLayoutData(new GridData());
30.                       mylabel.setText("Author");
31.                       textField = new Text(myComposite, SWT.BORDER);
32.                       textField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
33.                       textField.setText(getAuthor());
34.                      return myComposite;
35.          }

36.          protected String getAuthor() {
37.                       IResource resource =
38.                          ((TreeObject) getElement()).getResouce();
39.                       try {
40.                          String value =
41.                             resource.getPersistentProperty(
42.                                AUTHOR_PROP_KEY);
43.                          if (value == null)
44.                             return "";
45.                          return value;
46.                        }
47.                        catch (CoreException e) {
48.                           return e.getMessage();
49.                        }
50.                    }

51.          protected void setAuthor(String author) {
52.                       IResource resource =
53.                                   ((TreeObject) getElement()).getResouce();
54.                       String value = author;
55.                       if (value.equals(""))
56.                          value = null;
57.                       try {
58.                                   resource.setPersistentProperty(
59.                                         AUTHOR_PROP_KEY,
60.                             value);
61.                       }
62.                       catch (CoreException e) {
63.                       }
64.                    }

65.          public boolean performOk() {
66.                       setAuthor(textField.getText());
67.                       return super.performOk();
68.                    }
69. }

Lines 15 -16: We are extending abstract base class PropertyPage. This class is Abstract base implementation of a workbench property page (IWorkbenchPropertyPage). Subclasses of this base class should implement the createContents method in order to provide the property page's main control. Following figure shows the hierarchy of this class.

     Eclipse Hierarchy of ClassLines 22 – 35: Here we are implementing createContents method defined in PreferencePage class. This method creates and returns the SWT control. Nothing special here we are just using SWT controls to create the GUI of properties dialog. At line 33 we are calling getAuthor() method discussed next to get the existing author property.

Lines 37 - 38: In our properties dialog we will be showing single property named author for every “.properties” file. getAuthor() method is used to fetch the existing property saved in workspace. getElement() returns the object that owns the properties shown in the dialog. In our example case this dialog will open up whenever user right clicks on “.property” file in Property Manager View. Since our model object behind tree viewer is TreeObject we are casting getElement with TreeObject. Finally we are fetching corresponding resource object from treeObject.

Lines 39 – 50: There are two types of properties: persistent and session. Methods in IResource provide both session properties, which are discarded when Eclipse exits, and persistent properties, which are preserved across multiple workspace sessions. Following are the four methods provided by IResource implementation

    

       getPersistentProperty(QualifiedName) 

       getSessionProperty(QualifiedName) 

       setPersistentProperty(QualifiedName, String)

      setSessionProperty(QualifiedName, Object) 

Please follow the link to know more about these methods. 
In order to retrieve property we use qualified name. Qualified names are two-part names: qualifier and local name. AUTHOR_PROP_KEY(QualifiedName) is defined on line 18.

Lines 51 – 64: All concepts are similar to what we discussed above with exception that we are using setPersistentProperty to set the author property for every object.

Lines 65 – 68: finally we are overriding perform ok method to fetch author name from dialog and calling the setAuthor() method which will persist the preference.

In order to open properties dialog we will modify hookContextMenu() method in com.myplugin.rmp.views.PropertyManagerView as shown below.
We have used Eclipse inbuilt action “PropertyDialogAction to open up Properties Dialog Below.

private void hookContextMenu() {
              MenuManager menuMgr = new MenuManager("#PopupMenu");
              Menu menu = menuMgr.createContextMenu(viewer.getControl());
              viewer.getControl().setMenu(menu);
              Action refresh =new Action() {
                        public void run() {
                                  initialize();
                                  viewer.refresh();
                        }
              };
              refresh.setText("Refresh");
              menuMgr.add(refresh);
              menuMgr.add(new Separator());
              menuMgr.add(new PropertyDialogAction(getSite(), viewer));
}

      Eclipse plugin displaying properties in properties view

Like us on Facebook