ref #8890: handle dynamic menu for ref objects view and taxonnode wizard correctly
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / handler / defaultHandler / OpenReferencingObjectsViewHandler.java
index 7c88b71977bf7c9480e8e52da65fbfceb0a39074..0371f411d48f99545f1926ea04e4917399306e71 100644 (file)
@@ -1,5 +1,7 @@
 package eu.etaxonomy.taxeditor.handler.defaultHandler;
 
+import java.util.UUID;
+
 import javax.inject.Named;
 
 import org.eclipse.e4.core.di.annotations.CanExecute;
@@ -9,15 +11,40 @@ import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.e4.ui.workbench.modeling.EPartService;
 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
+import org.eclipse.jface.viewers.IStructuredSelection;
 
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
 import eu.etaxonomy.taxeditor.bulkeditor.AppModelId;
+import eu.etaxonomy.taxeditor.editor.IReferencingObjectsView;
 
 public class OpenReferencingObjectsViewHandler {
 
+    Object selectedObject;
     @Execute
     public void execute(EPartService partService) {
         MPart part = partService.createPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_BULKEDITOR_REFERENCINGOBJECTS_E4_REFERENCINGOBJECTSVIEWE4);
         part = partService.showPart(part, PartState.ACTIVATE);
+        IReferencingObjectsView view = (IReferencingObjectsView)part.getObject();
+        UUID uuid = null;
+        Class clazz = null;
+
+        if (selectedObject instanceof IStructuredSelection){
+            selectedObject = ((IStructuredSelection)selectedObject).getFirstElement();
+        }
+        if (selectedObject instanceof CdmBase){
+            uuid = ((CdmBase)selectedObject).getUuid();
+            clazz = selectedObject.getClass();
+        } else if (selectedObject instanceof UuidAndTitleCache){
+            uuid = ((UuidAndTitleCache)selectedObject).getUuid();
+            clazz = ((UuidAndTitleCache)selectedObject).getType();
+        }
+        if (uuid != null && clazz != null){
+            view.updateReferencingObjects(uuid, clazz);
+        }
+
+
+
     }
 
     @CanExecute
@@ -27,6 +54,15 @@ public class OpenReferencingObjectsViewHandler {
         boolean canExecute = false;
         MPart part = partService.findPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_BULKEDITOR_REFERENCINGOBJECTS_E4_REFERENCINGOBJECTSVIEWE4);
         canExecute = !part.equals(activePart);
+
+
+        if (canExecute){
+            String commandId = menuItem.getCommand().getElementId();
+            selectedObject = menuItem.getTransientData().get(commandId+".uuid");
+            if (selectedObject instanceof IStructuredSelection){
+                canExecute = ((IStructuredSelection)selectedObject).size() == 1;
+            }
+        }
         menuItem.setVisible(canExecute);
         return canExecute;
     }