6aade5e9a990ce118069ba257f0584da16ead8e3
[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 if (editor instanceof FormEditor) {
52 editor = ((FormEditor) editor).getActiveEditor();
53 }
54 IEditorInput input = editor.getEditorInput();
55 if (input instanceof TaxonEditorInput) {
56 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
57 ISelection selection = HandlerUtil.getCurrentSelection(event);
58
59 Object deleteElement = ((TreeSelection) selection).getFirstElement();
60
61 IUndoableOperation operation = null;
62 try {
63
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){
76
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);
82 }
83
84 EditorUtil.executeOperation(operation);
85 } catch (NotDefinedException e) {
86 logger.warn("Command name not set");
87 }
88 }
89 return null;
90 }
91 }