p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / cdm / SwapSynonymAndTaxonAction.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.actions.cdm;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.PartInitException;
18
19 import eu.etaxonomy.cdm.model.description.TaxonDescription;
20 import eu.etaxonomy.cdm.model.taxon.Synonym;
21 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
22 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
25 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
26 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
27 import eu.etaxonomy.taxeditor.controller.EditorController;
28 import eu.etaxonomy.taxeditor.controller.GlobalController;
29 import eu.etaxonomy.taxeditor.model.CdmUtil;
30
31 /**
32 * @author p.ciardelli
33 * @created 09.06.2008
34 * @version 1.0
35 */
36 public class SwapSynonymAndTaxonAction extends Action {
37 private static final Logger logger = Logger
38 .getLogger(SwapSynonymAndTaxonAction.class);
39
40 private static String text = "Make synonym accepted name for this taxon";
41 private static ImageDescriptor image = TaxEditorPlugin.getDefault()
42 .getImageDescriptor(ITaxEditorConstants.SYNONYM_TO_TAXON_ICON);
43
44 private Synonym synonym;
45 private Taxon taxon;
46
47 public SwapSynonymAndTaxonAction(){
48 super(text, image);
49 }
50
51 public SwapSynonymAndTaxonAction(Taxon taxon, Synonym synonym) {
52 this();
53 this.synonym = synonym;
54 this.taxon = taxon;
55 }
56
57 @Deprecated
58 public void run() {
59
60 // TODO: Call a taxon service method similar to makeTaxonSynonym()
61 // instead of implementing the logic here
62
63 try {
64 IEditorPart oldEditor = EditorController.getEditorByTaxon(taxon);
65 if (oldEditor.isDirty()) {
66 if (!MessageDialog.openConfirm(GlobalController.getShell(), "Save before proceeding",
67 "All changes must be saved before proceeding with this action.\n\n" +
68 "Press \"OK\" to save and continue, or \"Cancel\" to cancel this action.")) {
69 return;
70 }
71 oldEditor.doSave(null);
72 }
73 } catch (PartInitException e) {
74 e.printStackTrace();
75 MessageDialog.openError(GlobalController.getShell(), "Error", "Error swapping synonym and taxon");
76 return;
77 }
78
79 taxon.removeSynonym(synonym);
80
81 Taxon parentTaxon = taxon.getTaxonomicParent();
82 Taxon newTaxon = Taxon.NewInstance(synonym.getName(), taxon.getSec());
83 newTaxon.setTaxonomicParent(parentTaxon, null, null);
84
85 Synonym newSynonym = Synonym.NewInstance(taxon.getName(), taxon.getSec());
86
87 if (CdmUtil.isNameHomotypic(newSynonym.getName(), newTaxon)) {
88 newTaxon.addSynonym(newSynonym, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
89 } else {
90 newTaxon.addSynonym(newSynonym, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF());
91 }
92
93 // Move the other synonyms from old accepted taxon to new accepted taxon
94 for (Synonym synonym: taxon.getSynonyms()) {
95 SynonymRelationshipType synRelType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
96 if (CdmUtil.isNameHomotypic(synonym.getName(), newTaxon) != true) {
97 synRelType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
98 }
99 newTaxon.addSynonym(synonym, synRelType);
100 taxon.removeSynonym(synonym);
101 }
102
103 // Move misapplied names
104 for (Taxon misappliedNameTaxon: taxon.getMisappliedNames()) {
105 newTaxon.addMisappliedName(misappliedNameTaxon,
106 misappliedNameTaxon.getName().getCitation(),
107 ""); //TODO: Set the microcitation
108 taxon.removeTaxon(misappliedNameTaxon, TaxonRelationshipType.MISAPPLIED_NAME_FOR());
109 }
110
111 // Move descriptions
112 for(TaxonDescription taxDescription : taxon.getDescriptions()){
113 newTaxon.addDescription(taxDescription);
114 taxon.removeDescription(taxDescription);
115 }
116
117 firePropertyChange(ITaxEditorConstants.TAXON, null, newTaxon);
118 }
119 }