2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.operations
;
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
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
23 import eu
.etaxonomy
.taxeditor
.UiUtil
;
24 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
27 * Delete a taxon from the model.
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.
39 public class DeleteTaxonOperation
extends AbstractEditorOperation
{
41 @SuppressWarnings("unused")
42 private static Logger logger
= Logger
.getLogger(DeleteTaxonOperation
.class);
45 * The taxonomical parent of the taxon to be deleted.
47 private Taxon parentTaxon
;
49 public DeleteTaxonOperation(String text
, IUndoContext undoContext
,
51 super(text
, undoContext
, taxon
);
53 parentTaxon
= taxon
.getTaxonomicParent();
57 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
60 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
61 throws ExecutionException
{
63 // TODO move this logic to another place
66 String taxonName
= taxon
.getName().getTitleCache();
68 // Prompt user for confirmation
69 if(! MessageDialog
.openConfirm(UiUtil
.getShell(), "Confirm Deletion", "Are you sure you want to delete taxon '" + taxonName
+ "'?")){
71 return Status
.CANCEL_STATUS
;
74 // Get and start progress monitor
75 monitor
.beginTask("Deleting taxon '" + taxonName
+ "'.", 10);
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.");
88 return Status
.CANCEL_STATUS
;
93 // Close taxon's editor, if any is active
95 UiUtil
.closeEditor(taxon
, true);
96 } catch (PartInitException e
) {
101 // Delete taxon from CDM layer
102 CdmSessionDataRepository
.getDefault().removeTaxon(taxon
);
109 // Close the progress monitor
112 // Announce taxon was deleted on status line
113 UiUtil
.setStatusLine("Deleted taxon '" + taxonName
+ "'.");
115 return Status
.OK_STATUS
;
119 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
122 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
123 throws ExecutionException
{
124 return execute(monitor
, info
);
128 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
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
;