3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.handler
;
13 import org
.apache
.log4j
.Logger
;
14 import org
.eclipse
.core
.commands
.AbstractHandler
;
15 import org
.eclipse
.core
.commands
.ExecutionEvent
;
16 import org
.eclipse
.core
.commands
.ExecutionException
;
17 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
18 import org
.eclipse
.jface
.viewers
.ISelection
;
19 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
20 import org
.eclipse
.jface
.viewers
.ITreeSelection
;
21 import org
.eclipse
.jface
.viewers
.TreePath
;
22 import org
.eclipse
.swt
.widgets
.Event
;
23 import org
.eclipse
.ui
.IEditorInput
;
24 import org
.eclipse
.ui
.IEditorPart
;
25 import org
.eclipse
.ui
.IWorkbenchPart
;
26 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
27 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
30 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
31 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
32 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
33 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
34 import eu
.etaxonomy
.taxeditor
.operations
.AbstractPostOperation
;
35 import eu
.etaxonomy
.taxeditor
.operations
.CreateDescriptionElementOperation
;
36 import eu
.etaxonomy
.taxeditor
.operations
.IPostOperationEnabled
;
39 * <p>CreateDescriptionElementHandler class.</p>
45 public class CreateDescriptionElementHandler
extends AbstractHandler
{
51 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
55 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
56 IWorkbenchPart part
= HandlerUtil
.getActivePart(event
);
57 IPostOperationEnabled postOperationEnabled
= (part
instanceof IPostOperationEnabled
) ?
(IPostOperationEnabled
) part
60 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
61 if (editor
instanceof FormEditor
) {
62 editor
= ((FormEditor
) editor
).getActiveEditor();
64 IEditorInput input
= editor
.getEditorInput();
65 if (input
instanceof TaxonEditorInput
) {
66 Taxon taxon
= ((TaxonEditorInput
) input
).getTaxon();
68 TaxonDescription description
= null;
70 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
71 if (selection
instanceof ITreeSelection
) {
72 TreePath
[] paths
= ((ITreeSelection
) selection
).getPaths();
73 Object firstSegment
= paths
[0].getFirstSegment();
74 if (firstSegment
instanceof TaxonDescription
) {
75 description
= (TaxonDescription
) firstSegment
;
77 }else if (selection
instanceof IStructuredSelection
) {
78 Object selectedElement
= ((IStructuredSelection
) selection
)
80 if (selectedElement
instanceof TaxonDescription
){
81 description
= (TaxonDescription
) selectedElement
;
85 if (description
!= null) {
87 Feature feature
= (Feature
) ((Event
) event
.getTrigger()).data
;
89 AbstractPostOperation operation
= null;
91 // TODO use undo context specific to editor
92 operation
= new CreateDescriptionElementOperation(event
93 .getCommand().getName(),
94 EditorUtil
.getUndoContext(), taxon
,
95 description
, feature
, postOperationEnabled
);
96 EditorUtil
.executeOperation(operation
);
97 } catch (NotDefinedException e
) {
98 EditorUtil
.warn(getClass(), "Command name not set");
101 EditorUtil
.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));