2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.concept
.handler
;
12 import org
.eclipse
.core
.commands
.AbstractHandler
;
13 import org
.eclipse
.core
.commands
.ExecutionEvent
;
14 import org
.eclipse
.core
.commands
.ExecutionException
;
15 import org
.eclipse
.jface
.viewers
.ISelection
;
16 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
17 import org
.eclipse
.ui
.IEditorPart
;
18 import org
.eclipse
.ui
.PartInitException
;
19 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
23 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
24 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
25 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
26 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
27 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
28 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
29 import eu
.etaxonomy
.taxeditor
.model
.LineSelection
;
30 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
34 * @created Jan 25, 2011
37 public class OpenRelatedConceptHandler
extends AbstractHandler
{
40 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
41 ISelection selection
= HandlerUtil
.getCurrentSelection(event
);
42 TaxonBase
<?
> relatedTaxon
= null;
44 if(selection
instanceof IStructuredSelection
){
45 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
46 Object selectedElement
= structuredSelection
.getFirstElement();
48 if(selectedElement
instanceof TaxonRelationship
){
49 IEditorPart activeEditor
= AbstractUtility
.getActiveEditor();
50 if(activeEditor
instanceof MultiPageTaxonEditor
){
51 relatedTaxon
= getRelatedTaxon((TaxonRelationship
) selectedElement
, ((MultiPageTaxonEditor
) activeEditor
).getTaxon());
53 else if(activeEditor
instanceof BulkEditor
){
54 ISelection bulkSelection
= ((BulkEditor
)activeEditor
).getSelectionProvider().getSelection();
55 if(bulkSelection
instanceof LineSelection
){
56 Object firstElement
= ((LineSelection
)bulkSelection
).getFirstElement();
57 if(firstElement
instanceof Taxon
){
58 relatedTaxon
= getRelatedTaxon((TaxonRelationship
) selectedElement
, (Taxon
) firstElement
);
65 if(relatedTaxon
==null){
66 MessagingUtils
.messageDialog(Messages
.OpenRelatedConceptHandler_COULD_NOT_OPEN
, getClass(),
67 String
.format(Messages
.OpenRelatedConceptHandler_COULD_NOT_OPEN_MESSAGE
,relatedTaxon
), null);
70 openConcept(relatedTaxon
);
79 protected void openConcept(TaxonBase
<?
> relatedTaxon
) {
81 EditorUtil
.openTaxonBase(relatedTaxon
.getUuid());
82 } catch (PartInitException e
) {
83 MessagingUtils
.messageDialog(Messages
.OpenRelatedConceptHandler_COULD_NOT_OPEN
, getClass(),
84 String
.format(Messages
.OpenRelatedConceptHandler_COULD_NOT_OPEN_MESSAGE
,relatedTaxon
), e
);
88 private Taxon
getRelatedTaxon(TaxonRelationship relationship
, Taxon taxon
){
89 if (relationship
.getFromTaxon().equals(taxon
)){
90 return relationship
.getToTaxon();
92 else if(relationship
.getToTaxon().equals(taxon
)){
93 return relationship
.getFromTaxon();