adapt master to develop
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / details / DetailsPartE4.java
1 /**
2 * Copyright (C) 2017 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.details;
10
11 import javax.annotation.PostConstruct;
12 import javax.inject.Inject;
13
14 import org.eclipse.e4.core.contexts.ContextInjectionFactory;
15 import org.eclipse.e4.core.contexts.IEclipseContext;
16 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.jface.viewers.TreeSelection;
21 import org.eclipse.jface.viewers.TreeViewer;
22 import org.eclipse.swt.SWTException;
23 import org.eclipse.swt.widgets.Composite;
24
25 import eu.etaxonomy.cdm.model.description.Feature;
26 import eu.etaxonomy.taxeditor.l10n.Messages;
27 import eu.etaxonomy.taxeditor.model.AbstractUtility;
28 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
29 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
30 import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
31 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34 import eu.etaxonomy.taxeditor.view.detail.CdmSectionPart;
35 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPart;
36 import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
37
38 /**
39 * @author pplitzner
40 * @date 18.07.2017
41 */
42 public class DetailsPartE4 extends AbstractCdmEditorPart<DetailsViewerE4> implements IPartContentHasSupplementalData, IPartContentHasFactualData {
43
44 @Inject
45 public DetailsPartE4() {
46 }
47
48 @PostConstruct
49 public void create(Composite parent, MPart thisPart, IEclipseContext context) {
50
51 this.thisPart = thisPart;
52
53 viewer = ContextInjectionFactory.make(DetailsViewerE4.class, context);
54 viewer.init(parent, this);
55
56 // Propagate selection from viewer
57 selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
58 viewer.addSelectionChangedListener(selectionChangedListener);
59 }
60
61 @Override
62 public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
63 if (activePart == null) {
64 return;
65 }
66
67 if (activePart == thisPart ){
68 if (this.getViewer().isNeedsRefresh()) {
69 viewer.refresh();
70 viewer.setNeedsRefresh(false);
71 return;
72 }else {
73 return;
74 }
75 }
76
77
78
79 Object partObject = getPartObject(activePart);
80 if (partObject instanceof SupplementalDataPartE4 ) {
81 // do not show empty page
82 return;
83 }
84
85 if (partObject instanceof AbstractCdmEditorPart && ((AbstractCdmEditorPart)partObject).getViewer() instanceof TreeViewer ) {
86 //active part is factual data view or media view
87 IStructuredSelection structuredSelection = createSelection(selection);
88 if (structuredSelection != null && !structuredSelection.isEmpty()) {
89 //if the active part is empty the selection is still the previous selection
90 if (!(structuredSelection instanceof TreeSelection)) {
91 return;
92 }
93 }
94
95 }
96
97 if(partObject instanceof IPartContentHasDetails){
98 IStructuredSelection structuredSelection = createSelection(selection);
99 if(structuredSelection == null || structuredSelection.isEmpty()){
100 showEmptyPage();
101 return;
102 }
103
104 // do not show details for feature nodes TODO strange check to avoid having shown the distribution map for every FeatureNodeContainer
105 if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
106 // do show the map for distributions
107 Feature feature = ((FeatureNodeContainer) structuredSelection.getFirstElement()).getFeature();
108 if(!feature.equals(Feature.DISTRIBUTION())){
109 showEmptyPage();
110 return;
111 }
112 }
113
114 //FIXME this is a temporary workaround to fix selection handling for supplemental data view
115 // Now the supp data view gets double selection from details view and name editor (see #7126)
116
117 viewer.setSelection(structuredSelection, false);
118
119 showViewer(structuredSelection, activePart, viewer);
120 viewer.setNeedsRefresh(false);
121 return;
122 }
123 else{
124 showEmptyPage();
125 return;
126 }
127 }
128
129 public void refreshSelection(){
130 this.viewer.refresh_withoutnew_build();
131 }
132
133 @Override
134 protected void showEmptyPage() {
135 super.showEmptyPage();
136 if(viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed() ){
137 try{
138 viewer.destroySections();
139 }catch(SWTException e){
140 if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
141 MessagingUtils.errorDialog("Widget is disposed",
142 null,
143 MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
144 null,
145 e,
146 true);
147 }
148 }
149 }
150 }
151
152 @Override
153 protected String getViewName(){
154 return Messages.DetailsViewPart_VIEWER_NAME;
155 }
156
157 }