ref #6597 Avoid session errors and empty view for unaccepted selections
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / details / DetailsPartE4.java
1 // $Id$
2 /**
3 * Copyright (C) 2017 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.view.e4.details;
11
12 import javax.annotation.PostConstruct;
13 import javax.annotation.PreDestroy;
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.Viewer;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.IEditorPart;
25
26 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
27 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
28 import eu.etaxonomy.cdm.model.description.Feature;
29 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
30 import eu.etaxonomy.taxeditor.l10n.Messages;
31 import eu.etaxonomy.taxeditor.model.AbstractUtility;
32 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
33 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
34 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
35 import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
36
37 /**
38 * @author pplitzner
39 * @date 18.07.2017
40 *
41 */
42 public class DetailsPartE4 implements IConversationEnabled, IDirtyMarkable{
43
44 private DetailsViewerE4 viewer;
45
46 private MPart selectionProvidingPart;
47
48 @Inject
49 public DetailsPartE4() {
50 }
51
52 @PostConstruct
53 public void create(Composite parent) {
54 viewer = new DetailsViewerE4(parent, this);
55 }
56
57 @Inject
58 @Optional
59 public void selectionChanged(
60 @Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
61 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
62 MPart thisPart){
63 if(activePart==thisPart || selection==null){
64 return;
65 }
66
67 Object partObject = activePart;
68 Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
69 if(wrappedPart!=null){
70 partObject = wrappedPart;
71 }
72
73 IStructuredSelection structuredSelection;
74 if(!(selection instanceof IStructuredSelection)){
75 structuredSelection = new StructuredSelection(selection);
76 }
77 else{
78 structuredSelection = (IStructuredSelection) selection;
79 }
80 if((partObject instanceof IEditorPart) || (partObject instanceof IPartContentHasDetails)
81 && partObject instanceof IConversationEnabled) {
82 // do not show details for feature nodes TODO strange check to avoid having shown the distribution map for every FeatureNodeContainer
83 if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
84 // do show the map for distributions
85 Feature feature = ((FeatureNodeContainer) ((IStructuredSelection) selection).getFirstElement()).getFeature();
86 if(!feature.equals(Feature.DISTRIBUTION())){
87 showEmptyPage();
88 return;
89 }
90 }
91 showViewer(structuredSelection);
92 selectionProvidingPart = activePart;
93 }
94 else{
95 showEmptyPage();
96 }
97 }
98
99 public void showEmptyPage() {
100 viewer.showEmptyPage();
101 selectionProvidingPart = null;
102 }
103
104 protected String getViewName(){
105 return Messages.DetailsViewPart_VIEWER_NAME;
106 }
107
108 public Viewer getViewer() {
109 return viewer;
110 }
111
112
113 @PreDestroy
114 public void dispose() {
115 }
116
117 public void showViewer(IStructuredSelection selection){
118 if(getViewer()!=null){
119 Object element = selection.getFirstElement();
120 if(selection.getFirstElement()!=null){
121 getViewer().setInput(element);
122 }
123 }
124 }
125
126 /**
127 * {@inheritDoc}
128 */
129 @Override
130 public void update(CdmDataChangeMap arg0) {
131 // TODO Auto-generated method stub
132
133 }
134
135 /**
136 * {@inheritDoc}
137 */
138 @Override
139 public ConversationHolder getConversationHolder() {
140 if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
141 return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
142 }
143 return null;
144 }
145
146 /** {@inheritDoc} */
147 @Override
148 public void changed(Object object) {
149 Object part = selectionProvidingPart.getObject();
150 if(part instanceof IDirtyMarkable){
151 ((IDirtyMarkable) part).changed(object);
152 }
153 else {
154 IEditorPart editor = AbstractUtility.getActiveEditor();
155 if (editor != null && editor instanceof IDirtyMarkable) {
156 ((IDirtyMarkable) editor).changed(object);
157 }
158 }
159 }
160
161 /**
162 * {@inheritDoc}
163 */
164 @Override
165 public void forceDirty() {
166 // TODO Auto-generated method stub
167
168 }
169 }