fixes 777
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / operations / DeleteTaxonNodeOperation.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.operations;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.IUndoContext;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.swt.widgets.Shell;
21
22 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
23 import eu.etaxonomy.taxeditor.model.TaxonNodeUtil;
24
25 /**
26 * Delete a taxon node from its TaxonomicTree and thus from the current taxonomic view.
27 *
28 *
29 * @author n.hoffmann
30 * @created 16.01.2009
31 * @version 1.0
32 */
33 public class DeleteTaxonNodeOperation extends AbstractPostOperation {
34
35 @SuppressWarnings("unused")
36 private static Logger logger = Logger.getLogger(DeleteTaxonNodeOperation.class);
37
38 /**
39 * The taxonomical parent of the taxon to be deleted.
40 */
41 private TaxonNode parentTaxonNode;
42
43 public DeleteTaxonNodeOperation(String text, IUndoContext undoContext,
44 TaxonNode taxonNode) {
45 super(text, undoContext, taxonNode, null);
46
47 parentTaxonNode = taxonNode.getParent();
48 }
49
50 /* (non-Javadoc)
51 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
52 */
53 @Override
54 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
55 throws ExecutionException {
56
57 // Get taxon name - watch out for null names
58 String taxonName = "";
59 if (taxon.getName() != null) {
60 taxonName = taxon.getName().getTitleCache();
61 }
62
63 // Prompt user for confirmation
64 if(! MessageDialog.openConfirm((Shell) info.getAdapter(Shell.class), "Confirm Deletion", "Are you sure you want to delete taxon '" + taxonName + "' from this taxonomic view?")){
65 monitor.done();
66 return Status.CANCEL_STATUS;
67 }
68
69
70 // If the taxon node has child nodes, cancel operation
71 // TODO add option to continue, and delete children
72 if (taxonNode.getChildNodes().size() > 0) {
73 MessageDialog.openInformation((Shell) info.getAdapter(Shell.class), "Cannot delete taxon",
74 "'" + taxonName + "' has taxonomic children. " +
75 "These must be manually deleted before their parent.");
76 monitor.done();
77 return Status.CANCEL_STATUS;
78 }
79
80 // delete the taxon in an isolated conversation
81 TaxonNodeUtil.deleteTaxonNodeIsolated(taxonNode.getUuid());
82
83
84 return postExecute(null);
85 }
86
87 /* (non-Javadoc)
88 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
89 */
90 @Override
91 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
92 throws ExecutionException {
93 return execute(monitor, info);
94 }
95
96 /* (non-Javadoc)
97 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
98 */
99 @Override
100 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
101 throws ExecutionException {
102 // FIXME we have to add old citation
103 parentTaxonNode.addChild(taxon);
104 // FIXME readding has to take place in an isolated conversation as well
105 // so that all the mediation magic does the rest for us
106 return postExecute(null);
107 }
108 }