e2eb0ff0a5144ed37bd35b3017d51a92bff6fd6a
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / detail / DetailsViewPart.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.detail;
11
12 import java.util.Collection;
13 import java.util.Set;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17 import org.eclipse.e4.ui.workbench.modeling.EPartService;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.jface.viewers.TreeNode;
21 import org.eclipse.jface.viewers.Viewer;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IMemento;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.internal.e4.compatibility.CompatibilityPart;
28
29 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
30 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.description.Feature;
33 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34 import eu.etaxonomy.cdm.model.taxon.Taxon;
35 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
36 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
37 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
38 import eu.etaxonomy.taxeditor.l10n.Messages;
39 import eu.etaxonomy.taxeditor.model.AbstractUtility;
40 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
41 import eu.etaxonomy.taxeditor.model.IContextListener;
42 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
43 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
44 import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
45 import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
46
47 /**
48 * <p>DetailsViewPart class.</p>
49 *
50 * @author n.hoffmann
51 * @created Jun 10, 2010
52 * @version 1.0
53 */
54 public class DetailsViewPart extends AbstractCdmEditorViewPart implements IPartContentHasSupplementalData, IContextListener{
55
56 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.forms.det"{trunked}</code> */
57 public static String ID = "eu.etaxonomy.taxeditor.view.detail"; //$NON-NLS-1$
58
59 private DetailsViewer viewer;
60
61 //FIXME E4: this can be removed when working with E4 event system
62 private Object selectionProvidingPart;
63
64 /** {@inheritDoc} */
65 @Override
66 public void createViewer(Composite parent) {
67
68 viewer = new DetailsViewer(parent, this);
69 getSite().setSelectionProvider(viewer);
70 }
71
72 @Override
73 protected void selectionChanged_internal(IWorkbenchPart workbenchPart, ISelection selection){
74 Object part = workbenchPart;
75 Object wrappedPart = AbstractUtility.getE4WrappedPart(part);
76 if(wrappedPart!=null){
77 part = wrappedPart;
78 }
79 if(AbstractUtility.getActiveE4Editor() == null && !(AbstractUtility.getActiveE4Part() instanceof IConversationEnabled)){
80 showEmptyPage();
81 return;
82 }
83
84 //special case where the details view still open with details about the closed object #5495
85 try{
86 boolean isSelectionProvidingPartStillActive = false;
87 if(selectionProvidingPart!=null){
88 EPartService partService = TaxeditorStorePlugin.getDefault().getWorkbench().getService(EPartService.class);
89 Collection<MPart> parts = partService.getParts();
90 for (MPart mPart : parts) {
91 Object object = mPart.getObject();
92 if(object instanceof CompatibilityPart){
93 object = ((CompatibilityPart) object).getPart();
94 }
95 if(selectionProvidingPart.equals(object)){
96 isSelectionProvidingPartStillActive = true;
97 break;
98 }
99 }
100 if(!isSelectionProvidingPartStillActive &&
101 selection instanceof IStructuredSelection && ((IStructuredSelection) selection).getFirstElement() !=null){
102 showEmptyPage();
103 return;
104 }
105 }
106 }catch(IllegalStateException e){
107 //nothing
108 }
109
110 if(part == this){
111 return;
112 }
113
114 if(!(selection instanceof IStructuredSelection)){
115 return;
116 }
117
118 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
119
120 if((part instanceof IEditorPart) || (part instanceof IPartContentHasDetails)) {
121 if(structuredSelection.size() != 1){
122 showEmptyPage();
123 return;
124 }
125
126 // do not show details for feature nodes TODO really?
127 if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
128 // do show the map for distributions
129 Feature feature = ((FeatureNodeContainer) ((IStructuredSelection) selection).getFirstElement()).getFeature();
130 if(!feature.equals(Feature.DISTRIBUTION())){
131 showEmptyPage();
132 return;
133 }
134 }
135 selectionProvidingPart = part;
136 showViewer(part, structuredSelection);
137 }else{
138 showEmptyPage();
139 }
140 }
141
142 @Override
143 protected String getViewName(){
144 return Messages.DetailsViewPart_VIEWER_NAME;
145 }
146
147 @Override
148 protected String createPartTitle(Object selectedObject){
149 if(selectedObject!=null){
150 if(selectedObject instanceof TreeNode){
151 selectedObject = ((TreeNode) selectedObject).getValue();
152 }
153 if(selectedObject instanceof SpecimenOrObservationBase){
154 return getViewName()+": "+HibernateProxyHelper.deproxy(selectedObject, SpecimenOrObservationBase.class).getRecordBasis(); //$NON-NLS-1$
155 }
156 if(selectedObject instanceof CdmBase){
157 return getViewName()+": "+HibernateProxyHelper.deproxy(selectedObject, CdmBase.class).getClass().getSimpleName(); //$NON-NLS-1$
158 }
159 return getViewName()+": "+selectedObject.getClass().getSimpleName(); //$NON-NLS-1$
160 }
161 return getViewName();
162 }
163
164 @Override
165 public void showEmptyPage() {
166 selectionProvidingPart = null;
167 viewer.setSelection(null);
168 super.showEmptyPage();
169 }
170
171 /** {@inheritDoc} */
172 @Override
173 public boolean postOperation(CdmBase objectAffectedByOperation) {
174
175 viewer.setInput(objectAffectedByOperation);
176
177 return super.postOperation(objectAffectedByOperation);
178 }
179
180
181 /** {@inheritDoc} */
182 @Override
183 public Viewer getViewer() {
184 return viewer;
185 }
186
187
188 /** {@inheritDoc} */
189 @Override
190 public void dispose() {
191 selectionService.removePostSelectionListener(this);
192 super.dispose();
193
194 }
195
196 @Override
197 public boolean onComplete() {
198 return true;
199 }
200
201 /**
202 * {@inheritDoc}
203 */
204 @Override
205 public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
206 }
207
208 /**
209 * {@inheritDoc}
210 */
211 @Override
212 public void contextStop(IMemento memento, IProgressMonitor monitor) {
213 showEmptyPage();
214 }
215
216 /**
217 * {@inheritDoc}
218 */
219 @Override
220 public void contextStart(IMemento memento, IProgressMonitor monitor) {
221 }
222
223 /**
224 * {@inheritDoc}
225 */
226 @Override
227 public void contextRefresh(IProgressMonitor monitor) {
228 }
229
230 /**
231 * {@inheritDoc}
232 */
233 @Override
234 public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
235 }
236 @Override
237 public void showViewer(Object part, IStructuredSelection selection){
238 this.part = part;
239
240 Object element = selection.getFirstElement();
241 //avoid widget is disposed exceptions
242 if (getViewer().getControl()==null || getViewer().getControl().isDisposed()){
243 return;
244 }
245
246 setPartName(createPartTitle(element));
247 if (element instanceof Taxon){
248 Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
249 if (taxon.isMisapplication()){
250
251 if (part instanceof ITaxonEditor){
252 Taxon accepted= ((ITaxonEditor)part).getTaxon();
253 Set<TaxonRelationship> rels = taxon.getTaxonRelations(accepted);
254
255 if (rels.iterator().hasNext()){
256 TaxonRelationship rel = rels.iterator().next();
257 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
258 getViewer().setInput(rel);
259 showViewer();
260 return;
261 }
262 }
263 }
264
265
266 }
267 }
268 getViewer().setInput(element);
269 showViewer();
270 }
271 }
272
273