13.6. Configuring a View’s Actions

Problem

You need to make the items in a view do something when clicked, right-clicked, or double-clicked.

Solution

Configure the view’s actions. If you’ve used a PDE template to create your plug-in, edit the makeActions method.

Discussion

In the previous two recipes, we created a plug-in that supports a view based on an SWT tree control. To make the items created in the previous recipe do something when the user interacts with them, edit the code in the makeActions method in SampleView.java. In the code, you can get access to the item that was clicked, right-clicked, or double-clicked using the following line:

Object obj =  ((IStructuredSelection)selection).getFirstElement( )

Here’s how to modify the code in makeActions to create actions that will handle menu selections when you right-click or double-click the items in the view:

private void makeActions( ) {
    action1 = new Action( ) {
               public void run( ) {
               showMessage("You selected Action 1");
               }
               };
               action1.setText("Action 1");
               action1.setToolTipText("Action 1 tooltip");
               action1.setImageDescriptor(PlatformUI.getWorkbench( ).getSharedImages( ).
         getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
    
               action2 = new Action( ) {
               public void run( ) {
               showMessage("You selected Action 2");
               }
               };
               action2.setTex("Action 2");
               action2.setToolTipText("Action 2 tooltip");
               action2.setImageDescriptor(PlatformUI.getWorkbench( ).getSharedImages( ).
        getImageDescriptor(ISharedImages.IMG_OBJS_TASK_TSK));
               doubleClickAction = new ...

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.