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.e4.handler;
|
11
|
|
12
|
import javax.inject.Named;
|
13
|
|
14
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
15
|
import org.eclipse.e4.core.di.annotations.Execute;
|
16
|
import org.eclipse.e4.core.di.annotations.Optional;
|
17
|
import org.eclipse.e4.ui.model.application.MApplication;
|
18
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
19
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
20
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
21
|
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
22
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
23
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
24
|
|
25
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
26
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
27
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
28
|
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditor;
|
29
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
30
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
31
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditor;
|
32
|
import eu.etaxonomy.taxeditor.editor.view.concept.e4.ConceptViewPartE4;
|
33
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
34
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
35
|
|
36
|
/**
|
37
|
*
|
38
|
* @author pplitzner
|
39
|
* @date 21.08.2017
|
40
|
*
|
41
|
*/
|
42
|
public class OpenRelatedConceptHandlerE4 {
|
43
|
|
44
|
@Execute
|
45
|
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
46
|
@Optional@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
|
47
|
EModelService modelService, EPartService partService, MApplication application) {
|
48
|
|
49
|
TaxonNameEditor editor = null;
|
50
|
BulkEditor bulkEditor = null;
|
51
|
TaxonRelationship taxonRelationship = (TaxonRelationship) selection.getFirstElement();
|
52
|
|
53
|
ConceptViewPartE4 conceptView = (ConceptViewPartE4) activePart.getObject();
|
54
|
|
55
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(conceptView.getSelectionProvidingPart());
|
56
|
if(e4WrappedPart instanceof TaxonNameEditor){
|
57
|
editor = (TaxonNameEditor) e4WrappedPart;
|
58
|
}
|
59
|
else if(e4WrappedPart instanceof BulkEditor){
|
60
|
bulkEditor = (BulkEditor) e4WrappedPart;
|
61
|
}
|
62
|
TaxonBase<?> relatedTaxon = null;
|
63
|
if(editor!=null){
|
64
|
relatedTaxon = getRelatedTaxon(taxonRelationship, editor.getTaxon());
|
65
|
}
|
66
|
else if(bulkEditor!=null){
|
67
|
IStructuredSelection bulkSelection = bulkEditor.getSelection();
|
68
|
Object firstElement = bulkSelection.getFirstElement();
|
69
|
if(firstElement instanceof Taxon){
|
70
|
relatedTaxon = getRelatedTaxon(taxonRelationship, (Taxon) firstElement);
|
71
|
}
|
72
|
}
|
73
|
|
74
|
if(relatedTaxon==null){
|
75
|
MessagingUtils.messageDialog(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN, getClass(),
|
76
|
String.format(Messages.OpenRelatedConceptHandler_COULD_NOT_OPEN_MESSAGE ,relatedTaxon), null);
|
77
|
}
|
78
|
else{
|
79
|
openConcept(relatedTaxon, modelService, partService, application);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
protected void openConcept(TaxonBase<?> relatedTaxon, EModelService modelService, EPartService partService, MApplication application) {
|
84
|
EditorUtil.openTaxonBaseE4(relatedTaxon.getUuid(), modelService, partService, application);
|
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
|
|
99
|
@CanExecute
|
100
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
|
101
|
MHandledMenuItem menuItem) {
|
102
|
boolean canExecute = false;
|
103
|
canExecute = selection != null
|
104
|
&& selection.size()==1
|
105
|
&& selection.getFirstElement() instanceof TaxonRelationship;
|
106
|
menuItem.setVisible(canExecute);
|
107
|
return canExecute;
|
108
|
}
|
109
|
}
|