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.List;
|
14
|
|
15
|
import javax.inject.Named;
|
16
|
|
17
|
import org.eclipse.core.commands.operations.IUndoContext;
|
18
|
import org.eclipse.e4.core.di.annotations.Execute;
|
19
|
import org.eclipse.e4.core.di.annotations.Optional;
|
20
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
21
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
|
22
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
23
|
import org.eclipse.jface.viewers.TreePath;
|
24
|
import org.eclipse.jface.viewers.TreeSelection;
|
25
|
import org.eclipse.swt.widgets.Shell;
|
26
|
|
27
|
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
|
28
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
29
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
30
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
31
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
32
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
33
|
import eu.etaxonomy.cdm.model.media.Media;
|
34
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
35
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
36
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
|
37
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
|
38
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
|
39
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
|
40
|
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
|
41
|
import eu.etaxonomy.taxeditor.editor.view.media.operation.RemoveImageFromDescriptionElementOperation;
|
42
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
43
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
44
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
45
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
46
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
47
|
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
|
48
|
|
49
|
/**
|
50
|
*
|
51
|
* @author pplitzner
|
52
|
* @date 15.08.2017
|
53
|
*
|
54
|
*/
|
55
|
public class DeleteHandler {
|
56
|
|
57
|
@Execute
|
58
|
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
59
|
@Optional@Named(IServiceConstants.ACTIVE_SELECTION) Object selection,
|
60
|
@Named(IServiceConstants.ACTIVE_SHELL)Shell shell, MMenuItem menuItem) {
|
61
|
|
62
|
FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
|
63
|
|
64
|
Object selectionProvidingPart = factualDataPart.getSelectionProvidingPart();
|
65
|
ICdmEntitySessionEnabled cdmEntitySessionEnabled = null;
|
66
|
if(selectionProvidingPart instanceof ICdmEntitySessionEnabled){
|
67
|
cdmEntitySessionEnabled = (ICdmEntitySessionEnabled) selectionProvidingPart;
|
68
|
}
|
69
|
|
70
|
String label = menuItem.getLocalizedLabel();
|
71
|
|
72
|
IUndoContext undoContext = EditorUtil.getUndoContext();
|
73
|
|
74
|
List<AbstractPostOperation<?>> operations = new ArrayList<AbstractPostOperation<?>>();
|
75
|
|
76
|
// for(Object object : selection.toArray()){
|
77
|
|
78
|
// TaxonDescription
|
79
|
if(selection instanceof TaxonDescription){
|
80
|
operations.add(new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) selection, factualDataPart, cdmEntitySessionEnabled));
|
81
|
}
|
82
|
else if(selection instanceof SpecimenDescription){
|
83
|
operations.add(new DeleteSpecimenDescriptionOperation(label, undoContext, (SpecimenDescription) selection, factualDataPart, cdmEntitySessionEnabled)) ;
|
84
|
}
|
85
|
// DescriptionElementBase
|
86
|
else if(selection instanceof DescriptionElementBase){
|
87
|
operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) selection, factualDataPart, cdmEntitySessionEnabled));
|
88
|
}
|
89
|
else if(selection instanceof FeatureNodeContainer){
|
90
|
List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) selection).getDescriptionElementsForEntireBranch();
|
91
|
|
92
|
for(DescriptionElementBase description : descriptions){
|
93
|
operations.add(new DeleteDescriptionElementOperation(label, undoContext, description, factualDataPart, cdmEntitySessionEnabled));
|
94
|
}
|
95
|
}
|
96
|
// Media
|
97
|
else if(selection instanceof Media){
|
98
|
|
99
|
TreeSelection treeSelection = (TreeSelection) factualDataPart.getViewer().getSelection();
|
100
|
|
101
|
TreePath[] path = treeSelection.getPathsFor(selection);
|
102
|
|
103
|
DescriptionBase<?> imageGallery = (DescriptionBase<?>) path[0].getFirstSegment();
|
104
|
|
105
|
// TODO use undo context specific to editor
|
106
|
MediaDeletionConfigurator config = new MediaDeletionConfigurator();
|
107
|
config.setDeleteFromDescription(true);
|
108
|
config.setOnlyRemoveFromGallery(false);
|
109
|
|
110
|
if (imageGallery instanceof SpecimenDescription){
|
111
|
config.setDeleteFrom(((SpecimenDescription)imageGallery).getDescribedSpecimenOrObservation());
|
112
|
} else if (imageGallery instanceof TaxonDescription){
|
113
|
config.setDeleteFrom(((TaxonDescription)imageGallery).getTaxon());
|
114
|
}else if (imageGallery instanceof TaxonNameDescription){
|
115
|
config.setDeleteFrom(((TaxonNameDescription)imageGallery).getTaxonName());
|
116
|
}
|
117
|
|
118
|
|
119
|
if (!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, Messages.DeleteHandler_CONFIRM_DELETION, Messages.DeleteHandler_CONFIRM_DELETION_MESSAGE)){
|
120
|
return;
|
121
|
}
|
122
|
|
123
|
|
124
|
|
125
|
if (config.isOnlyRemoveFromGallery() || ((Media)selection).getId() == 0){
|
126
|
operations.add(new RemoveImageFromDescriptionElementOperation(label, undoContext, (Media) selection, imageGallery, factualDataPart));
|
127
|
}else{
|
128
|
operations.add(new DeleteMediaOperation(label, undoContext, imageGallery, (Media) selection, config, factualDataPart));
|
129
|
}
|
130
|
|
131
|
|
132
|
|
133
|
}
|
134
|
else{
|
135
|
MessagingUtils.error(getClass(), Messages.DeleteHandler_INVALID_SELECTION, null);
|
136
|
}
|
137
|
// }
|
138
|
|
139
|
// execute all cumulated operations
|
140
|
for(AbstractPostOperation<?> operation : operations){
|
141
|
AbstractUtility.executeOperation(operation);
|
142
|
}
|
143
|
}
|
144
|
}
|