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