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