Project

General

Profile

Download (4.51 KB) Statistics
| Branch: | Tag: | Revision:
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.SpecimenDescription;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.media.Media;
32
import eu.etaxonomy.taxeditor.editor.EditorUtil;
33
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
34
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
35
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
36
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
37
import eu.etaxonomy.taxeditor.model.AbstractUtility;
38
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
39
import eu.etaxonomy.taxeditor.model.MessagingUtils;
40
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
41
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
42

    
43
/**
44
 * <p>DeleteDescriptionHandler class.</p>
45
 *
46
 * @author n.hoffmann
47
 * @created Jun 22, 2010
48
 * @version 1.0
49
 */
50
public class DeleteHandler extends AbstractHandler {
51

    
52
	/* (non-Javadoc)
53
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
54
	 */
55
	/** {@inheritDoc} */
56
	@Override
57
    public Object execute(ExecutionEvent event) throws ExecutionException {
58
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
59

    
60
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
61
		IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
62

    
63
		try {
64
			String label = event.getCommand().getName();
65

    
66
			IUndoContext undoContext = EditorUtil.getUndoContext();
67

    
68
			List<AbstractPostOperation<?>> operations = new ArrayList<AbstractPostOperation<?>>();
69

    
70
			for(Object object : selection.toArray()){
71

    
72
				// TaxonDescription
73
				if(object instanceof TaxonDescription){
74
					operations.add(new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) object, postOperationEnabled));
75
				}
76
				if(object instanceof SpecimenDescription){
77
				    operations.add(new DeleteSpecimenDescriptionOperation(label, undoContext, (SpecimenDescription) object, postOperationEnabled));
78
				}
79
				// DescriptionElementBase
80
				else if(object instanceof DescriptionElementBase){
81
					operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) object, postOperationEnabled));
82
				}
83
				else if(object instanceof FeatureNodeContainer){
84
					List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) object).getDescriptionElementsForEntireBranch();
85

    
86
					for(DescriptionElementBase description : descriptions){
87
						operations.add(new DeleteDescriptionElementOperation(label, undoContext, description, postOperationEnabled));
88
					}
89
				}
90
				// Media
91
				else if(object instanceof Media){
92
					TreeSelection treeSelection = (TreeSelection) selection;
93

    
94
					TreePath[] path = treeSelection.getPathsFor(object);
95

    
96
					DescriptionBase<?> imageGallery = (DescriptionBase<?>) path[0].getFirstSegment();
97

    
98
					operations.add(new DeleteMediaOperation(label, undoContext, imageGallery, (Media) object, postOperationEnabled));
99
				}
100
				else{
101
					MessagingUtils.error(getClass(), "Selection is not valid for this delete handler", null);
102
				}
103
			}
104

    
105
			// execute all cumulated operations
106
			for(AbstractPostOperation<?> operation : operations){
107
				AbstractUtility.executeOperation(operation);
108
			}
109

    
110
		} catch (NotDefinedException e) {
111
			MessagingUtils.warn(getClass(), "Command name not set.");
112
		}
113

    
114

    
115
		return null;
116
	}
117
}
(3-3/8)