bfe10e7cd4b71689bd8a9f8c55d66e599dbf7c25
[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.handlers.IHandlerService;
24
25 import eu.etaxonomy.cdm.model.common.ICdmBase;
26 import eu.etaxonomy.taxeditor.Messages;
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<Command, String> enabledCommands = CdmViewerUtil.getAvailableViewers(firstElement);
46
47 //check if only one or multiple viewers/commands are available
48 if(enabledCommands.size()==1){
49 Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
50 final Command command = entry.getKey();
51 String viewerName = entry.getValue();
52
53 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
54 addItem.setText(String.format(Messages.CdmViewerContextMenu_OPEN, viewerName));
55 addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
56 }
57 else if(enabledCommands.size()>1){
58 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
59 addItem.setText(Messages.CdmViewerContextMenu_OPEN_IN);
60 Menu addMenu = new Menu(menu);
61 addItem.setMenu(addMenu);
62 for(Entry<Command, String> entry:enabledCommands.entrySet()){
63 final Command command = entry.getKey();
64 String viewerName = entry.getValue();
65
66 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
67 menuItem.setText(viewerName);
68 menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
69 }
70 }
71 }
72 }
73
74 }
75 };
76 return contributionItems;
77 }
78
79 private final class CommandInvoker extends SelectionAdapter {
80 private final Command command;
81 private final Object selectedObject;
82
83 private CommandInvoker(Command command, Object selectedObject) {
84 this.command = command;
85 this.selectedObject = selectedObject;
86 }
87
88 @Override
89 public void widgetSelected(SelectionEvent e) {
90 IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
91 Map<String, UUID> params = new HashMap<String, UUID>();
92 if(selectedObject instanceof ICdmBase){
93 params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid()); //$NON-NLS-1$
94 }
95 ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
96 try {
97 if(parameterizedCommand!=null){
98 handlerService.executeCommand(parameterizedCommand, null);
99 }
100 else{
101 handlerService.executeCommand(command.getId(), null);
102 }
103 } catch (NotDefinedException nde) {
104 throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
105 } catch (Exception exception) {
106 MessagingUtils.error(getClass(), "An exception occured while trying execute "+command.getId(), exception); //$NON-NLS-1$
107 }
108 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
109 }
110 }
111
112 }