1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.view.media.handler;
|
5
|
|
6
|
import org.apache.log4j.Logger;
|
7
|
import org.eclipse.core.commands.AbstractHandler;
|
8
|
import org.eclipse.core.commands.ExecutionEvent;
|
9
|
import org.eclipse.core.commands.ExecutionException;
|
10
|
import org.eclipse.core.commands.common.NotDefinedException;
|
11
|
import org.eclipse.jface.viewers.ISelection;
|
12
|
import org.eclipse.jface.viewers.TreePath;
|
13
|
import org.eclipse.jface.viewers.TreeSelection;
|
14
|
import org.eclipse.ui.IEditorInput;
|
15
|
import org.eclipse.ui.IEditorPart;
|
16
|
import org.eclipse.ui.IWorkbenchPart;
|
17
|
import org.eclipse.ui.forms.editor.FormEditor;
|
18
|
import org.eclipse.ui.handlers.HandlerUtil;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
21
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
22
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
23
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
24
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
25
|
import eu.etaxonomy.taxeditor.operations.AbstractPostOperation;
|
26
|
import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
|
27
|
import eu.etaxonomy.taxeditor.operations.RemoveImageFromDescriptionElementOperation;
|
28
|
|
29
|
/**
|
30
|
* <p>DeleteMediaHandler class.</p>
|
31
|
*
|
32
|
* @author p.ciardelli
|
33
|
* @version $Id: $
|
34
|
*/
|
35
|
public class DeleteMediaHandler extends AbstractHandler {
|
36
|
private static final Logger logger = Logger
|
37
|
.getLogger(DeleteMediaHandler.class);
|
38
|
|
39
|
/* (non-Javadoc)
|
40
|
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
41
|
*/
|
42
|
/** {@inheritDoc} */
|
43
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
44
|
IWorkbenchPart part = HandlerUtil.getActivePart(event);
|
45
|
IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
|
46
|
|
47
|
IEditorPart editor = HandlerUtil.getActiveEditor(event);
|
48
|
if (editor instanceof FormEditor) {
|
49
|
editor = ((FormEditor) editor).getActiveEditor();
|
50
|
}
|
51
|
IEditorInput input = editor.getEditorInput();
|
52
|
if (input instanceof TaxonEditorInput) {
|
53
|
Taxon taxon = ((TaxonEditorInput) input).getTaxon();
|
54
|
|
55
|
ISelection selection = HandlerUtil.getCurrentSelection(event);
|
56
|
if (selection instanceof TreeSelection) {
|
57
|
TreePath[] paths = ((TreeSelection) selection).getPaths();
|
58
|
|
59
|
int count = paths[0].getSegmentCount();
|
60
|
DescriptionElementBase element = null;
|
61
|
for (int i = 0; i < count; i++ ) {
|
62
|
if (paths[0].getSegment(i) instanceof DescriptionElementBase) {
|
63
|
element = (DescriptionElementBase) paths[0].getSegment(i);
|
64
|
break;
|
65
|
}
|
66
|
}
|
67
|
ImageFile image = (ImageFile) paths[0].getLastSegment();
|
68
|
|
69
|
AbstractPostOperation operation = null;
|
70
|
try {
|
71
|
// TODO use undo context specific to editor
|
72
|
operation = new RemoveImageFromDescriptionElementOperation(event.getCommand().getName(),
|
73
|
EditorUtil.getUndoContext(), taxon, image, element, postOperationEnabled);
|
74
|
EditorUtil.executeOperation(operation);
|
75
|
} catch (NotDefinedException e) {
|
76
|
logger.warn("Command name not set");
|
77
|
}
|
78
|
}
|
79
|
}
|
80
|
return null;
|
81
|
}
|
82
|
|
83
|
}
|