ref #10222 adapt taxeditor to changes (DerivedUnitFacade and CondensedDistributionRec...
[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 package eu.etaxonomy.taxeditor.view.e4.supplementaldata;
10
11 import java.util.Set;
12
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15
16 import org.eclipse.e4.core.contexts.ContextInjectionFactory;
17 import org.eclipse.e4.core.contexts.IEclipseContext;
18 import org.eclipse.e4.core.di.annotations.Optional;
19 import org.eclipse.e4.ui.di.UIEventTopic;
20 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TreeNode;
24 import org.eclipse.swt.SWTException;
25 import org.eclipse.swt.widgets.Composite;
26
27 import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
28 import eu.etaxonomy.cdm.facade.DerivedUnitFacade;
29 import eu.etaxonomy.cdm.model.common.ICdmBase;
30 import eu.etaxonomy.cdm.model.description.DescriptionBase;
31 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
33 import eu.etaxonomy.cdm.model.taxon.Taxon;
34 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
35 import eu.etaxonomy.cdm.persistence.dto.TermDto;
36 import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
37 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
38 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
39 import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
40 import eu.etaxonomy.taxeditor.l10n.Messages;
41 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
42 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
43 import eu.etaxonomy.taxeditor.model.MessagingUtils;
44 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPart;
46
47 /**
48 * @author pplitzner
49 * @since Aug 10, 2017
50 */
51 public class SupplementalDataPartE4 extends AbstractCdmEditorPart<SupplementalDataViewerE4> {
52
53 @Inject
54 public SupplementalDataPartE4() {}
55
56 @PostConstruct
57 public void create(Composite parent, MPart thisPart, IEclipseContext context) {
58
59 this.thisPart = thisPart;
60
61 viewer = ContextInjectionFactory.make(SupplementalDataViewerE4.class, context);
62 viewer.init(parent, this);
63 }
64
65 @Override
66 public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
67 if(activePart == thisPart){
68 return;
69 }
70 // setEnabled(true);
71 Object partObject = getPartObject(activePart);
72
73 if(partObject instanceof IPartContentHasSupplementalData) {
74 setEnabled(true);
75
76 IStructuredSelection structuredSelection = createSelection(selection);
77 if(structuredSelection == null || structuredSelection.isEmpty()){
78 showEmptyPage();
79 return;
80 }
81 if(structuredSelection.size() != 1){
82 showEmptyPage();
83 return;
84 }
85 if (partObject instanceof ITaxonEditor && structuredSelection.getFirstElement() instanceof Taxon ){
86 if (((ITaxonEditor)partObject).getTaxon() != structuredSelection.getFirstElement() && ((Taxon)structuredSelection.getFirstElement()).isMisapplication()){
87 Set<TaxonRelationship> rels =((Taxon)structuredSelection.getFirstElement()).getTaxonRelations(((ITaxonEditor)partObject).getTaxon());
88 if (rels != null && rels.size() == 1){
89 structuredSelection = new StructuredSelection(rels.iterator().next());
90 }else{
91 showEmptyPage();
92 return;
93 }
94 }
95 }
96 // do not show supplemental data for feature nodes
97 if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
98 showEmptyPage();
99 return;
100 }
101
102 else if(structuredSelection.getFirstElement() instanceof DerivedUnitFacade){
103 return;
104 }
105 else if(structuredSelection.getFirstElement() instanceof PolytomousKeyNode){
106 structuredSelection = new StructuredSelection(((PolytomousKeyNode)structuredSelection.getFirstElement()).getKey());
107 }
108 else if(structuredSelection.getFirstElement() instanceof TreeNode){
109 structuredSelection = new StructuredSelection(((TreeNode)structuredSelection.getFirstElement()).getValue());
110 }
111 else if(structuredSelection.getFirstElement() instanceof GbifResponse){
112 structuredSelection = new StructuredSelection(((GbifResponse)structuredSelection.getFirstElement()).getDerivedUnitFacade().innerDerivedUnit());
113 }
114 else if(structuredSelection.getFirstElement() instanceof DescriptionElementBase ){
115 structuredSelection = new StructuredSelection((structuredSelection.getFirstElement()));
116
117 if (((DescriptionElementBase)structuredSelection.getFirstElement()).getInDescription() != null && (((DescriptionElementBase)structuredSelection.getFirstElement()).getInDescription().isComputed() || ((DescriptionElementBase)structuredSelection.getFirstElement()).getInDescription().isCloneForSource())){
118 if (PreferencesUtil.isComputedDesciptionHandlingDisabled()){
119 setEnabled(false);
120 }else{
121 setEnabled(true);
122 }
123 }
124 }
125 else if(structuredSelection.getFirstElement() instanceof DescriptionBase ){
126
127 if (((DescriptionBase<?>)structuredSelection.getFirstElement()).isComputed() || ((DescriptionBase<?>)structuredSelection.getFirstElement()).isCloneForSource()){
128 if (PreferencesUtil.isComputedDesciptionHandlingDisabled()){
129 setEnabled(false);
130 }else{
131 setEnabled(true);
132 }
133 }
134 }
135 else if(structuredSelection.getFirstElement() instanceof TermDto
136 || structuredSelection.getFirstElement() instanceof TermVocabularyDto
137 || structuredSelection.getFirstElement() instanceof TermNodeDto){
138 showViewer(structuredSelection, activePart, viewer);
139 return;
140 }
141
142 if(!(structuredSelection.getFirstElement() instanceof ICdmBase)){
143 showEmptyPage();
144 return;
145 }
146 showViewer(structuredSelection, activePart, viewer);
147 viewer.setEnabled(isEnabled());
148
149 return;
150 }
151 else{
152 showEmptyPage();
153 return;
154 }
155 }
156
157 @Override
158 protected void showEmptyPage() {
159 super.showEmptyPage();
160 if(viewer != null && viewer.getControl() !=null && !viewer.getControl().isDisposed()){
161 try{
162 viewer.destroySections();
163 }catch (SWTException e){
164 if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
165 MessagingUtils.errorDialog("Widget is disposed",
166 null,
167 MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
168 null,
169 e,
170 true);
171 }
172 }
173 }
174 }
175
176 @Override
177 protected String getViewName() {
178 return Messages.SupplementalDataViewPart_VIEWER_NAME;
179 }
180
181 @Inject
182 @Optional
183 private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_SUPPLEMENTAL)boolean refresh){
184 if(refresh){
185 viewer.refresh();
186 }
187 }
188 }