Merge branch 'hotfix/3.12.2' into develop
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerContextMenu.java
index f2eef9fe3e63113dab460be3d7cf8bb122b58d87..db59d464d01ba40ddfd6c14b3a10fdb4a4b783d1 100644 (file)
@@ -1,9 +1,12 @@
 package eu.etaxonomy.taxeditor.view;
 
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.UUID;
 
 import org.eclipse.core.commands.Command;
+import org.eclipse.core.commands.ParameterizedCommand;
 import org.eclipse.core.commands.common.NotDefinedException;
 import org.eclipse.jface.action.ContributionItem;
 import org.eclipse.jface.action.IContributionItem;
@@ -20,6 +23,7 @@ import org.eclipse.ui.actions.CompoundContributionItem;
 import org.eclipse.ui.commands.ICommandService;
 import org.eclipse.ui.handlers.IHandlerService;
 
+import eu.etaxonomy.cdm.model.common.ICdmBase;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 
 /**
@@ -36,64 +40,87 @@ public class CdmViewerContextMenu extends CompoundContributionItem {
                     public void fill(Menu menu, int index) {
                         final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                         final ISelection selection = window.getActivePage().getSelection();
-
-                        MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
-                        addItem.setText("Open in...");
-                        Menu addMenu = new Menu(menu);
-                        addItem.setMenu(addMenu);
                         if(selection instanceof IStructuredSelection){
                             Object firstElement = ((IStructuredSelection) selection).getFirstElement();
                             Map<String, String> availableViewers = CdmViewerUtil.getAvailableViewers(firstElement);
-                            for(Entry<String, String> entry:availableViewers.entrySet()){
-                                final String commandId = entry.getKey();
+                            Map<Command, String> enabledCommands = getEnabledCommands(availableViewers);
+
+                            //check if only one or multiple viewers/commands are available
+                            if(enabledCommands.size()==1){
+                                Entry<Command, String> entry = enabledCommands.entrySet().iterator().next();
+                                final Command command = entry.getKey();
                                 String viewerName = entry.getValue();
-                                MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
-                                menuItem.setText(viewerName);
-                                menuItem.addSelectionListener(new SelectionAdapter() {
 
-                                    @Override
-                                    public void widgetSelected(SelectionEvent e) {
-                                        ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
+                                MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
+                                addItem.setText(String.format("Open (%s)", viewerName));
+                                addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
+                            }
+                            else if(enabledCommands.size()>1){
+                                MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
+                                addItem.setText("Open in...");
+                                Menu addMenu = new Menu(menu);
+                                addItem.setMenu(addMenu);
+                                for(Entry<Command, String> entry:enabledCommands.entrySet()){
+                                    final Command command = entry.getKey();
+                                    String viewerName = entry.getValue();
 
-                                        Command command = commandService.getCommand(commandId);
-                                        if(command.isEnabled()) {
-                                            IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
-                                            try {
-                                                handlerService.executeCommand(commandId, null);
-                                            } catch (NotDefinedException nde) {
-                                                throw new RuntimeException("Could not find open command: " + commandId);
-                                            } catch (Exception exception) {
-                                                MessagingUtils.error(getClass(), "An exception occured while trying execute "+commandId, exception);
-                                            }
-                                        }
-                                    }
-                                }) ;
+                                    MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
+                                    menuItem.setText(viewerName);
+                                    menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
+                                }
                             }
                         }
                     }
+
                 }
         };
         return contributionItems;
     }
 
-    private class OpenInViewerListener extends SelectionAdapter {
+    private Map<Command, String> getEnabledCommands(Map<String, String> availableViewers) {
+        Map<Command, String> enabledCommands = new HashMap<Command, String>();
+        for(Entry<String, String> entry:availableViewers.entrySet()){
+            final String commandId = entry.getKey();
+            ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
+            Command command = commandService.getCommand(commandId);
+            if(command.isEnabled()){
+                enabledCommands.put(command, entry.getValue());
+            }
+        }
+        return enabledCommands;
+    }
 
-        private final ICdmViewer cdmViewer;
-        private final Object input;
-        private final Class<?> viewerClass;
+    private final class CommandInvoker extends SelectionAdapter {
+        private final Command command;
+        private final Object selectedObject;
 
-        public OpenInViewerListener(ICdmViewer cdmViewer, Object input, Class<?> viewerClass) {
-            super();
-            this.cdmViewer = cdmViewer;
-            this.input = input;
-            this.viewerClass = viewerClass;
+        private CommandInvoker(Command command, Object selectedObject) {
+            this.command = command;
+            this.selectedObject = selectedObject;
         }
 
         @Override
         public void widgetSelected(SelectionEvent e) {
-            cdmViewer.show(input, viewerClass);
+            IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
+            Map<String, UUID> params = new HashMap<String, UUID>();
+            if(selectedObject instanceof ICdmBase){
+                params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid());
+            }
+            ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
+            try {
+                if(parameterizedCommand!=null){
+                    handlerService.executeCommand(parameterizedCommand, null);
+                }
+                else{
+                    handlerService.executeCommand(command.getId(), null);
+                }
+            } catch (NotDefinedException nde) {
+                throw new RuntimeException("Could not find open command: " + command.getId());
+            } catch (Exception exception) {
+                MessagingUtils.error(getClass(), "An exception occured while trying execute "+command.getId(), exception);
+            }
+            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
         }
-
     }
 
 }