Project

General

Profile

Download (5.97 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import org.eclipse.core.commands.AbstractHandler;
16
import org.eclipse.core.commands.ExecutionEvent;
17
import org.eclipse.core.commands.ExecutionException;
18
import org.eclipse.core.commands.common.NotDefinedException;
19
import org.eclipse.core.commands.operations.IUndoContext;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.TreePath;
22
import org.eclipse.jface.viewers.TreeSelection;
23
import org.eclipse.ui.IWorkbenchPart;
24
import org.eclipse.ui.handlers.HandlerUtil;
25

    
26
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
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.l10n.Messages;
34
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
35
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
36
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
37
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
38
import eu.etaxonomy.taxeditor.editor.view.media.operation.RemoveImageFromDescriptionElementOperation;
39
import eu.etaxonomy.taxeditor.model.AbstractUtility;
40
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
41
import eu.etaxonomy.taxeditor.model.MessagingUtils;
42
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
43
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
44
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
45
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
46

    
47
/**
48
 * <p>DeleteDescriptionHandler class.</p>
49
 *
50
 * @author n.hoffmann
51
 * @created Jun 22, 2010
52
 * @version 1.0
53
 */
54
public class DeleteHandler extends AbstractHandler {
55

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

    
64
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
65
		IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
66
		ICdmEntitySessionEnabled cdmEntitySessionEnabled = (part instanceof ICdmEntitySessionEnabled) ? (ICdmEntitySessionEnabled) part : null;
67

    
68
		try {
69
			String label = event.getCommand().getName();
70

    
71
			IUndoContext undoContext = EditorUtil.getUndoContext();
72

    
73
			List<AbstractPostOperation<?>> operations = new ArrayList<AbstractPostOperation<?>>();
74

    
75
			for(Object object : selection.toArray()){
76

    
77
				// TaxonDescription
78
				if(object instanceof TaxonDescription){
79
					operations.add(new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) object, postOperationEnabled, cdmEntitySessionEnabled));
80
				}
81
				else if(object instanceof SpecimenDescription){
82
				    operations.add(new DeleteSpecimenDescriptionOperation(label, undoContext, (SpecimenDescription) object, postOperationEnabled, cdmEntitySessionEnabled)) ;
83
				}
84
				// DescriptionElementBase
85
				else if(object instanceof DescriptionElementBase){
86
					operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) object, postOperationEnabled, cdmEntitySessionEnabled));
87
				}
88
				else if(object instanceof FeatureNodeContainer){
89
					List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) object).getDescriptionElementsForEntireBranch();
90

    
91
					for(DescriptionElementBase description : descriptions){
92
						operations.add(new DeleteDescriptionElementOperation(label, undoContext, description, postOperationEnabled, cdmEntitySessionEnabled));
93
					}
94
				}
95
				// Media
96
				else if(object instanceof Media){
97
					TreeSelection treeSelection = (TreeSelection) selection;
98

    
99
					TreePath[] path = treeSelection.getPathsFor(object);
100

    
101
					DescriptionBase<?> imageGallery = (DescriptionBase<?>) path[0].getFirstSegment();
102

    
103
					// TODO use undo context specific to editor
104
                    MediaDeletionConfigurator config = new MediaDeletionConfigurator();
105
                    config.setDeleteFromDescription(true);
106
                    config.setOnlyRemoveFromGallery(false);
107

    
108
                    if (!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, HandlerUtil.getActiveShell(event), Messages.DeleteHandler_CONFIRM_DELETION,  Messages.DeleteHandler_CONFIRM_DELETION_MESSAGE)){
109
                        return null;
110
                    }
111

    
112

    
113

    
114
                    if (config.isOnlyRemoveFromGallery() || ((Media)object).getId() == 0){
115
                        operations.add(new RemoveImageFromDescriptionElementOperation(label, undoContext,  (Media) object, imageGallery, postOperationEnabled));
116
                    }else{
117
                        operations.add(new DeleteMediaOperation(label, undoContext, imageGallery, (Media) object, config, postOperationEnabled));
118
                    }
119

    
120

    
121

    
122
				}
123
				else{
124
					MessagingUtils.error(getClass(), Messages.DeleteHandler_INVALID_SELECTION, null);
125
				}
126
			}
127

    
128
			// execute all cumulated operations
129
			for(AbstractPostOperation<?> operation : operations){
130
				AbstractUtility.executeOperation(operation);
131
			}
132

    
133
		} catch (NotDefinedException e) {
134
			MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
135
		}
136

    
137

    
138
		return null;
139
	}
140
}
(3-3/8)