Merge branch 'develop' into bulkEditorE4
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / handler / DeleteHandlerE4.java
index ecb5bb7fc76904a1e1592e8eb5e367716e8ea39e..1376af3861cafe9f91a06cc0036916e77cbe53ca 100644 (file)
@@ -13,17 +13,16 @@ package eu.etaxonomy.taxeditor.bulkeditor.e4.handler;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
+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.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.dialogs.MessageDialog;
-import org.eclipse.jface.text.TextSelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.ui.IEditorInput;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.handlers.HandlerUtil;
-import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.swt.widgets.Shell;
 
 import eu.etaxonomy.cdm.api.application.ICdmRepository;
 import eu.etaxonomy.cdm.api.service.DeleteResult;
@@ -47,10 +46,7 @@ import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
-import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService;
-import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation;
-import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel;
-import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
+import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
 import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
@@ -60,13 +56,12 @@ import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteCo
 
 
 /**
- * @author n.hoffmann
- * @created Mar 11, 2011
- * @version 1.0
+ *
+ * @author pplitzner
+ * @date 11.09.2017
+ *
  */
-public class DeleteHandlerE4 extends AbstractHandler {
-
-
+public class DeleteHandlerE4 {
 
     private static final String COULD_NOT_BE_DELETED = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_COULD_NOT_BE_DELETED;
     private static final String AN_EXCEPTION_OCCURED = Messages.DeleteHandler_EXCEPTION;
@@ -86,187 +81,184 @@ public class DeleteHandlerE4 extends AbstractHandler {
     private static final String DELETE = eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_DELETE;
     private static final String CONFIRM_DELETION = Messages.DeleteHandler_CONFIRM;
 
-    @Override
-    public Object execute(ExecutionEvent event) throws ExecutionException {
-
-
-        TextSelection selection = (TextSelection) HandlerUtil.getCurrentSelection(event);
-        IEditorPart editor = HandlerUtil.getActiveEditor(event);
-
-        IEditorInput input = editor.getEditorInput();
-
-        if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){
-
-
-            IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
-            LineAnnotationModel model =
-                    (LineAnnotationModel) provider.getAnnotationModel(input);
-
-
-            IStructuredSelection structuredSelection = (IStructuredSelection) selection;
-            DeleteConfiguratorBase config = null;
-            DeleteResult result = new DeleteResult();
-            String errorMessage= OBJECT;
-            for(Object object : structuredSelection.toList()){
-                if (object instanceof CdmBase){
-                    CdmBase base = (CdmBase)object;
-                    LineAnnotation<?> annotation = (LineAnnotation<?>) model.getAnnotation(object);
-                    if (base.getId() != 0){
-
-
-                        try {
-                            ICdmRepository controller;
-                            controller = CdmStore.getCurrentApplicationConfiguration();
-                            if (object instanceof SpecimenOrObservationBase){
-                                IOccurrenceService service = controller.getOccurrenceService();
-                                config = new SpecimenDeleteConfigurator();
-
-                                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
-                                        HandlerUtil.getActiveShell(event), CONFIRM_DELETION, null,
-                                        String.format(
-                                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
-                                                SPECIMEN_OR_OBSERVATION),
-                                        MessageDialog.WARNING,
-                                        new String[] { DELETE, SKIP }, 0, true);
-                                int result_dialog = dialog.open();
-                                if (result_dialog != IStatus.OK){
-                                    return null;
-                                }
-                                result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
-                                errorMessage = SPECIMEN_OR_OBSERVATION;
-
-                            } else if (object instanceof Reference){
-                                IReferenceService service = controller.getReferenceService();
-                                result = service.isDeletable(((Reference)object).getUuid(), null);
-                                errorMessage = REFERENCE;
-
-                            } else if (object instanceof Group){
-                                errorMessage = GROUP;
-                            }else if (object instanceof User){
-                                errorMessage = USER;
-                            } else if (object instanceof TaxonName){
-                                TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
-                                config = new NameDeletionConfigurator();
-
-                                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
-                                        HandlerUtil.getActiveShell(event), CONFIRM_DELETION, null,
-                                        String.format(
-                                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
-                                                NAME),
-                                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
-                                int result_dialog = dialog.open();
-                                if (result_dialog != IStatus.OK){
-                                    return null;
-                                }
-                                result = controller.getNameService().isDeletable(name.getUuid(), config);
-                                errorMessage = NAME;
-
-                            } else if (object instanceof TaxonBase){
-
-                                // synonym
-                                if(object instanceof Synonym){
-                                    Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
-                                    config = new SynonymDeletionConfigurator();
-                                    errorMessage = SYNONYM;
-                                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
-                                            HandlerUtil.getActiveShell(event), CONFIRM_DELETION, null,
-                                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
-                                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
-                                    int result_dialog = dialog.open();
-                                    if (result_dialog != IStatus.OK){
-                                        return null;
-                                    }
-                                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
-
-                                }
-                                else if(object instanceof Taxon ){
-                                    Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
-                                    if (((Taxon)object).getTaxonNodes().isEmpty()){
-                                        errorMessage = TAXON;
-                                    } else {
-                                        MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
-                                                Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
-                                                null);
-                                        return null;
-                                    }
-                                    config = new TaxonDeletionConfigurator();
-                                    ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
-                                    DeleteConfiguratorDialog dialog;
-                                    dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event),
-                                            CONFIRM_DELETION, null,
-                                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
-                                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
-                                    int result_dialog = dialog.open();
-                                    if (result_dialog != IStatus.OK){
-                                        return null;
-                                    }
-                                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
-
-                                }
-                            } else if (object instanceof TeamOrPersonBase){
-                                result = controller.getAgentService().isDeletable(((CdmBase) object).getUuid(), null);
-                                errorMessage = TEAM_OR_PERSON;
-                            } else if (object instanceof Media){
-                                config = new MediaDeletionConfigurator();
-
-                                Media media = HibernateProxyHelper.deproxy(object, Media.class);
-                                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
-                                DeleteConfiguratorDialog dialog;
-                                dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event),
-                                        CONFIRM_DELETION, null,
-                                        String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
-                                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
-                                int result_dialog = dialog.open();
-                                if (result_dialog != IStatus.OK){
-                                    return null;
-                                }
+    @Execute
+    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase object,
+            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
+            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
+
+        BulkEditorE4 editor= (BulkEditorE4) activePart.getObject();
+
+        DeleteConfiguratorBase config = null;
+        DeleteResult result = new DeleteResult();
+        String errorMessage= OBJECT;
+        if (editor.isDirty()){
+            boolean proceed = MessageDialog.openQuestion(null,
+                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
+            if (proceed) {
+                editor.save();
+            } else {
+                return ;
+            }
+        }
+        try {
+            ICdmRepository controller;
+            controller = CdmStore.getCurrentApplicationConfiguration();
+            if (object instanceof SpecimenOrObservationBase){
+                IOccurrenceService service = controller.getOccurrenceService();
+                config = new SpecimenDeleteConfigurator();
+
+                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
+                        shell, CONFIRM_DELETION, null,
+                        String.format(
+                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
+                                SPECIMEN_OR_OBSERVATION),
+                        MessageDialog.WARNING,
+                        new String[] { DELETE, SKIP }, 0, true);
+                int result_dialog = dialog.open();
+                if (result_dialog != IStatus.OK){
+                    return;
+                }
+                result = service.isDeletable(((SpecimenOrObservationBase<?>) object).getUuid(), config);
+                errorMessage = SPECIMEN_OR_OBSERVATION;
+
+            } else if (object instanceof Reference){
+                IReferenceService service = controller.getReferenceService();
+                result = service.isDeletable(((Reference)object).getUuid(), null);
+                errorMessage = REFERENCE;
+
+            } else if (object instanceof Group){
+                errorMessage = GROUP;
+            }else if (object instanceof User){
+                errorMessage = USER;
+            } else if (object instanceof TaxonName){
+                TaxonName name = HibernateProxyHelper.deproxy(object, TaxonName.class);
+                config = new NameDeletionConfigurator();
+
+                DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
+                        shell, CONFIRM_DELETION, null,
+                        String.format(
+                                DO_YOU_REALLY_WANT_TO_DELETE_THE_S+"\n"+Messages.DeleteHandler_IRREVERSIBLE,
+                                NAME),
+                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
+                int result_dialog = dialog.open();
+                if (result_dialog != IStatus.OK){
+                    return;
+                }
+                result = controller.getNameService().isDeletable(name.getUuid(), config);
+                errorMessage = NAME;
+
+            } else if (object instanceof TaxonBase){
+
+                // synonym
+                if(object instanceof Synonym){
+                    Synonym synonym = HibernateProxyHelper.deproxy(object, Synonym.class);
+                    config = new SynonymDeletionConfigurator();
+                    errorMessage = SYNONYM;
+                    DeleteConfiguratorDialog dialog = new DeleteConfiguratorDialog(config,
+                            shell, CONFIRM_DELETION, null,
+                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, SYNONYM),
+                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
+                    int result_dialog = dialog.open();
+                    if (result_dialog != IStatus.OK){
+                        return;
+                    }
+                    result = controller.getTaxonService().isDeletable(synonym.getUuid(), config);
 
-                                result = controller.getMediaService().isDeletable(media.getUuid(), config);
-                                errorMessage = MEDIA;
+                }
+                else if(object instanceof Taxon ){
+                    Taxon  taxon = HibernateProxyHelper.deproxy(object, Taxon.class);
+                    if (((Taxon)object).getTaxonNodes().isEmpty()){
+                        errorMessage = TAXON;
+                    } else {
+                        MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(),
+                                Messages.DeleteHandler_TAXON_CAN_NOT_BE_DELETED,
+                                null);
+                        return;
+                    }
+                    config = new TaxonDeletionConfigurator();
+                    ((TaxonDeletionConfigurator) config).setDeleteInAllClassifications(true);
+                    DeleteConfiguratorDialog dialog;
+                    dialog = new DeleteConfiguratorDialog(config, shell,
+                            CONFIRM_DELETION, null,
+                            String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, TAXON),
+                            MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
+                    int result_dialog = dialog.open();
+                    if (result_dialog != IStatus.OK){
+                        return;
+                    }
+                    result = controller.getTaxonService().isDeletable(taxon.getUuid(), config);
 
-                            }
+                }
+            } else if (object instanceof TeamOrPersonBase){
+                result = controller.getAgentService().isDeletable(object.getUuid(), null);
+                errorMessage = TEAM_OR_PERSON;
+            } else if (object instanceof Media){
+                config = new MediaDeletionConfigurator();
+
+                Media media = HibernateProxyHelper.deproxy(object, Media.class);
+                ((MediaDeletionConfigurator)config).setDeleteFrom(media);
+                DeleteConfiguratorDialog dialog;
+                dialog = new DeleteConfiguratorDialog(config, shell,
+                        CONFIRM_DELETION, null,
+                        String.format(DO_YOU_REALLY_WANT_TO_DELETE_THE_S, MEDIA),
+                        MessageDialog.WARNING, new String[] { DELETE, SKIP }, 0, true);
+                int result_dialog = dialog.open();
+                if (result_dialog != IStatus.OK){
+                    return;
+                }
 
+                result = controller.getMediaService().isDeletable(media.getUuid(), config);
+                errorMessage = MEDIA;
 
-                        } catch (Exception e){
-                            MessagingUtils.errorDialog(AN_EXCEPTION_OCCURED+" "+DELETE_NOT_POSSIBLE, getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true); //$NON-NLS-1$
-                        }
-                        if (result.isError() || result.isAbort()){
-                            //convert first letter to upper case
-                            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
-                            if (!result.getExceptions().isEmpty()) {
-                                List<String> messages = new ArrayList<String>();
-                                for (Exception e:result.getExceptions()){
-                                    messages.add(e.getMessage());
-                                }
-                                errorMessage += " " +COULD_NOT_BE_DELETED; //$NON-NLS-1$
-                                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
-                                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
-                            }else{
-                                MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(), errorMessage+" "+COULD_NOT_BE_DELETED+" "+AN_EXCEPTION_OCCURED, null); //$NON-NLS-1$ //$NON-NLS-2$
-                            }
-                        }
-                        annotation.markAsDeleted(config);
-                    }
-                    if (result.isOk() ){
+            }
 
-                        ((BulkEditor) editor).removeAnnotatedLine(annotation);
 
-                        if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
-                            List<String> messages = new ArrayList<String>();
-                            for (Exception e:result.getExceptions()){
-                                messages.add(e.getMessage());
-                            }
-                            //convert first letter to upper case
-                            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
-                            errorMessage += eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_CAN_BE_DELETED;
-                            //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
-                            DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
-                        }
-                    }
+        } catch (Exception e){
+            MessagingUtils.errorDialog(AN_EXCEPTION_OCCURED+" "+DELETE_NOT_POSSIBLE, getClass(), e.getMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID, null, true); //$NON-NLS-1$
+        }
+        if (result.isError() || result.isAbort()){
+            //convert first letter to upper case
+            errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
+            if (!result.getExceptions().isEmpty()) {
+                List<String> messages = new ArrayList<String>();
+                for (Exception e:result.getExceptions()){
+                    messages.add(e.getMessage());
                 }
+                errorMessage += " " +COULD_NOT_BE_DELETED; //$NON-NLS-1$
+                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
+                DeleteResultMessagingUtils.messageDialogWithDetails(result,errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
+            }else{
+                MessagingUtils.messageDialog(DELETE_NOT_POSSIBLE, getClass(), errorMessage+" "+COULD_NOT_BE_DELETED+" "+AN_EXCEPTION_OCCURED, null); //$NON-NLS-1$ //$NON-NLS-2$
+            }
+        }
+        if (result.isOk() ){
+            editor.getEditorInput().addToDelete(object, config);
+            editor.getEditorInput().getModel().remove(object);
+            editor.refresh(true);
+            editor.setDirty();
+
+            if(result.getUpdatedObjects().size() != 0 || !result.getExceptions().isEmpty()){
+                List<String> messages = new ArrayList<String>();
+                for (Exception e:result.getExceptions()){
+                    messages.add(e.getMessage());
+                }
+                //convert first letter to upper case
+                errorMessage = errorMessage.substring(0, 1).toUpperCase()+errorMessage.substring(1);
+                errorMessage += eu.etaxonomy.taxeditor.l10n.Messages.DeleteHandler_CAN_BE_DELETED;
+                //MessagingUtils.errorDialog("test", getClass(), "message", TaxeditorBulkeditorPlugin.PLUGIN_ID, result.getExceptions().iterator().next(),true);
+                DeleteResultMessagingUtils.messageDialogWithDetails(result, errorMessage, TaxeditorBulkeditorPlugin.PLUGIN_ID);
             }
         }
-        return null;
     }
 
+    @CanExecute
+    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
+            MHandledMenuItem menuItem){
+        boolean canExecute = false;
+        canExecute = selection!=null;
+        menuItem.setVisible(canExecute);
+        return canExecute;
+    }
+
+
 }