+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.taxeditor.editor.name.e4.handler;
+import javax.inject.Named;
+
+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.swt.widgets.Shell;
+
+import eu.etaxonomy.cdm.api.service.DeleteResult;
+import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
+import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.taxon.Synonym;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
+import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
+import eu.etaxonomy.taxeditor.editor.l10n.Messages;
+import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
+import eu.etaxonomy.taxeditor.editor.name.operation.DeleteMisapplicationOperation;
+import eu.etaxonomy.taxeditor.editor.name.operation.DeleteSynonymOperation;
+import eu.etaxonomy.taxeditor.editor.name.operation.DeleteTaxonBaseOperation;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
+import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
+import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
+
+/**
+ *
+ * @author pplitzner
+ * @since Aug 28, 2017
+ *
+ */
+public class DeleteTaxonBaseHandlerE4 implements IPostOperationEnabled {
+
+ protected TaxonNameEditorE4 editor;
+
+ @Execute
+ public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
+ @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
+ @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
+ MHandledMenuItem menuItem) {
+
+ editor = (TaxonNameEditorE4) activePart.getObject();
+
+ doExecute(menuItem.getLocalizedLabel(), shell, editor, selection.getFirstElement());
+
+ }
+
+ protected void doExecute(String commandName, Shell shell, TaxonNameEditorE4 editor, Object selectedElement) {
+ AbstractPostOperation operation = null;
+
+ if (selectedElement instanceof TaxonBase){
+ if (((TaxonBase)selectedElement).getId() == 0){
+ if (selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()) {
+ editor.getTaxon().removeTaxon((Taxon)selectedElement, TaxonRelationshipType.MISAPPLIED_NAME_FOR());
+
+ } else if (selectedElement instanceof Synonym){
+ editor.getTaxon().removeSynonym((Synonym)selectedElement);
+ }
+ editor.redraw();
+ return;
+
+ }
+
+ }
+
+ // synonym
+ if(selectedElement instanceof Synonym){
+ SynonymDeletionConfigurator deleteConfig = new SynonymDeletionConfigurator();
+ if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_SYNONYM)){
+ return ;
+ }
+ operation = new DeleteSynonymOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Synonym) selectedElement,this, editor, editor.getEditorInput());
+
+ }
+ // misapplication
+ else if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
+ TaxonDeletionConfigurator deleteConfig = new TaxonDeletionConfigurator();
+ if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_MISAPPLICATION)){
+ return ;
+ }
+ operation = new DeleteMisapplicationOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Taxon) selectedElement,this, editor, editor.getEditorInput());
+ } else {
+ throw new IllegalArgumentException(Messages.DeleteTaxonBaseHandler_ELEMENT_MUST_BE_SYNONYM_MISAPP_CONCEPT);
+ }
+
+ AbstractUtility.executeOperation(operation);
+ DeleteResult result = ((DeleteTaxonBaseOperation)operation).getResult();
+ if (result != null){
+ if (result.isError()){
+ DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
+ } else if (selectedElement instanceof Synonym){
+ this.editor.redraw();
+ if (!result.getExceptions().isEmpty()){
+ DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_SYNONYM_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
+ }
+ } else if (selectedElement instanceof Taxon && !result.getExceptions().isEmpty()){
+ this.editor.redraw();
+ DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_MISAPPLIEDNAME_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
+ }
+ }
+
+
+ }
+
+ @Override
+ public boolean postOperation(CdmBase objectAffectedByOperation) {
+
+ return true;
+ }
+
+ @Override
+ public boolean onComplete() {
+
+ return false;
+ }
+
+}