1
|
package eu.etaxonomy.taxeditor.view;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.Map;
|
5
|
import java.util.Map.Entry;
|
6
|
import java.util.UUID;
|
7
|
|
8
|
import org.eclipse.core.commands.Command;
|
9
|
import org.eclipse.core.commands.ParameterizedCommand;
|
10
|
import org.eclipse.core.commands.common.NotDefinedException;
|
11
|
import org.eclipse.jface.action.ContributionItem;
|
12
|
import org.eclipse.jface.action.IContributionItem;
|
13
|
import org.eclipse.jface.viewers.ISelection;
|
14
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
15
|
import org.eclipse.swt.SWT;
|
16
|
import org.eclipse.swt.events.SelectionAdapter;
|
17
|
import org.eclipse.swt.events.SelectionEvent;
|
18
|
import org.eclipse.swt.widgets.Menu;
|
19
|
import org.eclipse.swt.widgets.MenuItem;
|
20
|
import org.eclipse.ui.IWorkbenchWindow;
|
21
|
import org.eclipse.ui.PlatformUI;
|
22
|
import org.eclipse.ui.actions.CompoundContributionItem;
|
23
|
import org.eclipse.ui.commands.ICommandService;
|
24
|
import org.eclipse.ui.handlers.IHandlerService;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
27
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
28
|
|
29
|
/**
|
30
|
* Generic context menu for opening elements in the taxeditor.
|
31
|
*
|
32
|
*/
|
33
|
public class CdmViewerContextMenu extends CompoundContributionItem {
|
34
|
|
35
|
@Override
|
36
|
protected IContributionItem[] getContributionItems() {
|
37
|
IContributionItem[] contributionItems = new IContributionItem[] {
|
38
|
new ContributionItem() {
|
39
|
@Override
|
40
|
public void fill(Menu menu, int index) {
|
41
|
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
|
42
|
final ISelection selection = window.getActivePage().getSelection();
|
43
|
if(selection instanceof IStructuredSelection){
|
44
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
45
|
Map<String, String> availableViewers = CdmViewerUtil.getAvailableViewers(firstElement);
|
46
|
//if only one viewer/command is available then show only this one
|
47
|
if(availableViewers.size()==1){
|
48
|
//else show a list of possible viewers/commands
|
49
|
Entry<String, String> entry = availableViewers.entrySet().iterator().next();
|
50
|
final String commandId = entry.getKey();
|
51
|
String viewerName = entry.getValue();
|
52
|
MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
|
53
|
addItem.setText(String.format("Open (%s)", viewerName));
|
54
|
addItem.addSelectionListener(new CommandInvoker(commandId, firstElement)) ;
|
55
|
}
|
56
|
else if (availableViewers.size()>1){
|
57
|
//else show a list of possible viewers/commands
|
58
|
MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
|
59
|
addItem.setText("Open in...");
|
60
|
Menu addMenu = new Menu(menu);
|
61
|
addItem.setMenu(addMenu);
|
62
|
for(Entry<String, String> entry:availableViewers.entrySet()){
|
63
|
final String commandId = entry.getKey();
|
64
|
String viewerName = entry.getValue();
|
65
|
MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
|
66
|
menuItem.setText(viewerName);
|
67
|
menuItem.addSelectionListener(new CommandInvoker(commandId, firstElement)) ;
|
68
|
}
|
69
|
}
|
70
|
}
|
71
|
}
|
72
|
}
|
73
|
};
|
74
|
return contributionItems;
|
75
|
}
|
76
|
|
77
|
private final class CommandInvoker extends SelectionAdapter {
|
78
|
private final String commandId;
|
79
|
private final Object selectedObject;
|
80
|
|
81
|
private CommandInvoker(String commandId, Object selectedObject) {
|
82
|
this.commandId = commandId;
|
83
|
this.selectedObject = selectedObject;
|
84
|
}
|
85
|
|
86
|
@Override
|
87
|
public void widgetSelected(SelectionEvent e) {
|
88
|
ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
|
89
|
|
90
|
Command command = commandService.getCommand(commandId);
|
91
|
if(command.isEnabled()) {
|
92
|
IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
|
93
|
Map<String, UUID> params = new HashMap<String, UUID>();
|
94
|
if(selectedObject instanceof ICdmBase){
|
95
|
params.put(commandId+".uuid", ((ICdmBase) selectedObject).getUuid());
|
96
|
}
|
97
|
ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
|
98
|
try {
|
99
|
handlerService.executeCommand(parameterizedCommand, null);
|
100
|
} catch (NotDefinedException nde) {
|
101
|
throw new RuntimeException("Could not find open command: " + commandId);
|
102
|
} catch (Exception exception) {
|
103
|
MessagingUtils.error(getClass(), "An exception occured while trying execute "+commandId, exception);
|
104
|
}
|
105
|
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
|
110
|
}
|