commit aa42dfd92b08993bd5313a5e0e72bbe13a4aaa9c
Author: Patrick Plitzner <p.plitzner@bgbm.org>
Date:   Tue Jun 5 18:14:11 2018 +0200

    ref #7010 Adapt derivate view handlers for multiple selection

diff --git a/eu.etaxonomy.taxeditor.editor/fragment.e4xmi b/eu.etaxonomy.taxeditor.editor/fragment.e4xmi
index f9e7ccddb..b0aa42ba6 100644
--- a/eu.etaxonomy.taxeditor.editor/fragment.e4xmi
+++ b/eu.etaxonomy.taxeditor.editor/fragment.e4xmi
@@ -51,7 +51,6 @@
         <children xsi:type="menu:MenuSeparator" xmi:id="_9nI4IDumEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.4"/>
         <children xsi:type="menu:HandledMenuItem" xmi:id="_h4pX0DumEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandnamereuse_single_read" label="%command.label.REUSE_SINGLE_READ_HERE" command="_HmerkDumEeeOtqC_3qh40A"/>
         <children xsi:type="menu:HandledMenuItem" xmi:id="_oQXH0DumEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabelreuse_single_read_for_other_sequence" label="%command.label.REUSE_SINGLE_READ_FOR_OTHER_SEQUENCE" command="_N0wLsDumEeeOtqC_3qh40A"/>
-        <children xsi:type="menu:HandledMenuItem" xmi:id="_1apX0DumEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabelremove_single_read_from_this_sequence" label="%command.label.REMOVE_SINGLE_READ_FROM_THIS_SEQUENCE"/>
         <children xsi:type="menu:MenuSeparator" xmi:id="_c0f98DulEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.menuseparator.beforeDelete"/>
         <children xsi:type="menu:HandledMenuItem" xmi:id="_XWW6QDulEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel52" label="%command.label.52" iconURI="platform:/plugin/eu.etaxonomy.taxeditor.store/icons/delete.gif" command="_YPp1wDvREeeOtqC_3qh40A"/>
         <children xsi:type="menu:HandledMenuItem" xmi:id="_wg0-oDulEeeOtqC_3qh40A" elementId="eu.etaxonomy.taxeditor.editor.handledmenuitem.commandlabel54" label="%command.label.54" iconURI="platform:/plugin/eu.etaxonomy.taxeditor.editor/icons/deep-delete-16x16-32.png" command="_fKT-wDulEeeOtqC_3qh40A"/>
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateView.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateView.java
index 041a6f1cc..11dec7255 100644
--- a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateView.java
+++ b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/DerivateView.java
@@ -30,7 +30,6 @@ import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
 import org.eclipse.jface.util.LocalSelectionTransfer;
 import org.eclipse.jface.viewers.AbstractTreeViewer;
-import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.StructuredSelection;
@@ -61,7 +60,6 @@ import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
 import eu.etaxonomy.taxeditor.editor.view.derivate.searchFilter.DerivateSearchCompositeController;
-import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.IContextListener;
 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
@@ -224,7 +222,7 @@ public class DerivateView implements IPartContentHasFactualData, IConversationEn
         viewer.getTree().setEnabled(CdmStore.isActive());
 
         //propagate selection
-        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
+        selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
         viewer.addSelectionChangedListener(selectionChangedListener);
 
         //create context menu
@@ -511,7 +509,7 @@ public class DerivateView implements IPartContentHasFactualData, IConversationEn
 
     @Inject
     @Optional
-    public void selectionChanged(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) ISelection selection,
+    public void selectionChanged(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
             @Named(IServiceConstants.ACTIVE_PART) MPart activePart, MPart thisPart)
     {
     	if(activePart == thisPart || viewer==null){
@@ -525,8 +523,8 @@ public class DerivateView implements IPartContentHasFactualData, IConversationEn
             if(activePart instanceof TaxonNameEditorE4){
                 selectedTaxon = ((TaxonNameEditorE4) activePart).getTaxon();
             }
-            else if(selection instanceof IStructuredSelection){
-                Object selectedElement = ((IStructuredSelection) selection).getFirstElement();
+            else if(selection != null){
+                Object selectedElement = selection.getFirstElement();
                 if(selectedElement instanceof CdmBase){
                     if(((CdmBase) selectedElement).isInstanceOf(TaxonNode.class)){
                         selectedTaxon = HibernateProxyHelper.deproxy(selectedElement, TaxonNode.class).getTaxon();
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/AbstractAddDerivativeHandler.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/AbstractAddDerivativeHandler.java
index ea9ad3c9e..5e09e1da3 100644
--- a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/AbstractAddDerivativeHandler.java
+++ b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/AbstractAddDerivativeHandler.java
@@ -8,6 +8,7 @@ import org.eclipse.e4.core.di.annotations.Execute;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
@@ -23,12 +24,12 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
 public abstract class AbstractAddDerivativeHandler {
 
 	@Execute
-	public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
+	public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
 	    DerivateView derivateView = (DerivateView) part.getObject();
 
 	    checkDirtyState(derivateView);
 
-	    Object selectedDerivative = node.getValue();
+	    Object selectedDerivative = ((TreeNode) selection.getFirstElement()).getValue();
 	    if(isAllowed(selectedDerivative)){
 	        Object createdElement = addElement(selectedDerivative);
 	        refreshView(derivateView, createdElement);
@@ -36,11 +37,12 @@ public abstract class AbstractAddDerivativeHandler {
 	}
 
     @CanExecute
-    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node, MHandledMenuItem item) {
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem item) {
         boolean canExecute = false;
-
-        Object derivate = node.getValue();
-        canExecute = isAllowed(derivate);
+        if(selection.size()==1){
+            canExecute = selection.getFirstElement() instanceof TreeNode
+                    && isAllowed(((TreeNode) selection.getFirstElement()).getValue());
+        }
         item.setVisible(canExecute);
         return canExecute;
     }
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java
index c012821cd..e53b66afd 100644
--- a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java
+++ b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java
@@ -9,6 +9,8 @@
 
 package eu.etaxonomy.taxeditor.editor.view.derivate.handler;
 
+import java.util.Iterator;
+
 import javax.inject.Named;
 
 import org.eclipse.core.commands.ParameterizedCommand;
@@ -20,6 +22,7 @@ import org.eclipse.e4.core.di.annotations.Optional;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 
 import eu.etaxonomy.cdm.api.service.DeleteResult;
@@ -53,7 +56,9 @@ public class DeleteDerivateHandler {
     private SpecimenDeleteConfigurator deleteConfigurator;
 
     @Execute
-    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) Object selection, ParameterizedCommand command) {
+    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part,
+            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
+            ParameterizedCommand command) {
         deleteConfigurator = new SpecimenDeleteConfigurator();
         if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){
             deleteConfigurator.setDeleteChildren(true);
@@ -74,22 +79,18 @@ public class DeleteDerivateHandler {
             return;
         }
 
-        if(selection instanceof Object[]){
-            for (Object o : (Object[])selection) {
-                if(o instanceof TreeNode){
-                    delete((TreeNode)o, derivateView);
-                }
+        for(Iterator iter = selection.iterator();iter.hasNext();){
+            Object object = iter.next();
+            if(object instanceof TreeNode){
+                delete((TreeNode)object, derivateView);
             }
         }
-        if(selection instanceof TreeNode){
-            delete((TreeNode)selection, derivateView);
-        }
 
     }
 
     private void delete(TreeNode treeNode, DerivateView derivateView){
         Object value = treeNode.getValue();
-        IStatus allowStatus = allowOperations(derivateView, treeNode);
+        IStatus allowStatus = allowOperations(treeNode);
         if(allowStatus.isOK()) {
             DeleteResult deleteResult;
             if(value instanceof SingleRead
@@ -118,7 +119,7 @@ public class DeleteDerivateHandler {
 
     }
 
-    public IStatus allowOperations(DerivateView derivateView, TreeNode treeNode) {
+    public IStatus allowOperations(TreeNode treeNode) {
         DeleteResult deleteResult;
         Object value = treeNode.getValue();
         if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
@@ -144,10 +145,10 @@ public class DeleteDerivateHandler {
     }
 
     @CanExecute
-    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) Object selection,
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
             MHandledMenuItem menuItem){
         boolean canExecute = false;
-        canExecute = selection!=null;
+        canExecute = selection!=null && !selection.isEmpty();
         menuItem.setVisible(canExecute);
         return canExecute;
     }
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadCopyToClipboardHandler.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadCopyToClipboardHandler.java
index 595270938..4a0fdfab8 100644
--- a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadCopyToClipboardHandler.java
+++ b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadCopyToClipboardHandler.java
@@ -9,7 +9,7 @@ import org.eclipse.e4.ui.model.application.ui.basic.MPart;
 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.util.LocalSelectionTransfer;
-import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 
 import eu.etaxonomy.cdm.model.molecular.SingleRead;
@@ -18,20 +18,21 @@ import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
 public class SingleReadCopyToClipboardHandler {
 
     @Execute
-    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode){
+    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection){
         DerivateView derivateView = (DerivateView)part.getObject();
-        LocalSelectionTransfer.getTransfer().setSelection(new StructuredSelection(selectedTreeNode));
+        LocalSelectionTransfer.getTransfer().setSelection(selection);
         derivateView.updateLabelCache();
         derivateView.refreshTree();
     }
 
     @CanExecute
-    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
+            MHandledMenuItem menuItem){
         boolean canExecute = false;
-        if(selectedTreeNode!=null){
-            Object object = selectedTreeNode.getValue();
-            canExecute = (object instanceof SingleRead);
-        }
+        canExecute = selection!=null
+                && selection.size()==1
+                && selection.getFirstElement() instanceof TreeNode
+                && ((TreeNode) selection.getFirstElement()).getValue() instanceof SingleRead;
         menuItem.setVisible(canExecute);
         return canExecute;
     }
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadReuseHandler.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadReuseHandler.java
index ea7bea771..13cc2b409 100644
--- a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadReuseHandler.java
+++ b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/SingleReadReuseHandler.java
@@ -10,6 +10,7 @@ import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.util.LocalSelectionTransfer;
 import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.TreeNode;
 
 import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
@@ -23,7 +24,8 @@ import eu.etaxonomy.taxeditor.store.CdmStore;
 public class SingleReadReuseHandler {
 
     @Execute
-    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode){
+    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart part,
+            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection){
         ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
         TreeNode clipBoardTreeNode = EditorUtil.getTreeNodeOfSelection(clipBoardSelection);
         DerivateView derivateView = (DerivateView)part.getObject();
@@ -31,10 +33,9 @@ public class SingleReadReuseHandler {
             MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION);
             return;
         }
-        if(clipBoardTreeNode!=null && clipBoardTreeNode.getValue() instanceof SingleRead
-                && selectedTreeNode!=null && selectedTreeNode.getValue() instanceof Sequence){
+        if(clipBoardTreeNode!=null && clipBoardTreeNode.getValue() instanceof SingleRead){
             SingleRead singleRead = (SingleRead)clipBoardTreeNode.getValue();
-            Sequence sequence = (Sequence)selectedTreeNode.getValue();
+            Sequence sequence = (Sequence)((TreeNode) selection.getFirstElement()).getValue();
             sequence.addSingleRead(singleRead);
             CdmStore.getService(ISequenceService.class).merge(sequence);
 
@@ -47,16 +48,18 @@ public class SingleReadReuseHandler {
     }
 
     @CanExecute
-    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode selectedTreeNode, MHandledMenuItem menuItem) {
+    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
+            MHandledMenuItem menuItem){
         boolean canExecute = false;
-        if(selectedTreeNode!=null){
-            Object value = selectedTreeNode.getValue();
-            TreeNode clipboardNode = EditorUtil.getTreeNodeOfSelection(LocalSelectionTransfer.getTransfer().getSelection());
-            if(value instanceof Sequence && clipboardNode!=null && clipboardNode.getValue() instanceof SingleRead
-                    && !((Sequence) value).getSingleReads().contains(clipboardNode.getValue())){
-                canExecute = true;
-            }
-        }
+        canExecute = selection!=null
+                && selection.size()==1
+                && selection.getFirstElement() instanceof TreeNode
+                && ((TreeNode) selection.getFirstElement()).getValue() instanceof Sequence;
+        Sequence sequence = (Sequence) ((TreeNode) selection.getFirstElement()).getValue();
+        TreeNode clipboardNode = EditorUtil.getTreeNodeOfSelection(LocalSelectionTransfer.getTransfer().getSelection());
+        canExecute &= clipboardNode!=null
+                && clipboardNode.getValue() instanceof SingleRead
+                && !(sequence.getSingleReads().contains(clipboardNode.getValue()));
         menuItem.setVisible(canExecute);
         return canExecute;
     }
