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