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
.operations
;
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
.swt
.widgets
.Shell
;
21 import org
.eclipse
.ui
.IEditorPart
;
22 import org
.eclipse
.ui
.PartInitException
;
24 import sun
.reflect
.generics
.reflectiveObjects
.NotImplementedException
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
30 import eu
.etaxonomy
.taxeditor
.controller
.EditorController
;
31 import eu
.etaxonomy
.taxeditor
.controller
.GlobalController
;
32 import eu
.etaxonomy
.taxeditor
.editor
.SelectTaxonDialog
;
33 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
34 import eu
.etaxonomy
.taxeditor
.model
.CdmUtil
;
41 public class ChangeTaxonToSynonymOperation
extends AbstractEditorOperation
{
43 private static final Logger logger
= Logger
44 .getLogger(ChangeTaxonToSynonymOperation
.class);
46 private Taxon destinationTaxon
;
48 public ChangeTaxonToSynonymOperation(String label
,
49 IUndoContext undoContext
, Taxon taxon
) {
50 super(label
, undoContext
, taxon
);
54 public IStatus
execute(IProgressMonitor monitor
, IAdaptable info
)
55 throws ExecutionException
{
57 IEditorPart editor
= null;
60 // Prompt user "Would you like to save?" before showing dialog
61 // "Cancel" cancels action - "No" does not
62 editor
= EditorController
.getEditorByTaxon(taxon
);
63 if (editor
.isDirty()) {
64 if (!MessageDialog
.openConfirm(GlobalController
.getShell(), "Save before proceeding",
65 "All changes must be saved before proceeding with this action.\n\n" +
66 "Press \"OK\" to save and continue, or \"Cancel\" to cancel this action.")) {
67 return Status
.CANCEL_STATUS
;
71 // if (UiUtil.getActivePage().saveEditor(oldEditor, true) == false) {
74 } catch (PartInitException e1
) {
78 // Get destination taxon from dialog
79 Shell shell
= GlobalController
.getShell();
80 SelectTaxonDialog dialog
= new SelectTaxonDialog(shell
, SelectTaxonDialog
.TAXON_TO_SYNONYM
);
81 destinationTaxon
= dialog
.open(taxon
);
83 // Abort action if user cancelled dialog without choosing a taxon
84 if (destinationTaxon
== null) {
85 return Status
.CANCEL_STATUS
;
89 return makeTaxonSynonym(taxon
, destinationTaxon
) ? Status
.OK_STATUS
: Status
.CANCEL_STATUS
;
92 private boolean makeTaxonSynonym(Taxon taxon
, Taxon destinationTaxon
){
93 // Move taxon in CDM to new destinationTaxon
94 TaxonNameBase synonymName
= taxon
.getName();
95 SynonymRelationshipType synonymType
;
96 if (CdmUtil
.isNameHomotypic(synonymName
, destinationTaxon
)) {
97 synonymType
= SynonymRelationshipType
.HOMOTYPIC_SYNONYM_OF();
99 synonymType
= SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF();
101 CdmUtil
.makeTaxonSynonym(taxon
, destinationTaxon
, synonymType
,
104 // TODO move any children, descriptions, synonyms
105 for (TaxonDescription description
: taxon
.getDescriptions()) {
106 destinationTaxon
.addDescription(description
);
107 taxon
.removeDescription(description
);
110 for (TaxonRelationship fromRelation
: taxon
.getRelationsFromThisTaxon()) {
111 fromRelation
.setFromTaxon(destinationTaxon
);
114 for (TaxonRelationship toRelation
: taxon
.getRelationsToThisTaxon()) {
115 toRelation
.setToTaxon(destinationTaxon
);
118 CdmSessionDataRepository
.getDefault().removeTaxon(taxon
);
120 // Close open editor for oldTaxon without forcing save.
121 // User has already saved or declined to do so above.
122 EditorController
.close(taxon
, false);
123 CdmSessionDataRepository
.getDefault().saveTaxon(destinationTaxon
);
124 // Open editor for destinationTaxon
125 EditorController
.open(destinationTaxon
);
131 public IStatus
redo(IProgressMonitor monitor
, IAdaptable info
)
132 throws ExecutionException
{
133 return makeTaxonSynonym(taxon
, destinationTaxon
) ? Status
.OK_STATUS
: Status
.CANCEL_STATUS
;
137 public IStatus
undo(IProgressMonitor monitor
, IAdaptable info
)
138 throws ExecutionException
{
139 // TODO implement undo
140 throw new NotImplementedException();