Refactored concept relationship handling as it was not feasable before. Can be improv...
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / concept / handler / OpenRelatedConceptHandler.java
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
28 /**
29 * @author n.hoffmann
30 * @created Jan 25, 2011
31 * @version 1.0
32 */
33 public class OpenRelatedConceptHandler extends AbstractHandler {
34
35 /* (non-Javadoc)
36 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
37 */
38 @Override
39 public Object execute(ExecutionEvent event) throws ExecutionException {
40 MultiPageTaxonEditor editor = EditorUtil.getActiveMultiPageTaxonEditor();
41
42 ISelection selection = HandlerUtil.getActiveMenuSelection(event);
43
44 if(selection instanceof IStructuredSelection){
45 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
46 Iterator iterator = structuredSelection.iterator();
47
48 while (iterator.hasNext()){
49 Object element = iterator.next();
50 if(element instanceof TaxonRelationship){
51 Taxon relatedTaxon = getRelatedTaxon((TaxonRelationship) element, editor.getTaxon());
52
53 try {
54 EditorUtil.openTaxonBase(relatedTaxon.getUuid());
55 } catch (PartInitException e) {
56 EditorUtil.errorDialog("Could not open taxon", getClass(),
57 String.format("Could not open the taxon: %s" ,relatedTaxon), e);
58 }
59 }
60 }
61 }
62
63 return null;
64 }
65
66
67 private Taxon getRelatedTaxon(TaxonRelationship relationship, Taxon taxon){
68 if (relationship.getFromTaxon().equals(taxon)){
69 return relationship.getToTaxon();
70 }
71 else if(relationship.getToTaxon().equals(taxon)){
72 return relationship.getFromTaxon();
73 }
74 else{
75 return null;
76 }
77 }
78 }