#4389
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / media / handler / AddImageGalleryHandler.java
index 2739b337ea410875b7f351fa4ee4b118988dfcf9..a1e79087daaf5154b786cb63730ecdaf582754c7 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * 
+ *
  */
 package eu.etaxonomy.taxeditor.editor.view.media.handler;
 
@@ -7,15 +7,23 @@ import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
+import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
+import eu.etaxonomy.taxeditor.bulkeditor.input.OccurrenceEditorInput;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
+import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.AddDerivedUnitFacadeMediaOperation;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
@@ -32,26 +40,45 @@ public class AddImageGalleryHandler extends AbstractHandler {
         * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
         */
        /** {@inheritDoc} */
-       public Object execute(ExecutionEvent event) throws ExecutionException {
+       @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
 
                IWorkbenchPart part = HandlerUtil.getActivePart(event);
                IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
-               
-               
+
+
                IEditorPart editor = HandlerUtil.getActiveEditor(event);
                IEditorInput input = editor.getEditorInput();
+
                if (input instanceof TaxonEditorInput) {
-                       Taxon taxon = ((TaxonEditorInput) input).getTaxon();    
-                       AbstractPostOperation operation;
+                       Taxon taxon = ((TaxonEditorInput) input).getTaxon();
+                       AbstractPostOperation<?> operation;
                        try {
                                // TODO use undo context specific to editor
-                               operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(), 
+                               operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(),
                                                EditorUtil.getUndoContext(), taxon, postOperationEnabled, true);
-                               EditorUtil.executeOperation(operation);
+                               AbstractUtility.executeOperation(operation);
                        } catch (NotDefinedException e) {
                                MessagingUtils.warn(getClass(), "Command name not set.");
                        }
-               } 
+               }
+               else if(input instanceof OccurrenceEditorInput){
+                   BulkEditor bulkEditor = (BulkEditor) editor;
+                   ISelection selection = bulkEditor.getSelectionProvider().getSelection();
+
+                   if(selection instanceof IStructuredSelection){
+                       Object element = ((IStructuredSelection) selection).getFirstElement();
+                       if(element instanceof SpecimenOrObservationBase<?>){
+                           try {
+                               AbstractPostOperation<?> operation = new AddDerivedUnitFacadeMediaOperation(event.getCommand().getName(),
+                                       BulkEditorUtil.getUndoContext(), (SpecimenOrObservationBase<?>)element, postOperationEnabled);
+                               AbstractUtility.executeOperation(operation);
+                           } catch (NotDefinedException e) {
+                               MessagingUtils.warn(getClass(), "Command name not set.");
+                           }
+                       }
+                   }
+               }
                return null;
        }