refactoring actions in the treeviewer
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / operations / DeleteTaxonOperation.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.AbstractOperation;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.ui.PartInitException;
22
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.taxeditor.UiUtil;
25 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
26
27 /**
28 * @author n.hoffmann
29 * @created 16.01.2009
30 * @version 1.0
31 */
32 public class DeleteTaxonOperation extends AbstractEditorOperation {
33 private static Logger logger = Logger.getLogger(DeleteTaxonOperation.class);
34
35 private Taxon parentTaxon;
36 public DeleteTaxonOperation(String text, IUndoContext undoContext,
37 Taxon taxon) {
38 super(text, undoContext, taxon);
39
40 parentTaxon = taxon.getTaxonomicParent();
41 }
42
43 /* (non-Javadoc)
44 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
45 */
46 @Override
47 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
48 throws ExecutionException {
49
50 // TODO move this logic to another place
51
52 // Get taxon name
53 String taxonName = taxon.getName().getTitleCache();
54
55 // Prompt user for confirmation
56 if(! MessageDialog.openConfirm(UiUtil.getShell(), "Confirm Deletion", "Are you sure you want to delete taxon '" + taxonName + "'?")){
57 monitor.done();
58 return Status.CANCEL_STATUS;
59 }
60
61 // Get and start progress monitor
62 monitor.beginTask("Deleting taxon '" + taxonName + "'.", 10);
63
64 // Call save all
65 UiUtil.saveAll();
66 monitor.worked(2);
67
68 // If the taxon has children, cancel operation
69 // TODO add option to continue, and delete children
70 if (taxon.hasTaxonomicChildren()) {
71 MessageDialog.openInformation(UiUtil.getShell(), "Cannot delete taxon",
72 "'" + taxonName + "' has taxonomic children. " +
73 "These must be manually deleted before their parent.");
74 monitor.done();
75 return Status.CANCEL_STATUS;
76 }
77 monitor.worked(1);
78
79
80 // Close taxon's editor, if any is active
81 try {
82 UiUtil.closeEditor(taxon, true);
83 } catch (PartInitException e) {
84 e.printStackTrace();
85 }
86 monitor.worked(1);
87
88 // Delete taxon from CDM layer
89 CdmSessionDataRepository.getDefault().removeTaxon(taxon);
90 monitor.worked(4);
91
92 // Call save all
93 UiUtil.saveAll();
94 monitor.worked(2);
95
96 // Close the progress monitor
97 monitor.done();
98
99 // Announce taxon was deleted on status line
100 UiUtil.setStatusLine("Deleted taxon '" + taxonName + "'.");
101
102 return Status.OK_STATUS;
103 }
104
105 /* (non-Javadoc)
106 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
107 */
108 @Override
109 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
110 throws ExecutionException {
111 return execute(monitor, info);
112 }
113
114 /* (non-Javadoc)
115 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
116 */
117 @Override
118 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
119 throws ExecutionException {
120 // FIXME we have to add old citation
121 parentTaxon.addTaxonomicChild(taxon, null, null);
122 CdmSessionDataRepository.getDefault().addTaxon(taxon);
123 return Status.OK_STATUS;
124 }
125 }