refactoring actions in the treeviewer
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / operations / name / MakeSynonymAcceptedOperation.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.name;
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.IEditorPart;
21 import org.eclipse.ui.PartInitException;
22
23 import eu.etaxonomy.cdm.api.service.ITaxonService;
24 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25 import eu.etaxonomy.cdm.model.taxon.Synonym;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
28 import eu.etaxonomy.taxeditor.UiUtil;
29 import eu.etaxonomy.taxeditor.actions.ui.OpenTaxonEditorAction;
30 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
31 import eu.etaxonomy.taxeditor.navigation.RecentNamesView;
32 import eu.etaxonomy.taxeditor.operations.AbstractEditorOperation;
33
34 /**
35 * @author p.ciardelli
36 * @created 15.01.2009
37 * @version 1.0
38 */
39 public class MakeSynonymAcceptedOperation extends AbstractEditorOperation {
40 private static final Logger logger = Logger
41 .getLogger(MakeSynonymAcceptedOperation.class);
42 private Synonym synonym;
43 private Taxon parentTaxon;
44
45 public MakeSynonymAcceptedOperation(String text, IUndoContext undoContext,
46 Taxon taxon, Synonym synonym) {
47 super(text, undoContext, taxon);
48
49 this.synonym = synonym;
50 }
51
52 /* (non-Javadoc)
53 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
54 */
55 @Override
56 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
57 throws ExecutionException {
58
59 // If taxon editor is dirty force user to save before proceeding, then close editor
60 try {
61 IEditorPart oldEditor = UiUtil.getEditorByTaxon(taxon);
62 if (oldEditor.isDirty()) {
63 if (!MessageDialog.openConfirm(UiUtil.getShell(), "Save before proceeding",
64 "All changes must be saved before proceeding with this action.\n\n" +
65 "Press \"OK\" to save and continue, or \"Cancel\" to cancel this action.")) {
66 return Status.CANCEL_STATUS;
67 }
68 oldEditor.doSave(null);
69 UiUtil.closeEditor(oldEditor, true);
70 }
71 } catch (PartInitException e) {
72 e.printStackTrace();
73 MessageDialog.openError(UiUtil.getShell(), "Error", "Error saving and closing taxon");
74 return Status.CANCEL_STATUS;
75 }
76
77 // Create new taxon for synonym to be swapped into
78 TaxonNameBase synonymName = synonym.getName();
79 Taxon newAcceptedTaxon = Taxon.NewInstance(synonymName, synonym.getSec());
80
81 // Get taxon service and call its swap method
82 ITaxonService taxonService = TaxEditorPlugin.getDefault().getApplicationController().getTaxonService();
83 // TODO prompt user for citation
84 // TODO bug, anahit. new accepted taxon is not removed from synonymy
85 taxonService.makeTaxonSynonym(taxon, newAcceptedTaxon, null, null, null);
86
87 // Remove old taxon from tree
88 CdmSessionDataRepository.getDefault().removeTaxon(taxon);
89
90 // Add new taxon to tree
91 CdmSessionDataRepository.getDefault().addTaxon(newAcceptedTaxon);
92
93 // Open editor for new accepted taxon
94 try {
95 UiUtil.openTaxonEditor(newAcceptedTaxon);
96 } catch (PartInitException e) {
97 e.printStackTrace();
98 }
99 // Remove old taxon from recent names list
100 RecentNamesView.addRecentName(taxon);
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 logger.warn(this.getLabel() + ": Redo not yet implemented.");
112 return null;
113 }
114
115 /* (non-Javadoc)
116 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
117 */
118 @Override
119 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
120 throws ExecutionException {
121 logger.warn(this.getLabel() + ": Undo not yet implemented.");
122 return null;
123 }
124 }