X-Git-Url: https://dev.e-taxonomy.eu/gitweb/taxeditor.git/blobdiff_plain/9cb12847cbf7924475f214679c18f1530f0e1c54..e0a3a1c2922adc99fa1f53551698fdb48a5cd974:/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java diff --git a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java index 70de10d2d..6ccb5a00e 100644 --- a/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java +++ b/eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/derivate/handler/DeleteDerivateHandler.java @@ -1,4 +1,3 @@ -// $Id$ /** * Copyright (C) 2007 EDIT * European Distributed Institute of Taxonomy @@ -10,29 +9,35 @@ package eu.etaxonomy.taxeditor.editor.view.derivate.handler; -import org.eclipse.core.commands.AbstractHandler; -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.core.commands.common.NotDefinedException; -import org.eclipse.core.commands.operations.IUndoContext; -import org.eclipse.jface.viewers.IStructuredSelection; +import javax.inject.Named; + +import org.eclipse.core.commands.ParameterizedCommand; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.core.di.annotations.Optional; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; +import org.eclipse.e4.ui.services.IServiceConstants; import org.eclipse.jface.viewers.TreeNode; -import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.ui.handlers.HandlerUtil; -import eu.etaxonomy.cdm.api.conversation.ConversationHolder; -import eu.etaxonomy.cdm.api.conversation.IConversationEnabled; +import eu.etaxonomy.cdm.api.service.DeleteResult; +import eu.etaxonomy.cdm.api.service.IOccurrenceService; import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator; +import eu.etaxonomy.cdm.api.service.molecular.ISequenceService; import eu.etaxonomy.cdm.model.common.CdmBase; import eu.etaxonomy.cdm.model.molecular.Sequence; import eu.etaxonomy.cdm.model.molecular.SingleRead; import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase; -import eu.etaxonomy.taxeditor.editor.EditorUtil; -import eu.etaxonomy.taxeditor.editor.view.derivate.operation.DeleteDerivateOperation; -import eu.etaxonomy.taxeditor.model.AbstractUtility; +import eu.etaxonomy.taxeditor.editor.AppModelId; +import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin; +import eu.etaxonomy.taxeditor.editor.l10n.Messages; +import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView; +import eu.etaxonomy.taxeditor.model.DeleteResultMessagingUtils; import eu.etaxonomy.taxeditor.model.MessagingUtils; -import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled; -import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider; +import eu.etaxonomy.taxeditor.store.CdmStore; +import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog; /** * @@ -40,50 +45,92 @@ import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider; * @date Oct 21, 2014 * */ -public class DeleteDerivateHandler extends AbstractHandler { - - /* (non-Javadoc) - * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) - */ - /** {@inheritDoc} */ - @Override - public Object execute(ExecutionEvent event) throws ExecutionException { - IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection(); - - IWorkbenchPart part = HandlerUtil.getActivePart(event); - IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null; - - try { - String label = event.getCommand().getName(); - - IUndoContext undoContext = EditorUtil.getUndoContext(); - - if(selection.size()>0){ - Object object = selection.iterator().next(); - - if(object instanceof TreeNode){ - Object value = ((TreeNode) object).getValue(); - if(value instanceof SpecimenOrObservationBase || value instanceof Sequence || value instanceof SingleRead){ - if(postOperationEnabled instanceof IConversationEnabled){ - ConversationHolder conversationHolder = ((IConversationEnabled) postOperationEnabled).getConversationHolder(); - label += " "+DerivateLabelProvider.getDerivateText(value, conversationHolder); - } - SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator(); - boolean deepDelete = event.getCommand().getId().equals("eu.etaxonomy.taxeditor.editor.derivate.deepDelete"); - config.setDeleteChildren(deepDelete); - config.setDeleteMolecularData(deepDelete); - DeleteDerivateOperation deleteDerivateOperation = new DeleteDerivateOperation(label, undoContext, (CdmBase) value, postOperationEnabled, config); - AbstractUtility.executeOperation(deleteDerivateOperation); - } - } - else{ - MessagingUtils.error(getClass(), "Selection is not valid for this delete handler", null); - } +public class DeleteDerivateHandler { + + + private SpecimenDeleteConfigurator deleteConfigurator; + + @Execute + public Object execute(@Named(IServiceConstants.ACTIVE_PART) MPart part, @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode treeNode, ParameterizedCommand command) { + deleteConfigurator = new SpecimenDeleteConfigurator(); + if(command.getId().equals(AppModelId.COMMAND_EU_ETAXONOMY_TAXEDITOR_EDITOR_DERIVATE_DEEPDELETE)){ + deleteConfigurator.setDeleteChildren(true); + } + + DerivateView derivateView = (DerivateView) part.getObject(); + Object value = treeNode.getValue(); + IStatus allowStatus = allowOperations(derivateView, treeNode); + if(allowStatus.isOK()) { + DeleteResult deleteResult; + if(value instanceof SingleRead + && treeNode.getParent()!=null + && treeNode.getParent().getValue() instanceof Sequence){ + deleteResult = CdmStore.getService(ISequenceService.class).deleteSingleRead(((SingleRead)value).getUuid(), + ((Sequence) treeNode.getParent().getValue()).getUuid()); + } else if(value instanceof Sequence){ + deleteResult = CdmStore.getService(ISequenceService.class).delete(((Sequence) value).getUuid()); + } else { + deleteResult = CdmStore.getService(IOccurrenceService.class).delete(((CdmBase) value).getUuid(), deleteConfigurator); } - } catch (NotDefinedException e) { - MessagingUtils.warn(getClass(), "Command name not set."); + if (!deleteResult.isOk()) { + MessagingUtils.warningDialog(Messages.DeleteDerivateOperation_DELETE_FAILED, this, deleteResult.toString()); + + return Status.CANCEL_STATUS; + } + if(derivateView.postOperation(null)){ + derivateView.remove(treeNode); + + } + return Status.OK_STATUS; } return null; } + + public IStatus allowOperations(DerivateView derivateView, TreeNode treeNode) { + String confirmationQuestion = Messages.DeleteDerivateOperation_REALLY_DELETE; + if(deleteConfigurator.isDeleteChildren()){ + confirmationQuestion += Messages.DeleteDerivateOperation_AND_CHILDREN; + } + + if(derivateView.isDirty()){ + MessagingUtils.warningDialog(DerivateView.VIEW_HAS_UNSAVED_CHANGES, this, DerivateView.YOU_NEED_TO_SAVE_BEFORE_PERFORMING_THIS_ACTION); + return Status.CANCEL_STATUS; + } + confirmationQuestion += "?"; //$NON-NLS-1$ + if(!DeleteConfiguratorDialog.openConfirmWithConfigurator(deleteConfigurator, null, Messages.DeleteDerivateOperation_CONFIRM, confirmationQuestion)){ + return Status.CANCEL_STATUS; + } + DeleteResult deleteResult; + Object value = treeNode.getValue(); + if(value instanceof SpecimenOrObservationBase || value instanceof Sequence || value instanceof SingleRead){ + if (value instanceof Sequence || value instanceof SingleRead){ + deleteResult = CdmStore.getService(ISequenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator); + } else{ + deleteResult = CdmStore.getService(IOccurrenceService.class).isDeletable(((CdmBase)value).getUuid(), deleteConfigurator); + } + if (deleteResult.isOk() || deleteResult.getExceptions().isEmpty()){ return Status.OK_STATUS;} + else{ + if (!deleteResult.isOk()){ + DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateOperation_DELETE_FAILED, TaxeditorEditorPlugin.PLUGIN_ID); + } else { + if (!deleteResult.getExceptions().isEmpty()){ + DeleteResultMessagingUtils.messageDialogWithDetails(deleteResult, Messages.DeleteDerivateHandler_SUCCESSFULL_BUT_EXCEPTIONS, TaxeditorEditorPlugin.PLUGIN_ID); + } + } + + return Status.CANCEL_STATUS; + } + } + return Status.CANCEL_STATUS; + } + + @CanExecute + public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) TreeNode node, + MHandledMenuItem menuItem){ + boolean canExecute = node !=null; + menuItem.setVisible(canExecute); + return canExecute; + } + }