1
|
package eu.etaxonomy.taxeditor.editor.view.uses.handler;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
|
6
|
import org.eclipse.core.commands.AbstractHandler;
|
7
|
import org.eclipse.core.commands.ExecutionEvent;
|
8
|
import org.eclipse.core.commands.ExecutionException;
|
9
|
import org.eclipse.core.commands.common.NotDefinedException;
|
10
|
import org.eclipse.core.commands.operations.IUndoContext;
|
11
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
12
|
import org.eclipse.jface.viewers.TreePath;
|
13
|
import org.eclipse.jface.viewers.TreeSelection;
|
14
|
import org.eclipse.ui.IWorkbenchPart;
|
15
|
import org.eclipse.ui.handlers.HandlerUtil;
|
16
|
|
17
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
18
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
19
|
import eu.etaxonomy.cdm.model.media.Media;
|
20
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
21
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
|
22
|
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
|
23
|
import eu.etaxonomy.taxeditor.editor.view.uses.operation.DeleteTaxonUseOperation;
|
24
|
import eu.etaxonomy.taxeditor.editor.view.uses.operation.DeleteUseRecordOperation;
|
25
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
26
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
27
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
28
|
|
29
|
public class DeleteUseHandler extends AbstractHandler {
|
30
|
|
31
|
/*
|
32
|
* (non-Javadoc)
|
33
|
*
|
34
|
* @see
|
35
|
* org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
|
36
|
* ExecutionEvent)
|
37
|
*/
|
38
|
/** {@inheritDoc} */
|
39
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
40
|
IStructuredSelection selection = (IStructuredSelection) HandlerUtil
|
41
|
.getActiveSite(event).getSelectionProvider().getSelection();
|
42
|
|
43
|
IWorkbenchPart part = HandlerUtil.getActivePart(event);
|
44
|
IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
|
45
|
: null;
|
46
|
|
47
|
try {
|
48
|
String label = event.getCommand().getName();
|
49
|
|
50
|
IUndoContext undoContext = EditorUtil.getUndoContext();
|
51
|
|
52
|
List<AbstractPostOperation> operations = new ArrayList<AbstractPostOperation>();
|
53
|
|
54
|
for (Object object : selection.toArray()) {
|
55
|
|
56
|
AbstractPostOperation operation = null;
|
57
|
|
58
|
// TaxonDescription
|
59
|
if (object instanceof TaxonDescription) {
|
60
|
operations.add(new DeleteTaxonUseOperation(label,
|
61
|
undoContext, (TaxonDescription) object,
|
62
|
postOperationEnabled));
|
63
|
}
|
64
|
// DescriptionElementBase
|
65
|
else if (object instanceof DescriptionElementBase) {
|
66
|
operations.add(new DeleteUseRecordOperation(label,
|
67
|
undoContext, (DescriptionElementBase) object,
|
68
|
postOperationEnabled));
|
69
|
} else if (object instanceof FeatureNodeContainer) {
|
70
|
List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) object)
|
71
|
.getDescriptionElementsForEntireBranch();
|
72
|
|
73
|
for (DescriptionElementBase description : descriptions) {
|
74
|
operations.add(new DeleteUseRecordOperation(
|
75
|
label, undoContext, description,
|
76
|
postOperationEnabled));
|
77
|
}
|
78
|
}
|
79
|
// Media
|
80
|
else if (object instanceof Media) {
|
81
|
TreeSelection treeSelection = (TreeSelection) selection;
|
82
|
|
83
|
TreePath[] path = treeSelection.getPathsFor(object);
|
84
|
|
85
|
TaxonDescription imageGallery = (TaxonDescription) path[0]
|
86
|
.getFirstSegment();
|
87
|
|
88
|
operations
|
89
|
.add(new DeleteMediaOperation(label, undoContext,
|
90
|
imageGallery, (Media) object,
|
91
|
postOperationEnabled));
|
92
|
} else {
|
93
|
EditorUtil.error(getClass(),
|
94
|
"Selection is not valid for this delete handler",
|
95
|
null);
|
96
|
}
|
97
|
}
|
98
|
|
99
|
// execute all cumulated operations
|
100
|
for (AbstractPostOperation operation : operations) {
|
101
|
EditorUtil.executeOperation(operation);
|
102
|
}
|
103
|
|
104
|
} catch (NotDefinedException e) {
|
105
|
EditorUtil.warn(getClass(), "Command name not set.");
|
106
|
}
|
107
|
|
108
|
return null;
|
109
|
}
|
110
|
}
|