refactoring actions in the treeviewer
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / cdm / DeleteTaxonAction.java
index 3b8a251effe76c42bfbc8a33d200880448ce00a2..fabf90a7d1b286a7d03a41ba4e160f5f66df741d 100644 (file)
@@ -15,19 +15,20 @@ package eu.etaxonomy.taxeditor.actions.cdm;
  * @version 1.0\r
  */\r
 import org.apache.log4j.Logger;\r
+import org.eclipse.core.commands.ExecutionException;\r
+import org.eclipse.core.commands.operations.IOperationHistory;\r
+import org.eclipse.core.commands.operations.IUndoContext;\r
+import org.eclipse.core.commands.operations.IUndoableOperation;\r
 import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
 import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.dialogs.MessageDialog;\r
 import org.eclipse.jface.resource.ImageDescriptor;\r
-import org.eclipse.ui.PartInitException;\r
 \r
 import eu.etaxonomy.cdm.model.taxon.Taxon;\r
 import eu.etaxonomy.taxeditor.ITaxEditorConstants;\r
 import eu.etaxonomy.taxeditor.TaxEditorPlugin;\r
 import eu.etaxonomy.taxeditor.UiUtil;\r
-import eu.etaxonomy.taxeditor.actions.SaveAllAction;\r
-import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;\r
-import eu.etaxonomy.taxeditor.model.CdmUtil;\r
+import eu.etaxonomy.taxeditor.operations.DeleteTaxonOperation;\r
 \r
 /**\r
  * Delete this taxon from the CDM\r
@@ -43,11 +44,17 @@ public class DeleteTaxonAction extends Action {
        private ImageDescriptor image = TaxEditorPlugin.getDefault()\r
                        .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);\r
 \r
+       private IOperationHistory operationHistory;\r
+       private IUndoContext undoContext;\r
+       \r
        Taxon taxon;\r
 \r
        public DeleteTaxonAction() {\r
                super(text);\r
                setImageDescriptor(image);\r
+               \r
+               operationHistory = UiUtil.getOperationHistory();\r
+               undoContext = UiUtil.getWorkbenchUndoContext();\r
        }\r
 \r
        public DeleteTaxonAction(Taxon taxon) {\r
@@ -57,53 +64,69 @@ public class DeleteTaxonAction extends Action {
        }\r
 \r
        public void run() {\r
-               \r
-               // Get taxon name for progress monitor\r
-               String taxonName = CdmUtil.getDisplayName(taxon);\r
-               \r
-               // Get and start progress monitor\r
+               IUndoableOperation operation = new DeleteTaxonOperation\r
+                               (this.getText(), undoContext, taxon);\r
+\r
                IProgressMonitor monitor = UiUtil.getStatusLineProgressMonitor();\r
-               monitor.beginTask("Deleting taxon '" + taxonName + "'.", 10);\r
                \r
-               // Call save all action\r
-               new SaveAllAction().run(null);\r
-               monitor.worked(2);\r
-               \r
-               // If the taxon has children, cancel operation\r
-               // TODO add option to continue, and delete children\r
-               if (taxon.hasTaxonomicChildren()) {\r
-                       MessageDialog.openInformation(UiUtil.getShell(), "Cannot delete taxon", \r
-                                       "'" + CdmUtil.getDisplayName(taxon) + "' has taxonomic children. " +\r
-                                       "These must be manually deleted before their parent.");\r
-                       monitor.done();\r
-                       return;\r
-               }\r
-               monitor.worked(1);\r
-               \r
-               // Close taxon's editor, if any is active\r
                try {\r
-                       UiUtil.closeEditor(taxon, true);\r
-               } catch (PartInitException e) {\r
-                       e.printStackTrace();\r
-               }\r
-               monitor.worked(1);\r
-\r
-               // Delete taxon from CDM layer\r
-               CdmSessionDataRepository.getDefault().deleteTaxon(taxon);\r
-               monitor.worked(4);\r
-                               \r
-               // Call save all action\r
-               new SaveAllAction().run(null);\r
-               monitor.worked(2);\r
-               \r
-               // Close the progress monitor\r
-               monitor.done();\r
+                       IStatus status = operationHistory.execute(operation, monitor, null);\r
+               } catch (ExecutionException e) {\r
+                       logger.error("Error deleting taxon", e);\r
+               }       \r
                \r
-               // Announce taxon was deleted on status line\r
-               UiUtil.setStatusLine("Deleted taxon '" + taxonName + "'.");\r
+               // Handle this in an operation so we are able to undo it\r
                \r
-//             Status status = new Status(IStatus.INFO, TaxEditorPlugin.PLUGIN_ID, \r
-//                             "Deleted.", null);\r
-//             StatusManager.getManager().handle(status, StatusManager.SHOW);\r
+//             \r
+//             // Get taxon name for progress monitor\r
+//             String taxonName = CdmUtil.getDisplayName(taxon);\r
+//             \r
+//             // Get and start progress monitor\r
+//             IProgressMonitor monitor = UiUtil.getStatusLineProgressMonitor();\r
+//             monitor.beginTask("Deleting taxon '" + taxonName + "'.", 10);\r
+//             \r
+//             // Call save all action\r
+//             new SaveAllAction().run(null);\r
+//             monitor.worked(2);\r
+//             \r
+//             // If the taxon has children, cancel operation\r
+//             // TODO add option to continue, and delete children\r
+//             if (taxon.hasTaxonomicChildren()) {\r
+//                     MessageDialog.openInformation(UiUtil.getShell(), "Cannot delete taxon", \r
+//                                     "'" + CdmUtil.getDisplayName(taxon) + "' has taxonomic children. " +\r
+//                                     "These must be manually deleted before their parent.");\r
+//                     monitor.done();\r
+//                     return;\r
+//             }\r
+//             monitor.worked(1);\r
+//             \r
+//             \r
+//             // Close taxon's editor, if any is active\r
+//             try {\r
+//                     UiUtil.closeEditor(taxon, true);\r
+//             } catch (PartInitException e) {\r
+//                     e.printStackTrace();\r
+//             }\r
+//             monitor.worked(1);\r
+//\r
+//             // Delete taxon from CDM layer\r
+//             CdmSessionDataRepository.getDefault().deleteTaxon(taxon);\r
+//             monitor.worked(4);\r
+//                             \r
+//             \r
+//             \r
+//             // Call save all action\r
+//             new SaveAllAction().run(null);\r
+//             monitor.worked(2);\r
+//             \r
+//             // Close the progress monitor\r
+//             monitor.done();\r
+//             \r
+//             // Announce taxon was deleted on status line\r
+//             UiUtil.setStatusLine("Deleted taxon '" + taxonName + "'.");\r
+//             \r
+////           Status status = new Status(IStatus.INFO, TaxEditorPlugin.PLUGIN_ID, \r
+////                           "Deleted.", null);\r
+////           StatusManager.getManager().handle(status, StatusManager.SHOW);\r
        }\r
 }
\ No newline at end of file