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
.actions
.ui
;
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
;
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
;
39 public class ChangeTaxonToSynonymAction
extends Action
{
40 private static final Logger logger
= Logger
41 .getLogger(ChangeTaxonToSynonymAction
.class);
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
);
47 private Taxon oldTaxon
;
49 private ChangeTaxonToSynonymAction() {
51 setImageDescriptor(image
);
54 public ChangeTaxonToSynonymAction(Taxon oldTaxon
) {
56 this.oldTaxon
= oldTaxon
;
61 IEditorPart oldEditor
= null;
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.")) {
73 oldEditor
.doSave(null);
75 // if (UiUtil.getActivePage().saveEditor(oldEditor, true) == false) {
78 } catch (PartInitException e1
) {
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
);
87 // Abort action if user cancelled dialog without choosing a taxon
88 if (destinationTaxon
== null) {
92 // Move oldTaxon in CDM
93 TaxonNameBase synonymName
= oldTaxon
.getName();
94 SynonymRelationshipType synonymType
;
95 if (CdmUtil
.isNameHomotypic(synonymName
, destinationTaxon
)) {
97 // oldTaxon.makeSynonym();
98 // destinationTaxon.addHomotypicSynonymName(synonymName);
99 synonymType
= SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF();
101 // destinationTaxon.addHeterotypicSynonymName(synonymName);
102 synonymType
= SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF();
104 CdmUtil
.makeTaxonSynonym(oldTaxon
, destinationTaxon
, synonymType
,
107 // TODO move any children, descriptions, synonyms
108 for (TaxonDescription description
: oldTaxon
.getDescriptions()) {
109 destinationTaxon
.addDescription(description
);
110 oldTaxon
.removeDescription(description
);
113 for (TaxonRelationship fromRelation
: oldTaxon
.getRelationsFromThisTaxon()) {
114 fromRelation
.setFromTaxon(destinationTaxon
);
117 for (TaxonRelationship toRelation
: oldTaxon
.getRelationsToThisTaxon()) {
118 toRelation
.setToTaxon(destinationTaxon
);
121 // if (!TaxEditorPlugin.getDefault().getObservableRecentNamesList().
122 // contains(oldTaxon)) {
123 // TaxEditorPlugin.getDefault().getObservableRecentNamesList().
125 // RecentNamesView.removeRecentName(oldTaxon);
128 CdmSessionDataRepository
.getDefault().removeTaxon(oldTaxon
);
130 // Close open editor for oldTaxon without forcing save.
131 // User has already saved or declined to do so above.
133 UiUtil
.closeEditor(oldEditor
, false);
134 } catch (PartInitException e
) {
138 // Open editor for new oldTaxon
139 new SaveTaxonAction(destinationTaxon
).run();
140 new OpenTaxonEditorAction(destinationTaxon
).run();