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