From 4ec98d98c74b6bce57c3a3cb8f7a8099def10b82 Mon Sep 17 00:00:00 2001 From: Andre Bossert Date: Sun, 4 Oct 2020 22:33:07 +0200 Subject: [PATCH] #74 add "Open With" support valso fixes #164 open in Explorer from "Open Resource" window Signed-off-by: Andre Bossert --- plugin/plugin.xml | 21 ++++++- .../easyshell/plugin/ResourceUtils.java | 58 ++++++++++++------- .../plugin/commands/ExecuteCommandDialog.java | 10 ++-- .../plugin/commands/ExecuteCommandPopup.java | 9 ++- .../easyshell/plugin/editor/LauncherAll.java | 44 ++++++++++++++ .../plugin/editor/LauncherAllMulti.java | 23 ++++++++ .../easyshell/plugin/handlers/All.java | 12 ++-- .../easyshell/plugin/handlers/Execute.java | 8 +-- .../eclipse/easyshell/plugin/misc/Utils.java | 21 +++---- .../plugin/preferences/CommandDataDialog.java | 1 - .../plugin/preferences/MainPage.java | 2 +- 11 files changed, 158 insertions(+), 51 deletions(-) create mode 100644 plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAll.java create mode 100644 plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAllMulti.java diff --git a/plugin/plugin.xml b/plugin/plugin.xml index 2f16a63..79ba97d 100644 --- a/plugin/plugin.xml +++ b/plugin/plugin.xml @@ -71,12 +71,12 @@ + name="EasyShell Single Selection"> + name="EasyShell Multi Selection"> + + + + + + diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java index 9bf4c05..5e53cb5 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.List; +import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IAdaptable; @@ -27,12 +28,14 @@ import org.eclipse.jdt.internal.core.PackageFragment; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.swt.widgets.Event; import org.eclipse.jface.text.ITextSelection; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.handlers.HandlerUtil; import org.eclipse.ui.ide.FileStoreEditorInput; import org.eclipse.ui.part.MultiPageEditorPart; import org.osgi.framework.Bundle; @@ -50,12 +53,12 @@ @SuppressWarnings("restriction") public class ResourceUtils { - static public ISelection getResourceSelection(IWorkbenchPart part) { + static public ISelection getResourceSelection(Object obj) { ISelection selection = null; - if (part != null) { - if (part instanceof IEditorPart) { - // get file from part (can be only one) - Resource file = getResource((IEditorPart)part); + if (obj != null) { + Resource resource = getResource(obj); + if (obj instanceof IEditorPart) { + IEditorPart part = (IEditorPart)obj; ITextEditor editor = null; if (part instanceof ITextEditor) { editor = (ITextEditor) part; @@ -68,19 +71,19 @@ static public ISelection getResourceSelection(IWorkbenchPart part) { if (editor != null) { ITextSelection sel = (ITextSelection)editor.getSelectionProvider().getSelection(); String text = sel.getText(); - selection = getSelectionFromText(file, text); + selection = getSelectionFromText(resource, text); } - // fallback to the selected part if no selection - if ((selection == null) && (file != null)) { - selection = new StructuredSelection(file); - } - } else { + } else if (obj instanceof IWorkbenchPart) { try { - selection = part.getSite().getSelectionProvider().getSelection(); + selection = ((IWorkbenchPart)obj).getSite().getSelectionProvider().getSelection(); } catch(Exception e) { // no op } } + // fallback to the selected part if still no selection + if ((selection == null) && (resource != null)) { + selection = new StructuredSelection(resource); + } } return selection; } @@ -138,10 +141,13 @@ static public Resource getResource(Object myObj) { return new Resource((Resource)object); } if (object instanceof IFile) { - return new Resource(((IFile) object)); + return new Resource((IFile)object); + } + if (object instanceof IPath) { + return new Resource(((IPath)object).toFile()); } if (object instanceof File) { - return new Resource((File) object); + return new Resource((File)object); } // still adaptable @@ -214,8 +220,8 @@ static private File getJarFile(IAdaptable adaptable) { return file; } - static public ActionDelegate getActionCommonResourceType(IWorkbenchPart part, ResourceType resType) { - ISelection selection = getResourceSelection(part); + static public ActionDelegate getActionCommonResourceType(Object obj, ResourceType resType) { + ISelection selection = getResourceSelection(obj); if (selection != null) { ActionDelegate action = new ActionDelegate(); action.selectionChanged(null, selection); @@ -226,8 +232,8 @@ static public ActionDelegate getActionCommonResourceType(IWorkbenchPart part, Re return null; } - static public ActionDelegate getActionExactResourceType(IWorkbenchPart part, ResourceType resType) { - ISelection selection = getResourceSelection(part); + static public ActionDelegate getActionExactResourceType(Object obj, ResourceType resType) { + ISelection selection = getResourceSelection(obj); if (selection != null) { ActionDelegate action = new ActionDelegate(); action.selectionChanged(null, selection); @@ -238,8 +244,8 @@ static public ActionDelegate getActionExactResourceType(IWorkbenchPart part, Res return null; } - static public ResourceType getCommonResourceType(IWorkbenchPart part) { - ISelection selection = getResourceSelection(part); + static public ResourceType getCommonResourceType(Object obj) { + ISelection selection = getResourceSelection(obj); if (selection != null) { ActionDelegate action = new ActionDelegate(); action.selectionChanged(null, selection); @@ -250,4 +256,16 @@ static public ResourceType getCommonResourceType(IWorkbenchPart part) { return null; } + public static Object getEventData(ExecutionEvent event) { + Object triggerEventData = null; + Object trigger = event.getTrigger(); + if ((trigger != null) && (trigger instanceof Event)) { + triggerEventData = ((Event)trigger).data; + } + if (triggerEventData == null) { + triggerEventData = HandlerUtil.getActivePart(event); + } + return triggerEventData; + } + } diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/commands/ExecuteCommandDialog.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/commands/ExecuteCommandDialog.java index d3b7e1e..e0bcdde 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/commands/ExecuteCommandDialog.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/commands/ExecuteCommandDialog.java @@ -28,8 +28,9 @@ public class ExecuteCommandDialog extends ElementListSelectionDialog { private IWorkbench workbench; + private Object trigger; - public ExecuteCommandDialog(Shell parent, IWorkbench workbench, MenuDataList menuDataList, String title) + public ExecuteCommandDialog(Shell parent, IWorkbench workbench, Object trigger, MenuDataList menuDataList, String title) { super(parent, new ExecuteCommandLabelProvider()); setTitle(title); @@ -40,7 +41,7 @@ public ExecuteCommandDialog(Shell parent, IWorkbench workbench, MenuDataList men setMatchEmptyString(true); setMultipleSelection(true); setImage(Activator.getImage(Constants.IMAGE_EASYSHELL)); - init(workbench, menuDataList); + init(workbench, trigger, menuDataList); } @Override @@ -51,8 +52,9 @@ protected void okPressed() { //this.close(); } - void init(IWorkbench workbench, MenuDataList menuDataList) { + void init(IWorkbench workbench, Object trigger, MenuDataList menuDataList) { this.workbench = workbench; + this.trigger = trigger; Object[] elements = new MenuData[menuDataList.size()]; for (int i=0;i chars; - public ExecuteCommandPopup(Shell parent, IWorkbench workbench, MenuDataList menuDataList, String title) + public ExecuteCommandPopup(Shell parent, IWorkbench workbench, Object trigger, MenuDataList menuDataList, String title) { super(parent, INFOPOPUP_SHELLSTYLE, true, false, false, false, false, title, "..."); this.workbench = workbench; + this.trigger = trigger; this.menuDataList = menuDataList; chars = new ArrayList(); for (Character ch='0';ch<='9';ch++) { @@ -50,8 +52,9 @@ public ExecuteCommandPopup(Shell parent, IWorkbench workbench, MenuDataList menu chars.add(ch); } int charsSize = chars.size(); - if (menuDataList.size() < charsSize) + if (menuDataList.size() < charsSize) { charsSize = menuDataList.size(); + } String info = "use '0'"; if (charsSize > 1) { if( charsSize <= 10) { @@ -111,7 +114,7 @@ private void executeCommandFromList(int index) { e.printStackTrace(); } // execute - Utils.executeCommand(workbench, menuDataList.get(index), true); + Utils.executeCommand(workbench, trigger, menuDataList.get(index), true); } @Override diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAll.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAll.java new file mode 100644 index 0000000..3c8a1ec --- /dev/null +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAll.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2014-2020 Andre Bossert . + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ + +package de.anbos.eclipse.easyshell.plugin.editor; + +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.commands.NotEnabledException; +import org.eclipse.core.commands.NotHandledException; +import org.eclipse.core.commands.common.NotDefinedException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.swt.widgets.Event; +import org.eclipse.ui.IEditorLauncher; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.IHandlerService; + +public class LauncherAll implements IEditorLauncher { + + protected String getCommandId() { + return "de.anbos.eclipse.easyshell.plugin.commands.all"; + } + + @Override + public void open(IPath file) { + IHandlerService handlerService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService.class); + try { + Event event = new Event(); + event.data = file; + handlerService.executeCommand(getCommandId(), event); + } catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) { + e.printStackTrace(); + } + } + +} diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAllMulti.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAllMulti.java new file mode 100644 index 0000000..632c677 --- /dev/null +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/editor/LauncherAllMulti.java @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2014-2020 Andre Bossert . + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ + +package de.anbos.eclipse.easyshell.plugin.editor; + +public class LauncherAllMulti extends LauncherAll { + + @Override + protected String getCommandId() { + return "de.anbos.eclipse.easyshell.plugin.commands.allmulti"; + } + +} diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/All.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/All.java index 2f67724..8270a78 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/All.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/All.java @@ -43,16 +43,18 @@ public class All extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { // get resource type - IWorkbenchPart activePart = HandlerUtil.getActivePart(event); - ActionDelegate action = ResourceUtils.getActionExactResourceType(activePart, + Object triggerEventData = ResourceUtils.getEventData(event); + ActionDelegate action = ResourceUtils.getActionExactResourceType(triggerEventData, ResourceType.resourceTypeFileOrDirectory); if (action != null) { // load the preferences list = getMenuDataList(action.getCommonResourceType()); if (list.size() > 0) { + IWorkbenchPart activePart = HandlerUtil.getActivePart(event); + Object trigger = event.getTrigger(); IWorkbenchWindow workbenchWindow = activePart.getSite().getWorkbenchWindow(); if (list.size() == 1) { - Utils.executeCommand(workbenchWindow.getWorkbench(), list.get(0), false); + Utils.executeCommand(workbenchWindow.getWorkbench(), trigger, list.get(0), false); } else { // create and open a new dialog // close the old dialog @@ -62,10 +64,10 @@ public Object execute(ExecutionEvent event) throws ExecutionException { } if (usePopup) { dialog = new ExecuteCommandPopup(workbenchWindow.getShell(), workbenchWindow.getWorkbench(), - list, getTitle()); + trigger, list, getTitle()); } else { dialog = new ExecuteCommandDialog(workbenchWindow.getShell(), workbenchWindow.getWorkbench(), - list, getTitle()); + trigger, list, getTitle()); } dialog.open(); } diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/Execute.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/Execute.java index af7cbfc..eb232a9 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/Execute.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/Execute.java @@ -17,8 +17,6 @@ import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.action.IAction; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; import de.anbos.eclipse.easyshell.plugin.Activator; import de.anbos.eclipse.easyshell.plugin.ResourceUtils; @@ -31,15 +29,15 @@ public class Execute extends AbstractHandler { public Object execute(ExecutionEvent event) throws ExecutionException { - IWorkbenchPart activePart = HandlerUtil.getActivePart(event); - if (activePart != null) { + Object triggerEventData = ResourceUtils.getEventData(event); + if (triggerEventData != null) { String commandID = event.getCommand().getId(); ResourceType resourceType = ResourceType.getFromEnum(event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.resource")); CommandType commandType = CommandType.getFromAction(event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.type")); 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 = ResourceUtils.getActionExactResourceType(activePart, resourceType); + ActionDelegate action = ResourceUtils.getActionExactResourceType(triggerEventData, resourceType); if (action != null) { action.setResourceType(resourceType); action.setCommandType(commandType); diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/misc/Utils.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/misc/Utils.java index 3be0314..7221e78 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/misc/Utils.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/misc/Utils.java @@ -30,6 +30,7 @@ import org.eclipse.swt.dnd.Transfer; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.ToolTip; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.PlatformUI; @@ -270,44 +271,44 @@ public static Map getParameterMapFromMenuData(MenuData menuData) return params; } - public static void executeCommand(final IWorkbench workbench, final MenuData menuData, boolean asynch) { + public static void executeCommand(final IWorkbench workbench, final Object trigger, final MenuData menuData, boolean asynch) { //Activator.logInfo("executeCommand: " + menuData.getNameExpanded() + ", " + asynch, null); - executeCommand(workbench, "de.anbos.eclipse.easyshell.plugin.commands.execute", getParameterMapFromMenuData(menuData), asynch); + executeCommand(workbench, trigger, "de.anbos.eclipse.easyshell.plugin.commands.execute", getParameterMapFromMenuData(menuData), asynch); } - public static void executeCommand(final IWorkbench workbench, final String commandName, final Map params, boolean asynch) { + public static void executeCommand(final IWorkbench workbench, final Object trigger, final String commandName, final Map params, boolean asynch) { if (asynch) { Display display = workbench == null ? Display.getDefault() : workbench.getDisplay(); display.asyncExec( new Runnable(){ @Override public void run() { - executeCommand(workbench, commandName, params); + executeCommand(workbench, trigger, commandName, params); } }); } else { - executeCommand(workbench, commandName, params); + executeCommand(workbench, trigger, commandName, params); } } - public static void executeCommands(final IWorkbench workbench, final List menuData, boolean asynch) { + public static void executeCommands(final IWorkbench workbench, Object trigger, final List menuData, boolean asynch) { if (asynch) { Display display = workbench == null ? Display.getDefault() : workbench.getDisplay(); display.asyncExec( new Runnable(){ @Override public void run() { for (MenuData element : menuData) { - executeCommand(workbench, element, false); + executeCommand(workbench, trigger, element, false); } } }); } else { for (MenuData element : menuData) { - executeCommand(workbench, element, false); + executeCommand(workbench, trigger, element, false); } } } - private static void executeCommand(IWorkbench workbench, String commandName, Map params) { + private static void executeCommand(IWorkbench workbench, Object trigger, String commandName, Map params) { if (workbench == null) { workbench = PlatformUI.getWorkbench(); } @@ -321,7 +322,7 @@ private static void executeCommand(IWorkbench workbench, String commandName, Map if (command != null && handlerService != null) { ParameterizedCommand paramCommand = ParameterizedCommand.generateCommand(command, params); try { - handlerService.executeCommand(paramCommand, null); + handlerService.executeCommand(paramCommand, (Event)trigger); } catch (Exception e) { Activator.logError(Activator.getResourceString("easyshell.message.error.handlerservice.execution"), paramCommand.toString(), e, true); } diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/CommandDataDialog.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/CommandDataDialog.java index e50cdb0..a1f3487 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/CommandDataDialog.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/CommandDataDialog.java @@ -187,7 +187,6 @@ private ContentProposalAdapter addContentAssistSimple(Text textControl) { try { keyStroke = KeyStroke.getInstance("Ctrl+Space"); } catch (ParseException e) { - // TODO Auto-generated catch block e.printStackTrace(); } // assume that myTextControl has already been created in some way diff --git a/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/MainPage.java b/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/MainPage.java index af79e58..1dcd13e 100644 --- a/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/MainPage.java +++ b/plugin/src/de/anbos/eclipse/easyshell/plugin/preferences/MainPage.java @@ -140,7 +140,7 @@ protected Control createContents(Composite parent) { // forward to menu page Map params = new HashMap(); params.put("preferencePageId","de.anbos.eclipse.easyshell.plugin.preferences.MenuPage"); - Utils.executeCommand(workbench, "org.eclipse.ui.window.preferences", params, true); + Utils.executeCommand(workbench, null, "org.eclipse.ui.window.preferences", params, true); // create own Composite pageComponent = new Composite(parent, SWT.NONE);