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
.eclipse
.core
.commands
.AbstractHandler
;
14 import org
.eclipse
.core
.commands
.ExecutionEvent
;
15 import org
.eclipse
.core
.commands
.ExecutionException
;
16 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
17 import org
.eclipse
.jface
.viewers
.ISelection
;
18 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
19 import org
.eclipse
.jface
.viewers
.ITreeSelection
;
20 import org
.eclipse
.jface
.viewers
.TreePath
;
21 import org
.eclipse
.swt
.widgets
.Event
;
22 import org
.eclipse
.ui
.IEditorInput
;
23 import org
.eclipse
.ui
.IEditorPart
;
24 import org
.eclipse
.ui
.IWorkbenchPart
;
25 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
26 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
28 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
29 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
31 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
32 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
33 import eu
.etaxonomy
.taxeditor
.operations
.AbstractPostOperation
;
34 import eu
.etaxonomy
.taxeditor
.operations
.CreateDescriptionElementOperation
;
35 import eu
.etaxonomy
.taxeditor
.operations
.IPostOperationEnabled
;
38 * <p>CreateDescriptionElementHandler class.</p>
44 public class CreateDescriptionElementHandler
extends AbstractHandler
{
50 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
54 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
55 IWorkbenchPart part
= HandlerUtil
.getActivePart(event
);
56 IPostOperationEnabled postOperationEnabled
= (part
instanceof IPostOperationEnabled
) ?
(IPostOperationEnabled
) part
59 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
60 if (editor
instanceof FormEditor
) {
61 editor
= ((FormEditor
) editor
).getActiveEditor();
63 IEditorInput input
= editor
.getEditorInput();
64 if (input
instanceof TaxonEditorInput
) {
65 Taxon taxon
= ((TaxonEditorInput
) input
).getTaxon();
67 TaxonDescription description
= null;
69 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
70 if (selection
instanceof ITreeSelection
) {
71 TreePath
[] paths
= ((ITreeSelection
) selection
).getPaths();
72 Object firstSegment
= paths
[0].getFirstSegment();
73 if (firstSegment
instanceof TaxonDescription
) {
74 description
= (TaxonDescription
) firstSegment
;
76 }else if (selection
instanceof IStructuredSelection
) {
77 Object selectedElement
= ((IStructuredSelection
) selection
)
79 if (selectedElement
instanceof TaxonDescription
){
80 description
= (TaxonDescription
) selectedElement
;
84 if (description
!= null) {
86 Feature feature
= (Feature
) ((Event
) event
.getTrigger()).data
;
88 AbstractPostOperation operation
= null;
90 // TODO use undo context specific to editor
91 operation
= new CreateDescriptionElementOperation(event
92 .getCommand().getName(),
93 EditorUtil
.getUndoContext(), taxon
,
94 description
, feature
, postOperationEnabled
);
95 EditorUtil
.executeOperation(operation
);
96 } catch (NotDefinedException e
) {
97 EditorUtil
.warn(getClass(), "Command name not set");
100 EditorUtil
.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));