Managing menu items visibility

Managing menu items visibility

In order to manage the visibility of a menu item we can extend AbstractSourceProvider. Let’s see how to do that.

First of all we have to create a class that extends AbstractSourceProvider.
This class is intended to hold the state variable and provide it to the platform when needed.

In this example we intend to manage the visibility of a menu item depending on the user profile.

[java]
package com.rcpvision;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.ui.AbstractSourceProvider;
import org.eclipse.ui.ISources;
public class SessionSourceProvider extends AbstractSourceProvider {
public final static String SESSION_USER_CAN_SEE_PREFS = "com.rcpvision.session.user-can-see-preferences";
private final static String CAN_SEE = "canSee";
private final static String CANNOT_SEE = "cannotSee";
boolean canSee = true;

@Override
public String[] getProvidedSourceNames() {
return new String[] { SESSION_USER_CAN_SEE_PREFS };
}

@Override
public Map<String, String> getCurrentState() {
Map<String, String> currentState = new HashMap<String, String>(1);
String currentState1 = canSee ? CAN_SEE : CANNOT_SEE;
currentState.put(SESSION_USER_CAN_SEE_PREFS, currentState1);
return currentState;
}

@Override
public void dispose() {
}

public void setUserCanSeePreferences(boolean canSee) {
if (this.canSee == canSee)
return; // no change
this.canSee = canSee;
String currentState = canSee ? CAN_SEE : CANNOT_SEE;
fireSourceChanged(ISources.WORKBENCH,SESSION_USER_CAN_SEE_PREFS, currentState);
}
}
[/java]

Then we have to define the correspondent Extension in plugin.xml

[java]
<extension point="org.eclipse.ui.services">
<sourceProvider provider="com.rcpvision.SessionSourceProvider">
<variable name="com.rcpvision.session.user-can-see-preferences" priorityLevel="workbench">
</variable>
</sourceProvider>
</extension>
[/java]

let’s say we now want to handle the menu item visibility upon the state represented by the variable com.rcpvision.session.user-can-see-preferences, here is how to modify plugin.xml

[java]
<pre><menu label="Show my view">
<command commandId="org.eclipse.ui.views.showView" label="My View" style="push">
<parameter name="org.eclipse.ui.views.showView.viewId" value="com.rcpvision.my.view">
</parameter>
<visibleWhen checkEnabled="false">
<with variable="com.rcpvision.session.user-can-see-preferences">
<equals value="canSee">
</equals>
</with>
</visibleWhen>
</command>
</menu>
[/java]

now we just need to invoke the setter method in order to modify the state and hence the menu item visibility.

[java]

IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
ISourceProviderService service = (ISourceProviderService) window.getService(ISourceProviderService.class);
SessionSourceProvider sessionSourceProvider = (SessionSourceProvider)service.getSourceProvider(SessionSourceProvider.SESSION_USER_CAN_SEE_PREFS);
sessionSourceProvider.setUserCanSeePreferences( true/false );
[/java]

Just one note: be careful to avoid using “true” and “false” as String constants as they would be converted automatically into Booleans thus leading to wrong “equals” test results.

References:

Eclipse Tips – Prakash G.R.: Commands Part 5: Authentication in RCP applications

2013-01-25T11:19:18+00:00By |

One Comment

  1. atsmin Wednesday August 14th, 2019 at 07:10 AM - Reply

    > Just one note: be careful to avoid using “true” and “false” as String constants as they would be converted automatically into Booleans thus leading to wrong “equals” test results

    This is really helpful. Thanks a lot! I lost a lot of time to investigate that problem.

Leave A Comment