Merge branch 'hotfix/4.8.2'
[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.jface.viewers.TreeNode;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.SelectionAdapter;
18 import org.eclipse.swt.events.SelectionEvent;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.swt.widgets.MenuItem;
21 import org.eclipse.ui.IWorkbenchPage;
22 import org.eclipse.ui.IWorkbenchWindow;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.actions.CompoundContributionItem;
25 import org.eclipse.ui.handlers.IHandlerService;
26
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.ICdmBase;
29 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
30 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
31 import eu.etaxonomy.taxeditor.l10n.Messages;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * Generic context menu for opening elements in the taxeditor.
37 *
38 */
39 public class CdmViewerContextMenu extends CompoundContributionItem {
40
41 @Override
42 protected IContributionItem[] getContributionItems() {
43 IContributionItem[] contributionItems = new IContributionItem[] {
44 new ContributionItem() {
45 @Override
46 public void fill(Menu menu, int index) {
47 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
48 IWorkbenchPage activePage = window.getActivePage();
49 if(activePage!=null){
50 final ISelection selection = activePage.getSelection();
51 if(selection instanceof IStructuredSelection){
52 Object firstElement = ((IStructuredSelection) selection).getFirstElement();
53 if (firstElement instanceof TaxonNode && !((TaxonNode)firstElement).hasTaxon()){
54 firstElement = ((TaxonNode)firstElement).getClassification();
55 }
56 Map<Command, String> enabledCommands = CdmViewerUtil.getAvailableViewers(firstElement);
57
58 //check if only one or multiple viewers/commands are available
59 if(enabledCommands.size()==1){
60 Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
61 final Command command = entry.getKey();
62 String viewerName = entry.getValue();
63
64 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
65 addItem.setText(String.format(Messages.CdmViewerContextMenu_OPEN, viewerName));
66 addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
67 }
68 else if(enabledCommands.size()>1){
69 MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
70 addItem.setText(Messages.CdmViewerContextMenu_OPEN_IN);
71 Menu addMenu = new Menu(menu);
72 addItem.setMenu(addMenu);
73 for(Entry<Command, String> entry:enabledCommands.entrySet()){
74 final Command command = entry.getKey();
75 String viewerName = entry.getValue();
76
77 MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
78 menuItem.setText(viewerName);
79 menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
80 }
81 }
82 }
83 }
84 }
85
86 }
87 };
88 return contributionItems;
89 }
90
91 private final class CommandInvoker extends SelectionAdapter {
92 private final Command command;
93 private Object selectedObject;
94
95 private CommandInvoker(Command command, Object selectedObject) {
96 this.command = command;
97 this.selectedObject = selectedObject;
98 }
99
100 @Override
101 public void widgetSelected(SelectionEvent e) {
102 IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
103 Map<String, UUID> params = new HashMap<String, UUID>();
104 //for generic UuidAndTitleCache objects try to load the object
105 if (selectedObject instanceof UuidAndTitleCache){
106 selectedObject = CdmStore.getCommonService().find(CdmBase.class, ((UuidAndTitleCache)selectedObject).getUuid());
107 }
108 //for tree nodes get the value resp. the object of the node
109 else if (selectedObject instanceof TreeNode){
110 selectedObject = ((TreeNode) selectedObject).getValue();
111 }
112 if(selectedObject instanceof ICdmBase){
113 params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid()); //$NON-NLS-1$
114 }
115 ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
116 try {
117 if(parameterizedCommand!=null){
118 handlerService.executeCommand(parameterizedCommand, null);
119 }
120 else{
121 handlerService.executeCommand(command.getId(), null);
122 }
123 } catch (NotDefinedException nde) {
124 throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
125 } catch (Exception exception) {
126 MessagingUtils.error(getClass(), "An exception occured while trying to execute "+command.getId(), exception); //$NON-NLS-1$
127 }
128 }
129 }
130
131 }