Skip to content

Commit 71fdd59

Browse files
committed
Implement activity support filtering for e4 dynamic menu contribution.
Similar to PluginActionContributionItem we can support activity filtering of menu item to be shown or not. see #2217
1 parent 69f6e45 commit 71fdd59

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

bundles/org.eclipse.e4.ui.workbench.renderers.swt/src/org/eclipse/e4/ui/workbench/renderers/swt/DynamicContributionContributionItem.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414

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

17+
import org.eclipse.core.runtime.Platform;
18+
import org.eclipse.e4.ui.activitysupport.IActivityManagerProxy;
1719
import org.eclipse.e4.ui.model.application.ui.menu.MDynamicMenuContribution;
1820
import org.eclipse.jface.action.ContributionItem;
1921
import org.eclipse.jface.action.IContributionManager;
2022
import org.eclipse.jface.action.IMenuListener;
2123
import org.eclipse.jface.action.IMenuManager;
24+
import org.osgi.framework.BundleContext;
25+
import org.osgi.framework.FrameworkUtil;
26+
import org.osgi.util.tracker.ServiceTracker;
2227

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

3035
private IMenuListener menuListener = IMenuManager::markDirty;
3136

37+
private static final String BUNDLE_CLASS_PREFIX = "bundleclass://"; //$NON-NLS-1$
38+
39+
IActivityManagerProxy activitySupportProxy;
40+
3241
/**
3342
* Create the item and associated model;
3443
*/
3544
public DynamicContributionContributionItem(MDynamicMenuContribution item) {
3645
super(item.getElementId());
3746
model = item;
47+
initializeAcivitySupportProxy();
3848
}
3949

4050
@Override
@@ -66,4 +76,34 @@ public void setParent(IContributionManager parent) {
6676
}
6777
super.setParent(parent);
6878
}
79+
80+
@Override
81+
public boolean isVisible() {
82+
if (this.activitySupportProxy != null) {
83+
// Contribution URI has the scheme bundleclass://. Ex:
84+
// bundleclass://org.eclipse.pde.spy.core/org.eclipse.pde.spy.core.SpyProcessor
85+
String contributionURI = this.getModel().getContributionURI();
86+
if (contributionURI.startsWith(BUNDLE_CLASS_PREFIX)) {
87+
return activitySupportProxy
88+
.isIdentifierEnabled(contributionURI.substring(BUNDLE_CLASS_PREFIX.length()));
89+
}
90+
}
91+
return true;
92+
}
93+
94+
/**
95+
* Initialize the Activity Support proxy from Platform Context
96+
*/
97+
private void initializeAcivitySupportProxy() {
98+
BundleContext context = FrameworkUtil.getBundle(Platform.class).getBundleContext();
99+
if (context != null) {
100+
ServiceTracker<IActivityManagerProxy, IActivityManagerProxy> tracker = new ServiceTracker<>(context,
101+
IActivityManagerProxy.class, null);
102+
if (tracker != null) {
103+
tracker.open();
104+
this.activitySupportProxy = tracker.getService();
105+
tracker.close();
106+
}
107+
}
108+
}
69109
}

bundles/org.eclipse.e4.ui.workbench/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Require-Bundle: org.eclipse.e4.ui.model.workbench;bundle-version="1.2.0",
2020
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)"
2121
Bundle-ActivationPolicy: lazy
2222
Bundle-RequiredExecutionEnvironment: JavaSE-17
23-
Export-Package: org.eclipse.e4.ui.internal.workbench;
23+
Export-Package: org.eclipse.e4.ui.activitysupport,
24+
org.eclipse.e4.ui.internal.workbench;
2425
x-friends:="org.eclipse.e4.ui.workbench.fragment,
2526
org.eclipse.e4.ui.workbench.addons.swt,
2627
org.eclipse.e4.ui.workbench.renderers.swt,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Advantest Europe GmbH and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Raghunandana Murthappa
13+
*******************************************************************************/
14+
package org.eclipse.e4.ui.activitysupport;
15+
16+
/**
17+
* A bridge between org.eclipse.ui.workbench and
18+
* org.eclipse.e4.ui.workbench.renderers.swt.
19+
*
20+
* Service for this interface is bound to Platform.class bundle at
21+
* Workbench.class. We cannot depend on org.eclipse.ui.workbench from
22+
* org.eclipse.e4.ui.workbench.renderers.swt
23+
*
24+
* @since 1.16
25+
*/
26+
public interface IActivityManagerProxy {
27+
/**
28+
* Checks whether the given element is enabled or not in the workbench activity
29+
* support.
30+
*
31+
* @param identifierId A qualified id if the contribution. Which has format of
32+
* bundle-id/element. Ex:
33+
* org.eclipse.pde.spy.core/org.eclipse.pde.spy.core.SpyProcessor
34+
* @return {@code true} if the given identifierId is enabled within workbench
35+
* activity support.
36+
*/
37+
public boolean isIdentifierEnabled(String identifierId);
38+
}

bundles/org.eclipse.ui.workbench/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
113113
org.eclipse.e4.ui.workbench.addons.swt;bundle-version="0.10.0",
114114
org.eclipse.e4.ui.services;bundle-version="1.3.0",
115115
org.eclipse.emf.ecore.xmi;bundle-version="2.11.0",
116-
org.eclipse.e4.core.di.extensions;bundle-version="0.13.0"
116+
org.eclipse.e4.core.di.extensions;bundle-version="0.13.0",
117+
org.eclipse.e4.ui.workbench;bundle-version="1.16.0"
117118
Import-Package: com.ibm.icu.util,
118119
jakarta.annotation;version="[2.1.0,3.0.0)",
119120
jakarta.inject;version="[2.0.0,3.0.0)",

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/Workbench.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import org.eclipse.e4.core.contexts.IEclipseContext;
8787
import org.eclipse.e4.core.di.InjectionException;
8888
import org.eclipse.e4.core.services.events.IEventBroker;
89+
import org.eclipse.e4.ui.activitysupport.IActivityManagerProxy;
8990
import org.eclipse.e4.ui.internal.workbench.E4Workbench;
9091
import org.eclipse.e4.ui.internal.workbench.E4XMIResource;
9192
import org.eclipse.e4.ui.internal.workbench.renderers.swt.IUpdateService;
@@ -245,6 +246,7 @@
245246
import org.eclipse.ui.internal.themes.FontDefinition;
246247
import org.eclipse.ui.internal.themes.ThemeElementHelper;
247248
import org.eclipse.ui.internal.themes.WorkbenchThemeManager;
249+
import org.eclipse.ui.internal.util.ActivityManagerProxy;
248250
import org.eclipse.ui.internal.util.PrefUtil;
249251
import org.eclipse.ui.intro.IIntroManager;
250252
import org.eclipse.ui.keys.IBindingService;
@@ -2206,6 +2208,11 @@ public Object compute(IEclipseContext context, String contextKey) {
22062208
}
22072209
});
22082210
WorkbenchPlugin.getDefault().initializeContext(e4Context);
2211+
2212+
BundleContext context = FrameworkUtil.getBundle(Platform.class).getBundleContext();
2213+
IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench().getActivitySupport();
2214+
ActivityManagerProxy service = new ActivityManagerProxy(activitySupport);
2215+
context.registerService(IActivityManagerProxy.class.getName(), service, null);
22092216
}
22102217

22112218
private ArrayList<MCommand> commandsToRemove = new ArrayList<>();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Advantest Europe GmbH and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Raghunandana Murthappa
13+
*******************************************************************************/
14+
package org.eclipse.ui.internal.util;
15+
16+
import org.eclipse.e4.ui.activitysupport.IActivityManagerProxy;
17+
import org.eclipse.ui.activities.IWorkbenchActivitySupport;
18+
19+
public class ActivityManagerProxy implements IActivityManagerProxy {
20+
21+
private final IWorkbenchActivitySupport wbActivitySupport;
22+
23+
public ActivityManagerProxy(IWorkbenchActivitySupport wbActivitySupport) {
24+
this.wbActivitySupport = wbActivitySupport;
25+
}
26+
27+
@Override
28+
public boolean isIdentifierEnabled(String identifierId) {
29+
boolean isEnabled = wbActivitySupport.getActivityManager().getIdentifier(identifierId).isEnabled();
30+
return isEnabled;
31+
}
32+
}

0 commit comments

Comments
 (0)