had to rename the packages to make them compliant with buckminster
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / operation / ChangeHomotypicGroupOperation.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.editor.name.operation;
11
12 import org.eclipse.core.commands.ExecutionException;
13 import org.eclipse.core.commands.operations.IUndoContext;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17
18 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
19 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
20 import eu.etaxonomy.cdm.model.taxon.Synonym;
21 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
24 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
25
26 /**
27 * Change the homotypical group of a given synonym.
28 *
29 * @author n.hoffmann
30 * @created 19.01.2009
31 * @version 1.0
32 */
33 public class ChangeHomotypicGroupOperation extends AbstractPostOperation {
34
35 /**
36 * The synonym to be moved.
37 */
38 private Synonym synonym;
39 /**
40 * The former homotypical group the synonym belonged to
41 */
42 private HomotypicalGroup oldHomotypicalGroup;
43 /**
44 * The homotypical group the synonym is to be moved to
45 */
46 private HomotypicalGroup newHomotypicalGroup;
47
48 /**
49 * <p>Constructor for ChangeHomotypicGroupOperation.</p>
50 *
51 * @param label a {@link java.lang.String} object.
52 * @param undoContext a {@link org.eclipse.core.commands.operations.IUndoContext} object.
53 * @param taxon a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
54 * @param synonym a {@link eu.etaxonomy.cdm.model.taxon.Synonym} object.
55 * @param newHomotypicalGroup a {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup} object.
56 * @param postOperationEnabled a {@link eu.etaxonomy.taxeditor.operation.IPostOperationEnabled} object.
57 */
58 public ChangeHomotypicGroupOperation(String label, IUndoContext undoContext,
59 Taxon taxon, Synonym synonym, HomotypicalGroup newHomotypicalGroup, IPostOperationEnabled postOperationEnabled) {
60 super(label, undoContext, taxon, postOperationEnabled);
61
62 this.synonym = synonym;
63 if(synonym == null){
64 throw new IllegalArgumentException(
65 "A null synonym was provided.");
66 }
67
68 this.oldHomotypicalGroup = synonym.getHomotypicGroup();
69 this.newHomotypicalGroup = newHomotypicalGroup != null ? newHomotypicalGroup : HomotypicalGroup.NewInstance();
70 }
71
72 /* (non-Javadoc)
73 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
74 */
75 /** {@inheritDoc} */
76 @Override
77 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
78 throws ExecutionException {
79
80 // Get synonym name
81 TaxonNameBase<?, ?> synonymName = synonym.getName();
82 monitor.worked(20);
83
84 // TODO pass in homotypical group's taxon in case we are dragging from one editor to another
85
86 // Switch groups
87 oldHomotypicalGroup.removeTypifiedName(synonymName);
88 monitor.worked(40);
89
90 newHomotypicalGroup.addTypifiedName(synonymName);
91
92 if(! synonym.getAcceptedTaxa().contains(taxon)){
93 for(Taxon acceptedTaxon : synonym.getAcceptedTaxa()){
94 acceptedTaxon.removeSynonym(synonym);
95 }
96
97 SynonymRelationshipType type = SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF();
98 if(newHomotypicalGroup.getTypifiedNames().contains(taxon.getName())){
99 type = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
100 }
101
102 taxon.addSynonym(synonym, type);
103 }
104
105 // Redraw editor if it exists
106 return postExecute(synonym);
107 }
108
109 /* (non-Javadoc)
110 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
111 */
112 /** {@inheritDoc} */
113 @Override
114 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
115 throws ExecutionException {
116 return execute(monitor, info);
117 }
118
119 /* (non-Javadoc)
120 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
121 */
122 /** {@inheritDoc} */
123 @Override
124 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
125 throws ExecutionException {
126
127 // Get synonym name
128 TaxonNameBase<?, ?> synonymName = this.synonym.getName();
129 if(synonymName == null){
130 // TODO
131 }
132
133 // TODO pass in homotypical group's taxon in case we are dragging from one editor to another
134
135 // Switch groups
136 newHomotypicalGroup.removeTypifiedName(synonymName);
137 oldHomotypicalGroup.addTypifiedName(synonymName);
138
139 // Redraw editor if it exists
140 return postExecute(synonym);
141 }
142 }