Project

General

Profile

Download (4.12 KB) Statistics
| Branch: | Tag: | Revision:
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.hibernate.HibernateProxyHelper;
19
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
20
import eu.etaxonomy.cdm.model.name.TaxonName;
21
import eu.etaxonomy.cdm.model.taxon.Synonym;
22
import eu.etaxonomy.cdm.model.taxon.SynonymType;
23
import eu.etaxonomy.cdm.model.taxon.Taxon;
24
import eu.etaxonomy.taxeditor.operation.AbstractPostTaxonOperation;
25
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
26

    
27
/**
28
 * Change the homotypical group of a given synonym.
29
 *
30
 * @author n.hoffmann
31
 * @created 19.01.2009
32
 */
33
public class ChangeHomotypicGroupOperation extends AbstractPostTaxonOperation {
34

    
35
	/**
36
	 * The synonym to be moved.
37
	 */
38
	private final 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."); //$NON-NLS-1$
66
		}
67

    
68
		this.oldHomotypicalGroup = synonym.getHomotypicGroup();
69
		this.newHomotypicalGroup = newHomotypicalGroup != null ? newHomotypicalGroup : HomotypicalGroup.NewInstance();
70
	}
71

    
72
	@Override
73
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
74
			throws ExecutionException {
75

    
76
    	// Get synonym name
77
		TaxonName synonymName = synonym.getName();
78
		monitor.worked(20);
79

    
80
		// TODO pass in homotypical group's taxon in case we are dragging from one editor to another
81

    
82
		// Switch groups
83
		oldHomotypicalGroup.removeTypifiedName(synonymName, false);
84
		monitor.worked(40);
85
		newHomotypicalGroup = HibernateProxyHelper.deproxy(newHomotypicalGroup, HomotypicalGroup.class);
86
		newHomotypicalGroup.addTypifiedName(synonymName);
87

    
88
		Taxon acc = synonym.getAcceptedTaxon();
89

    
90
		if(acc != null){
91
			acc.removeSynonym(synonym);
92
		}
93

    
94
		SynonymType type = SynonymType.HETEROTYPIC_SYNONYM_OF();
95
		if(newHomotypicalGroup.getTypifiedNames().contains(element.getName())){
96
			type = SynonymType.HOMOTYPIC_SYNONYM_OF();
97
		}
98

    
99
		element.addSynonym(synonym, type);
100

    
101
		// Redraw editor if it exists
102
		return postExecute(synonym);
103
	}
104

    
105
	@Override
106
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
107
			throws ExecutionException {
108
		return execute(monitor, info);
109
	}
110

    
111
	@Override
112
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
113
			throws ExecutionException {
114

    
115
		// Get synonym name
116
		TaxonName synonymName = this.synonym.getName();
117
		if(synonymName == null){
118
			// TODO
119
		}
120

    
121
		// TODO pass in homotypical group's taxon in case we are dragging from one editor to another
122

    
123
		// Switch groups
124
		newHomotypicalGroup.removeTypifiedName(synonymName, false);
125
		oldHomotypicalGroup.addTypifiedName(synonymName);
126

    
127
		// Redraw editor if it exists
128
		return postExecute(synonym);
129
	}
130

    
131

    
132

    
133

    
134
}
(3-3/15)