ref #6905, #6597 Propagate selection for supplemental data view
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / supplementaldata / SupplementalDataPartE4.java
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.view.e4.supplementaldata;
11
12 import java.util.Set;
13
14 import javax.inject.Inject;
15 import javax.inject.Named;
16
17 import org.eclipse.e4.core.di.annotations.Optional;
18 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19 import org.eclipse.e4.ui.services.IServiceConstants;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.jface.viewers.TreeNode;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.part.EditorPart;
25
26 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
27 import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
28 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
31 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
32 import eu.etaxonomy.taxeditor.l10n.Messages;
33 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
34 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
35 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmDataViewerE4;
36 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
37 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
38
39
40 /**
41 *
42 * @author pplitzner
43 * @since Aug 10, 2017
44 *
45 */
46 public class SupplementalDataPartE4 extends AbstractCdmEditorPartE4 {
47
48 @Inject
49 public SupplementalDataPartE4() {
50 }
51
52 @Override
53 public AbstractCdmDataViewerE4 createViewer(Composite parent) {
54 return new SupplementalDataViewerE4(parent, this);
55 }
56
57 @Inject
58 @Optional
59 protected void selectionChanged_internal(
60 @Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
61 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
62 MPart thisPart) {
63 if(activePart==thisPart){
64 return;
65 }
66 if(selection==null){
67 showEmptyPage();
68 return;
69 }
70
71 Object partObject = activePart;
72 Object wrappedPart = WorkbenchUtility.getE4WrappedPart(partObject);
73 if(wrappedPart!=null){
74 partObject = wrappedPart;
75 }
76
77 IStructuredSelection structuredSelection;
78 if(!(selection instanceof IStructuredSelection)){
79 structuredSelection = new StructuredSelection(selection);
80 }
81 else{
82 structuredSelection = (IStructuredSelection) selection;
83 }
84 if(structuredSelection.isEmpty()){
85 showEmptyPage();
86 return;
87 }
88
89 if((partObject instanceof EditorPart || partObject instanceof IPartContentHasSupplementalData)) {
90 if(structuredSelection.size() != 1){
91 showEmptyPage();
92 return;
93 }
94 if (partObject instanceof ITaxonEditor && structuredSelection.getFirstElement() instanceof Taxon ){
95 if (((ITaxonEditor)partObject).getTaxon() != structuredSelection.getFirstElement() && ((Taxon)structuredSelection.getFirstElement()).isMisapplication()){
96 Set<TaxonRelationship> rels =((Taxon)structuredSelection.getFirstElement()).getTaxonRelations(((ITaxonEditor)partObject).getTaxon());
97 if (rels.size() == 1){
98 structuredSelection = new StructuredSelection(rels.iterator().next());
99 }else{
100 showEmptyPage();
101 return;
102 }
103
104 }
105 }
106 // do not show supplemental data for feature nodes
107 if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
108 showEmptyPage();
109 return;
110 }
111 else if(structuredSelection.getFirstElement() instanceof DerivedUnitFacade){
112 return;
113 }
114 else if(structuredSelection.getFirstElement() instanceof PolytomousKeyNode){
115 structuredSelection = new StructuredSelection(((PolytomousKeyNode)structuredSelection.getFirstElement()).getKey());
116 }
117 else if(structuredSelection.getFirstElement() instanceof TreeNode){
118 structuredSelection = new StructuredSelection(((TreeNode)structuredSelection.getFirstElement()).getValue());
119 }
120 else if(structuredSelection.getFirstElement() instanceof GbifResponse){
121 structuredSelection = new StructuredSelection(((GbifResponse)structuredSelection.getFirstElement()).getDerivedUnitFacade().innerDerivedUnit());
122 }
123
124 showViewer(structuredSelection, activePart);
125 return;
126 }else{
127 showEmptyPage();
128 return;
129 }
130 }
131
132 @Override
133 protected String getViewName() {
134 return Messages.SupplementalDataViewPart_VIEWER_NAME;
135 }
136 }