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