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