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