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