Skip to content

Commit

Permalink
#74 add "Open With" support
Browse files Browse the repository at this point in the history
valso fixes #164 open in Explorer from "Open Resource" window

Signed-off-by: Andre Bossert <[email protected]>
  • Loading branch information
anb0s committed Oct 4, 2020
1 parent 84e124a commit 4ec98d9
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 51 deletions.
21 changes: 19 additions & 2 deletions plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
<command
categoryId="de.anbos.eclipse.easyshell.plugin.commands.category"
id="de.anbos.eclipse.easyshell.plugin.commands.all"
name="EasyShell Single Selection Popup">
name="EasyShell Single Selection">
</command>
<command
categoryId="de.anbos.eclipse.easyshell.plugin.commands.category"
id="de.anbos.eclipse.easyshell.plugin.commands.allmulti"
name="EasyShell Multi Selection Dialog">
name="EasyShell Multi Selection">
</command>
</extension>
<extension
Expand Down Expand Up @@ -214,5 +214,22 @@
sequence="M2+M3+E">
</key>
</extension>
<extension
point="org.eclipse.ui.editors">
<editor
default="false"
icon="images/easyshell.png"
id="de.anbos.eclipse.easyshell.plugin.editor"
launcher="de.anbos.eclipse.easyshell.plugin.editor.LauncherAll"
name="EasyShell Single Selection">
</editor>
<editor
default="false"
icon="images/easyshell.png"
id="de.anbos.eclipse.easyshell.plugin.editor"
launcher="de.anbos.eclipse.easyshell.plugin.editor.LauncherAllMulti"
name="EasyShell Multi Selection">
</editor>
</extension>

</plugin>
58 changes: 38 additions & 20 deletions plugin/src/de/anbos/eclipse/easyshell/plugin/ResourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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<menuDataList.size();i++) {
MenuData item = menuDataList.get(i);
Expand All @@ -67,7 +69,7 @@ private void executeCommandFromList() {
for (Object element : objects) {
menues.add((MenuData)element);
}
Utils.executeCommands(workbench, menues, true);
Utils.executeCommands(workbench, trigger, menues, true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
public class ExecuteCommandPopup extends org.eclipse.jface.dialogs.PopupDialog implements SelectionListener, KeyListener {

private IWorkbench workbench;
private Object trigger;
private MenuDataList menuDataList;
private org.eclipse.swt.widgets.List listView;
private List<Character> 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<Character>();
for (Character ch='0';ch<='9';ch++) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2014-2020 Andre Bossert <[email protected]>.
*
* 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();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2014-2020 Andre Bossert <[email protected]>.
*
* 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";
}

}
12 changes: 7 additions & 5 deletions plugin/src/de/anbos/eclipse/easyshell/plugin/handlers/All.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 4ec98d9

Please sign in to comment.