Project

General

Profile

Download (2.38 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.editor.view.concept.handler;
12

    
13
import java.util.Iterator;
14

    
15
import org.eclipse.core.commands.AbstractHandler;
16
import org.eclipse.core.commands.ExecutionEvent;
17
import org.eclipse.core.commands.ExecutionException;
18
import org.eclipse.jface.viewers.ISelection;
19
import org.eclipse.jface.viewers.IStructuredSelection;
20
import org.eclipse.ui.PartInitException;
21
import org.eclipse.ui.handlers.HandlerUtil;
22

    
23
import eu.etaxonomy.cdm.model.taxon.Taxon;
24
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
25
import eu.etaxonomy.taxeditor.editor.EditorUtil;
26
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
27
import eu.etaxonomy.taxeditor.model.MessagingUtils;
28

    
29
/**
30
 * @author n.hoffmann
31
 * @created Jan 25, 2011
32
 * @version 1.0
33
 */
34
public class OpenRelatedConceptHandler extends AbstractHandler {
35

    
36
	/* (non-Javadoc)
37
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
38
	 */
39
	@Override
40
	public Object execute(ExecutionEvent event) throws ExecutionException {
41
		MultiPageTaxonEditor editor = EditorUtil.getActiveMultiPageTaxonEditor();
42
		
43
		ISelection selection = HandlerUtil.getCurrentSelection(event);
44
		
45
		if(selection instanceof IStructuredSelection){
46
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
47
			Iterator iterator = structuredSelection.iterator();
48
			
49
			while (iterator.hasNext()){
50
				Object element = iterator.next();
51
				if(element instanceof TaxonRelationship){
52
					Taxon relatedTaxon = getRelatedTaxon((TaxonRelationship) element, editor.getTaxon());
53
					
54
					try {
55
						EditorUtil.openTaxonBase(relatedTaxon.getUuid());
56
					} catch (PartInitException e) {
57
						MessagingUtils.messageDialog("Could not open taxon", getClass(), 
58
								String.format("Could not open the taxon: %s" ,relatedTaxon), e);
59
					}
60
				}
61
			}
62
		}
63
		
64
		return null;
65
	}
66

    
67
	
68
	private Taxon getRelatedTaxon(TaxonRelationship relationship, Taxon taxon){
69
		if (relationship.getFromTaxon().equals(taxon)){
70
			return relationship.getToTaxon();
71
		}
72
		else if(relationship.getToTaxon().equals(taxon)){
73
			return relationship.getFromTaxon();
74
		}
75
		else{
76
			return null;
77
		}
78
	}
79
}
(8-8/8)