Skip to content

Commit 73abc2f

Browse files
raghucssitiloveeclipse
authored andcommitted
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. Fixes #2217
1 parent 55481d3 commit 73abc2f

File tree

8 files changed

+143
-5
lines changed

8 files changed

+143
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Bundle-Name: %pluginName
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin
88
Bundle-RequiredExecutionEnvironment: JavaSE-17
9-
Require-Bundle: org.eclipse.e4.ui.workbench;bundle-version="0.9.0",
9+
Require-Bundle: org.eclipse.e4.ui.workbench;bundle-version="1.17.0",
1010
org.eclipse.e4.core.services;bundle-version="0.9.0",
1111
org.eclipse.e4.core.contexts;bundle-version="1.0.0",
1212
org.eclipse.e4.core.di;bundle-version="1.1.0",

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

Lines changed: 43 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+
private 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,37 @@ public void setParent(IContributionManager parent) {
6676
}
6777
super.setParent(parent);
6878
}
79+
80+
@Override
81+
public boolean isVisible() {
82+
if (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+
try {
105+
activitySupportProxy = tracker.getService();
106+
} finally {
107+
tracker.close();
108+
}
109+
}
110+
}
111+
}
69112
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-SymbolicName: org.eclipse.e4.ui.workbench;singleton:=true
4-
Bundle-Version: 1.16.100.qualifier
4+
Bundle-Version: 1.17.0.qualifier
55
Bundle-Name: %pluginName
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin
@@ -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.17
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.33.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.17.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
@@ -87,6 +87,7 @@
8787
import org.eclipse.e4.core.contexts.IEclipseContext;
8888
import org.eclipse.e4.core.di.InjectionException;
8989
import org.eclipse.e4.core.services.events.IEventBroker;
90+
import org.eclipse.e4.ui.activitysupport.IActivityManagerProxy;
9091
import org.eclipse.e4.ui.internal.workbench.E4Workbench;
9192
import org.eclipse.e4.ui.internal.workbench.E4XMIResource;
9293
import org.eclipse.e4.ui.internal.workbench.renderers.swt.IUpdateService;
@@ -246,6 +247,7 @@
246247
import org.eclipse.ui.internal.themes.FontDefinition;
247248
import org.eclipse.ui.internal.themes.ThemeElementHelper;
248249
import org.eclipse.ui.internal.themes.WorkbenchThemeManager;
250+
import org.eclipse.ui.internal.util.ActivityManagerProxy;
249251
import org.eclipse.ui.internal.util.PrefUtil;
250252
import org.eclipse.ui.intro.IIntroManager;
251253
import org.eclipse.ui.keys.IBindingService;
@@ -2212,6 +2214,11 @@ public Object compute(IEclipseContext context, String contextKey) {
22122214
}
22132215
});
22142216
WorkbenchPlugin.getDefault().initializeContext(e4Context);
2217+
2218+
BundleContext context = FrameworkUtil.getBundle(Platform.class).getBundleContext();
2219+
IWorkbenchActivitySupport activitySupport = PlatformUI.getWorkbench().getActivitySupport();
2220+
ActivityManagerProxy service = new ActivityManagerProxy(activitySupport);
2221+
context.registerService(IActivityManagerProxy.class.getName(), service, null);
22152222
}
22162223

22172224
private ArrayList<MCommand> commandsToRemove = new ArrayList<>();

bundles/org.eclipse.ui.workbench/eclipseui/org/eclipse/ui/internal/quickaccess/providers/ViewProvider.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.eclipse.jface.resource.ImageDescriptor;
2424
import org.eclipse.ui.IWorkbenchWindow;
2525
import org.eclipse.ui.PlatformUI;
26+
import org.eclipse.ui.activities.IActivityManager;
27+
import org.eclipse.ui.activities.IIdentifier;
2628
import org.eclipse.ui.activities.WorkbenchActivityHelper;
2729
import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
2830
import org.eclipse.ui.internal.WorkbenchImages;
@@ -39,6 +41,11 @@
3941
*/
4042
public class ViewProvider extends QuickAccessProvider {
4143

44+
/**
45+
* URI of any view on the platform has this prefix. ex:
46+
* bundleclass://org.eclipse.pde.spy.bundle/org.eclipse.pde.spy.bundle.BundleSpyPart
47+
*/
48+
private static final String BUNDLE_CLASS_SCHEME = "bundleclass://"; //$NON-NLS-1$
4249
private MApplication application;
4350
private MWindow window;
4451
private Map<String, QuickAccessElement> idToElement = new HashMap<>();
@@ -69,6 +76,7 @@ public QuickAccessElement[] getElements() {
6976
}
7077

7178
if (idToElement.isEmpty()) {
79+
IActivityManager activityManager = PlatformUI.getWorkbench().getActivitySupport().getActivityManager();
7280
for (MPartDescriptor descriptor : application.getDescriptors()) {
7381
String uri = descriptor.getContributionURI();
7482
if (uri != null) {
@@ -85,7 +93,15 @@ public QuickAccessElement[] getElements() {
8593
idToElement.put(element.getId(), element);
8694
}
8795
} else {
88-
idToElement.put(id, element);
96+
if (uri.startsWith(BUNDLE_CLASS_SCHEME)) {
97+
String viewQualUri = uri.substring(BUNDLE_CLASS_SCHEME.length());
98+
IIdentifier identifier = activityManager.getIdentifier(viewQualUri);
99+
if (identifier.isEnabled()) {
100+
idToElement.put(element.getId(), element);
101+
}
102+
} else {
103+
idToElement.put(element.getId(), element);
104+
}
89105
}
90106
}
91107
}
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)