Skip to content

Commit

Permalink
Implement activity support filtering for e4 dynamic menu contribution.
Browse files Browse the repository at this point in the history
Similar to PluginActionContributionItem we can support activity
filtering of menu item to be shown or not.

see #2217
  • Loading branch information
raghucssit committed Aug 26, 2024
1 parent a801237 commit 69e65fa
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Require-Bundle: org.eclipse.e4.ui.workbench;bundle-version="0.9.0",
org.eclipse.emf.ecore;bundle-version="2.35.0",
org.eclipse.e4.ui.css.swt;bundle-version="0.11.0",
org.eclipse.e4.core.di.extensions;bundle-version="0.12.0",
org.eclipse.core.runtime;bundle-version="3.29.0"
org.eclipse.core.runtime;bundle-version="3.29.0",
org.eclipse.e4.ui.workbench3;bundle-version="0.17.400"
Export-Package: org.eclipse.e4.ui.internal.workbench.renderers.swt;x-friends:="org.eclipse.ui.workbench",
org.eclipse.e4.ui.workbench.renderers.swt;x-friends:="org.eclipse.e4.ui.workbench.addons.swt,org.eclipse.ui.workbench"
Bundle-ActivationPolicy: lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

package org.eclipse.e4.ui.workbench.renderers.swt;

import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.ui.model.application.ui.menu.MDynamicMenuContribution;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.ui.internal.activitysupport.IActivityManagerProxy;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.util.tracker.ServiceTracker;

/**
* This item currently serves as a placeholder to determine the correct location
Expand All @@ -29,12 +34,17 @@ class DynamicContributionContributionItem extends ContributionItem {

private IMenuListener menuListener = IMenuManager::markDirty;

private static final String BUNDLE_CLASS_PREFIX = "bundleclass://"; //$NON-NLS-1$

IActivityManagerProxy activitySupportProxy;

/**
* Create the item and associated model;
*/
public DynamicContributionContributionItem(MDynamicMenuContribution item) {
super(item.getElementId());
model = item;
initializeAcivitySupportProxy();
}

@Override
Expand Down Expand Up @@ -66,4 +76,34 @@ public void setParent(IContributionManager parent) {
}
super.setParent(parent);
}

@Override
public boolean isVisible() {
if (this.activitySupportProxy != null) {
// Contribution URI has the scheme bundleclass://. Ex:
// bundleclass://org.eclipse.pde.spy.core/org.eclipse.pde.spy.core.SpyProcessor
String contributionURI = this.getModel().getContributionURI();
if (contributionURI.startsWith(BUNDLE_CLASS_PREFIX)) {
return activitySupportProxy
.isIdentifierEnabled(contributionURI.substring(BUNDLE_CLASS_PREFIX.length()));
}
}
return true;
}

/**
* Initialize the Activity Support proxy from Platform Context
*/
private void initializeAcivitySupportProxy() {
BundleContext context = FrameworkUtil.getBundle(Platform.class).getBundleContext();
if (context != null) {
ServiceTracker<IActivityManagerProxy, IActivityManagerProxy> tracker = new ServiceTracker<>(context,
IActivityManagerProxy.class, null);
if (tracker != null) {
tracker.open();
this.activitySupportProxy = tracker.getService();
tracker.close();
}
}
}
}
5 changes: 3 additions & 2 deletions bundles/org.eclipse.e4.ui.workbench3/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.e4.ui.workbench3;singleton:=true
Bundle-Version: 0.17.400.qualifier
Bundle-Version: 0.18.0.qualifier
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand All @@ -10,6 +10,7 @@ Require-Bundle: org.eclipse.equinox.common;bundle-version="[3.7.0,4.0.0)",
org.eclipse.swt;bundle-version="[3.6.0,4.0.0)",
org.eclipse.equinox.registry;bundle-version="[3.5.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)"
Export-Package: org.eclipse.ui.testing,
Export-Package: org.eclipse.ui.internal.activitysupport,
org.eclipse.ui.testing,
org.eclipse.ui.testing.dumps
Automatic-Module-Name: org.eclipse.e4.ui.workbench3
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.eclipse.ui.internal.activitysupport;

/**
* A bridge between org.eclipse.ui.workbenk and
* org.eclipse.e4.ui.workbench.renderers.swt.
*
* Service for this interface is bound to Platform.class bundle at
* Workbench.class. We cannot depend on org.eclipse.ui.workbench from
* org.eclipse.e4.ui.workbench.renderers.swt
*
* @since 0.18
*/
public interface IActivityManagerProxy {
/**
* Checks whether the given element is enabled or not in the workbench activity
* support.
*
* @param identifierId A qualified id if the contribution. Which has format of
* bundle-id/element. Ex:
* org.eclipse.pde.spy.core/org.eclipse.pde.spy.core.SpyProcessor
* @return
*/
public boolean isIdentifierEnabled(String identifierId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
import org.eclipse.ui.internal.WorkbenchWindow.WWinPartServiceSaveHandler;
import org.eclipse.ui.internal.actions.CommandAction;
import org.eclipse.ui.internal.activities.ws.WorkbenchActivitySupport;
import org.eclipse.ui.internal.activitysupport.IActivityManagerProxy;
import org.eclipse.ui.internal.browser.WorkbenchBrowserSupport;
import org.eclipse.ui.internal.commands.CommandImageManager;
import org.eclipse.ui.internal.commands.CommandImageService;
Expand Down Expand Up @@ -241,6 +242,7 @@
import org.eclipse.ui.internal.themes.FontDefinition;
import org.eclipse.ui.internal.themes.ThemeElementHelper;
import org.eclipse.ui.internal.themes.WorkbenchThemeManager;
import org.eclipse.ui.internal.util.ActivityManagerProxy;
import org.eclipse.ui.internal.util.PrefUtil;
import org.eclipse.ui.intro.IIntroManager;
import org.eclipse.ui.keys.IBindingService;
Expand Down Expand Up @@ -2173,6 +2175,11 @@ public Object compute(IEclipseContext context, String contextKey) {
}
});
WorkbenchPlugin.getDefault().initializeContext(e4Context);

BundleContext context = FrameworkUtil.getBundle(Platform.class).getBundleContext();
IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench().getActivitySupport();
ActivityManagerProxy service = new ActivityManagerProxy(activitySupport);
context.registerService(IActivityManagerProxy.class.getName(), service, null);
}

private ArrayList<MCommand> commandsToRemove = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.eclipse.ui.internal.util;

import org.eclipse.ui.activities.IWorkbenchActivitySupport;
import org.eclipse.ui.internal.activitysupport.IActivityManagerProxy;

/**
* @since 3.5
*
*/
public class ActivityManagerProxy implements IActivityManagerProxy {

private IWorkbenchActivitySupport wbActivitySupport;

public ActivityManagerProxy(IWorkbenchActivitySupport wbActivitySupport) {
this.wbActivitySupport = wbActivitySupport;
}

@Override
public boolean isIdentifierEnabled(String identifierId) {
boolean isEnabled = wbActivitySupport.getActivityManager().getIdentifier(identifierId).isEnabled();
return isEnabled;
}
}

0 comments on commit 69e65fa

Please sign in to comment.