(no commit message)
[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 import java.util.List;
4 import java.util.Set;
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.swt.widgets.Event;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.IWorkbenchPart;
18 import org.eclipse.ui.forms.editor.FormEditor;
19 import org.eclipse.ui.handlers.HandlerUtil;
20
21 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22 import eu.etaxonomy.cdm.model.description.CategoricalData;
23 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
24 import eu.etaxonomy.cdm.model.description.Feature;
25 import eu.etaxonomy.cdm.model.description.TaxonDescription;
26 import eu.etaxonomy.cdm.model.description.TextData;
27 import eu.etaxonomy.cdm.model.taxon.Taxon;
28 import eu.etaxonomy.taxeditor.editor.EditorUtil;
29 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
30 import eu.etaxonomy.taxeditor.editor.view.uses.operation.CreateUseRecordOperation;
31 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 public class CreateUseRecordHandler extends AbstractHandler {
36 public Object execute(ExecutionEvent event) throws ExecutionException {
37 IWorkbenchPart part = HandlerUtil.getActivePart(event);
38 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
39 : null;
40
41 IEditorPart editor = HandlerUtil.getActiveEditor(event);
42 if (editor instanceof FormEditor) {
43 editor = ((FormEditor) editor).getActiveEditor();
44 }
45 IEditorInput input = editor.getEditorInput();
46 if (input instanceof TaxonEditorInput) {
47 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
48
49 TaxonDescription description = null;
50
51 ISelection selection = HandlerUtil.getActiveMenuSelection(event);
52 if (selection instanceof ITreeSelection) {
53 TreePath[] paths = ((ITreeSelection) selection).getPaths();
54 Object firstSegment = paths[0].getFirstSegment();
55 if (firstSegment instanceof TaxonDescription) {
56 description = (TaxonDescription) firstSegment;
57 }
58 }else if (selection instanceof IStructuredSelection) {
59 Object selectedElement = ((IStructuredSelection) selection)
60 .getFirstElement();
61 if (selectedElement instanceof TaxonDescription){
62 description = (TaxonDescription) selectedElement;
63 }
64 }
65
66 if (description != null) {
67 //CategoricalData element = CategoricalData.NewInstance();
68 AbstractPostOperation operation = null;
69 try {
70 List<Feature> features = CdmStore.getTermManager().getPreferredTerms(Feature.class);
71 //TextData element = TextData.NewInstance();
72 /*TextData feature = TextData.NewInstance();
73 *Feature feature = (Feature) HibernateProxyHelper.deproxy(feature);*/
74 for (Feature feature : features) {
75 if(feature.getTitleCache().equals("Use Record")) {
76 feature.setSupportsCategoricalData(true);
77 //CategoricalData element = CategoricalData.NewInstance();
78 //element.setFeature(feature);
79 /*operation = new CreateUseRecordOperation(event
80 .getCommand().getName(),
81 EditorUtil.getUndoContext(), taxon,
82 description, feature, postOperationEnabled);*/
83 operation = new CreateUseRecordOperation(event
84 .getCommand().getName(),
85 EditorUtil.getUndoContext(), taxon,
86 description, feature, postOperationEnabled);
87
88 }
89 }
90 // TODO use undo context specific to editor
91
92 EditorUtil.executeOperation(operation);
93 } catch (NotDefinedException e) {
94 EditorUtil.warn(getClass(), "Command name not set");
95 }
96 } else {
97 EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
98 return null;
99 }
100 }
101 return null;
102
103 }
104 }