Skip to content

Commit 4ec98d9

Browse files
committed
#74 add "Open With" support
valso fixes #164 open in Explorer from "Open Resource" window Signed-off-by: Andre Bossert <[email protected]>
1 parent 84e124a commit 4ec98d9

File tree

11 files changed

+158
-51
lines changed

11 files changed

+158
-51
lines changed

plugin/plugin.xml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@
7171
<command
7272
categoryId="de.anbos.eclipse.easyshell.plugin.commands.category"
7373
id="de.anbos.eclipse.easyshell.plugin.commands.all"
74-
name="EasyShell Single Selection Popup">
74+
name="EasyShell Single Selection">
7575
</command>
7676
<command
7777
categoryId="de.anbos.eclipse.easyshell.plugin.commands.category"
7878
id="de.anbos.eclipse.easyshell.plugin.commands.allmulti"
79-
name="EasyShell Multi Selection Dialog">
79+
name="EasyShell Multi Selection">
8080
</command>
8181
</extension>
8282
<extension
@@ -214,5 +214,22 @@
214214
sequence="M2+M3+E">
215215
</key>
216216
</extension>
217+
<extension
218+
point="org.eclipse.ui.editors">
219+
<editor
220+
default="false"
221+
icon="images/easyshell.png"
222+
id="de.anbos.eclipse.easyshell.plugin.editor"
223+
launcher="de.anbos.eclipse.easyshell.plugin.editor.LauncherAll"
224+
name="EasyShell Single Selection">
225+
</editor>
226+
<editor
227+
default="false"
228+
icon="images/easyshell.png"
229+
id="de.anbos.eclipse.easyshell.plugin.editor"
230+
launcher="de.anbos.eclipse.easyshell.plugin.editor.LauncherAllMulti"
231+
name="EasyShell Multi Selection">
232+
</editor>
233+
</extension>
217234

218235
</plugin>

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020

21+
import org.eclipse.core.commands.ExecutionEvent;
2122
import org.eclipse.core.resources.IFile;
2223
import org.eclipse.core.resources.IResource;
2324
import org.eclipse.core.runtime.IAdaptable;
@@ -27,12 +28,14 @@
2728
import org.eclipse.jdt.internal.core.PackageFragment;
2829
import org.eclipse.jface.viewers.ISelection;
2930
import org.eclipse.jface.viewers.StructuredSelection;
31+
import org.eclipse.swt.widgets.Event;
3032
import org.eclipse.jface.text.ITextSelection;
3133
import org.eclipse.ui.IEditorInput;
3234
import org.eclipse.ui.IEditorPart;
3335
import org.eclipse.ui.IFileEditorInput;
3436
import org.eclipse.ui.texteditor.ITextEditor;
3537
import org.eclipse.ui.IWorkbenchPart;
38+
import org.eclipse.ui.handlers.HandlerUtil;
3639
import org.eclipse.ui.ide.FileStoreEditorInput;
3740
import org.eclipse.ui.part.MultiPageEditorPart;
3841
import org.osgi.framework.Bundle;
@@ -50,12 +53,12 @@
5053
@SuppressWarnings("restriction")
5154
public class ResourceUtils {
5255

53-
static public ISelection getResourceSelection(IWorkbenchPart part) {
56+
static public ISelection getResourceSelection(Object obj) {
5457
ISelection selection = null;
55-
if (part != null) {
56-
if (part instanceof IEditorPart) {
57-
// get file from part (can be only one)
58-
Resource file = getResource((IEditorPart)part);
58+
if (obj != null) {
59+
Resource resource = getResource(obj);
60+
if (obj instanceof IEditorPart) {
61+
IEditorPart part = (IEditorPart)obj;
5962
ITextEditor editor = null;
6063
if (part instanceof ITextEditor) {
6164
editor = (ITextEditor) part;
@@ -68,19 +71,19 @@ static public ISelection getResourceSelection(IWorkbenchPart part) {
6871
if (editor != null) {
6972
ITextSelection sel = (ITextSelection)editor.getSelectionProvider().getSelection();
7073
String text = sel.getText();
71-
selection = getSelectionFromText(file, text);
74+
selection = getSelectionFromText(resource, text);
7275
}
73-
// fallback to the selected part if no selection
74-
if ((selection == null) && (file != null)) {
75-
selection = new StructuredSelection(file);
76-
}
77-
} else {
76+
} else if (obj instanceof IWorkbenchPart) {
7877
try {
79-
selection = part.getSite().getSelectionProvider().getSelection();
78+
selection = ((IWorkbenchPart)obj).getSite().getSelectionProvider().getSelection();
8079
} catch(Exception e) {
8180
// no op
8281
}
8382
}
83+
// fallback to the selected part if still no selection
84+
if ((selection == null) && (resource != null)) {
85+
selection = new StructuredSelection(resource);
86+
}
8487
}
8588
return selection;
8689
}
@@ -138,10 +141,13 @@ static public Resource getResource(Object myObj) {
138141
return new Resource((Resource)object);
139142
}
140143
if (object instanceof IFile) {
141-
return new Resource(((IFile) object));
144+
return new Resource((IFile)object);
145+
}
146+
if (object instanceof IPath) {
147+
return new Resource(((IPath)object).toFile());
142148
}
143149
if (object instanceof File) {
144-
return new Resource((File) object);
150+
return new Resource((File)object);
145151
}
146152

147153
// still adaptable
@@ -214,8 +220,8 @@ static private File getJarFile(IAdaptable adaptable) {
214220
return file;
215221
}
216222

217-
static public ActionDelegate getActionCommonResourceType(IWorkbenchPart part, ResourceType resType) {
218-
ISelection selection = getResourceSelection(part);
223+
static public ActionDelegate getActionCommonResourceType(Object obj, ResourceType resType) {
224+
ISelection selection = getResourceSelection(obj);
219225
if (selection != null) {
220226
ActionDelegate action = new ActionDelegate();
221227
action.selectionChanged(null, selection);
@@ -226,8 +232,8 @@ static public ActionDelegate getActionCommonResourceType(IWorkbenchPart part, Re
226232
return null;
227233
}
228234

229-
static public ActionDelegate getActionExactResourceType(IWorkbenchPart part, ResourceType resType) {
230-
ISelection selection = getResourceSelection(part);
235+
static public ActionDelegate getActionExactResourceType(Object obj, ResourceType resType) {
236+
ISelection selection = getResourceSelection(obj);
231237
if (selection != null) {
232238
ActionDelegate action = new ActionDelegate();
233239
action.selectionChanged(null, selection);
@@ -238,8 +244,8 @@ static public ActionDelegate getActionExactResourceType(IWorkbenchPart part, Res
238244
return null;
239245
}
240246

241-
static public ResourceType getCommonResourceType(IWorkbenchPart part) {
242-
ISelection selection = getResourceSelection(part);
247+
static public ResourceType getCommonResourceType(Object obj) {
248+
ISelection selection = getResourceSelection(obj);
243249
if (selection != null) {
244250
ActionDelegate action = new ActionDelegate();
245251
action.selectionChanged(null, selection);
@@ -250,4 +256,16 @@ static public ResourceType getCommonResourceType(IWorkbenchPart part) {
250256
return null;
251257
}
252258

259+
public static Object getEventData(ExecutionEvent event) {
260+
Object triggerEventData = null;
261+
Object trigger = event.getTrigger();
262+
if ((trigger != null) && (trigger instanceof Event)) {
263+
triggerEventData = ((Event)trigger).data;
264+
}
265+
if (triggerEventData == null) {
266+
triggerEventData = HandlerUtil.getActivePart(event);
267+
}
268+
return triggerEventData;
269+
}
270+
253271
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
public class ExecuteCommandDialog extends ElementListSelectionDialog {
2929

3030
private IWorkbench workbench;
31+
private Object trigger;
3132

32-
public ExecuteCommandDialog(Shell parent, IWorkbench workbench, MenuDataList menuDataList, String title)
33+
public ExecuteCommandDialog(Shell parent, IWorkbench workbench, Object trigger, MenuDataList menuDataList, String title)
3334
{
3435
super(parent, new ExecuteCommandLabelProvider());
3536
setTitle(title);
@@ -40,7 +41,7 @@ public ExecuteCommandDialog(Shell parent, IWorkbench workbench, MenuDataList men
4041
setMatchEmptyString(true);
4142
setMultipleSelection(true);
4243
setImage(Activator.getImage(Constants.IMAGE_EASYSHELL));
43-
init(workbench, menuDataList);
44+
init(workbench, trigger, menuDataList);
4445
}
4546

4647
@Override
@@ -51,8 +52,9 @@ protected void okPressed() {
5152
//this.close();
5253
}
5354

54-
void init(IWorkbench workbench, MenuDataList menuDataList) {
55+
void init(IWorkbench workbench, Object trigger, MenuDataList menuDataList) {
5556
this.workbench = workbench;
57+
this.trigger = trigger;
5658
Object[] elements = new MenuData[menuDataList.size()];
5759
for (int i=0;i<menuDataList.size();i++) {
5860
MenuData item = menuDataList.get(i);
@@ -67,7 +69,7 @@ private void executeCommandFromList() {
6769
for (Object element : objects) {
6870
menues.add((MenuData)element);
6971
}
70-
Utils.executeCommands(workbench, menues, true);
72+
Utils.executeCommands(workbench, trigger, menues, true);
7173
}
7274

7375
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@
3333
public class ExecuteCommandPopup extends org.eclipse.jface.dialogs.PopupDialog implements SelectionListener, KeyListener {
3434

3535
private IWorkbench workbench;
36+
private Object trigger;
3637
private MenuDataList menuDataList;
3738
private org.eclipse.swt.widgets.List listView;
3839
private List<Character> chars;
3940

40-
public ExecuteCommandPopup(Shell parent, IWorkbench workbench, MenuDataList menuDataList, String title)
41+
public ExecuteCommandPopup(Shell parent, IWorkbench workbench, Object trigger, MenuDataList menuDataList, String title)
4142
{
4243
super(parent, INFOPOPUP_SHELLSTYLE, true, false, false, false, false, title, "...");
4344
this.workbench = workbench;
45+
this.trigger = trigger;
4446
this.menuDataList = menuDataList;
4547
chars = new ArrayList<Character>();
4648
for (Character ch='0';ch<='9';ch++) {
@@ -50,8 +52,9 @@ public ExecuteCommandPopup(Shell parent, IWorkbench workbench, MenuDataList menu
5052
chars.add(ch);
5153
}
5254
int charsSize = chars.size();
53-
if (menuDataList.size() < charsSize)
55+
if (menuDataList.size() < charsSize) {
5456
charsSize = menuDataList.size();
57+
}
5558
String info = "use '0'";
5659
if (charsSize > 1) {
5760
if( charsSize <= 10) {
@@ -111,7 +114,7 @@ private void executeCommandFromList(int index) {
111114
e.printStackTrace();
112115
}
113116
// execute
114-
Utils.executeCommand(workbench, menuDataList.get(index), true);
117+
Utils.executeCommand(workbench, trigger, menuDataList.get(index), true);
115118
}
116119

117120
@Override
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) 2014-2020 Andre Bossert <[email protected]>.
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
14+
package de.anbos.eclipse.easyshell.plugin.editor;
15+
16+
import org.eclipse.core.commands.ExecutionException;
17+
import org.eclipse.core.commands.NotEnabledException;
18+
import org.eclipse.core.commands.NotHandledException;
19+
import org.eclipse.core.commands.common.NotDefinedException;
20+
import org.eclipse.core.runtime.IPath;
21+
import org.eclipse.swt.widgets.Event;
22+
import org.eclipse.ui.IEditorLauncher;
23+
import org.eclipse.ui.PlatformUI;
24+
import org.eclipse.ui.handlers.IHandlerService;
25+
26+
public class LauncherAll implements IEditorLauncher {
27+
28+
protected String getCommandId() {
29+
return "de.anbos.eclipse.easyshell.plugin.commands.all";
30+
}
31+
32+
@Override
33+
public void open(IPath file) {
34+
IHandlerService handlerService = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getService(IHandlerService.class);
35+
try {
36+
Event event = new Event();
37+
event.data = file;
38+
handlerService.executeCommand(getCommandId(), event);
39+
} catch (ExecutionException | NotDefinedException | NotEnabledException | NotHandledException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (c) 2014-2020 Andre Bossert <[email protected]>.
3+
*
4+
* See the NOTICE file(s) distributed with this work for additional
5+
* information regarding copyright ownership.
6+
*
7+
* This program and the accompanying materials are made available under the
8+
* terms of the Eclipse Public License 2.0 which is available at
9+
* http://www.eclipse.org/legal/epl-2.0
10+
*
11+
* SPDX-License-Identifier: EPL-2.0
12+
*/
13+
14+
package de.anbos.eclipse.easyshell.plugin.editor;
15+
16+
public class LauncherAllMulti extends LauncherAll {
17+
18+
@Override
19+
protected String getCommandId() {
20+
return "de.anbos.eclipse.easyshell.plugin.commands.allmulti";
21+
}
22+
23+
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ public class All extends AbstractHandler {
4343
@Override
4444
public Object execute(ExecutionEvent event) throws ExecutionException {
4545
// get resource type
46-
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
47-
ActionDelegate action = ResourceUtils.getActionExactResourceType(activePart,
46+
Object triggerEventData = ResourceUtils.getEventData(event);
47+
ActionDelegate action = ResourceUtils.getActionExactResourceType(triggerEventData,
4848
ResourceType.resourceTypeFileOrDirectory);
4949
if (action != null) {
5050
// load the preferences
5151
list = getMenuDataList(action.getCommonResourceType());
5252
if (list.size() > 0) {
53+
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
54+
Object trigger = event.getTrigger();
5355
IWorkbenchWindow workbenchWindow = activePart.getSite().getWorkbenchWindow();
5456
if (list.size() == 1) {
55-
Utils.executeCommand(workbenchWindow.getWorkbench(), list.get(0), false);
57+
Utils.executeCommand(workbenchWindow.getWorkbench(), trigger, list.get(0), false);
5658
} else {
5759
// create and open a new dialog
5860
// close the old dialog
@@ -62,10 +64,10 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
6264
}
6365
if (usePopup) {
6466
dialog = new ExecuteCommandPopup(workbenchWindow.getShell(), workbenchWindow.getWorkbench(),
65-
list, getTitle());
67+
trigger, list, getTitle());
6668
} else {
6769
dialog = new ExecuteCommandDialog(workbenchWindow.getShell(), workbenchWindow.getWorkbench(),
68-
list, getTitle());
70+
trigger, list, getTitle());
6971
}
7072
dialog.open();
7173
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import org.eclipse.core.commands.ExecutionEvent;
1818
import org.eclipse.core.commands.ExecutionException;
1919
import org.eclipse.jface.action.IAction;
20-
import org.eclipse.ui.IWorkbenchPart;
21-
import org.eclipse.ui.handlers.HandlerUtil;
2220

2321
import de.anbos.eclipse.easyshell.plugin.Activator;
2422
import de.anbos.eclipse.easyshell.plugin.ResourceUtils;
@@ -31,15 +29,15 @@
3129
public class Execute extends AbstractHandler {
3230

3331
public Object execute(ExecutionEvent event) throws ExecutionException {
34-
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
35-
if (activePart != null) {
32+
Object triggerEventData = ResourceUtils.getEventData(event);
33+
if (triggerEventData != null) {
3634
String commandID = event.getCommand().getId();
3735
ResourceType resourceType = ResourceType.getFromEnum(event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.resource"));
3836
CommandType commandType = CommandType.getFromAction(event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.type"));
3937
String commandValue = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.value");
4038
String commandWorkingDir = event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.workingdir");
4139
CommandTokenizer commandTokenizer = CommandTokenizer.getFromEnum(event.getParameter("de.anbos.eclipse.easyshell.plugin.commands.parameter.tokenizer"));
42-
ActionDelegate action = ResourceUtils.getActionExactResourceType(activePart, resourceType);
40+
ActionDelegate action = ResourceUtils.getActionExactResourceType(triggerEventData, resourceType);
4341
if (action != null) {
4442
action.setResourceType(resourceType);
4543
action.setCommandType(commandType);

0 commit comments

Comments
 (0)