1f429451b3cd0f2842b1b4e4bc9f1f408f245f3d
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / handler / DeleteDerivateHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.view.derivate.handler;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.common.NotDefinedException;
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.TreeNode;
20 import org.eclipse.ui.IWorkbenchPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22
23 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
25 import eu.etaxonomy.cdm.api.service.config.SpecimenDeleteConfigurator;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.molecular.Sequence;
28 import eu.etaxonomy.cdm.model.molecular.SingleRead;
29 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
30 import eu.etaxonomy.taxeditor.editor.EditorUtil;
31 import eu.etaxonomy.taxeditor.editor.view.derivate.operation.DeleteDerivateOperation;
32 import eu.etaxonomy.taxeditor.model.AbstractUtility;
33 import eu.etaxonomy.taxeditor.model.MessagingUtils;
34 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
35 import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
36
37 /**
38 *
39 * @author pplitzner
40 * @date Oct 21, 2014
41 *
42 */
43 public class DeleteDerivateHandler extends AbstractHandler {
44
45 /* (non-Javadoc)
46 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
47 */
48 /** {@inheritDoc} */
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
52
53 IWorkbenchPart part = HandlerUtil.getActivePart(event);
54 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
55
56 try {
57 String label = event.getCommand().getName();
58
59 IUndoContext undoContext = EditorUtil.getUndoContext();
60
61 if(selection.size()>0){
62 Object object = selection.iterator().next();
63
64 if(object instanceof TreeNode){
65 Object value = ((TreeNode) object).getValue();
66 if(value instanceof SpecimenOrObservationBase<?> || value instanceof Sequence || value instanceof SingleRead){
67 if(postOperationEnabled instanceof IConversationEnabled){
68 ConversationHolder conversationHolder = ((IConversationEnabled) postOperationEnabled).getConversationHolder();
69 label += " "+DerivateLabelProvider.getDerivateText(value, conversationHolder);
70 }
71 SpecimenDeleteConfigurator config = new SpecimenDeleteConfigurator();
72 boolean deepDelete = event.getCommand().getId().equals("eu.etaxonomy.taxeditor.editor.derivate.deepDelete");
73 config.setDeleteChildren(deepDelete);
74 config.setDeleteMolecularData(deepDelete);
75 DeleteDerivateOperation deleteDerivateOperation = new DeleteDerivateOperation(label, undoContext, (CdmBase) value, (TreeNode) object, postOperationEnabled, config);
76 AbstractUtility.executeOperation(deleteDerivateOperation);
77 }
78 }
79 else{
80 MessagingUtils.error(getClass(), "Selection is not valid for this delete handler", null);
81 }
82 }
83
84 } catch (NotDefinedException e) {
85 MessagingUtils.warn(getClass(), "Command name not set.");
86 }
87 return null;
88 }
89 }