ref #6595 migrate concept menu handlers
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / e4 / handler / DeleteTaxonBaseHandlerE4.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.name.e4.handler;
11 import javax.inject.Named;
12
13 import org.eclipse.e4.core.di.annotations.Execute;
14 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
15 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
16 import org.eclipse.e4.ui.services.IServiceConstants;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.swt.widgets.Shell;
19
20 import eu.etaxonomy.cdm.api.service.DeleteResult;
21 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
22 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.cdm.model.taxon.Synonym;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
28 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
29 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
30 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
31 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteMisapplicationOperation;
32 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteSynonymOperation;
33 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteTaxonBaseOperation;
34 import eu.etaxonomy.taxeditor.model.AbstractUtility;
35 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
36 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
37 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
38 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
39
40 /**
41 *
42 * @author pplitzner
43 * @since Aug 28, 2017
44 *
45 */
46 public class DeleteTaxonBaseHandlerE4 implements IPostOperationEnabled {
47
48 protected TaxonNameEditorE4 editor;
49
50 @Execute
51 public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
52 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
53 @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
54 MHandledMenuItem menuItem) {
55
56 editor = (TaxonNameEditorE4) activePart.getObject();
57
58 doExecute(menuItem.getLocalizedLabel(), shell, editor, selection.getFirstElement());
59
60 }
61
62 protected void doExecute(String commandName, Shell shell, TaxonNameEditorE4 editor, Object selectedElement) {
63 AbstractPostOperation operation = null;
64
65 if (selectedElement instanceof TaxonBase){
66 if (((TaxonBase)selectedElement).getId() == 0){
67 if (selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()) {
68 editor.getTaxon().removeTaxon((Taxon)selectedElement, TaxonRelationshipType.MISAPPLIED_NAME_FOR());
69
70 } else if (selectedElement instanceof Synonym){
71 editor.getTaxon().removeSynonym((Synonym)selectedElement);
72 }
73 editor.redraw();
74 return;
75
76 }
77
78 }
79
80 // synonym
81 if(selectedElement instanceof Synonym){
82 SynonymDeletionConfigurator deleteConfig = new SynonymDeletionConfigurator();
83 if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_SYNONYM)){
84 return ;
85 }
86 operation = new DeleteSynonymOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Synonym) selectedElement,this, editor, editor.getEditorInput());
87
88 }
89 // misapplication
90 else if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
91 TaxonDeletionConfigurator deleteConfig = new TaxonDeletionConfigurator();
92 if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_MISAPPLICATION)){
93 return ;
94 }
95 operation = new DeleteMisapplicationOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Taxon) selectedElement,this, editor, editor.getEditorInput());
96 } else {
97 throw new IllegalArgumentException(Messages.DeleteTaxonBaseHandler_ELEMENT_MUST_BE_SYNONYM_MISAPP_CONCEPT);
98 }
99
100 AbstractUtility.executeOperation(operation);
101 DeleteResult result = ((DeleteTaxonBaseOperation)operation).getResult();
102 if (result != null){
103 if (result.isError()){
104 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
105 } else if (selectedElement instanceof Synonym){
106 this.editor.redraw();
107 if (!result.getExceptions().isEmpty()){
108 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_SYNONYM_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
109 }
110 } else if (selectedElement instanceof Taxon && !result.getExceptions().isEmpty()){
111 this.editor.redraw();
112 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_MISAPPLIEDNAME_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
113 }
114 }
115
116
117 }
118
119 @Override
120 public boolean postOperation(CdmBase objectAffectedByOperation) {
121
122 return true;
123 }
124
125 @Override
126 public boolean onComplete() {
127
128 return false;
129 }
130
131 }