ref #8471: allow delete again and disable description parts
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / e4 / handler / DeleteHandlerE4.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.view.descriptive.e4.handler;
11
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15
16 import javax.inject.Named;
17
18 import org.eclipse.core.commands.operations.IUndoContext;
19 import org.eclipse.e4.core.di.annotations.CanExecute;
20 import org.eclipse.e4.core.di.annotations.Execute;
21 import org.eclipse.e4.core.di.annotations.Optional;
22 import org.eclipse.e4.ui.di.UISynchronize;
23 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
24 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
25 import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
26 import org.eclipse.e4.ui.services.IServiceConstants;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.TreeViewer;
30 import org.eclipse.swt.widgets.Shell;
31
32 import eu.etaxonomy.cdm.api.service.DeleteResult;
33 import eu.etaxonomy.cdm.api.service.IDescriptionService;
34 import eu.etaxonomy.cdm.api.service.UpdateResult.Status;
35 import eu.etaxonomy.cdm.model.description.DescriptionBase;
36 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
38 import eu.etaxonomy.cdm.model.description.TaxonDescription;
39 import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
40 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
41 import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
42 import eu.etaxonomy.taxeditor.editor.EditorUtil;
43 import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
44 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
45 import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
46 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
47 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
48 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
49 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonNameDescriptionOperation;
50 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
51 import eu.etaxonomy.taxeditor.model.MessagingUtils;
52 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
53 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
54 import eu.etaxonomy.taxeditor.store.CdmStore;
55
56 /**
57 *
58 * @author pplitzner
59 * @date 15.08.2017
60 *
61 */
62 public class DeleteHandlerE4 {
63
64 @Execute
65 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
66 @Named(IServiceConstants.ACTIVE_SHELL)Shell shell, MMenuItem menuItem,
67 UISynchronize sync) {
68
69 FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
70 IStructuredSelection selection = (IStructuredSelection) factualDataPart.getViewer().getSelection();
71
72 Object selectionProvidingPart = factualDataPart.getSelectionProvidingPart();
73 ICdmEntitySessionEnabled cdmEntitySessionEnabled = null;
74 if(selectionProvidingPart instanceof ICdmEntitySessionEnabled){
75 cdmEntitySessionEnabled = (ICdmEntitySessionEnabled) selectionProvidingPart;
76 }
77 TreeViewer viewer = (TreeViewer)factualDataPart.getViewer();
78 Object[] expandedElements = viewer.getExpandedElements();
79
80 String label = menuItem.getLocalizedLabel();
81
82 IUndoContext undoContext = EditorUtil.getUndoContext();
83
84 List<AbstractPostOperation> operations = new ArrayList();
85 AbstractPostOperation operation = null;
86 for(Iterator iter = selection.iterator();iter.hasNext();){
87 Object object = iter.next();
88 if (object == null){
89 continue;
90 }
91 DescriptionBase description = null;
92 SpecimenOrObservationBase specimen = null;
93 DeleteResult result = new DeleteResult();
94 if (object instanceof DescriptionBase){
95 result = CdmStore.getService(IDescriptionService.class).isDeletable(((DescriptionBase) object).getUuid());
96 description = (DescriptionBase) object;
97 } else if (object instanceof DescriptionElementBase){
98 description = ((DescriptionElementBase)object).getInDescription();
99 }
100 if (result.getStatus().equals(Status.OK)){
101 List<Object> expandedElementsList = new ArrayList<>();
102
103 for (Object o: expandedElements){
104 if(!o.equals(object)){
105 expandedElementsList.add(o);
106 }
107 }
108 expandedElements = expandedElementsList.toArray();
109 // TaxonDescription
110 if(object instanceof TaxonDescription ){
111
112 operation = new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) object, factualDataPart, cdmEntitySessionEnabled);
113
114 ((TaxonDescription) object).getTaxon().removeDescription((TaxonDescription) object);
115
116
117 } else if(object instanceof TaxonNameDescription ){
118 operation = new DeleteTaxonNameDescriptionOperation(label, undoContext, (TaxonNameDescription) object, factualDataPart, cdmEntitySessionEnabled);
119
120 ((TaxonNameDescription) object).getTaxonName().removeDescription((TaxonNameDescription) object);
121
122 } else if(object instanceof SpecimenDescription){
123 operation = new DeleteSpecimenDescriptionOperation(label, undoContext, (SpecimenDescription) object, factualDataPart, cdmEntitySessionEnabled);
124 specimen = ((SpecimenDescription) object).getDescribedSpecimenOrObservation();
125 specimen.removeDescription((SpecimenDescription) object);
126
127 } else if(object instanceof DescriptionElementBase){
128 // DescriptionElementBase
129 operation = new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) object, factualDataPart, cdmEntitySessionEnabled);
130 ((DescriptionElementBase) object).getInDescription().removeElement((DescriptionElementBase) object);
131 } else if(object instanceof FeatureNodeContainer){
132 List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) object).getDescriptionElementsForEntireBranch();
133
134 for(DescriptionElementBase descriptionElement : descriptions){
135 operation = new DeleteDescriptionElementOperation(label, undoContext, descriptionElement, factualDataPart, cdmEntitySessionEnabled);
136 }
137 ((FeatureNodeContainer)object).getContainerTree().removeContainer((FeatureNodeContainer)object);
138
139
140 }
141
142
143 if (factualDataPart.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4){
144 ((TaxonNameEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(operation);
145 ((TaxonNameEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).setDirty();
146 ((TaxonNameEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).redraw();
147 }
148 if (factualDataPart.getSelectionProvidingPart().getObject() instanceof DerivateView){
149 ((DerivateView)factualDataPart.getSelectionProvidingPart().getObject()).addOperation(operation);
150 ((DerivateView)factualDataPart.getSelectionProvidingPart().getObject()).setDirty(true);
151
152 if (object instanceof DescriptionBase){
153 factualDataPart.getViewer().refresh();
154 factualDataPart.collapse();
155 } else if (object instanceof DescriptionElementBase){
156 if (description != null){
157 factualDataPart.changed(description);
158 }
159 }
160 }
161 if (factualDataPart.getSelectionProvidingPart().getObject() instanceof BulkEditorE4){
162 ((BulkEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).addOperation(operation);
163 ((BulkEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).setDirty(true);
164 if (object instanceof SpecimenDescription){
165 ((BulkEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).changed(((SpecimenDescription)object).getDescribedSpecimenOrObservation());
166 }else if (object instanceof TaxonDescription){
167 ((BulkEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).changed(((TaxonDescription)object).getTaxon());
168 }else if (object instanceof TaxonNameDescription){
169 ((BulkEditorE4)factualDataPart.getSelectionProvidingPart().getObject()).changed(((TaxonNameDescription)object).getTaxonName());
170 }
171 }
172 viewer.refresh();
173
174 if (expandedElements.length>0){
175 for (Object o: expandedElements){
176 if (o instanceof FeatureNodeContainer && object instanceof DescriptionElementBase){
177 FeatureNodeContainer container = (FeatureNodeContainer)o;
178 if (container.getDescriptionElements().contains(object)){
179 container.getDescriptionElements().remove(object);
180 break;
181 }
182 }
183 }
184
185 viewer.setExpandedElements(expandedElements);
186 }
187 }
188 else{
189 Exception e = result.getExceptions() != null && !result.getExceptions().isEmpty()? result.getExceptions().iterator().next() : null;
190
191 MessagingUtils.informationDialog("Delete not possible", e.getMessage());
192 break;
193
194 }
195
196 }
197
198
199
200 }
201
202
203 @CanExecute
204 public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
205 MHandledMenuItem menuItem){
206 FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
207 ISelection selection = factualDataPart.getViewer().getSelection();
208 boolean canExecute = true;
209 if (selection instanceof IStructuredSelection && !selection.isEmpty()){
210 IStructuredSelection sel = (IStructuredSelection) selection;
211 if (sel.getFirstElement() instanceof FeatureNodeContainer){
212 canExecute = false;
213 }
214
215 }
216 canExecute &= selection instanceof IStructuredSelection && !selection.isEmpty() && !(((IStructuredSelection)selection).getFirstElement() instanceof FeatureNodeContainer);
217
218 menuItem.setVisible(canExecute);
219 return canExecute;
220 }
221 }