Refactor generic "open in..." framework
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Jan 2016 18:13:27 +0000 (19:13 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 13 Jan 2016 18:13:27 +0000 (19:13 +0100)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerChooser.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerContextMenu.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/CdmViewerUtil.java

index 939f753b698d3874cd784a9a863c1d43c886c09b..09f3784042fbbc3cd5f4476c1f55c12aefc5968f 100644 (file)
@@ -48,7 +48,7 @@ import eu.etaxonomy.taxeditor.model.MessagingUtils;
  */
 public class CdmViewerChooser extends PopupDialog implements ISelectionChangedListener, ILabelProvider{
 
-    private Map<String, String> nameViewerMap;
+    private Map<Command, String> nameViewerMap;
     private Object input;
 
     public CdmViewerChooser(Shell parentShell) {
@@ -73,8 +73,8 @@ public class CdmViewerChooser extends PopupDialog implements ISelectionChangedLi
 
         //if only one editor is available then open it
         if(nameViewerMap.size()==1){
-            String commandId = nameViewerMap.keySet().iterator().next();
-            executeCommand(commandId, input);
+            Command command = nameViewerMap.keySet().iterator().next();
+            executeCommand(command.getId(), input);
         }
         else{
             if(nameViewerMap.isEmpty()){
index 2811fe14a38fc719795857cae83e4b0b24fe4f7f..bfe10e7cd4b71689bd8a9f8c55d66e599dbf7c25 100644 (file)
@@ -20,7 +20,6 @@ import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.PlatformUI;
 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;
@@ -43,8 +42,7 @@ public class CdmViewerContextMenu extends CompoundContributionItem {
                         final ISelection selection = window.getActivePage().getSelection();
                         if(selection instanceof IStructuredSelection){
                             Object firstElement = ((IStructuredSelection) selection).getFirstElement();
-                            Map<String, String> availableViewers = CdmViewerUtil.getAvailableViewers(firstElement);
-                            Map<Command, String> enabledCommands = getEnabledCommands(availableViewers);
+                            Map<Command, String> enabledCommands = CdmViewerUtil.getAvailableViewers(firstElement);
 
                             //check if only one or multiple viewers/commands are available
                             if(enabledCommands.size()==1){
@@ -78,19 +76,6 @@ public class CdmViewerContextMenu extends CompoundContributionItem {
         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 Command command;
         private final Object selectedObject;
index 4436ced693e23b3b1f6cb5ae5157418aedcb20be..8113b9703dca8d066640d50ec9957dc895d3b5a8 100644 (file)
@@ -38,8 +38,8 @@ public class CdmViewerUtil {
      * @return a key-value map of available commands and their string
      *         representation
      */
-    public static  Map<String, String> getAvailableViewers(Object input){
-        Map<String, String> commandViewerNameMap = new HashMap<String, String>();
+    public static  Map<Command, String> getAvailableViewers(Object input){
+        Map<Command, String> commandViewerNameMap = new HashMap<Command, String>();
 
         if(input!=null){
             IExtensionRegistry reg = Platform.getExtensionRegistry();
@@ -56,7 +56,7 @@ public class CdmViewerUtil {
                             Command command = commandService.getCommand(commandId);
                             //TODO: maybe pass the command directly instead of just the command id
                             if(command.isEnabled()){
-                                commandViewerNameMap.put(commandId, viewerName);
+                                commandViewerNameMap.put(command, viewerName);
                             }
                         }
                     } catch (ClassNotFoundException e) {