Skip to content

Commit cbb8ac5

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 cbb8ac5

File tree

7 files changed

+152
-2
lines changed

7 files changed

+152
-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;
1718
import org.eclipse.e4.ui.model.application.ui.menu.MDynamicMenuContribution;
1819
import org.eclipse.jface.action.ContributionItem;
1920
import org.eclipse.jface.action.IContributionManager;
2021
import org.eclipse.jface.action.IMenuListener;
2122
import org.eclipse.jface.action.IMenuManager;
23+
import org.eclipse.ui.activitysupport.IActivityManagerProxy;
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
@@ -33,7 +33,8 @@ Export-Package: org.eclipse.e4.ui.internal.workbench;
3333
org.eclipse.e4.ui.internal.workbench.handlers;x-internal:=true,
3434
org.eclipse.e4.ui.workbench,
3535
org.eclipse.e4.ui.workbench.lifecycle,
36-
org.eclipse.e4.ui.workbench.modeling
36+
org.eclipse.e4.ui.workbench.modeling,
37+
org.eclipse.ui.activitysupport
3738
Bundle-Activator: org.eclipse.e4.ui.internal.workbench.Activator
3839
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)",
3940
jakarta.inject;version="[2.0.0,3.0.0)",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.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
35+
*/
36+
public boolean isIdentifierEnabled(String identifierId);
37+
}
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.ui.activities.IWorkbenchActivitySupport;
17+
import org.eclipse.ui.activitysupport.IActivityManagerProxy;
18+
19+
public class ActivityManagerProxy implements IActivityManagerProxy {
20+
21+
private 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+
}

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.15.500"
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
@@ -190,6 +190,7 @@
190190
import org.eclipse.ui.WorkbenchException;
191191
import org.eclipse.ui.XMLMemento;
192192
import org.eclipse.ui.activities.IWorkbenchActivitySupport;
193+
import org.eclipse.ui.activitysupport.IActivityManagerProxy;
193194
import org.eclipse.ui.application.WorkbenchAdvisor;
194195
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
195196
import org.eclipse.ui.commands.ICommandImageService;
@@ -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.ui.activities.IWorkbenchActivitySupport;
17+
import org.eclipse.ui.activitysupport.IActivityManagerProxy;
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)