0e8324c4ebe724b7a61cbcf75b6fa7c622a41988
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / 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.view.descriptive.handler;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.common.NotDefinedException;
20 import org.eclipse.core.commands.operations.IUndoContext;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.TreePath;
23 import org.eclipse.jface.viewers.TreeSelection;
24 import org.eclipse.ui.IWorkbenchPart;
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.Media;
30 import eu.etaxonomy.taxeditor.editor.EditorUtil;
31 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
32 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
33 import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
34 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
35 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
36 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
37
38 /**
39 * <p>DeleteDescriptionHandler class.</p>
40 *
41 * @author n.hoffmann
42 * @created Jun 22, 2010
43 * @version 1.0
44 */
45 public class DeleteHandler extends AbstractHandler {
46
47 /* (non-Javadoc)
48 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 /** {@inheritDoc} */
51 public Object execute(ExecutionEvent event) throws ExecutionException {
52 IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveMenuSelection(event);
53
54 IWorkbenchPart part = HandlerUtil.getActivePart(event);
55 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
56
57 try {
58 String label = event.getCommand().getName();
59
60 IUndoContext undoContext = EditorUtil.getUndoContext();
61
62 List<AbstractPostOperation> operations = new ArrayList<AbstractPostOperation>();
63
64 for(Object object : selection.toArray()){
65
66 AbstractPostOperation operation = null;
67
68 // TaxonDescription
69 if(object instanceof TaxonDescription){
70 operations.add(new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) object, postOperationEnabled));
71 }
72 // DescriptionElementBase
73 else if(object instanceof DescriptionElementBase){
74 operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) object, postOperationEnabled));
75 }
76 else if(object instanceof FeatureNodeContainer){
77 List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) object).getDescriptionElementsForEntireBranch();
78
79 for(DescriptionElementBase description : descriptions){
80 operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) object, postOperationEnabled));
81 }
82 }
83 // Media
84 else if(object instanceof Media){
85 TreeSelection treeSelection = (TreeSelection) HandlerUtil.getActiveMenuSelection(event);
86
87 TreePath[] path = treeSelection.getPathsFor(object);
88
89 TaxonDescription imageGallery = (TaxonDescription) path[0].getFirstSegment();
90
91 operations.add(new DeleteMediaOperation(label, undoContext, imageGallery, (Media) object, postOperationEnabled));
92 }
93 else{
94 EditorUtil.error(getClass(), "Selection is not valid for this delete handler", null);
95 }
96 }
97
98 // execute all cumulated operations
99 for(AbstractPostOperation operation : operations){
100 EditorUtil.executeOperation(operation);
101 }
102
103 } catch (NotDefinedException e) {
104 EditorUtil.warn(getClass(), "Command name not set.");
105 }
106
107
108 return null;
109 }
110 }