db59d464d01ba40ddfd6c14b3a10fdb4a4b783d1
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerContextMenu.java
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 Map<Command, String> enabledCommands = getEnabledCommands(availableViewers);
47
48 //check if only one or multiple viewers/commands are available
49 if(enabledCommands.size()==1){
50 Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
51 final Command command = entry.getKey();
52 String viewerName = entry.getValue();
53
54 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
55 addItem.setText(String.format("Open (%s)", viewerName));
56 addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
57 }
58 else if(enabledCommands.size()>1){
59 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
60 addItem.setText("Open in...");
61 Menu addMenu = new Menu(menu);
62 addItem.setMenu(addMenu);
63 for(Entry<Command, String> entry:enabledCommands.entrySet()){
64 final Command command = entry.getKey();
65 String viewerName = entry.getValue();
66
67 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
68 menuItem.setText(viewerName);
69 menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
70 }
71 }
72 }
73 }
74
75 }
76 };
77 return contributionItems;
78 }
79
80 private Map<Command, String> getEnabledCommands(Map<String, String> availableViewers) {
81 Map<Command, String> enabledCommands = new HashMap<Command, String>();
82 for(Entry<String, String> entry:availableViewers.entrySet()){
83 final String commandId = entry.getKey();
84 ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
85 Command command = commandService.getCommand(commandId);
86 if(command.isEnabled()){
87 enabledCommands.put(command, entry.getValue());
88 }
89 }
90 return enabledCommands;
91 }
92
93 private final class CommandInvoker extends SelectionAdapter {
94 private final Command command;
95 private final Object selectedObject;
96
97 private CommandInvoker(Command command, Object selectedObject) {
98 this.command = command;
99 this.selectedObject = selectedObject;
100 }
101
102 @Override
103 public void widgetSelected(SelectionEvent e) {
104 IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
105 Map<String, UUID> params = new HashMap<String, UUID>();
106 if(selectedObject instanceof ICdmBase){
107 params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid());
108 }
109 ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
110 try {
111 if(parameterizedCommand!=null){
112 handlerService.executeCommand(parameterizedCommand, null);
113 }
114 else{
115 handlerService.executeCommand(command.getId(), null);
116 }
117 } catch (NotDefinedException nde) {
118 throw new RuntimeException("Could not find open command: " + command.getId());
119 } catch (Exception exception) {
120 MessagingUtils.error(getClass(), "An exception occured while trying execute "+command.getId(), exception);
121 }
122 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
123 }
124 }
125
126 }