Filter out disabled commands
authorPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 15 Dec 2015 07:16:00 +0000 (08:16 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 15 Dec 2015 07:16:00 +0000 (08:16 +0100)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerContextMenu.java

index baae7e71c5947abb9dbf11d4df5504042e00c454..59082ad86eb3fc3ffd822d0dbc0c24333f69b9c4 100644 (file)
@@ -43,67 +43,78 @@ public class CdmViewerContextMenu extends CompoundContributionItem {
                         if(selection instanceof IStructuredSelection){
                             Object firstElement = ((IStructuredSelection) selection).getFirstElement();
                             Map<String, String> availableViewers = CdmViewerUtil.getAvailableViewers(firstElement);
-                            //if only one viewer/command is available then show only this one
-                            if(availableViewers.size()==1){
-                                //else show a list of possible viewers/commands
-                                Entry<String, String> entry = availableViewers.entrySet().iterator().next();
-                                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 addItem = new MenuItem(menu, SWT.CASCADE);
                                 addItem.setText(String.format("Open (%s)", viewerName));
-                                addItem.addSelectionListener(new CommandInvoker(commandId, firstElement)) ;
+                                addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
                             }
-                            else if (availableViewers.size()>1){
-                                //else show a list of possible viewers/commands
+                            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<String, String> entry:availableViewers.entrySet()){
-                                    final String commandId = entry.getKey();
+                                for(Entry<Command, String> entry:enabledCommands.entrySet()){
+                                    final Command command = entry.getKey();
                                     String viewerName = entry.getValue();
+
                                     MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
                                     menuItem.setText(viewerName);
-                                    menuItem.addSelectionListener(new CommandInvoker(commandId, firstElement)) ;
+                                    menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
                                 }
                             }
                         }
                     }
+
                 }
         };
         return contributionItems;
     }
 
+    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 class CommandInvoker extends SelectionAdapter {
-        private final String commandId;
+        private final Command command;
         private final Object selectedObject;
 
-        private CommandInvoker(String commandId, Object selectedObject) {
-            this.commandId = commandId;
+        private CommandInvoker(Command command, Object selectedObject) {
+            this.command = command;
             this.selectedObject = selectedObject;
         }
 
         @Override
         public void widgetSelected(SelectionEvent e) {
-            ICommandService commandService = (ICommandService)PlatformUI.getWorkbench().getService(ICommandService.class);
-
-            Command command = commandService.getCommand(commandId);
-            if(command.isEnabled()) {
-                IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
-                Map<String, UUID> params = new HashMap<String, UUID>();
-                if(selectedObject instanceof ICdmBase){
-                    params.put(commandId+".uuid", ((ICdmBase) selectedObject).getUuid());
-                }
-                ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
-                try {
-                    handlerService.executeCommand(parameterizedCommand, 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);
-                }
-                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
+            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 {
+                handlerService.executeCommand(parameterizedCommand, 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();
         }
     }