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: