Project

General

Profile

Download (4.25 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.TaxonNameBase;
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.");
66
		}
67

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

    
72
	/** {@inheritDoc} */
73
	@Override
74
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
75
			throws ExecutionException {
76

    
77
    	// Get synonym name
78
		TaxonNameBase<?, ?> synonymName = synonym.getName();
79
		monitor.worked(20);
80

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

    
83
		// Switch groups
84
		oldHomotypicalGroup.removeTypifiedName(synonymName, false);
85
		monitor.worked(40);
86
		newHomotypicalGroup = HibernateProxyHelper.deproxy(newHomotypicalGroup, HomotypicalGroup.class);
87
		newHomotypicalGroup.addTypifiedName(synonymName);
88
		
89
		Taxon acc = synonym.getAcceptedTaxon();
90
		if(acc == null || !acc.equals(element)){
91
			if(acc != null){
92
				acc.removeSynonym(synonym);
93
			}
94
			
95
			SynonymType type = SynonymType.HETEROTYPIC_SYNONYM_OF();
96
			if(newHomotypicalGroup.getTypifiedNames().contains(element.getName())){
97
				type = SynonymType.HOMOTYPIC_SYNONYM_OF();
98
			}
99

    
100
			element.addSynonym(synonym, type);
101
		}
102

    
103
		// Redraw editor if it exists
104
		return postExecute(synonym);
105
	}
106

    
107
	/** {@inheritDoc} */
108
	@Override
109
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
110
			throws ExecutionException {
111
		return execute(monitor, info);
112
	}
113

    
114
	/** {@inheritDoc} */
115
	@Override
116
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
117
			throws ExecutionException {
118

    
119
		// Get synonym name
120
		TaxonNameBase<?, ?> synonymName = this.synonym.getName();
121
		if(synonymName == null){
122
			// TODO
123
		}
124

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

    
127
		// Switch groups
128
		newHomotypicalGroup.removeTypifiedName(synonymName, false);
129
		oldHomotypicalGroup.addTypifiedName(synonymName);
130

    
131
		// Redraw editor if it exists
132
		return postExecute(synonym);
133
	}
134
}
(3-3/19)