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
);
52 if (editor
instanceof FormEditor
) {
53 editor
= ((FormEditor
) editor
).getActiveEditor();
56 IEditorInput input
= editor
.getEditorInput();
58 if (input
instanceof TaxonEditorInput
) {
59 Taxon taxon
= ((TaxonEditorInput
) input
).getTaxon();
61 ISelection selection
= EditorUtil
.getCurrentSelection();
63 Object deleteElement
= ((TreeSelection
) selection
).getFirstElement();
65 IUndoableOperation operation
= null;
66 String commandName
= null;
69 commandName
= event
.getCommand().getName();
70 } catch (NotDefinedException e
) {
72 throw new RuntimeException(e
);
75 // TODO use undo context specific to editor
76 if (deleteElement
instanceof DescriptionElementBase
) {
77 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) deleteElement
;
79 operation
= new DeleteDescriptionElementOperation(commandName
,
80 EditorUtil
.getUndoContext(), taxon
, descriptionElement
, (IPostOperationEnabled
) editor
);
82 else if (deleteElement
instanceof ImageFile
) {
83 ImageFile imageFile
= (ImageFile
) deleteElement
;
84 TreePath
[] paths
= ((TreeSelection
) selection
).getPaths();
85 TaxonDescription description
= (TaxonDescription
) (paths
[0]).getFirstSegment();
87 operation
= new DeleteImageOperation(commandName
,
88 EditorUtil
.getUndoContext(), taxon
, description
, imageFile
, (IPostOperationEnabled
) editor
);
90 else if(deleteElement
instanceof TaxonDescription
){
92 TaxonDescription description
= (TaxonDescription
) deleteElement
;
94 operation
= new DeleteTaxonDescriptionOperation(commandName
,
95 EditorUtil
.getUndoContext(), taxon
, description
, (IPostOperationEnabled
) editor
);
96 EditorUtil
.executeOperation(operation
);
99 EditorUtil
.executeOperation(operation
);