7940a81ff69a1122d4266c561ce9f3748c0eeaaf
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / ui / ChangeTaxonToSynonymAction.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.ui;
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.swt.widgets.Shell;
17 import org.eclipse.ui.IEditorPart;
18 import org.eclipse.ui.PartInitException;
19
20 import eu.etaxonomy.cdm.model.description.TaxonDescription;
21 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
22 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
25 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
26 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
27 import eu.etaxonomy.taxeditor.UiUtil;
28 import eu.etaxonomy.taxeditor.actions.cdm.SaveTaxonAction;
29 import eu.etaxonomy.taxeditor.editor.SelectTaxonDialog;
30 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
31 import eu.etaxonomy.taxeditor.model.CdmUtil;
32 import eu.etaxonomy.taxeditor.navigation.RecentNamesView;
33
34 /**
35 * @author p.ciardelli
36 * @created 26.05.2008
37 * @version 1.0
38 */
39 public class ChangeTaxonToSynonymAction extends Action {
40 private static final Logger logger = Logger
41 .getLogger(ChangeTaxonToSynonymAction.class);
42
43 private static String text = "Change this taxon to a synonym";
44 private ImageDescriptor image = TaxEditorPlugin.getDefault()
45 .getImageRegistry().getDescriptor(ITaxEditorConstants.TAXON_TO_SYNONYM_ICON);
46
47 private Taxon oldTaxon;
48
49 private ChangeTaxonToSynonymAction() {
50 super(text);
51 setImageDescriptor(image);
52 }
53
54 public ChangeTaxonToSynonymAction(Taxon oldTaxon) {
55 this();
56 this.oldTaxon = oldTaxon;
57 }
58
59 public void run() {
60
61 IEditorPart oldEditor = null;
62
63 try {
64 // Prompt user "Would you like to save?" before showing dialog
65 // "Cancel" cancels action - "No" does not
66 oldEditor = UiUtil.getEditorByTaxon(oldTaxon);
67 if (oldEditor.isDirty()) {
68 if (!MessageDialog.openConfirm(UiUtil.getShell(), "Save before proceeding",
69 "All changes must be saved before proceeding with this action.\n\n" +
70 "Press \"OK\" to save and continue, or \"Cancel\" to cancel this action.")) {
71 return;
72 }
73 oldEditor.doSave(null);
74 }
75 // if (UiUtil.getActivePage().saveEditor(oldEditor, true) == false) {
76 // return;
77 // }
78 } catch (PartInitException e1) {
79 e1.printStackTrace();
80 }
81
82 // Get destination taxon from dialog
83 Shell shell = UiUtil.getShell();
84 SelectTaxonDialog dialog = new SelectTaxonDialog(shell, SelectTaxonDialog.TAXON_TO_SYNONYM);
85 Taxon destinationTaxon = dialog.open(oldTaxon);
86
87 // Abort action if user cancelled dialog without choosing a taxon
88 if (destinationTaxon == null) {
89 return;
90 }
91
92 // Move oldTaxon in CDM
93 TaxonNameBase synonymName = oldTaxon.getName();
94 SynonymRelationshipType synonymType;
95 if (CdmUtil.isNameHomotypic(synonymName, destinationTaxon)) {
96 // makeTaxonSynonym
97 // oldTaxon.makeSynonym();
98 // destinationTaxon.addHomotypicSynonymName(synonymName);
99 synonymType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
100 } else {
101 // destinationTaxon.addHeterotypicSynonymName(synonymName);
102 synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
103 }
104 CdmUtil.makeTaxonSynonym(oldTaxon, destinationTaxon, synonymType,
105 null, null);
106
107 // TODO move any children, descriptions, synonyms
108 for (TaxonDescription description: oldTaxon.getDescriptions()) {
109 destinationTaxon.addDescription(description);
110 oldTaxon.removeDescription(description);
111 }
112
113 for (TaxonRelationship fromRelation : oldTaxon.getRelationsFromThisTaxon()) {
114 fromRelation.setFromTaxon(destinationTaxon);
115 }
116
117 for (TaxonRelationship toRelation : oldTaxon.getRelationsToThisTaxon()) {
118 toRelation.setToTaxon(destinationTaxon);
119 }
120
121 // if (!TaxEditorPlugin.getDefault().getObservableRecentNamesList().
122 // contains(oldTaxon)) {
123 // TaxEditorPlugin.getDefault().getObservableRecentNamesList().
124 // remove(oldTaxon);
125 // RecentNamesView.removeRecentName(oldTaxon);
126 // }
127
128 CdmSessionDataRepository.getDefault().removeTaxon(oldTaxon);
129
130 // Close open editor for oldTaxon without forcing save.
131 // User has already saved or declined to do so above.
132 try {
133 UiUtil.closeEditor(oldEditor, false);
134 } catch (PartInitException e) {
135 e.printStackTrace();
136 }
137
138 // Open editor for new oldTaxon
139 new SaveTaxonAction(destinationTaxon).run();
140 new OpenTaxonEditorAction(destinationTaxon).run();
141 }
142 }