Fixes #1412
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiontree / handler / DeleteHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.descriptiontree.handler;
12
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;
26
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;
37
38 /**
39 * @author n.hoffmann
40 * @created Jan 19, 2010
41 * @version 1.0
42 */
43 public class DeleteHandler extends AbstractHandler {
44 private static final Logger logger = Logger.getLogger(DeleteHandler.class);
45
46 /* (non-Javadoc)
47 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
48 */
49 public Object execute(ExecutionEvent event) throws ExecutionException {
50 IEditorPart editor = HandlerUtil.getActiveEditor(event);
51
52 if (editor instanceof FormEditor) {
53 editor = ((FormEditor) editor).getActiveEditor();
54 }
55
56 IEditorInput input = editor.getEditorInput();
57
58 if (input instanceof TaxonEditorInput) {
59 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
60
61 ISelection selection = EditorUtil.getCurrentSelection();
62
63 Object deleteElement = ((TreeSelection) selection).getFirstElement();
64
65 IUndoableOperation operation = null;
66 String commandName = null;
67
68 try {
69 commandName = event.getCommand().getName();
70 } catch (NotDefinedException e) {
71 logger.error(e);
72 throw new RuntimeException(e);
73 }
74
75 // TODO use undo context specific to editor
76 if (deleteElement instanceof DescriptionElementBase) {
77 DescriptionElementBase descriptionElement = (DescriptionElementBase) deleteElement;
78
79 operation = new DeleteDescriptionElementOperation(commandName,
80 EditorUtil.getUndoContext(), taxon, descriptionElement, (IPostOperationEnabled) editor);
81 }
82 else if (deleteElement instanceof ImageFile) {
83 ImageFile imageFile = (ImageFile) deleteElement;
84 TreePath[] paths = ((TreeSelection) selection).getPaths();
85 TaxonDescription description = (TaxonDescription) (paths[0]).getFirstSegment();
86
87 operation = new DeleteImageOperation(commandName,
88 EditorUtil.getUndoContext(), taxon, description, imageFile, (IPostOperationEnabled) editor);
89 }
90 else if(deleteElement instanceof TaxonDescription){
91
92 TaxonDescription description = (TaxonDescription) deleteElement;
93
94 operation = new DeleteTaxonDescriptionOperation(commandName,
95 EditorUtil.getUndoContext(), taxon, description, (IPostOperationEnabled) editor);
96 EditorUtil.executeOperation(operation);
97 }
98
99 EditorUtil.executeOperation(operation);
100
101 }
102 return null;
103 }
104 }