Revision 8eeb8146
Added by Patrick Plitzner almost 7 years ago
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
package eu.etaxonomy.taxeditor.editor.view.derivate.handler; |
11 | 11 |
|
12 |
import org.eclipse.core.commands.ExecutionEvent; |
|
13 |
import org.eclipse.core.commands.common.NotDefinedException; |
|
14 |
import org.eclipse.core.commands.operations.AbstractOperation; |
|
15 |
import org.eclipse.core.commands.operations.IUndoContext; |
|
12 |
import javax.inject.Named; |
|
13 |
|
|
14 |
import org.eclipse.core.commands.ParameterizedCommand; |
|
16 | 15 |
import org.eclipse.core.runtime.IStatus; |
17 | 16 |
import org.eclipse.core.runtime.Status; |
18 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
17 |
import org.eclipse.e4.core.di.annotations.CanExecute; |
|
18 |
import org.eclipse.e4.core.di.annotations.Execute; |
|
19 |
import org.eclipse.e4.core.di.annotations.Optional; |
|
20 |
import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
|
21 |
import org.eclipse.e4.ui.services.IServiceConstants; |
|
19 | 22 |
import org.eclipse.jface.viewers.TreeNode; |
20 |
import org.eclipse.ui.IEditorPart; |
|
21 |
import org.eclipse.ui.IWorkbenchPart; |
|
22 |
import org.eclipse.ui.handlers.HandlerUtil; |
|
23 | 23 |
|
24 |
import eu.etaxonomy.cdm.api.conversation.ConversationHolder; |
|
25 |
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled; |
|
26 | 24 |
import eu.etaxonomy.cdm.api.service.DeleteResult; |
27 | 25 |
import eu.etaxonomy.cdm.api.service.IOccurrenceService; |
28 | 26 |
import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator; |
... | ... | |
31 | 29 |
import eu.etaxonomy.cdm.model.molecular.Sequence; |
32 | 30 |
import eu.etaxonomy.cdm.model.molecular.SingleRead; |
33 | 31 |
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase; |
34 |
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
|
32 |
import eu.etaxonomy.taxeditor.editor.AppModelId;
|
|
35 | 33 |
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin; |
36 | 34 |
import eu.etaxonomy.taxeditor.editor.l10n.Messages; |
37 | 35 |
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView; |
38 |
import eu.etaxonomy.taxeditor.editor.view.derivate.operation.DeleteDerivateOperation; |
|
39 | 36 |
import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils; |
40 | 37 |
import eu.etaxonomy.taxeditor.model.MessagingUtils; |
41 |
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation; |
|
42 |
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled; |
|
43 |
import eu.etaxonomy.taxeditor.operation.RemotingCdmHandler; |
|
44 | 38 |
import eu.etaxonomy.taxeditor.store.CdmStore; |
45 | 39 |
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog; |
46 |
import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider; |
|
47 | 40 |
|
48 | 41 |
/** |
49 | 42 |
* |
... | ... | |
51 | 44 |
* @date Oct 21, 2014 |
52 | 45 |
* |
53 | 46 |
*/ |
54 |
public class DeleteDerivateHandler extends RemotingCdmHandler {
|
|
47 |
public class DeleteDerivateHandler { |
|
55 | 48 |
|
56 |
SpecimenDeleteConfigurator deleteConfigurator; |
|
57 |
IEditorPart editor; |
|
58 |
TreeNode node; |
|
59 | 49 |
|
60 |
/** |
|
61 |
* @param label |
|
62 |
*/ |
|
63 |
public DeleteDerivateHandler(String label) { |
|
64 |
super(label); |
|
50 |
private SpecimenDeleteConfigurator deleteConfigurator; |
|
65 | 51 |
|
66 |
} |
|
52 |
@Execute |
|
53 |
public Object execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode treeNode, ParameterizedCommand command) { |
|
54 |
deleteConfigurator = new SpecimenDeleteConfigurator(); |
|
55 |
if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){ |
|
56 |
deleteConfigurator.setDeleteChildren(true); |
|
57 |
} |
|
67 | 58 |
|
68 |
/** |
|
69 |
* @param label |
|
70 |
*/ |
|
71 |
public DeleteDerivateHandler() { |
|
72 |
super(Messages.DeleteDerivateHandler_LABEL); |
|
59 |
DerivateView derivateView = (DerivateView) part.getObject(); |
|
60 |
Object value = treeNode.getValue(); |
|
61 |
IStatus allowStatus = allowOperations(derivateView, treeNode); |
|
62 |
if(allowStatus.isOK()) { |
|
63 |
DeleteResult deleteResult; |
|
64 |
if(value instanceof SingleRead |
|
65 |
&& treeNode.getParent()!=null |
|
66 |
&& treeNode.getParent().getValue() instanceof Sequence){ |
|
67 |
deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)value).getUuid(), |
|
68 |
((Sequence) treeNode.getParent().getValue()).getUuid()); |
|
69 |
} else if(value instanceof Sequence){ |
|
70 |
deleteResult = CdmStore.getService(ISequenceService.class).delete(((Sequence) value).getUuid()); |
|
71 |
} else { |
|
72 |
deleteResult = CdmStore.getService(IOccurrenceService.class).delete(((CdmBase) value).getUuid(), deleteConfigurator); |
|
73 |
} |
|
73 | 74 |
|
74 |
} |
|
75 |
if (!deleteResult.isOk()) { |
|
76 |
MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString()); |
|
75 | 77 |
|
76 |
// /** {@inheritDoc} */ |
|
77 |
// @Override |
|
78 |
// public Object execute(ExecutionEvent event) throws ExecutionException { |
|
79 |
// IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection(); |
|
80 |
// |
|
81 |
// IWorkbenchPart part = HandlerUtil.getActivePart(event); |
|
82 |
// IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null; |
|
83 |
// |
|
84 |
// try { |
|
85 |
// String label = event.getCommand().getName(); |
|
86 |
// |
|
87 |
// IUndoContext undoContext = EditorUtil.getUndoContext(); |
|
88 |
// |
|
89 |
// if(selection.size()>0){ |
|
90 |
// Object object = selection.iterator().next(); |
|
91 |
// |
|
92 |
// if(object instanceof TreeNode){ |
|
93 |
// Object value = ((TreeNode) object).getValue(); |
|
94 |
// if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){ |
|
95 |
// if(postOperationEnabled instanceof IConversationEnabled){ |
|
96 |
// ConversationHolder conversationHolder = ((IConversationEnabled) postOperationEnabled).getConversationHolder(); |
|
97 |
// label += " "+DerivateLabelProvider.getDerivateText(value, conversationHolder); //$NON-NLS-1$ |
|
98 |
// } |
|
99 |
// SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator(); |
|
100 |
// boolean deepDelete = event.getCommand().getId().equals("eu.etaxonomy.taxeditor.editor.derivate.deepDelete"); //$NON-NLS-1$ |
|
101 |
// config.setDeleteChildren(deepDelete); |
|
102 |
// config.setDeleteMolecularData(deepDelete); |
|
103 |
// DeleteDerivateOperation deleteDerivateOperation = new DeleteDerivateOperation(label, undoContext, (CdmBase) value, (TreeNode) object, postOperationEnabled, config); |
|
104 |
// IStatus status = AbstractUtility.executeOperation(deleteDerivateOperation); |
|
105 |
// DerivateView derivateView = (DerivateView)postOperationEnabled; |
|
106 |
// if(derivateView.postOperation(null)){ |
|
107 |
// derivateView.remove(object); |
|
108 |
// |
|
109 |
// // derivateView.updateRootEntities(); |
|
110 |
// //update DerivateView |
|
111 |
// |
|
112 |
// |
|
113 |
// } |
|
114 |
// } |
|
115 |
// } |
|
116 |
// else{ |
|
117 |
// MessagingUtils.error(getClass(), Messages.DeleteDerivateHandler_INVALID_SELECTION, null); |
|
118 |
// } |
|
119 |
// } |
|
120 |
// |
|
121 |
// } catch (NotDefinedException e) { |
|
122 |
// MessagingUtils.warn(getClass(), "Command name not set"); //$NON-NLS-1$ |
|
123 |
// } |
|
124 |
// return null; |
|
125 |
// } |
|
126 |
|
|
127 |
/** |
|
128 |
* {@inheritDoc} |
|
129 |
*/ |
|
130 |
@Override |
|
131 |
public IStatus allowOperations(ExecutionEvent event) { |
|
132 |
//check whether object can be deleted |
|
133 |
editor = HandlerUtil.getActiveEditor(event); |
|
134 |
IStructuredSelection selection = (IStructuredSelection) HandlerUtil |
|
135 |
.getCurrentSelection(event); |
|
136 |
AbstractPostOperation operation; |
|
137 |
deleteConfigurator = new SpecimenDeleteConfigurator(); |
|
78 |
return Status.CANCEL_STATUS; |
|
79 |
} |
|
80 |
if(derivateView.postOperation(null)){ |
|
81 |
derivateView.remove(treeNode); |
|
138 | 82 |
|
83 |
} |
|
84 |
return Status.OK_STATUS; |
|
85 |
} |
|
86 |
return null; |
|
87 |
} |
|
88 |
|
|
89 |
public IStatus allowOperations(DerivateView derivateView, TreeNode treeNode) { |
|
139 | 90 |
String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE; |
140 | 91 |
if(deleteConfigurator.isDeleteChildren()){ |
141 | 92 |
confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN; |
142 | 93 |
} |
143 | 94 |
|
144 |
if(editor.isDirty()){
|
|
95 |
if(derivateView.isDirty()){
|
|
145 | 96 |
MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION); |
146 | 97 |
return Status.CANCEL_STATUS; |
147 | 98 |
} |
148 |
|
|
149 | 99 |
confirmationQuestion += "?"; //$NON-NLS-1$ |
150 | 100 |
if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){ |
151 | 101 |
return Status.CANCEL_STATUS; |
152 | 102 |
} |
153 |
Object object = selection.iterator().next(); |
|
154 | 103 |
DeleteResult deleteResult; |
155 |
if(object instanceof TreeNode){ |
|
156 |
Object value = ((TreeNode) object).getValue(); |
|
157 |
if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){ |
|
158 |
if (value instanceof Sequence || value instanceof SingleRead){ |
|
159 |
deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator); |
|
160 |
} else{ |
|
161 |
deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator); |
|
162 |
} |
|
163 |
if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;} |
|
164 |
else{ |
|
165 |
if (!deleteResult.isOk()){ |
|
166 |
DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID); |
|
167 |
} else { |
|
168 |
if (!deleteResult.getExceptions().isEmpty()){ |
|
169 |
DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID); |
|
170 |
} |
|
104 |
Object value = treeNode.getValue(); |
|
105 |
if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){ |
|
106 |
if (value instanceof Sequence || value instanceof SingleRead){ |
|
107 |
deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator); |
|
108 |
} else{ |
|
109 |
deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator); |
|
110 |
} |
|
111 |
if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;} |
|
112 |
else{ |
|
113 |
if (!deleteResult.isOk()){ |
|
114 |
DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID); |
|
115 |
} else { |
|
116 |
if (!deleteResult.getExceptions().isEmpty()){ |
|
117 |
DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID); |
|
171 | 118 |
} |
119 |
} |
|
172 | 120 |
|
173 |
return Status.CANCEL_STATUS; |
|
174 |
} |
|
121 |
return Status.CANCEL_STATUS; |
|
175 | 122 |
} |
176 |
|
|
177 | 123 |
} |
178 |
|
|
179 | 124 |
return Status.CANCEL_STATUS; |
180 |
|
|
181 | 125 |
} |
182 | 126 |
|
183 |
/** |
|
184 |
* {@inheritDoc} |
|
185 |
*/ |
|
186 |
@Override |
|
187 |
public AbstractOperation prepareOperation(ExecutionEvent event) { |
|
188 |
IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection(); |
|
189 |
|
|
190 |
IWorkbenchPart part = HandlerUtil.getActivePart(event); |
|
191 |
IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null; |
|
192 |
|
|
193 |
try { |
|
194 |
String label = event.getCommand().getName(); |
|
195 |
|
|
196 |
IUndoContext undoContext = EditorUtil.getUndoContext(); |
|
197 |
|
|
198 |
if(selection.size()>0){ |
|
199 |
Object object = selection.iterator().next(); |
|
200 |
|
|
201 |
if(object instanceof TreeNode){ |
|
202 |
node = (TreeNode)object; |
|
203 |
Object value = ((TreeNode) object).getValue(); |
|
204 |
if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){ |
|
205 |
if(postOperationEnabled instanceof IConversationEnabled){ |
|
206 |
ConversationHolder conversationHolder = ((IConversationEnabled) postOperationEnabled).getConversationHolder(); |
|
207 |
label += " "+DerivateLabelProvider.getDerivateText(value, conversationHolder); //$NON-NLS-1$ |
|
208 |
} |
|
209 |
|
|
210 |
|
|
211 |
DeleteDerivateOperation deleteDerivateOperation = new DeleteDerivateOperation(label, undoContext, (CdmBase) value, (TreeNode) object, postOperationEnabled, this.deleteConfigurator); |
|
212 |
return deleteDerivateOperation; |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
} catch (NotDefinedException e) { |
|
217 |
MessagingUtils.warn(getClass(), "Command name not set"); //$NON-NLS-1$ |
|
218 |
} |
|
219 |
return null; |
|
220 |
|
|
127 |
@CanExecute |
|
128 |
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node){ |
|
129 |
return node !=null; |
|
221 | 130 |
} |
222 | 131 |
|
223 |
/** |
|
224 |
* {@inheritDoc} |
|
225 |
*/ |
|
226 |
@Override |
|
227 |
public void onComplete() { |
|
228 |
|
|
229 |
IPostOperationEnabled postOperationEnabled = (editor instanceof IPostOperationEnabled) ? (IPostOperationEnabled) editor : null; |
|
230 |
if (postOperationEnabled != null){ |
|
231 |
DerivateView derivateView = (DerivateView)postOperationEnabled; |
|
232 |
if(derivateView.postOperation(null)){ |
|
233 |
derivateView.remove(node); |
|
234 |
|
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 | 132 |
} |
Also available in: Unified diff
ref #6566 Migrate "Delete" context menu items