AT: committing latest changes to the Tax Editor to fix the issue with the description...
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / uses / handler / CreateUseSummaryHandler.java
diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/uses/handler/CreateUseSummaryHandler.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/uses/handler/CreateUseSummaryHandler.java
new file mode 100644 (file)
index 0000000..50cc81d
--- /dev/null
@@ -0,0 +1,94 @@
+package eu.etaxonomy.taxeditor.editor.view.uses.handler;\r
+\r
+\r
+import java.util.UUID;\r
+\r
+import org.eclipse.core.commands.AbstractHandler;\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.commands.common.NotDefinedException;\r
+import org.eclipse.jface.viewers.ISelection;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.viewers.ITreeSelection;\r
+import org.eclipse.jface.viewers.TreePath;\r
+import org.eclipse.ui.IEditorInput;\r
+import org.eclipse.ui.IEditorPart;\r
+import org.eclipse.ui.IWorkbenchPart;\r
+import org.eclipse.ui.forms.editor.FormEditor;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+\r
+import eu.etaxonomy.cdm.api.service.ITermService;\r
+import eu.etaxonomy.cdm.model.description.Feature;\r
+import eu.etaxonomy.cdm.model.description.TaxonDescription;\r
+import eu.etaxonomy.cdm.model.taxon.Taxon;\r
+import eu.etaxonomy.taxeditor.editor.EditorUtil;\r
+import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;\r
+import eu.etaxonomy.taxeditor.editor.view.uses.operation.CreateUseRecordOperation;\r
+import eu.etaxonomy.taxeditor.editor.view.uses.operation.CreateUseSummaryOperation;\r
+import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;\r
+import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;\r
+import eu.etaxonomy.taxeditor.store.CdmStore;\r
+\r
+/**\r
+ * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.\r
+ *\r
+ * @author a.theys     \r
+ * @created mar 13, 2012\r
+ * @version 1.0\r
+ */\r
+public class CreateUseSummaryHandler extends AbstractHandler {\r
+       public Object execute(ExecutionEvent event) throws ExecutionException {\r
+               IWorkbenchPart part = HandlerUtil.getActivePart(event);\r
+               IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part\r
+                               : null;\r
+\r
+               IEditorPart editor = HandlerUtil.getActiveEditor(event);\r
+               if (editor instanceof FormEditor) {\r
+                       editor = ((FormEditor) editor).getActiveEditor();\r
+               }\r
+               IEditorInput input = editor.getEditorInput();\r
+               if (input instanceof TaxonEditorInput) {\r
+                       Taxon taxon = ((TaxonEditorInput) input).getTaxon();\r
+\r
+                       TaxonDescription description = null;\r
+\r
+                       ISelection selection = HandlerUtil.getActiveMenuSelection(event);\r
+                       if (selection instanceof ITreeSelection) {\r
+                               TreePath[] paths = ((ITreeSelection) selection).getPaths();\r
+                               Object firstSegment = paths[0].getFirstSegment();\r
+                               if (firstSegment instanceof TaxonDescription) {\r
+                                       description = (TaxonDescription) firstSegment;\r
+                               }\r
+                       }else if (selection instanceof IStructuredSelection) {\r
+                               Object selectedElement = ((IStructuredSelection) selection)\r
+                                               .getFirstElement();\r
+                               if (selectedElement instanceof TaxonDescription){\r
+                                       description = (TaxonDescription) selectedElement;\r
+                               }\r
+                       } \r
+\r
+                       if (description != null) {\r
+                               AbstractPostOperation operation = null;\r
+                               try {\r
+                                       //Use Record Feature retrieval below\r
+                                       Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("6acb0348-c070-4512-a37c-67bcac016279"));\r
+                                       //feature.setSupportsCategoricalData(true);\r
+                                       feature.setSupportsTextData(true);\r
+                                       operation = new CreateUseSummaryOperation(event\r
+                                                       .getCommand().getName(),\r
+                                                       EditorUtil.getUndoContext(), taxon,\r
+                                                       description, feature, postOperationEnabled);\r
+                                       \r
+                                       EditorUtil.executeOperation(operation);\r
+                               } catch (NotDefinedException e) {\r
+                                       EditorUtil.warn(getClass(), "Command name not set");\r
+                               }\r
+                       } else {\r
+                               EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));\r
+                               return null;\r
+                       }\r
+               }\r
+               return null;\r
+\r
+       }\r
+}\r