- added static method to DerivateLabelProvider to get the derivate text
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / operation / DeleteDerivateOperation.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.derivate.operation;
11
12 import org.eclipse.core.commands.ExecutionException;
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.ui.ISaveablePart;
19
20 import eu.etaxonomy.cdm.api.service.DeleteResult;
21 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
22 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
25 import eu.etaxonomy.taxeditor.model.MessagingUtils;
26 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
27 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
28 import eu.etaxonomy.taxeditor.store.CdmStore;
29
30 /**
31 *
32 * @author pplitzner
33 * @date Oct 21, 2014
34 *
35 */
36 public class DeleteDerivateOperation extends AbstractPostOperation<CdmBase> {
37
38 private final SpecimenDeleteConfigurator deleteConfigurator;
39
40 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element,
41 IPostOperationEnabled postOperationEnabled) {
42 this(label, undoContext, element, postOperationEnabled, new SpecimenDeleteConfigurator());
43 }
44
45 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element,
46 IPostOperationEnabled postOperationEnabled, SpecimenDeleteConfigurator config) {
47 super(label, undoContext, element, postOperationEnabled);
48 this.deleteConfigurator = config;
49 }
50
51
52 /*
53 * (non-Javadoc)
54 *
55 * @see
56 * org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse
57 * .core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
58 */
59 /** {@inheritDoc} */
60 @Override
61 public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
62 if(getPostOperationEnabled() instanceof ISaveablePart){
63 if(!MessagingUtils.confirmDialog("Confirm deletion", "Do you really want to delete the selected element?")){
64 return Status.CANCEL_STATUS;
65 }
66 if(((ISaveablePart) getPostOperationEnabled()).isDirty()){
67 MessagingUtils.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
68 return Status.CANCEL_STATUS;
69 }
70 }
71 DeleteResult deleteResult = CdmStore.getService(IOccurrenceService.class).deleteDerivateHierarchy(element, deleteConfigurator);
72 if(deleteResult.isOk()){
73 if(getPostOperationEnabled() instanceof DerivateView){
74 DerivateView derivateView = (DerivateView) getPostOperationEnabled();
75 //update DerivateView
76 derivateView.getConversationHolder().commit();
77 return postExecute(null);
78 }
79 }
80 else{
81 String exceptionMessage = "";
82 for(Exception exception:deleteResult.getExceptions()){
83 exceptionMessage += exception.getLocalizedMessage();
84 }
85 //TODO: add method to DeleteResult to sum up exceptions
86 MessagingUtils.warningDialog("Deletion failed", this, exceptionMessage);
87 return Status.CANCEL_STATUS;
88 }
89 return Status.OK_STATUS;
90 }
91
92 /*
93 * (non-Javadoc)
94 *
95 * @see
96 * org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse
97 * .core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
98 */
99 /** {@inheritDoc} */
100 @Override
101 public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
102 //no redo possible
103 return Status.CANCEL_STATUS ;
104 }
105
106 /*
107 * (non-Javadoc)
108 *
109 * @see
110 * org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse
111 * .core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
112 */
113 /** {@inheritDoc} */
114 @Override
115 public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
116 //no undo possible
117 return Status.CANCEL_STATUS;
118 }
119 }