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