merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / CreateDescriptionElementHandler.java
index 618d9c0ee3a6870466e65438279199ff9e014609..d8b1bf94b9f529c23c2bbc1ae8f0cfbebcfdb9e5 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
  * Copyright (C) 2007 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.
  */
@@ -19,18 +19,14 @@ import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.ITreeSelection;
 import org.eclipse.jface.viewers.TreePath;
 import org.eclipse.swt.widgets.Event;
-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.DescriptionBase;
 import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.description.TaxonDescription;
-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.CreateDescriptionElementOperation;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 
@@ -45,61 +41,52 @@ public class CreateDescriptionElementHandler extends AbstractHandler {
 
        /*
         * (non-Javadoc)
-        * 
+        *
         * @see
         * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
         * ExecutionEvent)
         */
-       /** {@inheritDoc} */
-       public Object execute(ExecutionEvent event) throws ExecutionException {
-               IWorkbenchPart part = HandlerUtil.getActivePart(event);
-               IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
-                               : null;
+    /** {@inheritDoc} */
+    @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();
-               if (input instanceof TaxonEditorInput) {
-                       Taxon taxon = ((TaxonEditorInput) input).getTaxon();
 
-                       TaxonDescription description = null;
+        DescriptionBase<?> description = null;
 
-                       ISelection selection = HandlerUtil.getCurrentSelection(event);
-                       if (selection instanceof ITreeSelection) {
-                               TreePath[] paths = ((ITreeSelection) selection).getPaths();
-                               Object firstSegment = paths[0].getFirstSegment();
-                               if (firstSegment instanceof TaxonDescription) {
-                                       description = (TaxonDescription) firstSegment;
-                               }
-                       }else if (selection instanceof IStructuredSelection) {
-                               Object selectedElement = ((IStructuredSelection) selection)
-                                               .getFirstElement();
-                               if (selectedElement instanceof TaxonDescription){
-                                       description = (TaxonDescription) selectedElement;
-                               }
-                       } 
+        ISelection selection = HandlerUtil.getCurrentSelection(event);
+        Object selectedElement = null;
+        if (selection instanceof ITreeSelection) {
+            TreePath[] paths = ((ITreeSelection) selection).getPaths();
+            selectedElement = paths[0].getFirstSegment();
+        }
+        else if (selection instanceof IStructuredSelection) {
+            selectedElement = ((IStructuredSelection) selection).getFirstElement();
+        }
+        if (selectedElement instanceof DescriptionBase<?>) {
+            description = (DescriptionBase<?>) selectedElement;
+        }
 
-                       if (description != null) {
-                               AbstractPostOperation operation = null;
-                               try {
-                                       // TODO use undo context specific to editor
-                                       operation = operationCreationInstance(event.getCommand().getName(), event, taxon, description, postOperationEnabled);
-                                       EditorUtil.executeOperation(operation);
-                               } catch (NotDefinedException e) {
-                                       EditorUtil.warn(getClass(), "Command name not set");
-                               }
-                       } else {
-                               EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
-                               return null;
-                       }
-               }
-               return null;
+        if (description != null) {
+            AbstractPostOperation operation = null;
+            try {
+                // TODO use undo context specific to editor
+                operation = operationCreationInstance(event.getCommand().getName(), event, description, postOperationEnabled);
+                AbstractUtility.executeOperation(operation);
+            } catch (NotDefinedException e) {
+                AbstractUtility.warn(getClass(), "Command name not set");
+            }
+        } else {
+            AbstractUtility.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
+            return null;
+        }
+        return null;
 
-       }
-       
-       /**
+    }
+
+    /**
         * Added to make the Class more generic and limit the amount of code changes required
         * @param label
         * @param event
@@ -108,11 +95,9 @@ public class CreateDescriptionElementHandler extends AbstractHandler {
         * @param postOperationEnabled
         * @return
         */
-       protected AbstractPostOperation operationCreationInstance(String label, ExecutionEvent event, Taxon taxon, TaxonDescription description, IPostOperationEnabled postOperationEnabled) {
+       protected AbstractPostOperation operationCreationInstance(String label, ExecutionEvent event, DescriptionBase<?> description, IPostOperationEnabled postOperationEnabled) {
                Feature feature = (Feature) ((Event) event.getTrigger()).data;
-               return new CreateDescriptionElementOperation(label,
-                                       EditorUtil.getUndoContext(), taxon,
-                                       description, feature, postOperationEnabled);
+               return new CreateDescriptionElementOperation(label, EditorUtil.getUndoContext(), description, feature, postOperationEnabled);
        }
 
 }