AT: committing latest changes to the Tax Editor after a first round of Code review
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / uses / handler / CreateUseRecordHandler.java
1 package eu.etaxonomy.taxeditor.editor.view.uses.handler;
2
3
4 import java.util.UUID;
5
6 import org.eclipse.core.commands.AbstractHandler;
7 import org.eclipse.core.commands.ExecutionEvent;
8 import org.eclipse.core.commands.ExecutionException;
9 import org.eclipse.core.commands.common.NotDefinedException;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.IStructuredSelection;
12 import org.eclipse.jface.viewers.ITreeSelection;
13 import org.eclipse.jface.viewers.TreePath;
14 import org.eclipse.ui.IEditorInput;
15 import org.eclipse.ui.IEditorPart;
16 import org.eclipse.ui.IWorkbenchPart;
17 import org.eclipse.ui.forms.editor.FormEditor;
18 import org.eclipse.ui.handlers.HandlerUtil;
19
20 import eu.etaxonomy.cdm.api.service.ITermService;
21 import eu.etaxonomy.cdm.model.description.Feature;
22 import eu.etaxonomy.cdm.model.description.TaxonDescription;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.taxeditor.editor.EditorUtil;
25 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
26 import eu.etaxonomy.taxeditor.editor.view.uses.operation.CreateUseRecordOperation;
27 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
28 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
30
31 /**
32 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
33 *
34 * @author a.theys
35 * @created mar 13, 2012
36 * @version 1.0
37 */
38 public class CreateUseRecordHandler extends AbstractHandler {
39 public Object execute(ExecutionEvent event) throws ExecutionException {
40 IWorkbenchPart part = HandlerUtil.getActivePart(event);
41 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
42 : null;
43
44 IEditorPart editor = HandlerUtil.getActiveEditor(event);
45 if (editor instanceof FormEditor) {
46 editor = ((FormEditor) editor).getActiveEditor();
47 }
48 IEditorInput input = editor.getEditorInput();
49 if (input instanceof TaxonEditorInput) {
50 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
51
52 TaxonDescription description = null;
53
54 ISelection selection = HandlerUtil.getActiveMenuSelection(event);
55 if (selection instanceof ITreeSelection) {
56 TreePath[] paths = ((ITreeSelection) selection).getPaths();
57 Object firstSegment = paths[0].getFirstSegment();
58 if (firstSegment instanceof TaxonDescription) {
59 description = (TaxonDescription) firstSegment;
60 }
61 }else if (selection instanceof IStructuredSelection) {
62 Object selectedElement = ((IStructuredSelection) selection)
63 .getFirstElement();
64 if (selectedElement instanceof TaxonDescription){
65 description = (TaxonDescription) selectedElement;
66 }
67 }
68
69 if (description != null) {
70 AbstractPostOperation operation = null;
71 try {
72 //Use Record Feature retrieval below
73 Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
74 //The code below retrieves the feature "Uses" as a work around
75 //Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("e5374d39-b210-47c7-bec1-bee05b5f1cb6"));
76 feature.setSupportsCategoricalData(true);
77 operation = new CreateUseRecordOperation(event
78 .getCommand().getName(),
79 EditorUtil.getUndoContext(), taxon,
80 description, feature, postOperationEnabled);
81
82 EditorUtil.executeOperation(operation);
83 } catch (NotDefinedException e) {
84 EditorUtil.warn(getClass(), "Command name not set");
85 }
86 } else {
87 EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
88 return null;
89 }
90 }
91 return null;
92
93 }
94 }