p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / operations / ChangeTaxonToSynonymOperation.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;
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.swt.widgets.Shell;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.PartInitException;
23
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;
35
36 /**
37 * @author n.hoffmann
38 * @created 20.01.2009
39 * @version 1.0
40 */
41 public class ChangeTaxonToSynonymOperation extends AbstractEditorOperation{
42
43 private static final Logger logger = Logger
44 .getLogger(ChangeTaxonToSynonymOperation.class);
45
46 private Taxon destinationTaxon;
47
48 public ChangeTaxonToSynonymOperation(String label,
49 IUndoContext undoContext, Taxon taxon) {
50 super(label, undoContext, taxon);
51 }
52
53 @Override
54 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
55 throws ExecutionException {
56
57 IEditorPart editor = null;
58
59 try {
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;
68 }
69 editor.doSave(null);
70 }
71 // if (UiUtil.getActivePage().saveEditor(oldEditor, true) == false) {
72 // return;
73 // }
74 } catch (PartInitException e1) {
75 e1.printStackTrace();
76 }
77
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);
82
83 // Abort action if user cancelled dialog without choosing a taxon
84 if (destinationTaxon == null) {
85 return Status.CANCEL_STATUS;
86 }
87
88
89 return makeTaxonSynonym(taxon, destinationTaxon) ? Status.OK_STATUS : Status.CANCEL_STATUS;
90 }
91
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();
98 } else {
99 synonymType = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
100 }
101 CdmUtil.makeTaxonSynonym(taxon, destinationTaxon, synonymType,
102 null, null);
103
104 // TODO move any children, descriptions, synonyms
105 for (TaxonDescription description: taxon.getDescriptions()) {
106 destinationTaxon.addDescription(description);
107 taxon.removeDescription(description);
108 }
109
110 for (TaxonRelationship fromRelation : taxon.getRelationsFromThisTaxon()) {
111 fromRelation.setFromTaxon(destinationTaxon);
112 }
113
114 for (TaxonRelationship toRelation : taxon.getRelationsToThisTaxon()) {
115 toRelation.setToTaxon(destinationTaxon);
116 }
117
118 CdmSessionDataRepository.getDefault().removeTaxon(taxon);
119
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);
126
127 return true;
128 }
129
130 @Override
131 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
132 throws ExecutionException {
133 return makeTaxonSynonym(taxon, destinationTaxon) ? Status.OK_STATUS : Status.CANCEL_STATUS;
134 }
135
136 @Override
137 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
138 throws ExecutionException {
139 // TODO implement undo
140 throw new NotImplementedException();
141 }
142 }