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