00ec54f6e42058c573ec5b5d2495545f3c991209
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / media / handler / AddImageGalleryHandler.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.IStructuredSelection;
12 import org.eclipse.jface.viewers.TreeNode;
13 import org.eclipse.ui.IEditorInput;
14 import org.eclipse.ui.IEditorPart;
15 import org.eclipse.ui.IWorkbenchPart;
16 import org.eclipse.ui.handlers.HandlerUtil;
17
18 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
21 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
22 import eu.etaxonomy.taxeditor.bulkeditor.input.OccurrenceEditorInput;
23 import eu.etaxonomy.taxeditor.editor.EditorUtil;
24 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
25 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
26 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
27 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.AddDerivedUnitFacadeMediaOperation;
28 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
29 import eu.etaxonomy.taxeditor.model.AbstractUtility;
30 import eu.etaxonomy.taxeditor.model.MessagingUtils;
31 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33
34 /**
35 * <p>AddImageGalleryHandler class.</p>
36 *
37 * @author p.ciardelli
38 * @version $Id: $
39 */
40 public class AddImageGalleryHandler extends AbstractHandler {
41
42 /* (non-Javadoc)
43 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
44 */
45 /** {@inheritDoc} */
46 @Override
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48
49 IWorkbenchPart part = HandlerUtil.getActivePart(event);
50 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
51
52
53 IEditorPart editor = HandlerUtil.getActiveEditor(event);
54 IEditorInput input = editor.getEditorInput();
55
56 if (input instanceof TaxonEditorInput) {
57 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
58 AbstractPostOperation<?> operation;
59 try {
60 // TODO use undo context specific to editor
61 operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(),
62 EditorUtil.getUndoContext(), taxon, postOperationEnabled, true);
63 AbstractUtility.executeOperation(operation);
64 } catch (NotDefinedException e) {
65 MessagingUtils.warn(getClass(), "Command name not set.");
66 }
67 }
68 else if(input instanceof OccurrenceEditorInput){
69 BulkEditor bulkEditor = (BulkEditor) editor;
70 ISelection selection = bulkEditor.getSelectionProvider().getSelection();
71 invokeOperation(event, postOperationEnabled, selection);
72 }
73 else if(input instanceof DerivateViewEditorInput){
74 ISelection selection = ((DerivateView)editor).getSelection();
75 invokeOperation(event, postOperationEnabled, selection);
76 }
77 return null;
78 }
79
80 /**
81 * @param event
82 * @param postOperationEnabled
83 * @param selection
84 */
85 private void invokeOperation(ExecutionEvent event, IPostOperationEnabled postOperationEnabled, ISelection selection) {
86 if(selection instanceof IStructuredSelection){
87 Object element = ((IStructuredSelection) selection).getFirstElement();
88 if(element instanceof TreeNode){
89 element = ((TreeNode) element).getValue();
90 }
91 if(element instanceof SpecimenOrObservationBase<?>){
92 try {
93 AbstractPostOperation<?> operation = new AddDerivedUnitFacadeMediaOperation(event.getCommand().getName(),
94 BulkEditorUtil.getUndoContext(), (SpecimenOrObservationBase<?>)element, postOperationEnabled);
95 AbstractUtility.executeOperation(operation);
96 } catch (NotDefinedException e) {
97 MessagingUtils.warn(getClass(), "Command name not set.");
98 }
99 }
100 }
101 }
102
103 }