1 package eu
.etaxonomy
.taxeditor
.editor
.view
.uses
.handler
;
7 import org
.eclipse
.core
.commands
.AbstractHandler
;
8 import org
.eclipse
.core
.commands
.ExecutionEvent
;
9 import org
.eclipse
.core
.commands
.ExecutionException
;
10 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
11 import org
.eclipse
.jface
.viewers
.ISelection
;
12 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
13 import org
.eclipse
.jface
.viewers
.ITreeSelection
;
14 import org
.eclipse
.jface
.viewers
.TreePath
;
15 import org
.eclipse
.swt
.widgets
.Event
;
16 import org
.eclipse
.ui
.IEditorInput
;
17 import org
.eclipse
.ui
.IEditorPart
;
18 import org
.eclipse
.ui
.IWorkbenchPart
;
19 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
20 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
22 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
23 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
24 import eu
.etaxonomy
.cdm
.model
.description
.CategoricalData
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
26 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
27 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
31 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
32 import eu
.etaxonomy
.taxeditor
.editor
.view
.uses
.operation
.CreateUseRecordOperation
;
33 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
34 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
35 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 public class CreateUseRecordHandler
extends AbstractHandler
{
38 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
39 IWorkbenchPart part
= HandlerUtil
.getActivePart(event
);
40 IPostOperationEnabled postOperationEnabled
= (part
instanceof IPostOperationEnabled
) ?
(IPostOperationEnabled
) part
43 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
44 if (editor
instanceof FormEditor
) {
45 editor
= ((FormEditor
) editor
).getActiveEditor();
47 IEditorInput input
= editor
.getEditorInput();
48 if (input
instanceof TaxonEditorInput
) {
49 Taxon taxon
= ((TaxonEditorInput
) input
).getTaxon();
51 TaxonDescription description
= null;
53 ISelection selection
= HandlerUtil
.getActiveMenuSelection(event
);
54 if (selection
instanceof ITreeSelection
) {
55 TreePath
[] paths
= ((ITreeSelection
) selection
).getPaths();
56 Object firstSegment
= paths
[0].getFirstSegment();
57 if (firstSegment
instanceof TaxonDescription
) {
58 description
= (TaxonDescription
) firstSegment
;
60 }else if (selection
instanceof IStructuredSelection
) {
61 Object selectedElement
= ((IStructuredSelection
) selection
)
63 if (selectedElement
instanceof TaxonDescription
){
64 description
= (TaxonDescription
) selectedElement
;
68 if (description
!= null) {
69 AbstractPostOperation operation
= null;
71 //Use Record Feature retrieval below
72 //Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
73 //Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("247606f2-5f24-421f-92ca-dad2351b171e"));
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
);
82 EditorUtil
.executeOperation(operation
);
83 } catch (NotDefinedException e
) {
84 EditorUtil
.warn(getClass(), "Command name not set");
87 EditorUtil
.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));