Moving editor sources back into trunk
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / images / handler / DeleteImageHandler.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.images.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.IHandler;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.core.commands.operations.IUndoableOperation;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.ui.handlers.HandlerUtil;
23
24 import eu.etaxonomy.cdm.model.description.TaxonDescription;
25 import eu.etaxonomy.cdm.model.media.ImageFile;
26 import eu.etaxonomy.taxeditor.editor.EditorUtil;
27 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
28 import eu.etaxonomy.taxeditor.editor.Page;
29 import eu.etaxonomy.taxeditor.editor.images.GalleryLabelComposite;
30 import eu.etaxonomy.taxeditor.editor.images.TaxonImageEditor;
31 import eu.etaxonomy.taxeditor.propertysheet.images.ImagePropertySource;
32 import eu.etaxonomy.taxeditor.store.operations.DeleteImageOperation;
33
34 /**
35 * @author n.hoffmann
36 * @created 27.04.2009
37 * @version 1.0
38 */
39 public class DeleteImageHandler extends AbstractHandler implements IHandler {
40 private static final Logger logger = Logger
41 .getLogger(DeleteImageHandler.class);
42
43 /* (non-Javadoc)
44 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
45 */
46 public Object execute(ExecutionEvent event) throws ExecutionException {
47 MultiPageTaxonEditor editor = EditorUtil.getActiveEditor();
48
49 TaxonImageEditor page = (TaxonImageEditor) editor.getPage(Page.IMAGE);
50
51 ISelection menuSelection = HandlerUtil.getActiveMenuSelection(event);
52
53 // FIXME right now we have only one description per taxon
54 // this will change in the future and then attaching the feature to
55 // the firstGroupedComposite will not work anymore
56 TaxonDescription description = ((GalleryLabelComposite) page.getFirstGroupedComposite()).getDescription();
57
58
59 ImageFile imageFile = ((ImagePropertySource) ((StructuredSelection) menuSelection).getFirstElement()).getImageFile();
60
61 IUndoableOperation operation;
62 try {
63 // we are not passing the description and thereby assume that all occurrences of the image
64 // should be deleted
65 operation = new DeleteImageOperation(event.getCommand().getName(),
66 editor.getUndoContext(), editor.getTaxon(), description, imageFile, editor);
67 EditorUtil.executeOperation(operation);
68 } catch (NotDefinedException e) {
69 logger.warn("Command name not set");
70 }
71
72 return null;
73 }
74 }