Merge branch 'develop' into nameEditorE4
[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.CanExecute;
14 import org.eclipse.e4.core.di.annotations.Execute;
15 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
16 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
17 import org.eclipse.e4.ui.services.IServiceConstants;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.swt.widgets.Shell;
20
21 import eu.etaxonomy.cdm.api.service.DeleteResult;
22 import eu.etaxonomy.cdm.api.service.config.SynonymDeletionConfigurator;
23 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.taxon.Synonym;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
28 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
29 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
30 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
31 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
32 import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
33 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteMisapplicationOperation;
34 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteSynonymOperation;
35 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteTaxonBaseOperation;
36 import eu.etaxonomy.taxeditor.model.AbstractUtility;
37 import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils;
38 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
39 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
40 import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
41
42 /**
43 *
44 * @author pplitzner
45 * @since Aug 28, 2017
46 *
47 */
48 public class DeleteTaxonBaseHandlerE4 implements IPostOperationEnabled {
49
50 protected TaxonNameEditorE4 editor;
51
52 @Execute
53 public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
54 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
55 @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
56 MHandledMenuItem menuItem) {
57
58 editor = (TaxonNameEditorE4) activePart.getObject();
59
60 doExecute(menuItem.getLocalizedLabel(), shell, editor, selection.getFirstElement());
61
62 }
63
64 protected void doExecute(String commandName, Shell shell, TaxonNameEditorE4 editor, Object selectedElement) {
65 AbstractPostOperation operation = null;
66
67 if (selectedElement instanceof TaxonBase){
68 if (((TaxonBase)selectedElement).getId() == 0){
69 if (selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()) {
70 editor.getTaxon().removeTaxon((Taxon)selectedElement, TaxonRelationshipType.MISAPPLIED_NAME_FOR());
71
72 } else if (selectedElement instanceof Synonym){
73 editor.getTaxon().removeSynonym((Synonym)selectedElement);
74 }
75 editor.redraw();
76 return;
77
78 }
79
80 }
81
82 // synonym
83 if(selectedElement instanceof Synonym){
84 SynonymDeletionConfigurator deleteConfig = new SynonymDeletionConfigurator();
85 if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_SYNONYM)){
86 return ;
87 }
88 operation = new DeleteSynonymOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Synonym) selectedElement,this, editor, editor.getEditorInput());
89
90 }
91 // misapplication
92 else if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
93 TaxonDeletionConfigurator deleteConfig = new TaxonDeletionConfigurator();
94 if(! DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfig, shell, Messages.DeleteTaxonBaseHandler_CONFIRM_DELETION, Messages.DeleteTaxonBaseHandler_REALLY_DELETE_MISAPPLICATION)){
95 return ;
96 }
97 operation = new DeleteMisapplicationOperation(commandName, editor.getUndoContext(), deleteConfig, editor.getTaxon(), (Taxon) selectedElement,this, editor, editor.getEditorInput());
98 } else {
99 throw new IllegalArgumentException(Messages.DeleteTaxonBaseHandler_ELEMENT_MUST_BE_SYNONYM_MISAPP_CONCEPT);
100 }
101
102 AbstractUtility.executeOperation(operation);
103 DeleteResult result = ((DeleteTaxonBaseOperation)operation).getResult();
104 if (result != null){
105 if (result.isError()){
106 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID);
107 } else if (selectedElement instanceof Synonym){
108 this.editor.redraw();
109 if (!result.getExceptions().isEmpty()){
110 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_SYNONYM_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
111 }
112 } else if (selectedElement instanceof Taxon && !result.getExceptions().isEmpty()){
113 this.editor.redraw();
114 DeleteResultMessagingUtils.messageDialogWithDetails(result, Messages.DeleteTaxonBaseHandler_DELETE_MISAPPLIEDNAME_SUCCESSFULL_BUT_REMAINING_RELATED_OBJECTS, TaxeditorEditorPlugin.PLUGIN_ID);
115 }
116 }
117 }
118
119 @CanExecute
120 public boolean canExecute(
121 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
122 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
123 MHandledMenuItem menuItem){
124 boolean canExecute = false;
125 Object selectedElement = selection.getFirstElement();
126 canExecute =
127 NameEditorMenuPropertyTester.isSynonym(selectedElement)
128 || NameEditorMenuPropertyTester.isMisapplication(selectedElement)
129 || NameEditorMenuPropertyTester.isRelatedConcept(selectedElement);
130 menuItem.setVisible(canExecute);
131 return canExecute;
132 }
133
134 @Override
135 public boolean postOperation(CdmBase objectAffectedByOperation) {
136 editor.redraw();
137 return true;
138 }
139
140 @Override
141 public boolean onComplete() {
142
143 return false;
144 }
145
146 }