Fix possible NPE #4850
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / detail / DetailsViewPart.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.view.detail;
12
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15 import org.eclipse.jface.viewers.TreeNode;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IWorkbenchPart;
20
21 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23 import eu.etaxonomy.cdm.model.description.Feature;
24 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
25 import eu.etaxonomy.taxeditor.model.AbstractUtility;
26 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
27 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
28 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
29 import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
30
31 /**
32 * <p>DetailsViewPart class.</p>
33 *
34 * @author n.hoffmann
35 * @created Jun 10, 2010
36 * @version 1.0
37 */
38 public class DetailsViewPart extends AbstractCdmEditorViewPart implements IPartContentHasSupplementalData{
39
40 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.forms.det"{trunked}</code> */
41 public static String ID = "eu.etaxonomy.taxeditor.view.detail";
42
43 private DetailsViewer viewer;
44
45 /** {@inheritDoc} */
46 @Override
47 public void createViewer(Composite parent) {
48
49 viewer = new DetailsViewer(parent, this);
50 getSite().setSelectionProvider(viewer);
51 }
52
53 @Override
54 protected void selectionChanged_internal(IWorkbenchPart part, ISelection selection){
55 if(AbstractUtility.getActiveEditor() == null){
56 showEmptyPage();
57 return;
58 }
59
60 if(part == this){
61 return;
62 }
63
64 if(!(selection instanceof IStructuredSelection)){
65 return;
66 }
67
68 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
69
70 if((part instanceof IEditorPart) || (part instanceof IPartContentHasDetails)) {
71 if(structuredSelection.size() != 1){
72 setPartName(createPartTitle(null));
73 showEmptyPage();
74 return;
75 }
76
77 // do not show details for feature nodes TODO really?
78 if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
79 // do show the map for distributions
80 Feature feature = ((FeatureNodeContainer) ((IStructuredSelection) selection).getFirstElement()).getFeature();
81 if(!feature.equals(Feature.DISTRIBUTION())){
82 setPartName(createPartTitle(null));
83 showEmptyPage();
84 return;
85 }
86 }
87
88
89 setPartName(createPartTitle(((IStructuredSelection) selection).getFirstElement()));
90 showViewer(part, structuredSelection);
91 }else{
92 setPartName(createPartTitle(null));
93 showEmptyPage();
94 }
95 }
96
97 private String getViewName(){
98 return "Details";
99 }
100
101 private String createPartTitle(Object selectedObject){
102 if(selectedObject!=null){
103 if(selectedObject instanceof TreeNode){
104 selectedObject = ((TreeNode) selectedObject).getValue();
105 }
106 if(selectedObject instanceof SpecimenOrObservationBase){
107 return getViewName()+": "+HibernateProxyHelper.deproxy(selectedObject, SpecimenOrObservationBase.class).getRecordBasis();
108 }
109 if(selectedObject instanceof CdmBase){
110 return getViewName()+": "+HibernateProxyHelper.deproxy(selectedObject, CdmBase.class).getClass().getSimpleName();
111 }
112 return getViewName()+": "+selectedObject.getClass().getSimpleName();
113 }
114 return getViewName();
115 }
116
117 @Override
118 public void showEmptyPage() {
119 viewer.setSelection(null);
120 super.showEmptyPage();
121 }
122
123 /** {@inheritDoc} */
124 @Override
125 public boolean postOperation(CdmBase objectAffectedByOperation) {
126
127 viewer.setInput(objectAffectedByOperation);
128
129 return super.postOperation(objectAffectedByOperation);
130 }
131
132
133 /** {@inheritDoc} */
134 @Override
135 public Viewer getViewer() {
136 return viewer;
137 }
138
139
140 /** {@inheritDoc} */
141 @Override
142 public void dispose() {
143 selectionService.removePostSelectionListener(this);
144 super.dispose();
145
146 }
147
148 @Override
149 public boolean onComplete() {
150 return true;
151 }
152 }