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