ref #6906 Fix Delete and move handler for media view
authorPatrick Plitzner <p.plitzner@bgbm.org>
Fri, 3 Nov 2017 09:36:59 +0000 (10:36 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Fri, 3 Nov 2017 09:37:06 +0000 (10:37 +0100)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/media/handler/DeleteMediaHandler.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/media/handler/MoveImageDownInListHandler.java
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/media/handler/MoveImageUpInListHandler.java

index 013525623aa5c4e1c62560723633fd98f7f12bda..1cbec16db9129669917dd29255d87960a548637b 100644 (file)
@@ -6,9 +6,11 @@ package eu.etaxonomy.taxeditor.editor.view.media.handler;
 import javax.inject.Named;
 
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.e4.core.di.annotations.CanExecute;
 import org.eclipse.e4.core.di.annotations.Execute;
 import org.eclipse.e4.ui.di.UISynchronize;
 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.model.application.ui.menu.MMenuItem;
 import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.dialogs.MessageDialog;
@@ -16,9 +18,6 @@ import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.TreePath;
 import org.eclipse.jface.viewers.TreeSelection;
 import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.forms.editor.FormEditor;
 
 import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
@@ -26,16 +25,15 @@ import eu.etaxonomy.cdm.model.description.SpecimenDescription;
 import eu.etaxonomy.cdm.model.description.TaxonDescription;
 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
 import eu.etaxonomy.cdm.model.media.Media;
-import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
-import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
+import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
+import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
 import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
 import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
-import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
 
 /**
  *
@@ -47,98 +45,71 @@ public class DeleteMediaHandler  {
 
     @Execute
     public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
-            @Named(IServiceConstants.ACTIVE_SELECTION) Media media,
+            @Named(IServiceConstants.ACTIVE_SELECTION) Object object,
             @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
             MMenuItem menuItem,
             UISynchronize sync) {
 
         MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
 
-
-        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(mediaView.getSelectionProvidingPart());
-        if (e4WrappedPart instanceof FormEditor) {
-               IEditorPart editor = null;
-            editor = (FormEditor) e4WrappedPart;
-            IEditorInput input = editor.getEditorInput();
-            if (input instanceof CdmEntitySessionInput) {
-                ISelection selection = mediaView.getViewer().getSelection();
-                if (selection instanceof TreeSelection) {
-                    TreePath[] paths = ((TreeSelection) selection).getPaths();
-                    int count = paths[0].getSegmentCount();
-                    DescriptionBase description = null;
-                    for (int i = 0; i < count; i++ ) {
-                        if (paths[0].getSegment(i) instanceof DescriptionBase) {
-                            description = (DescriptionBase) paths[0].getSegment(i);
-                            break;
-                        }
-                    }
-                    // TODO use undo context specific to editor
-                    MediaDeletionConfigurator config = new MediaDeletionConfigurator();
-
-                    config.setDeleteFromDescription(true);
-                    config.setOnlyRemoveFromGallery(false);
-
-                    if (description instanceof SpecimenDescription){
-                       config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
-                    } else if (description instanceof TaxonDescription){
-                       config.setDeleteFrom(((TaxonDescription)description).getTaxon());
-                    }else if (description instanceof TaxonNameDescription){
-                       config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
-                    }
-
-
-                    DeleteConfiguratorDialog dialog;
-                    dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
-                    int result_dialog= dialog.open();
-                    if (result_dialog != IStatus.OK){
-                        return;
-                    }
-                    AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, media, config, mediaView);
-                    AbstractUtility.executeOperation(operation, sync);
-
+        ISelection selection = mediaView.getViewer().getSelection();
+        if (selection instanceof TreeSelection) {
+            TreePath[] paths = ((TreeSelection) selection).getPaths();
+            int count = paths[0].getSegmentCount();
+            DescriptionBase description = null;
+            for (int i = 0; i < count; i++ ) {
+                if (paths[0].getSegment(i) instanceof DescriptionBase) {
+                    description = (DescriptionBase) paths[0].getSegment(i);
+                    break;
+                }
+            }
+            // TODO use undo context specific to editor
+            MediaDeletionConfigurator config = new MediaDeletionConfigurator();
+
+            config.setDeleteFromDescription(true);
+            config.setOnlyRemoveFromGallery(false);
+
+            if (description instanceof SpecimenDescription){
+                config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
+            } else if (description instanceof TaxonDescription){
+                config.setDeleteFrom(((TaxonDescription)description).getTaxon());
+            }else if (description instanceof TaxonNameDescription){
+                config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
+            }
+            DeleteConfiguratorDialog dialog;
+            dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
+            int result_dialog= dialog.open();
+            if (result_dialog != IStatus.OK){
+                return;
+            }
+            if(object instanceof Media){
+                AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, (Media) object, config, mediaView);
+                AbstractUtility.executeOperation(operation, sync);
+            }
+            if(object instanceof DescriptionBase && ((DescriptionBase) object).isImageGallery()){
+                if(object instanceof TaxonDescription){
+                    DeleteTaxonDescriptionOperation deleteTaxonDescriptionOperation = new DeleteTaxonDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (TaxonDescription)object, mediaView, null);
+                    AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
+                }
+                else if(object instanceof SpecimenDescription){
+                    DeleteSpecimenDescriptionOperation deleteTaxonDescriptionOperation = new DeleteSpecimenDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (SpecimenDescription)object, mediaView, null);
+                    AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
                 }
             }
-        } else if (e4WrappedPart instanceof DerivateView){
-                DerivateView view = (DerivateView) e4WrappedPart;
-
-                 ISelection selection = mediaView.getViewer().getSelection();
-                 if (selection instanceof TreeSelection) {
-                     TreePath[] paths = ((TreeSelection) selection).getPaths();
-                     int count = paths[0].getSegmentCount();
-                     DescriptionBase description = null;
-                     for (int i = 0; i < count; i++ ) {
-                         if (paths[0].getSegment(i) instanceof DescriptionBase) {
-                             description = (DescriptionBase) paths[0].getSegment(i);
-                             break;
-                         }
-                     }
-                     // TODO use undo context specific to editor
-                     MediaDeletionConfigurator config = new MediaDeletionConfigurator();
-
-                     config.setDeleteFromDescription(true);
-                     config.setOnlyRemoveFromGallery(false);
-
-                     if (description instanceof SpecimenDescription){
-                       config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
-                     } else if (description instanceof TaxonDescription){
-                       config.setDeleteFrom(((TaxonDescription)description).getTaxon());
-                     }else if (description instanceof TaxonNameDescription){
-                       config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
-                     }
-
-
-                     DeleteConfiguratorDialog dialog;
-                     dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
-                     int result_dialog= dialog.open();
-                     if (result_dialog != IStatus.OK){
-                         return;
-                     }
-                     AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, media, config, mediaView);
-                     AbstractUtility.executeOperation(operation, sync);
 
-                 }
-             }
         }
+    }
+
+    @CanExecute
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) Object object,
+            MHandledMenuItem menuItem){
+        boolean canExecute = false;
+        canExecute = object instanceof Media
+                || (object instanceof DescriptionBase && ((DescriptionBase) object).isImageGallery());
+        menuItem.setVisible(canExecute);
+        return canExecute;
+    }
+
 
 
 }
\ No newline at end of file
index 95de13a65fe26540281b71255d6b1af94b41f993..f401ebf6859472b0f2fa4a68242c9a9a9d5f5b57 100644 (file)
@@ -17,15 +17,13 @@ import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.TreePath;
 import org.eclipse.jface.viewers.TreeSelection;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.forms.editor.FormEditor;
 
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.media.Media;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
-import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
+import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
+import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
 import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
 import eu.etaxonomy.taxeditor.editor.view.media.operation.MoveMediaInListOperation;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
@@ -48,33 +46,31 @@ public class MoveImageDownInListHandler  {
 
         MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
 
-        IEditorPart editor = null;
+        TaxonNameEditorE4 editor = null;
         Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(mediaView.getSelectionProvidingPart());
-        if (e4WrappedPart instanceof FormEditor) {
-            editor = (FormEditor) e4WrappedPart;
-            IEditorInput input = editor.getEditorInput();
-            if (input instanceof TaxonEditorInput) {
-                Taxon taxon = ((TaxonEditorInput) input).getTaxon();
+        if (e4WrappedPart instanceof TaxonNameEditorE4) {
+            editor = (TaxonNameEditorE4) e4WrappedPart;
+            TaxonEditorInputE4 input = editor.getEditorInput();
+            Taxon taxon = input.getTaxon();
 
-                ISelection selection = mediaView.getViewer().getSelection();
-                if (selection instanceof TreeSelection) {
-                    TreePath[] paths = ((TreeSelection) selection).getPaths();
-                    int count = paths[0].getSegmentCount();
-                    DescriptionBase description = null;
-                    for (int i = 0; i < count; i++ ) {
-                        if (paths[0].getSegment(i) instanceof DescriptionBase) {
-                            description = (DescriptionBase) paths[0].getSegment(i);
-                            break;
-                        }
+            ISelection selection = mediaView.getViewer().getSelection();
+            if (selection instanceof TreeSelection) {
+                TreePath[] paths = ((TreeSelection) selection).getPaths();
+                int count = paths[0].getSegmentCount();
+                DescriptionBase description = null;
+                for (int i = 0; i < count; i++ ) {
+                    if (paths[0].getSegment(i) instanceof DescriptionBase) {
+                        description = (DescriptionBase) paths[0].getSegment(i);
+                        break;
                     }
-                    AbstractPostOperation operation = null;
-                    // TODO use undo context specific to editor
-                    operation = new MoveMediaInListOperation(menuItem.getLocalizedLabel(),
-                            EditorUtil.getUndoContext(), taxon, description, media, MoveMediaInListOperation.DOWN, mediaView);
-                    AbstractUtility.executeOperation(operation, sync);
                 }
+                AbstractPostOperation operation = null;
+                // TODO use undo context specific to editor
+                operation = new MoveMediaInListOperation(menuItem.getLocalizedLabel(),
+                        EditorUtil.getUndoContext(), taxon, description, media, MoveMediaInListOperation.DOWN, mediaView);
+                AbstractUtility.executeOperation(operation, sync);
             }
         }
-       }
+    }
 
 }
index 2037ba4b17421ca34b9745069f7795b8ae81ae08..a46b6e097451e73a95c45004b9c0cb6fd36e9e0c 100644 (file)
@@ -17,15 +17,13 @@ import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.TreePath;
 import org.eclipse.jface.viewers.TreeSelection;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.forms.editor.FormEditor;
 
 import eu.etaxonomy.cdm.model.description.DescriptionBase;
 import eu.etaxonomy.cdm.model.media.Media;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
-import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
+import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
+import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
 import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
 import eu.etaxonomy.taxeditor.editor.view.media.operation.MoveMediaInListOperation;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
@@ -48,31 +46,29 @@ public class MoveImageUpInListHandler  {
 
         MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
 
-        IEditorPart editor = null;
+        TaxonNameEditorE4 editor = null;
         Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(mediaView.getSelectionProvidingPart());
-        if (e4WrappedPart instanceof FormEditor) {
-            editor = (FormEditor) e4WrappedPart;
-            IEditorInput input = editor.getEditorInput();
-            if (input instanceof TaxonEditorInput) {
-                Taxon taxon = ((TaxonEditorInput) input).getTaxon();
+        if (e4WrappedPart instanceof TaxonNameEditorE4) {
+            editor = (TaxonNameEditorE4) e4WrappedPart;
+            TaxonEditorInputE4 input = editor.getEditorInput();
+            Taxon taxon =input.getTaxon();
 
-                ISelection selection = mediaView.getViewer().getSelection();
-                if (selection instanceof TreeSelection) {
-                    TreePath[] paths = ((TreeSelection) selection).getPaths();
-                    int count = paths[0].getSegmentCount();
-                    DescriptionBase element = null;
-                    for (int i = 0; i < count; i++ ) {
-                        if (paths[0].getSegment(i) instanceof DescriptionBase) {
-                            element = (DescriptionBase) paths[0].getSegment(i);
-                            break;
-                        }
+            ISelection selection = mediaView.getViewer().getSelection();
+            if (selection instanceof TreeSelection) {
+                TreePath[] paths = ((TreeSelection) selection).getPaths();
+                int count = paths[0].getSegmentCount();
+                DescriptionBase element = null;
+                for (int i = 0; i < count; i++ ) {
+                    if (paths[0].getSegment(i) instanceof DescriptionBase) {
+                        element = (DescriptionBase) paths[0].getSegment(i);
+                        break;
                     }
-                    AbstractPostOperation operation = null;
-                    // TODO use undo context specific to editor
-                    operation = new MoveMediaInListOperation(menuItem.getLocalizedLabel(),
-                            EditorUtil.getUndoContext(), taxon, element, media, MoveMediaInListOperation.UP, mediaView);
-                    AbstractUtility.executeOperation(operation, sync);
                 }
+                AbstractPostOperation operation = null;
+                // TODO use undo context specific to editor
+                operation = new MoveMediaInListOperation(menuItem.getLocalizedLabel(),
+                        EditorUtil.getUndoContext(), taxon, element, media, MoveMediaInListOperation.UP, mediaView);
+                AbstractUtility.executeOperation(operation, sync);
             }
         }
     }