Merge branch 'develop' into featureTreeEditor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / CdmViewerContextMenu.java
index 59082ad86eb3fc3ffd822d0dbc0c24333f69b9c4..09edf27acceb7a3cba2e134461b5419bc6c398a4 100644 (file)
@@ -12,19 +12,24 @@ import org.eclipse.jface.action.ContributionItem;
 import org.eclipse.jface.action.IContributionItem;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.TreeNode;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.MenuItem;
+import org.eclipse.ui.IWorkbenchPage;
 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.CdmBase;
 import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
+import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
  * Generic context menu for opening elements in the taxeditor.
@@ -39,36 +44,38 @@ public class CdmViewerContextMenu extends CompoundContributionItem {
                     @Override
                     public void fill(Menu menu, int index) {
                         final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
-                        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);
+                        IWorkbenchPage activePage = window.getActivePage();
+                        if(activePage!=null){
+                               final ISelection selection = activePage.getSelection();
+                               if(selection instanceof IStructuredSelection){
+                                       Object firstElement = ((IStructuredSelection) selection).getFirstElement();
+                                       Map<Command, String> enabledCommands = CdmViewerUtil.getAvailableViewers(firstElement);
 
-                            //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();
+                                       //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(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();
+                                               MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
+                                               addItem.setText(String.format(Messages.CdmViewerContextMenu_OPEN, viewerName));
+                                               addItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
+                                       }
+                                       else if(enabledCommands.size()>1){
+                                               MenuItem addItem = new MenuItem(menu, SWT.CASCADE);
+                                               addItem.setText(Messages.CdmViewerContextMenu_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();
 
-                                    MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
-                                    menuItem.setText(viewerName);
-                                    menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
-                                }
-                            }
+                                                       MenuItem menuItem = new MenuItem(addMenu, SWT.NONE);
+                                                       menuItem.setText(viewerName);
+                                                       menuItem.addSelectionListener(new CommandInvoker(command, firstElement)) ;
+                                               }
+                                       }
+                               }
                         }
                     }
 
@@ -77,22 +84,9 @@ 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;
+        private Object selectedObject;
 
         private CommandInvoker(Command command, Object selectedObject) {
             this.command = command;
@@ -103,18 +97,30 @@ public class CdmViewerContextMenu extends CompoundContributionItem {
         public void widgetSelected(SelectionEvent e) {
             IHandlerService handlerService = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);
             Map<String, UUID> params = new HashMap<String, UUID>();
+            //for generic UuidAndTitleCache objects try to load the object
+            if (selectedObject instanceof UuidAndTitleCache){
+                selectedObject = CdmStore.getCommonService().find(CdmBase.class, ((UuidAndTitleCache)selectedObject).getUuid());
+            }
+            //for tree nodes get the value resp. the object of the node
+            else if (selectedObject instanceof TreeNode){
+                selectedObject = ((TreeNode) selectedObject).getValue();
+            }
             if(selectedObject instanceof ICdmBase){
-                params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid());
+                params.put(command.getId()+".uuid", ((ICdmBase) selectedObject).getUuid()); //$NON-NLS-1$
             }
             ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, params);
             try {
-                handlerService.executeCommand(parameterizedCommand, null);
+                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());
+                throw new RuntimeException("Could not find open command: " + command.getId()); //$NON-NLS-1$
             } catch (Exception exception) {
-                MessagingUtils.error(getClass(), "An exception occured while trying execute "+command.getId(), exception);
+                MessagingUtils.error(getClass(), "An exception occured while trying to execute "+command.getId(), exception); //$NON-NLS-1$
             }
-            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getSelection();
         }
     }