Project

General

Profile

Download (3.52 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.operations.name;
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

    
19
import eu.etaxonomy.cdm.model.taxon.Taxon;
20
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
21
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
22
import eu.etaxonomy.taxeditor.operations.AbstractEditorOperation;
23

    
24
/**
25
 * @author n.hoffmann
26
 * @created 03.02.2009
27
 * @version 1.0
28
 */
29
public class ChangeConceptRelationOperation extends AbstractEditorOperation {
30

    
31

    
32
	private static final Logger logger = Logger
33
			.getLogger(ChangeConceptRelationOperation.class);
34
	private Taxon relatedTaxon;
35
	private TaxonRelationshipType oldRelationshipType;
36
	private TaxonRelationshipType newRelationshipType;
37

    
38
	public ChangeConceptRelationOperation(String label,
39
			IUndoContext undoContext, Taxon taxon, Taxon relatedTaxon, TaxonRelationshipType type) {
40
		super(label, undoContext, taxon);
41
		this.relatedTaxon = relatedTaxon;
42
		this.newRelationshipType = type;
43
	}
44
	
45
	/* (non-Javadoc)
46
	 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
47
	 */
48
	@Override
49
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
50
			throws ExecutionException {
51
		
52
		// Remove relatedTaxon and old relationship
53
		TaxonRelationship relationship = getTaxonRelationship(taxon, relatedTaxon);
54
		taxon.removeTaxonRelation(relationship);
55
		oldRelationshipType = relationship.getType();		
56
		
57
		// Add new relationship
58
		// TODO add microcitation for misapplied name to property sheet
59
		relatedTaxon.addTaxonRelation(taxon, newRelationshipType, null, null);	
60
		
61
		return redrawOpenEditor();
62
	}
63

    
64
	/* (non-Javadoc)
65
	 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
66
	 */
67
	@Override
68
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
69
			throws ExecutionException {
70
		return execute(monitor, info);
71
	}
72

    
73
	/* (non-Javadoc)
74
	 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
75
	 */
76
	@Override
77
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
78
			throws ExecutionException {
79
		TaxonRelationship relationship = getTaxonRelationship(taxon, relatedTaxon);
80
		taxon.removeTaxonRelation(relationship);
81
		
82
		// Add new relationship
83
		// TODO add microcitation for misapplied name to property sheet
84
		relatedTaxon.addTaxonRelation(taxon, oldRelationshipType, null, null);
85
		
86
		return redrawOpenEditor();
87
	}
88
	
89
	private TaxonRelationship getTaxonRelationship(Taxon taxon1, Taxon taxon2) {
90
		for (TaxonRelationship relationship : taxon1.getTaxonRelations()) {
91
			if ((relationship.getToTaxon().equals(taxon1) 
92
					&& relationship.getFromTaxon().equals(taxon2))
93
					||
94
					(relationship.getFromTaxon().equals(taxon1) 
95
							&& relationship.getToTaxon().equals(taxon2))) {
96
				return relationship;
97
			}
98
		}
99
		return null;
100
	}
101
}
(1-1/16)