Merge branch 'develop' into remoting-4.0
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / DeleteTaxonBaseHandler.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.name.handler;
11 import org.eclipse.core.commands.AbstractHandler;
12 import org.eclipse.core.commands.ExecutionEvent;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.IHandler;
15 import org.eclipse.core.commands.common.NotDefinedException;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.handlers.HandlerUtil;
20
21 import eu.etaxonomy.cdm.api.service.config.TaxonDeletionConfigurator;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23 import eu.etaxonomy.cdm.model.taxon.Synonym;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.taxeditor.editor.EditorUtil;
26 import eu.etaxonomy.taxeditor.editor.Page;
27 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
28 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteMisapplicationOperation;
29 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteSynonymOperation;
30 import eu.etaxonomy.taxeditor.editor.name.operation.DeleteTaxonOperation;
31 import eu.etaxonomy.taxeditor.model.AbstractUtility;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
34 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
35 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
36
37 /**
38 * <p>DeleteTaxonBaseHandler class.</p>
39 *
40 * @author n.hoffmann
41 * @created 21.04.2009
42 * @version 1.0
43 */
44 public class DeleteTaxonBaseHandler extends AbstractHandler implements IHandler, IPostOperationEnabled {
45
46 TaxonNameEditor editor;
47 /* (non-Javadoc)
48 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 /** {@inheritDoc} */
51 @Override
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53 editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
54
55 Object selectedElement = EditorUtil.getSelection(event).getFirstElement();
56
57 doExecute(event, editor, selectedElement);
58
59 return null;
60 }
61
62 /**
63 * @param editor
64 * @param selectedElement
65 * @param operation
66 * @param commandName
67 */
68 protected void doExecute(ExecutionEvent event, TaxonNameEditor editor, Object selectedElement) {
69 AbstractPostOperation operation = null;
70 String commandName = null;
71
72 try {
73 commandName = event.getCommand().getName();
74 } catch (NotDefinedException e) {
75 MessagingUtils.error(getClass(), e);
76 }
77
78 // synonym
79 if(selectedElement instanceof Synonym){
80 operation = new DeleteSynonymOperation(commandName, editor.getUndoContext(), editor.getTaxon(), (Synonym) selectedElement, editor);
81 }
82 // misapplication
83 else if(selectedElement instanceof Taxon && ((Taxon) selectedElement).isMisapplication()){
84 operation = new DeleteMisapplicationOperation(commandName, editor.getUndoContext(), editor
85 .getTaxon(), (Taxon) selectedElement, editor);
86 }
87 else if (selectedElement instanceof Taxon){
88 //TODO: how to get the actual classification
89 if(! MessageDialog.openConfirm(HandlerUtil.getActiveShell(event), "Confirm Deletion", "Are you sure you want to delete the selected taxon?")){
90 return ;
91 }
92
93 IWorkbenchPage activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
94
95 operation = new DeleteTaxonOperation(commandName,
96 editor.getUndoContext(),
97 (Taxon) selectedElement,
98 new TaxonDeletionConfigurator(),
99 null,
100 activePage,
101 this,
102 editor,
103 (ICdmEntitySessionEnabled)editor.getEditorInput());
104 //editor.dispose();
105
106 }
107 else {
108 throw new IllegalArgumentException("Element has to be Synonym, Misapplication or Concept");
109 }
110
111 AbstractUtility.executeOperation(operation);
112 }
113
114 @Override
115 public boolean postOperation(CdmBase objectAffectedByOperation) {
116 Display.getDefault().asyncExec(new Runnable(){
117
118 @Override
119 public void run() {
120 AbstractUtility.close(editor.getMultiPageTaxonEditor());
121 }
122 });
123 return true;
124 }
125
126 @Override
127 public boolean onComplete() {
128 // TODO Auto-generated method stub
129 return false;
130 }
131
132 }