- enabled DescriptiveView (Factual Data) to show descriptions of SpecimenOrObservati...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / CreateDescriptionHandler.java
index 9542fb5cd364519d8bcba15453d26bc560a9c96e..4191fca4f22ccec45730fd3757bc33ddd532ac1d 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * Copyright (C) 2011 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -13,16 +13,21 @@ 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.forms.editor.FormEditor;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.model.description.SpecimenDescription;
+import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 
@@ -39,31 +44,44 @@ public class CreateDescriptionHandler 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);
                if (editor instanceof FormEditor) {
                        editor = ((FormEditor) editor).getActiveEditor();
                }
                IEditorInput input = editor.getEditorInput();
+               // taxon description
                if (input instanceof TaxonEditorInput) {
-                       Taxon taxon = ((TaxonEditorInput) input).getTaxon();    
+                       Taxon taxon = ((TaxonEditorInput) input).getTaxon();
                        AbstractPostOperation operation;
                        try {
-                               
+
                                operation = createOperationInstance(event.getCommand().getName(), taxon, postOperationEnabled);
-                               EditorUtil.executeOperation(operation);
+                               AbstractUtility.executeOperation(operation);
                        } catch (NotDefinedException e) {
-                               EditorUtil.warn(getClass(), "Command name not set.");
+                               AbstractUtility.warn(getClass(), "Command name not set.");
                        }
-               } 
+               }
+               // specimen description
+               else{
+                   //FIXME: no undo and redo supported. Should also use postOperation
+                   ISelection selection = editor.getSite().getSelectionProvider().getSelection();
+                   if(selection instanceof IStructuredSelection){
+                       Object selectedElement = ((IStructuredSelection) selection).getFirstElement();
+                       if(selectedElement instanceof SpecimenOrObservationBase<?>){
+                           SpecimenDescription description = SpecimenDescription.NewInstance((SpecimenOrObservationBase<?>) selectedElement);
+                           postOperationEnabled.postOperation(description);
+                       }
+                   }
+               }
                return null;
        }
-       
-       /**Comments for funtion createOperationInstance
+
+       /**
         * The function is used to make the specific object creation more generic
         * @param eventLabel
         * @param taxon
@@ -71,9 +89,9 @@ public class CreateDescriptionHandler extends AbstractHandler {
         * @return
         */
        protected AbstractPostOperation createOperationInstance(String eventLabel, Taxon taxon, IPostOperationEnabled postOperationEnabled) {
-               // TODO use undo context specific to editor     
+               // TODO use undo context specific to editor
                return new CreateTaxonDescriptionOperation(eventLabel,  EditorUtil.getUndoContext(), taxon, postOperationEnabled);
        }
-       
+
 
 }