05 - Getting Started with Actions: Page 2 of 2

Adding actions to View Toolbar

In this section we will add actions to out Resource Manager View. In order to add these actions, open the Resource Manager plug-in manifest editor, navigate to Extensions tab and click the "Add" button. Select org.eclipse.ui.viewActions from the list. Click the Finish button to add this extension to the plug-in manifest.

       

       Eclipse Plugin Add Extention to Plugin Menifest

                                                          Figure 5-12

Next, right click and add viewActions > New > viewContribution.

         Add View Contribution

                                              Figure 5-13

Next, modify the viewContribution attributes as shown below, Notice that the targetID attribute points to the ID of the view to which we are adding this action. In this example the target view is Resource Manager View.

         Eclipse Plugin Modify View contribution Attributes

                                                           Figure 5-14

Next, Right click on the ResourceManagerViewContribution > New > Action to create a new action which will be associated with the View toolbar. Provide action attributes as shown below.

       Create a New Action.

                                                                     Figure 5-15

 

Creating an action delegate

Action delegate is a class which implements the behavior associated with the action. Next, we will create a action delegate class.

Click on the class: label that appears to the left of the class field to create a new action delegate class. We will modify the class so that it opens up a pop up when action is clicked on the View tool bar.

package com.myplugin.rmp;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

public class ResourceManagerViewActionDelegate implements IViewActionDelegate {
    private IViewPart view;

    public void init(IViewPart view) {
             this.view = view;
    }

    public void run(IAction action) {
             MessageBox box = new MessageBox(view.getSite().getShell(),SWT.ICON_INFORMATION);
             box.setMessage("Hello! You clicked view action!");
             box.open();
    }
    public void selectionChanged(IAction action, ISelection selection) {}
}

 

Testing the new action

Launch the Runtime Workbench. You will see a new push button has been added to Resource Manager View tool bar. It will open up a Message box when clicked (See Below).

            Eclipse Plugin Testing New Action

Like us on Facebook