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
.descriptiontree
.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
.core
.commands
.operations
.IUndoableOperation
;
19 import org
.eclipse
.jface
.viewers
.ISelection
;
20 import org
.eclipse
.jface
.viewers
.TreePath
;
21 import org
.eclipse
.jface
.viewers
.TreeSelection
;
22 import org
.eclipse
.ui
.IEditorInput
;
23 import org
.eclipse
.ui
.IEditorPart
;
24 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
25 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
29 import eu
.etaxonomy
.cdm
.model
.media
.ImageFile
;
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
.DeleteDescriptionElementOperation
;
34 import eu
.etaxonomy
.taxeditor
.operations
.DeleteImageOperation
;
35 import eu
.etaxonomy
.taxeditor
.operations
.DeleteTaxonDescriptionOperation
;
36 import eu
.etaxonomy
.taxeditor
.operations
.IPostOperationEnabled
;
40 * @created Jan 19, 2010
43 public class DeleteHandler
extends AbstractHandler
{
44 private static final Logger logger
= Logger
.getLogger(DeleteHandler
.class);
47 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
50 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
51 if (editor
instanceof FormEditor
) {
52 editor
= ((FormEditor
) editor
).getActiveEditor();
54 IEditorInput input
= editor
.getEditorInput();
55 if (input
instanceof TaxonEditorInput
) {
56 Taxon taxon
= ((TaxonEditorInput
) input
).getTaxon();
57 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
59 Object deleteElement
= ((TreeSelection
) selection
).getFirstElement();
61 IUndoableOperation operation
= null;
64 if (deleteElement
instanceof DescriptionElementBase
) {
65 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) deleteElement
;
66 // TODO use undo context specific to editor
67 operation
= new DeleteDescriptionElementOperation(event
.getCommand().getName(),
68 EditorUtil
.getUndoContext(), taxon
, descriptionElement
, (IPostOperationEnabled
) editor
);
69 }else if (deleteElement
instanceof ImageFile
) {
70 ImageFile imageFile
= (ImageFile
) deleteElement
;
71 TreePath
[] paths
= ((TreeSelection
) selection
).getPaths();
72 TaxonDescription description
= (TaxonDescription
) (paths
[0]).getFirstSegment();
73 operation
= new DeleteImageOperation(event
.getCommand().getName(),
74 EditorUtil
.getUndoContext(), taxon
, description
, imageFile
, (IPostOperationEnabled
) editor
);
75 }else if(deleteElement
instanceof TaxonDescription
){
77 TaxonDescription description
= (TaxonDescription
) deleteElement
;
78 // TODO use undo context specific to editor
79 operation
= new DeleteTaxonDescriptionOperation(event
.getCommand().getName(),
80 EditorUtil
.getUndoContext(), taxon
, description
, (IPostOperationEnabled
) editor
);
81 EditorUtil
.executeOperation(operation
);
84 EditorUtil
.executeOperation(operation
);
85 } catch (NotDefinedException e
) {
86 logger
.warn("Command name not set");