- close Derivate Editor if all elements are deleted
[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.AbstractUtility;
26 import eu.etaxonomy.taxeditor.model.MessagingUtils;
27 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
28 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
29 import eu.etaxonomy.taxeditor.store.CdmStore;
30
31 /**
32 *
33 * @author pplitzner
34 * @date Oct 21, 2014
35 *
36 */
37 public class DeleteDerivateOperation extends AbstractPostOperation<CdmBase> {
38
39 private final SpecimenDeleteConfigurator deleteConfigurator;
40
41 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element,
42 IPostOperationEnabled postOperationEnabled) {
43 this(label, undoContext, element, postOperationEnabled, new SpecimenDeleteConfigurator());
44 }
45
46 public DeleteDerivateOperation(String label, IUndoContext undoContext, CdmBase element,
47 IPostOperationEnabled postOperationEnabled, SpecimenDeleteConfigurator config) {
48 super(label, undoContext, element, postOperationEnabled);
49 this.deleteConfigurator = config;
50 }
51
52
53 /*
54 * (non-Javadoc)
55 *
56 * @see
57 * org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse
58 * .core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
59 */
60 /** {@inheritDoc} */
61 @Override
62 public IStatus execute(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
63 if(getPostOperationEnabled() instanceof ISaveablePart){
64 String confirmationQuestion = "Do you really want to delete the selected element";
65 if(deleteConfigurator.isDeleteChildren()){
66 confirmationQuestion += " and its children";
67 }
68 confirmationQuestion += "?";
69 if(!MessagingUtils.confirmDialog("Confirm deletion", confirmationQuestion)){
70 return Status.CANCEL_STATUS;
71 }
72 if(((ISaveablePart) getPostOperationEnabled()).isDirty()){
73 MessagingUtils.warningDialog("View has unsaved changes", this, "You need to save before performing this action");
74 return Status.CANCEL_STATUS;
75 }
76 }
77 DeleteResult deleteResult = CdmStore.getService(IOccurrenceService.class).deleteDerivateHierarchy(element, deleteConfigurator);
78 if(deleteResult.isOk()){
79 if(getPostOperationEnabled() instanceof DerivateView){
80 DerivateView derivateView = (DerivateView) getPostOperationEnabled();
81 //update DerivateView
82 derivateView.getConversationHolder().commit();
83 IStatus returnStatus = postExecute(null);
84 //close if no more items left
85 if(derivateView.getViewer().getTree().getItemCount()<1){
86 AbstractUtility.close(derivateView);
87 }
88 return returnStatus;
89 }
90 }
91 else{
92 MessagingUtils.warningDialog("Deletion failed", this, deleteResult.toString());
93 return Status.CANCEL_STATUS;
94 }
95 return Status.OK_STATUS;
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see
102 * org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse
103 * .core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
104 */
105 /** {@inheritDoc} */
106 @Override
107 public IStatus redo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
108 //no redo possible
109 return Status.CANCEL_STATUS ;
110 }
111
112 /*
113 * (non-Javadoc)
114 *
115 * @see
116 * org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse
117 * .core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
118 */
119 /** {@inheritDoc} */
120 @Override
121 public IStatus undo(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
122 //no undo possible
123 return Status.CANCEL_STATUS;
124 }
125 }