Merge branch 'release/4.5.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / DeleteHandler.java
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.core.runtime.Status;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.viewers.TreePath;
24 import org.eclipse.jface.viewers.TreeSelection;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.handlers.HandlerUtil;
27
28 import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
29 import eu.etaxonomy.cdm.model.description.DescriptionBase;
30 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
32 import eu.etaxonomy.cdm.model.description.TaxonDescription;
33 import eu.etaxonomy.cdm.model.media.Media;
34 import eu.etaxonomy.taxeditor.editor.EditorUtil;
35 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
36 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
37 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
38 import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
39 import eu.etaxonomy.taxeditor.editor.view.media.operation.RemoveImageFromDescriptionElementOperation;
40 import eu.etaxonomy.taxeditor.model.AbstractUtility;
41 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
42 import eu.etaxonomy.taxeditor.model.MessagingUtils;
43 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
44 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
45 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
46 import eu.etaxonomy.taxeditor.ui.dialog.deleteConfigurator.DeleteConfiguratorDialog;
47
48 /**
49 * <p>DeleteDescriptionHandler class.</p>
50 *
51 * @author n.hoffmann
52 * @created Jun 22, 2010
53 * @version 1.0
54 */
55 public class DeleteHandler extends AbstractHandler {
56
57 /* (non-Javadoc)
58 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
59 */
60 /** {@inheritDoc} */
61 @Override
62 public Object execute(ExecutionEvent event) throws ExecutionException {
63 IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
64
65 IWorkbenchPart part = HandlerUtil.getActivePart(event);
66 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
67 ICdmEntitySessionEnabled cdmEntitySessionEnabled = (part instanceof ICdmEntitySessionEnabled) ? (ICdmEntitySessionEnabled) part : null;
68
69 try {
70 String label = event.getCommand().getName();
71
72 IUndoContext undoContext = EditorUtil.getUndoContext();
73
74 List<AbstractPostOperation<?>> operations = new ArrayList<AbstractPostOperation<?>>();
75
76 for(Object object : selection.toArray()){
77
78 // TaxonDescription
79 if(object instanceof TaxonDescription){
80 operations.add(new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) object, postOperationEnabled, cdmEntitySessionEnabled));
81 }
82 else if(object instanceof SpecimenDescription){
83 operations.add(new DeleteSpecimenDescriptionOperation(label, undoContext, (SpecimenDescription) object, postOperationEnabled, cdmEntitySessionEnabled)) ;
84 }
85 // DescriptionElementBase
86 else if(object instanceof DescriptionElementBase){
87 operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) object, postOperationEnabled, cdmEntitySessionEnabled));
88 }
89 else if(object instanceof FeatureNodeContainer){
90 List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) object).getDescriptionElementsForEntireBranch();
91
92 for(DescriptionElementBase description : descriptions){
93 operations.add(new DeleteDescriptionElementOperation(label, undoContext, description, postOperationEnabled, cdmEntitySessionEnabled));
94 }
95 }
96 // Media
97 else if(object instanceof Media){
98 TreeSelection treeSelection = (TreeSelection) selection;
99
100 TreePath[] path = treeSelection.getPathsFor(object);
101
102 DescriptionBase<?> imageGallery = (DescriptionBase<?>) path[0].getFirstSegment();
103
104 // TODO use undo context specific to editor
105 MediaDeletionConfigurator config = new MediaDeletionConfigurator();
106
107 DeleteConfiguratorDialog dialog;
108 dialog = new DeleteConfiguratorDialog(config, HandlerUtil.getActiveShell(event), "Confirm Deletion", null, "Do you really want to delete the media?", MessageDialog.WARNING, new String[] { "Delete", "Skip" }, 0);
109 int result_dialog= dialog.open();
110 if (result_dialog != Status.OK){
111 return null;
112 }
113
114
115
116 if (config.isOnlyRemoveFromGallery()){
117 operations.add(new RemoveImageFromDescriptionElementOperation(label, undoContext, (Media) object, imageGallery, postOperationEnabled));
118 }else{
119 operations.add(new DeleteMediaOperation(label, undoContext, imageGallery, (Media) object, config, postOperationEnabled));
120 }
121
122
123
124 }
125 else{
126 MessagingUtils.error(getClass(), "Selection is not valid for this delete handler", null);
127 }
128 }
129
130 // execute all cumulated operations
131 for(AbstractPostOperation<?> operation : operations){
132 AbstractUtility.executeOperation(operation);
133 }
134
135 } catch (NotDefinedException e) {
136 MessagingUtils.warn(getClass(), "Command name not set.");
137 }
138
139
140 return null;
141 }
142 }