Fix and refactor addImageGallerHandler
authorPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 27 Oct 2015 10:49:00 +0000 (11:49 +0100)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Tue, 27 Oct 2015 10:49:00 +0000 (11:49 +0100)
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/media/handler/AddImageGalleryHandler.java

index 4664282982638f122139ec308f039423d4e558e3..894440b0bbc64a74154b1d2734829c18456591e1 100644 (file)
@@ -7,28 +7,16 @@ 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.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.jface.viewers.TreeNode;
-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.hibernate.HibernateProxyHelper;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.handlers.HandlerUtil;
 
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
+import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
-import eu.etaxonomy.cdm.model.taxon.TaxonBase;
-import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
-import eu.etaxonomy.taxeditor.bulkeditor.input.OccurrenceEditorInput;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
-import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
-import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
-import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
-import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.AddDerivedUnitFacadeMediaOperation;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.AddDerivedUnitFacadeMediaOperation;
-import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateSpecimenDescriptionOperation;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
 import eu.etaxonomy.taxeditor.editor.view.media.MediaViewPart;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
 import eu.etaxonomy.taxeditor.editor.view.media.MediaViewPart;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
@@ -37,89 +25,44 @@ import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 
 /**
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 
 /**
- * <p>AddImageGalleryHandler class.</p>
- *
  * @author p.ciardelli
  * @version $Id: $
  */
 public class AddImageGalleryHandler extends AbstractHandler {
 
  * @author p.ciardelli
  * @version $Id: $
  */
 public class AddImageGalleryHandler extends AbstractHandler {
 
-       /* (non-Javadoc)
-        * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
-        */
        /** {@inheritDoc} */
        @Override
     public Object execute(ExecutionEvent event) throws ExecutionException {
 
        /** {@inheritDoc} */
        @Override
     public Object execute(ExecutionEvent event) throws ExecutionException {
 
-               IWorkbenchPart part = HandlerUtil.getActivePart(event);
-               IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
+           IWorkbenchPart part = HandlerUtil.getActivePart(event);
+           IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
 
 
+           if(part instanceof MediaViewPart){
+               Object viewerInput = ((MediaViewPart)part).getViewer().getInput();
+               try {
+                   if(viewerInput instanceof CdmBase){
+                       if(((CdmBase) viewerInput).isInstanceOf(Taxon.class)){
+                           Taxon taxon = HibernateProxyHelper.deproxy(viewerInput, Taxon.class);
 
 
-               IEditorPart editor = HandlerUtil.getActiveEditor(event);
-               IEditorInput input = editor.getEditorInput();
+                           AbstractPostOperation<?> operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(),
+                                   EditorUtil.getUndoContext(), taxon, postOperationEnabled, true);
 
 
-               if (input instanceof TaxonEditorInput) {
-                       Taxon taxon = ((TaxonEditorInput) input).getTaxon();
-                       AbstractPostOperation<?> operation;
-                       try {
-                               // TODO use undo context specific to editor
-                               operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(),
-                                               EditorUtil.getUndoContext(), taxon, postOperationEnabled, true);
-                               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();
-                   invokeOperation(event, postOperationEnabled, selection);
-               }
-               else if(input instanceof DerivateViewEditorInput){
-                   ISelection selection = ((DerivateView)editor).getSelection();
-                   invokeOperation(event, postOperationEnabled, selection);
-               } else if(part instanceof MediaViewPart){
-                   Object viewerInput = ((MediaViewPart)part).getViewer().getInput();
-                   
-                   if(viewerInput instanceof Taxon){
-                       Taxon taxon = HibernateProxyHelper.deproxy(viewerInput, Taxon.class);
-                       AbstractPostOperation<?> operation;
-                               try {
-                                       // TODO use undo context specific to editor
-                                       operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(),
-                                                       EditorUtil.getUndoContext(), taxon, postOperationEnabled, true);
-                                       AbstractUtility.executeOperation(operation);
-                               } catch (NotDefinedException e) {
-                                       MessagingUtils.warn(getClass(), "Command name not set.");
-                               }
-                       
-                   }
-               }
-               
-               return null;
-       }
+                           AbstractUtility.executeOperation(operation);
+                       }
+                       else if(((CdmBase) viewerInput).isInstanceOf(SpecimenOrObservationBase.class)){
+                           SpecimenOrObservationBase<?> specimen = HibernateProxyHelper.deproxy(viewerInput, SpecimenOrObservationBase.class);
 
 
-    /**
-     * @param event
-     * @param postOperationEnabled
-     * @param selection
-     */
-    private void invokeOperation(ExecutionEvent event, IPostOperationEnabled postOperationEnabled, ISelection selection) {
-        if(selection instanceof IStructuredSelection){
-            Object element = ((IStructuredSelection) selection).getFirstElement();
-            if(element instanceof TreeNode){
-                element = ((TreeNode) element).getValue();
-            }
-            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.");
-                }
-            }
-        }
-    }
+                        AbstractPostOperation<?> operation = new AddDerivedUnitFacadeMediaOperation(event.getCommand().getName(),
+                                   BulkEditorUtil.getUndoContext(), specimen, postOperationEnabled);
+
+                           AbstractUtility.executeOperation(operation);
+                       }
+                   }
+               } catch (NotDefinedException e) {
+                   MessagingUtils.warn(getClass(), "Command name not set.");
+               }
+           }
+           return null;
+       }
 
 
-}
+}
\ No newline at end of file