Eclipse Activities – Hide / Display certain UI elements

Eclipse Activities can be used to hide / display certain UI elements, for example based on the role of the user. The related extension point is “org.eclipse.ui.activities”. A common use case for using activities is authorization, e.g. certain user get to see only a certain part of the ui which fits to their role.

Via activities you can for example restrict:

  • Editors
  • Views
  • Menus
  • Wizards

You define first the activities and then one or several activityPatternBindings. The activityPatternBindings defines which UI elements belong to an activities. These elements will only be displayed if the activity is set to true.

The Eclipse workbench will automatically persists activated activities.

The following will demonstate how activities can be used.

Create a new Eclipse RCP project “de.vogella.rcp.activities” based on the “Hello RCP” template.

Add a view “de.vogella.rcp.activities.view” to your RCP application and add this view via “org.eclipse.ui.perspectiveExtensions” to your application. The view should have the id “de.vogella.rcp.activities.view”. This view will later be filtered by the activity.

See Eclipse RCP Tutorial for how to create a view and add it to the perspective.

Add now two commands “de.vogella.rcp.activities.activate” and “de.vogella.rcp.activities.deactivate” with the following default handler to your application. Comands are in detail described in Eclipse Commands.


package de.vogella.rcp.activities.handler;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.activities.IActivityManager;
import org.eclipse.ui.activities.IWorkbenchActivitySupport;
import org.eclipse.ui.handlers.HandlerUtil;

public class Activate extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchActivitySupport activitySupport = HandlerUtil
.getActiveWorkbenchWindow(event).getWorkbench()
.getActivitySupport();
IActivityManager activityManager = activitySupport.getActivityManager();
Set enabledActivities = new HashSet();
String id = "de.vogella.rcp.activities.view";
if (activityManager.getActivity(id).isDefined()) {
enabledActivities.add(id);

}
activitySupport.setEnabledActivityIds(enabledActivities);
return null;
}

}


package de.vogella.rcp.activities.handler;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.activities.IWorkbenchActivitySupport;
import org.eclipse.ui.handlers.HandlerUtil;

public class DeActivate extends AbstractHandler {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchActivitySupport activitySupport = HandlerUtil
.getActiveWorkbenchWindow(event).getWorkbench()
.getActivitySupport();
Set<String> enabledActivities = new HashSet<String>();
activitySupport.setEnabledActivityIds(enabledActivities);
// Now I have to reset the perspective to update also the views
HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().resetPerspective();
return null;
}
}

Add both commands to the menu.

Add also the standard command “org.eclipse.ui.views.showView” to your menu. This command will later also be filtered by the activity.

Now add the extension point “org.eclipse.ui.activities”. Add an activity with the id “de.vogella.rcp.activities.view”. As you see above this activity will be activated / deactivated by the commands from above.

Add then two activityPatternBinding to your extension point. To assign a ui element to an activity you can use patterns ( isEqualityPattern=false) or Strings ( isEqualityPattern=true). The first part of the pattern / string is the plug-in id and the second part is the UI element.

For example the correct String for the view is: “de.vogella.rcp.activities/de.vogella.rcp.activities.view”.

After doing this your extension point should look like this in plugin.xml:

   <extension
         point="org.eclipse.ui.activities">
      <activity
            id="de.vogella.rcp.activities.view"
            name="Activate View">
      </activity>
      <activityPatternBinding
            activityId="de.vogella.rcp.activities.view"
            isEqualityPattern="false"
            pattern="de.vogella.rcp.activities/de.vogella.rcp.activities.view">
      </activityPatternBinding>
      <activityPatternBinding
            activityId="de.vogella.rcp.activities.view"
            isEqualityPattern="true"
            pattern="de.vogella.rcp.activities/org.eclipse.ui.views.showView">
      </activityPatternBinding>
   </extension>

 

If you run your application the view and menu item should not be there. If you press your command “Activate” then the view and the menu should get displayed. If you stop and re-start the application the activity should be still active as it gets persisted.

If you press your command “Deactivate” then the menu and the view is removed.

Activities can also be used together with core expressions and your own define expressions (which you define via ISourceProvider). See Eclipse Commands – Own expressions on how to define them.

Expression based activities are even better for defining authorization in your application as they will ignore API calls to them.

The full project can be downloaded here:

de.vogella.rcp.activities

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Eclipse and tagged , , . Bookmark the permalink.

7 Responses to Eclipse Activities – Hide / Display certain UI elements

  1. Olivier VIGNON says:

    Hello,
    Nice article.
    But are your sure thar your project sample is OK ?
    I tried to import it but Eclipse didn’t recognize it as a project.

  2. Lars Vogel says:

    @Olivier: Sorry for that; I updated the download link. It should work now.

  3. David CHAUTARD says:

    Hy,
    very interesting article.
    I try to use it in my application, which contains an action instead a view.
    But if you have a shortcut to launch the action, it will be executed, even if the activity is deactivated.
    Is there a way to also deactivated the funtionality, without deactivated the plugin?
    Thanks.

  4. Lars Vogel says:

    @David: I believe activities do not work with actions.

  5. Dmitry Geraskov says:

    Hi,
    the same for the ui commands. They disappears, but available via key bindings.

  6. Thorsten Waldner says:

    Hi all,

    I try to get the activity handle whether a perspective is allowed to be viewed or not. I can’t find a way doing this. Is there a chance to avoid opening a perspective without having the required rights? The rights are checked with the help of a SourceProvider.

    Thanks!

  7. Lars Vogel says:

    @Thorsten: I never tried this. I suggest to ask this in the Eclipse Forum.

Comments are closed.