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 Oct 8, 2024
1 parent cc770ec commit 30f6fc5
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 7 deletions.
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.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.workbench/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.workbench;singleton:=true
Bundle-Version: 1.15.500.qualifier
Bundle-Version: 1.16.0.qualifier
Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Expand Down Expand Up @@ -33,7 +33,8 @@ Export-Package: org.eclipse.e4.ui.internal.workbench;
org.eclipse.e4.ui.internal.workbench.handlers;x-internal:=true,
org.eclipse.e4.ui.workbench,
org.eclipse.e4.ui.workbench.lifecycle,
org.eclipse.e4.ui.workbench.modeling
org.eclipse.e4.ui.workbench.modeling,
org.eclipse.ui.activitysupport
Bundle-Activator: org.eclipse.e4.ui.internal.workbench.Activator
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)",
jakarta.inject;version="[2.0.0,3.0.0)",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2024 Advantest Europe GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Raghunandana Murthappa
*******************************************************************************/
package org.eclipse.ui.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 1.16
*/
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 @@ -87,7 +87,6 @@
import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.ui.statushandlers.AbstractStatusHandler;
import org.eclipse.urischeme.AutoRegisterSchemeHandlersJob;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
Expand Down Expand Up @@ -226,9 +225,9 @@ public void initialize(IWorkbenchConfigurer configurer) {
jfaceComparatorIsSet = true;
}

if (!Platform.inDevelopmentMode() && !JUnitTestUtil.isJunitTestRunning()) {
new AutoRegisterSchemeHandlersJob().schedule();
}
// if (!Platform.inDevelopmentMode() && !JUnitTestUtil.isJunitTestRunning()) {
// new AutoRegisterSchemeHandlersJob().schedule();
// }
}

protected void initResourceTracking() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.activities.IWorkbenchActivitySupport;
import org.eclipse.ui.activitysupport.IActivityManagerProxy;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.commands.ICommandImageService;
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,32 @@
/*******************************************************************************
* Copyright (c) 2024 Advantest Europe GmbH and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Raghunandana Murthappa
*******************************************************************************/
package org.eclipse.ui.internal.util;

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

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;
}
}
3 changes: 2 additions & 1 deletion bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.e4.ui.workbench.addons.swt;bundle-version="0.10.0",
org.eclipse.e4.ui.services;bundle-version="1.3.0",
org.eclipse.emf.ecore.xmi;bundle-version="2.11.0",
org.eclipse.e4.core.di.extensions;bundle-version="0.13.0"
org.eclipse.e4.core.di.extensions;bundle-version="0.13.0",
org.eclipse.e4.ui.workbench;bundle-version="1.15.500"
Import-Package: com.ibm.icu.util,
jakarta.annotation;version="[2.1.0,3.0.0)",
jakarta.inject;version="[2.0.0,3.0.0)",
Expand Down

0 comments on commit 30f6fc5

Please sign in to comment.