Skip to content

Commit 6d1df19

Browse files
committed
improve dynamic popup menu support #178
fixes #151 EasyShell context menu option not showing in STS 3.8.4.RELEASE Navigator View Signed-off-by: Andre Bossert <[email protected]>
1 parent f389f87 commit 6d1df19

File tree

9 files changed

+79
-172
lines changed

9 files changed

+79
-172
lines changed

plugin/plugin.xml

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -120,74 +120,15 @@
120120
</extension>
121121
<extension
122122
point="org.eclipse.ui.menus">
123-
<menuContribution
124-
locationURI="popup:org.eclipse.ui.popup.any?after=group.open">
125-
<menu
126-
icon="images/easyshell.png"
127-
id="de.anbos.eclipse.easyshell.plugin.menu"
128-
label="EasyShell">
129-
<visibleWhen
130-
checkEnabled="false">
131-
<with
132-
variable="activePart">
133-
<test
134-
args="resourceType"
135-
forcePluginActivation="true"
136-
property="de.anbos.eclipse.easyshell.plugin.EditorPropertyTester.hasResourceSelection"
137-
value="resourceTypeFileOrDirectory">
138-
</test>
139-
</with>
140-
</visibleWhen>
141-
</menu>
142-
<menu
143-
icon="images/easyshell.png"
144-
id="de.anbos.eclipse.easyshell.plugin.menu.file"
145-
label="EasyShell">
146-
<visibleWhen
147-
checkEnabled="false">
148-
<with
149-
variable="activePart">
150-
<test
151-
args="resourceType"
152-
forcePluginActivation="true"
153-
property="de.anbos.eclipse.easyshell.plugin.EditorPropertyTester.hasResourceSelection"
154-
value="resourceTypeFile">
155-
</test>
156-
</with>
157-
</visibleWhen>
158-
</menu>
159-
<menu
160-
icon="images/easyshell.png"
161-
id="de.anbos.eclipse.easyshell.plugin.menu.directory"
162-
label="EasyShell">
163-
<visibleWhen
164-
checkEnabled="false">
165-
<with
166-
variable="activePart">
167-
<test
168-
args="resourceType"
169-
forcePluginActivation="true"
170-
property="de.anbos.eclipse.easyshell.plugin.EditorPropertyTester.hasResourceSelection"
171-
value="resourceTypeDirectory">
172-
</test>
173-
</with>
174-
</visibleWhen>
175-
</menu>
176-
</menuContribution>
177123
<menuContribution
178124
allPopups="true"
179125
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommands"
180-
locationURI="popup:de.anbos.eclipse.easyshell.plugin.menu">
181-
</menuContribution>
182-
<menuContribution
183-
allPopups="true"
184-
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommandsForFile"
185-
locationURI="popup:de.anbos.eclipse.easyshell.plugin.menu.file">
126+
locationURI="popup:org.eclipse.ui.popup.any?after=group.open">
186127
</menuContribution>
187128
<menuContribution
188129
allPopups="true"
189-
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommandsForDirectory"
190-
locationURI="popup:de.anbos.eclipse.easyshell.plugin.menu.directory">
130+
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommandsForResourceNavigator"
131+
locationURI="popup:org.eclipse.ui.popup.any">
191132
</menuContribution>
192133
</extension>
193134
<extension

plugin/src/de/anbos/eclipse/easyshell/plugin/EditorPropertyTester.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

plugin/src/de/anbos/eclipse/easyshell/plugin/Resource.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public class Resource {
4040
// resolved
4141
private String projectName = Activator.getResourceString("easyshell.plugin.name");
4242

43-
//Activator.logDebug("full_path : >" + fullPath + "<");
44-
//Activator.logDebug("parent_path: >" + parentPath + "<");
45-
//Activator.logDebug("file_name : >" + fileName + "<");
46-
4743
public Resource(Resource myRes) {
4844
this.file = myRes.getFile();
4945
this.resource = myRes.getResource();

plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
import org.eclipse.ui.IWorkbenchPart;
3232
import org.eclipse.ui.ide.FileStoreEditorInput;
3333
import org.osgi.framework.Bundle;
34+
35+
import de.anbos.eclipse.easyshell.plugin.actions.ActionDelegate;
36+
import de.anbos.eclipse.easyshell.plugin.types.ResourceType;
37+
3438
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
3539
import org.eclipse.cdt.internal.core.model.IncludeReference;
3640
import org.eclipse.cdt.internal.ui.cview.IncludeReferenceProxy;
@@ -167,4 +171,40 @@ static private File getJarFile(IAdaptable adaptable) {
167171
return file;
168172
}
169173

174+
static public ActionDelegate getActionCommonResourceType(IWorkbenchPart part, ResourceType resType) {
175+
ISelection selection = getResourceSelection(part);
176+
if (selection != null) {
177+
ActionDelegate action = new ActionDelegate();
178+
action.selectionChanged(null, selection);
179+
if (action.isEnabled(ResourceType.resourceTypeFileOrDirectory) && resType == action.getCommonResourceType()) {
180+
return action;
181+
}
182+
}
183+
return null;
184+
}
185+
186+
static public ActionDelegate getActionExactResourceType(IWorkbenchPart part, ResourceType resType) {
187+
ISelection selection = getResourceSelection(part);
188+
if (selection != null) {
189+
ActionDelegate action = new ActionDelegate();
190+
action.selectionChanged(null, selection);
191+
if (action.isEnabled(resType)) {
192+
return action;
193+
}
194+
}
195+
return null;
196+
}
197+
198+
static public ResourceType getCommonResourceType(IWorkbenchPart part) {
199+
ISelection selection = getResourceSelection(part);
200+
if (selection != null) {
201+
ActionDelegate action = new ActionDelegate();
202+
action.selectionChanged(null, selection);
203+
if (action.isEnabled(ResourceType.resourceTypeFileOrDirectory)) {
204+
return action.getCommonResourceType();
205+
}
206+
}
207+
return null;
208+
}
209+
170210
}

plugin/src/de/anbos/eclipse/easyshell/plugin/commands/DefineCommands.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,58 @@
1515

1616
import java.util.Map;
1717

18+
import org.eclipse.jface.action.IContributionManager;
19+
import org.eclipse.jface.action.MenuManager;
1820
import org.eclipse.swt.SWT;
21+
import org.eclipse.ui.IWorkbenchPart;
1922
import org.eclipse.ui.menus.CommandContributionItem;
2023
import org.eclipse.ui.menus.CommandContributionItemParameter;
2124
import org.eclipse.ui.menus.ExtensionContributionFactory;
2225
import org.eclipse.ui.menus.IContributionRoot;
2326
import org.eclipse.ui.services.IServiceLocator;
24-
2527
import de.anbos.eclipse.easyshell.plugin.Activator;
28+
import de.anbos.eclipse.easyshell.plugin.ResourceUtils;
29+
import de.anbos.eclipse.easyshell.plugin.Constants;
2630
import de.anbos.eclipse.easyshell.plugin.exceptions.UnknownCommandID;
2731
import de.anbos.eclipse.easyshell.plugin.misc.Utils;
2832
import de.anbos.eclipse.easyshell.plugin.preferences.MenuData;
2933
import de.anbos.eclipse.easyshell.plugin.preferences.MenuDataList;
3034
import de.anbos.eclipse.easyshell.plugin.preferences.MenuDataStore;
35+
3136
import de.anbos.eclipse.easyshell.plugin.types.ResourceType;
3237

3338
public class DefineCommands extends ExtensionContributionFactory {
3439

3540
public DefineCommands() {
3641
}
3742

38-
public ResourceType getWantedResourceType() {
39-
return ResourceType.resourceTypeFileOrDirectory;
40-
}
41-
4243
@Override
4344
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
45+
IWorkbenchPart activePart = serviceLocator.getService(IWorkbenchPart.class);
46+
boolean isResourceNavigator = false;
47+
isResourceNavigator = activePart instanceof org.eclipse.ui.views.navigator.ResourceNavigator;
48+
if ((isResourceNavigator == isForResourceNavigator())) {
49+
ResourceType resType = ResourceUtils.getCommonResourceType(activePart);
50+
if (resType != null) {
51+
createContributionItemsForResType(resType, serviceLocator, additions);
52+
}
53+
}
54+
}
55+
56+
public boolean isForResourceNavigator() {
57+
return false;
58+
}
59+
60+
private void createContributionItemsForResType(ResourceType resType, IServiceLocator serviceLocator, IContributionRoot additions) {
61+
MenuManager submenu = new MenuManager("EasyShell", Activator.getImageDescriptor(Constants.IMAGE_EASYSHELL), "de.anbos.eclipse.easyshell.plugin.menu");
4462
MenuDataList items = MenuDataStore.instance().getEnabledCommandMenuDataList();
4563
for (MenuData item : items) {
46-
ResourceType resTypeWanted = getWantedResourceType();
4764
ResourceType resTypeSupported;
4865
try {
4966
resTypeSupported = item.getCommandData().getResourceType();
5067
if ((resTypeSupported == ResourceType.resourceTypeFileOrDirectory)
51-
|| (resTypeSupported == resTypeWanted)) {
52-
addItem(serviceLocator, additions, item.getNameExpanded(), item.getCommand(),
68+
|| (resTypeSupported == resType)) {
69+
addItem(serviceLocator, submenu, item.getNameExpanded(), item.getCommand(),
5370
"de.anbos.eclipse.easyshell.plugin.commands.execute",
5471
Utils.getParameterMapFromMenuData(item), item.getImageId(),
5572
true);
@@ -58,9 +75,10 @@ public void createContributionItems(IServiceLocator serviceLocator, IContributio
5875
e.logInternalError();
5976
}
6077
}
78+
additions.addContributionItem(submenu, null);
6179
}
6280

63-
private void addItem(IServiceLocator serviceLocator, IContributionRoot additions, String commandLabel, String commandToolTip,
81+
private void addItem(IServiceLocator serviceLocator, IContributionManager submenu, String commandLabel, String commandToolTip,
6482
String commandId, Map<String, Object> commandParamametersMap, String commandImageId, boolean visible) {
6583
CommandContributionItemParameter param = new CommandContributionItemParameter(serviceLocator, "", commandId,
6684
SWT.PUSH);
@@ -70,6 +88,7 @@ private void addItem(IServiceLocator serviceLocator, IContributionRoot additions
7088
param.parameters = commandParamametersMap;
7189
CommandContributionItem item = new CommandContributionItem(param);
7290
item.setVisible(visible);
73-
additions.addContributionItem(item, null);
91+
submenu.add(item);
7492
}
93+
7594
}

plugin/src/de/anbos/eclipse/easyshell/plugin/commands/DefineCommandsForDirectory.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

plugin/src/de/anbos/eclipse/easyshell/plugin/commands/DefineCommandsForFile.java renamed to plugin/src/de/anbos/eclipse/easyshell/plugin/commands/DefineCommandsForResourceNavigator.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313

1414
package de.anbos.eclipse.easyshell.plugin.commands;
1515

16-
import de.anbos.eclipse.easyshell.plugin.types.ResourceType;
16+
public class DefineCommandsForResourceNavigator extends DefineCommands {
1717

18-
public class DefineCommandsForFile extends DefineCommands {
19-
20-
public ResourceType getWantedResourceType() {
21-
return ResourceType.resourceTypeFile;
18+
public boolean isForResourceNavigator() {
19+
return true;
2220
}
23-
2421
}

plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/All.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.eclipse.ui.handlers.HandlerUtil;
2323

2424
import de.anbos.eclipse.easyshell.plugin.Activator;
25-
import de.anbos.eclipse.easyshell.plugin.EditorPropertyTester;
25+
import de.anbos.eclipse.easyshell.plugin.ResourceUtils;
2626
import de.anbos.eclipse.easyshell.plugin.actions.ActionDelegate;
2727
import de.anbos.eclipse.easyshell.plugin.commands.ExecuteCommandPopup;
2828
import de.anbos.eclipse.easyshell.plugin.exceptions.UnknownCommandID;
@@ -44,7 +44,7 @@ public class All extends AbstractHandler {
4444
public Object execute(ExecutionEvent event) throws ExecutionException {
4545
// get resource type
4646
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
47-
ActionDelegate action = EditorPropertyTester.getActionExactResourceType(activePart,
47+
ActionDelegate action = ResourceUtils.getActionExactResourceType(activePart,
4848
ResourceType.resourceTypeFileOrDirectory);
4949
if (action != null) {
5050
// load the preferences

plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/Execute.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.eclipse.ui.handlers.HandlerUtil;
2222

2323
import de.anbos.eclipse.easyshell.plugin.Activator;
24-
import de.anbos.eclipse.easyshell.plugin.EditorPropertyTester;
24+
import de.anbos.eclipse.easyshell.plugin.ResourceUtils;
2525
import de.anbos.eclipse.easyshell.plugin.actions.Action;
2626
import de.anbos.eclipse.easyshell.plugin.actions.ActionDelegate;
2727
import de.anbos.eclipse.easyshell.plugin.types.CommandType;
@@ -39,7 +39,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
3939
String commandValue = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.value");
4040
String commandWorkingDir = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.workingdir");
4141
CommandTokenizer commandTokenizer = CommandTokenizer.getFromEnum(event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.tokenizer"));
42-
ActionDelegate action = EditorPropertyTester.getActionExactResourceType(activePart, resourceType);
42+
ActionDelegate action = ResourceUtils.getActionExactResourceType(activePart, resourceType);
4343
if (action != null) {
4444
action.setResourceType(resourceType);
4545
action.setCommandType(commandType);

0 commit comments

Comments
 (0)