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.view.concept.handler;
|
11
|
|
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.PartInitException;
|
18
|
import org.eclipse.ui.handlers.HandlerUtil;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
21
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
22
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
23
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
|
24
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
25
|
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
|
26
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
27
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
28
|
import eu.etaxonomy.taxeditor.model.LineSelection;
|
29
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
30
|
|
31
|
/**
|
32
|
* @author n.hoffmann
|
33
|
* @created Jan 25, 2011
|
34
|
* @version 1.0
|
35
|
*/
|
36
|
public class OpenRelatedConceptHandler extends AbstractHandler {
|
37
|
|
38
|
@Override
|
39
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
40
|
ISelection selection = HandlerUtil.getCurrentSelection(event);
|
41
|
TaxonBase<?> relatedTaxon = null;
|
42
|
|
43
|
if(selection instanceof IStructuredSelection){
|
44
|
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
|
45
|
Object selectedElement = structuredSelection.getFirstElement();
|
46
|
|
47
|
if(selectedElement instanceof TaxonRelationship){
|
48
|
Object activeEditor = AbstractUtility.getActiveE4Editor();
|
49
|
if(activeEditor instanceof MultiPageTaxonEditor){
|
50
|
relatedTaxon = getRelatedTaxon((TaxonRelationship) selectedElement, ((MultiPageTaxonEditor) activeEditor).getTaxon());
|
51
|
}
|
52
|
else if(activeEditor instanceof BulkEditor){
|
53
|
ISelection bulkSelection = ((BulkEditor)activeEditor).getSelectionProvider().getSelection();
|
54
|
if(bulkSelection instanceof LineSelection){
|
55
|
Object firstElement = ((LineSelection)bulkSelection).getFirstElement();
|
56
|
if(firstElement instanceof Taxon){
|
57
|
relatedTaxon = getRelatedTaxon((TaxonRelationship) selectedElement, (Taxon) firstElement);
|
58
|
}
|
59
|
}
|
60
|
}
|
61
|
}
|
62
|
}
|
63
|
|
64
|
if(relatedTaxon==null){
|
65
|
MessagingUtils.messageDialog(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN, getClass(),
|
66
|
String.format(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN_MESSAGE ,relatedTaxon), null);
|
67
|
}
|
68
|
else{
|
69
|
openConcept(relatedTaxon);
|
70
|
}
|
71
|
|
72
|
return null;
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* @param relatedTaxon
|
77
|
*/
|
78
|
protected void openConcept(TaxonBase<?> relatedTaxon) {
|
79
|
try {
|
80
|
EditorUtil.openTaxonBase(relatedTaxon.getUuid());
|
81
|
} catch (PartInitException e) {
|
82
|
MessagingUtils.messageDialog(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN, getClass(),
|
83
|
String.format(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN_MESSAGE ,relatedTaxon), e);
|
84
|
}
|
85
|
}
|
86
|
|
87
|
private Taxon getRelatedTaxon(TaxonRelationship relationship, Taxon taxon){
|
88
|
if (relationship.getFromTaxon().equals(taxon)){
|
89
|
return relationship.getToTaxon();
|
90
|
}
|
91
|
else if(relationship.getToTaxon().equals(taxon)){
|
92
|
return relationship.getFromTaxon();
|
93
|
}
|
94
|
else{
|
95
|
return null;
|
96
|
}
|
97
|
}
|
98
|
}
|