Skip to content

Commit

Permalink
improve dynamic popup menu support #178
Browse files Browse the repository at this point in the history
fixes #151 EasyShell context menu option not showing in STS
3.8.4.RELEASE Navigator View

Signed-off-by: Andre Bossert <[email protected]>
  • Loading branch information
anb0s committed Sep 19, 2020
1 parent f389f87 commit 6d1df19
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 172 deletions.
65 changes: 3 additions & 62 deletions plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,74 +120,15 @@
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:org.eclipse.ui.popup.any?after=group.open">
<menu
icon="images/easyshell.png"
id="de.anbos.eclipse.easyshell.plugin.menu"
label="EasyShell">
<visibleWhen
checkEnabled="false">
<with
variable="activePart">
<test
args="resourceType"
forcePluginActivation="true"
property="de.anbos.eclipse.easyshell.plugin.EditorPropertyTester.hasResourceSelection"
value="resourceTypeFileOrDirectory">
</test>
</with>
</visibleWhen>
</menu>
<menu
icon="images/easyshell.png"
id="de.anbos.eclipse.easyshell.plugin.menu.file"
label="EasyShell">
<visibleWhen
checkEnabled="false">
<with
variable="activePart">
<test
args="resourceType"
forcePluginActivation="true"
property="de.anbos.eclipse.easyshell.plugin.EditorPropertyTester.hasResourceSelection"
value="resourceTypeFile">
</test>
</with>
</visibleWhen>
</menu>
<menu
icon="images/easyshell.png"
id="de.anbos.eclipse.easyshell.plugin.menu.directory"
label="EasyShell">
<visibleWhen
checkEnabled="false">
<with
variable="activePart">
<test
args="resourceType"
forcePluginActivation="true"
property="de.anbos.eclipse.easyshell.plugin.EditorPropertyTester.hasResourceSelection"
value="resourceTypeDirectory">
</test>
</with>
</visibleWhen>
</menu>
</menuContribution>
<menuContribution
allPopups="true"
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommands"
locationURI="popup:de.anbos.eclipse.easyshell.plugin.menu">
</menuContribution>
<menuContribution
allPopups="true"
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommandsForFile"
locationURI="popup:de.anbos.eclipse.easyshell.plugin.menu.file">
locationURI="popup:org.eclipse.ui.popup.any?after=group.open">
</menuContribution>
<menuContribution
allPopups="true"
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommandsForDirectory"
locationURI="popup:de.anbos.eclipse.easyshell.plugin.menu.directory">
class="de.anbos.eclipse.easyshell.plugin.commands.DefineCommandsForResourceNavigator"
locationURI="popup:org.eclipse.ui.popup.any">
</menuContribution>
</extension>
<extension
Expand Down

This file was deleted.

4 changes: 0 additions & 4 deletions plugin/src/de/anbos/eclipse/easyshell/plugin/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public class Resource {
// resolved
private String projectName = Activator.getResourceString("easyshell.plugin.name");

//Activator.logDebug("full_path : >" + fullPath + "<");
//Activator.logDebug("parent_path: >" + parentPath + "<");
//Activator.logDebug("file_name : >" + fileName + "<");

public Resource(Resource myRes) {
this.file = myRes.getFile();
this.resource = myRes.getResource();
Expand Down
40 changes: 40 additions & 0 deletions plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.osgi.framework.Bundle;

import de.anbos.eclipse.easyshell.plugin.actions.ActionDelegate;
import de.anbos.eclipse.easyshell.plugin.types.ResourceType;

import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.internal.core.model.IncludeReference;
import org.eclipse.cdt.internal.ui.cview.IncludeReferenceProxy;
Expand Down Expand Up @@ -167,4 +171,40 @@ static private File getJarFile(IAdaptable adaptable) {
return file;
}

static public ActionDelegate getActionCommonResourceType(IWorkbenchPart part, ResourceType resType) {
ISelection selection = getResourceSelection(part);
if (selection != null) {
ActionDelegate action = new ActionDelegate();
action.selectionChanged(null, selection);
if (action.isEnabled(ResourceType.resourceTypeFileOrDirectory) && resType == action.getCommonResourceType()) {
return action;
}
}
return null;
}

static public ActionDelegate getActionExactResourceType(IWorkbenchPart part, ResourceType resType) {
ISelection selection = getResourceSelection(part);
if (selection != null) {
ActionDelegate action = new ActionDelegate();
action.selectionChanged(null, selection);
if (action.isEnabled(resType)) {
return action;
}
}
return null;
}

static public ResourceType getCommonResourceType(IWorkbenchPart part) {
ISelection selection = getResourceSelection(part);
if (selection != null) {
ActionDelegate action = new ActionDelegate();
action.selectionChanged(null, selection);
if (action.isEnabled(ResourceType.resourceTypeFileOrDirectory)) {
return action.getCommonResourceType();
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,58 @@

import java.util.Map;

import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;
import org.eclipse.ui.menus.ExtensionContributionFactory;
import org.eclipse.ui.menus.IContributionRoot;
import org.eclipse.ui.services.IServiceLocator;

import de.anbos.eclipse.easyshell.plugin.Activator;
import de.anbos.eclipse.easyshell.plugin.ResourceUtils;
import de.anbos.eclipse.easyshell.plugin.Constants;
import de.anbos.eclipse.easyshell.plugin.exceptions.UnknownCommandID;
import de.anbos.eclipse.easyshell.plugin.misc.Utils;
import de.anbos.eclipse.easyshell.plugin.preferences.MenuData;
import de.anbos.eclipse.easyshell.plugin.preferences.MenuDataList;
import de.anbos.eclipse.easyshell.plugin.preferences.MenuDataStore;

import de.anbos.eclipse.easyshell.plugin.types.ResourceType;

public class DefineCommands extends ExtensionContributionFactory {

public DefineCommands() {
}

public ResourceType getWantedResourceType() {
return ResourceType.resourceTypeFileOrDirectory;
}

@Override
public void createContributionItems(IServiceLocator serviceLocator, IContributionRoot additions) {
IWorkbenchPart activePart = serviceLocator.getService(IWorkbenchPart.class);
boolean isResourceNavigator = false;
isResourceNavigator = activePart instanceof org.eclipse.ui.views.navigator.ResourceNavigator;
if ((isResourceNavigator == isForResourceNavigator())) {
ResourceType resType = ResourceUtils.getCommonResourceType(activePart);
if (resType != null) {
createContributionItemsForResType(resType, serviceLocator, additions);
}
}
}

public boolean isForResourceNavigator() {
return false;
}

private void createContributionItemsForResType(ResourceType resType, IServiceLocator serviceLocator, IContributionRoot additions) {
MenuManager submenu = new MenuManager("EasyShell", Activator.getImageDescriptor(Constants.IMAGE_EASYSHELL), "de.anbos.eclipse.easyshell.plugin.menu");
MenuDataList items = MenuDataStore.instance().getEnabledCommandMenuDataList();
for (MenuData item : items) {
ResourceType resTypeWanted = getWantedResourceType();
ResourceType resTypeSupported;
try {
resTypeSupported = item.getCommandData().getResourceType();
if ((resTypeSupported == ResourceType.resourceTypeFileOrDirectory)
|| (resTypeSupported == resTypeWanted)) {
addItem(serviceLocator, additions, item.getNameExpanded(), item.getCommand(),
|| (resTypeSupported == resType)) {
addItem(serviceLocator, submenu, item.getNameExpanded(), item.getCommand(),
"de.anbos.eclipse.easyshell.plugin.commands.execute",
Utils.getParameterMapFromMenuData(item), item.getImageId(),
true);
Expand All @@ -58,9 +75,10 @@ public void createContributionItems(IServiceLocator serviceLocator, IContributio
e.logInternalError();
}
}
additions.addContributionItem(submenu, null);
}

private void addItem(IServiceLocator serviceLocator, IContributionRoot additions, String commandLabel, String commandToolTip,
private void addItem(IServiceLocator serviceLocator, IContributionManager submenu, String commandLabel, String commandToolTip,
String commandId, Map<String, Object> commandParamametersMap, String commandImageId, boolean visible) {
CommandContributionItemParameter param = new CommandContributionItemParameter(serviceLocator, "", commandId,
SWT.PUSH);
Expand All @@ -70,6 +88,7 @@ private void addItem(IServiceLocator serviceLocator, IContributionRoot additions
param.parameters = commandParamametersMap;
CommandContributionItem item = new CommandContributionItem(param);
item.setVisible(visible);
additions.addContributionItem(item, null);
submenu.add(item);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@

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

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

public class DefineCommandsForFile extends DefineCommands {

public ResourceType getWantedResourceType() {
return ResourceType.resourceTypeFile;
public boolean isForResourceNavigator() {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.eclipse.ui.handlers.HandlerUtil;

import de.anbos.eclipse.easyshell.plugin.Activator;
import de.anbos.eclipse.easyshell.plugin.EditorPropertyTester;
import de.anbos.eclipse.easyshell.plugin.ResourceUtils;
import de.anbos.eclipse.easyshell.plugin.actions.ActionDelegate;
import de.anbos.eclipse.easyshell.plugin.commands.ExecuteCommandPopup;
import de.anbos.eclipse.easyshell.plugin.exceptions.UnknownCommandID;
Expand All @@ -44,7 +44,7 @@ public class All extends AbstractHandler {
public Object execute(ExecutionEvent event) throws ExecutionException {
// get resource type
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
ActionDelegate action = EditorPropertyTester.getActionExactResourceType(activePart,
ActionDelegate action = ResourceUtils.getActionExactResourceType(activePart,
ResourceType.resourceTypeFileOrDirectory);
if (action != null) {
// load the preferences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.ui.handlers.HandlerUtil;

import de.anbos.eclipse.easyshell.plugin.Activator;
import de.anbos.eclipse.easyshell.plugin.EditorPropertyTester;
import de.anbos.eclipse.easyshell.plugin.ResourceUtils;
import de.anbos.eclipse.easyshell.plugin.actions.Action;
import de.anbos.eclipse.easyshell.plugin.actions.ActionDelegate;
import de.anbos.eclipse.easyshell.plugin.types.CommandType;
Expand All @@ -39,7 +39,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
String commandValue = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.value");
String commandWorkingDir = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.workingdir");
CommandTokenizer commandTokenizer = CommandTokenizer.getFromEnum(event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.tokenizer"));
ActionDelegate action = EditorPropertyTester.getActionExactResourceType(activePart, resourceType);
ActionDelegate action = ResourceUtils.getActionExactResourceType(activePart, resourceType);
if (action != null) {
action.setResourceType(resourceType);
action.setCommandType(commandType);
Expand Down

0 comments on commit 6d1df19

Please sign in to comment.