Project

General

Profile

Download (8.23 KB) Statistics
| Branch: | Tag: | Revision:
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

    
46
/**
47
 * <p>DetailsViewPart class.</p>
48
 *
49
 * @author n.hoffmann
50
 * @created Jun 10, 2010
51
 * @version 1.0
52
 */
53
public class DetailsViewPart extends AbstractCdmEditorViewPart implements IPartContentHasSupplementalData, IContextListener{
54

    
55
	/** Constant <code>ID="eu.etaxonomy.taxeditor.editor.forms.det"{trunked}</code> */
56
	public static String ID = "eu.etaxonomy.taxeditor.view.detail"; //$NON-NLS-1$
57

    
58
	private DetailsViewer viewer;
59

    
60
	/** {@inheritDoc} */
61
	@Override
62
	public void createViewer(Composite parent) {
63

    
64
		viewer = new DetailsViewer(parent, this);
65
		getSite().setSelectionProvider(viewer);
66
	}
67

    
68
	@Override
69
    protected void selectionChanged_internal(IWorkbenchPart workbenchPart, ISelection selection){
70
	    Object part = workbenchPart;
71
	    Object wrappedPart = AbstractUtility.getE4WrappedPart(part);
72
	    if(wrappedPart!=null){
73
	        part = wrappedPart;
74
	    }
75
	    if(AbstractUtility.getActiveE4Editor() == null && !(AbstractUtility.getActiveE4Part() instanceof IConversationEnabled)){
76
            showEmptyPage();
77
            return;
78
        }
79

    
80
	    //special case where the details view still open with details about the closed object #5495
81
	    try{
82
	        boolean isSelectionProvidingPartStillActive = false;
83
	        if(selectionProvidingPart!=null){
84
	            EPartService partService = TaxeditorStorePlugin.getDefault().getWorkbench().getService(EPartService.class);
85
	            Collection<MPart> parts = partService.getParts();
86
	            for (MPart mPart : parts) {
87
	                Object object = mPart.getObject();
88
	                if(object instanceof CompatibilityPart){
89
	                    object = ((CompatibilityPart) object).getPart();
90
	                }
91
	                if(selectionProvidingPart.equals(object)){
92
	                    isSelectionProvidingPartStillActive = true;
93
	                    break;
94
	                }
95
	            }
96
	            if(!isSelectionProvidingPartStillActive &&
97
	                    selection instanceof IStructuredSelection && ((IStructuredSelection) selection).getFirstElement() !=null){
98
	                showEmptyPage();
99
	                return;
100
	            }
101
	        }
102
	    }catch(IllegalStateException e){
103
	        //nothing
104
	    }
105

    
106
	    if(part == this){
107
	        return;
108
	    }
109

    
110
        if(!(selection instanceof IStructuredSelection)){
111
            return;
112
        }
113

    
114
        IStructuredSelection structuredSelection = (IStructuredSelection) selection;
115

    
116
        if((part instanceof IEditorPart) || (part instanceof IPartContentHasDetails)) {
117
            if(structuredSelection.size() != 1){
118
                showEmptyPage();
119
                return;
120
            }
121

    
122
            // do not show details for feature nodes TODO really?
123
            if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
124
                // do show the map for distributions
125
                Feature feature = ((FeatureNodeContainer) ((IStructuredSelection) selection).getFirstElement()).getFeature();
126
                if(!feature.equals(Feature.DISTRIBUTION())){
127
                    showEmptyPage();
128
                    return;
129
                }
130
            }
131
            showViewer(part, structuredSelection);
132
        }else{
133
            showEmptyPage();
134
        }
135
	}
136

    
137
	@Override
138
	protected String getViewName(){
139
	    return Messages.DetailsViewPart_VIEWER_NAME;
140
	}
141

    
142
	@Override
143
	protected String createPartTitle(Object selectedObject){
144
	    if(selectedObject!=null){
145
	        if(selectedObject instanceof TreeNode){
146
	            selectedObject = ((TreeNode) selectedObject).getValue();
147
	        }
148
	        if(selectedObject instanceof SpecimenOrObservationBase){
149
	            return getViewName()+": "+HibernateProxyHelper.deproxy(selectedObject, SpecimenOrObservationBase.class).getRecordBasis(); //$NON-NLS-1$
150
	        }
151
	        if(selectedObject instanceof CdmBase){
152
	            return getViewName()+": "+HibernateProxyHelper.deproxy(selectedObject, CdmBase.class).getClass().getSimpleName(); //$NON-NLS-1$
153
	        }
154
	        return getViewName()+": "+selectedObject.getClass().getSimpleName(); //$NON-NLS-1$
155
	    }
156
	    return getViewName();
157
	}
158

    
159
	@Override
160
	public void showEmptyPage() {
161
	    viewer.setSelection(null);
162
	    super.showEmptyPage();
163
	}
164

    
165
	/** {@inheritDoc} */
166
	@Override
167
	public boolean postOperation(CdmBase objectAffectedByOperation) {
168

    
169
		viewer.setInput(objectAffectedByOperation);
170

    
171
		return super.postOperation(objectAffectedByOperation);
172
	}
173

    
174

    
175
	/** {@inheritDoc} */
176
	@Override
177
	public Viewer getViewer() {
178
		return viewer;
179
	}
180

    
181

    
182
	/** {@inheritDoc} */
183
	@Override
184
	public void dispose() {
185
		selectionService.removePostSelectionListener(this);
186
		super.dispose();
187

    
188
	}
189

    
190
	@Override
191
    public boolean onComplete() {
192
		return true;
193
	}
194

    
195
    /**
196
     * {@inheritDoc}
197
     */
198
    @Override
199
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
200
    }
201

    
202
    /**
203
     * {@inheritDoc}
204
     */
205
    @Override
206
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
207
        showEmptyPage();
208
    }
209

    
210
    /**
211
     * {@inheritDoc}
212
     */
213
    @Override
214
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
215
    }
216

    
217
    /**
218
     * {@inheritDoc}
219
     */
220
    @Override
221
    public void contextRefresh(IProgressMonitor monitor) {
222
    }
223

    
224
    /**
225
     * {@inheritDoc}
226
     */
227
    @Override
228
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
229
    }
230
    @Override
231
    public void showViewer(Object part, IStructuredSelection selection){
232
    	this.part = part;
233

    
234
        Object element = selection.getFirstElement();
235
      //avoid widget is disposed exceptions
236
        if (getViewer().getControl()==null || getViewer().getControl().isDisposed()){
237
            return;
238
        }
239

    
240
        setPartName(createPartTitle(element));
241
        if (element instanceof Taxon){
242
        	Taxon taxon = HibernateProxyHelper.deproxy(element, Taxon.class);
243
        	if (taxon.isMisapplication()){
244

    
245
        		if (part instanceof ITaxonEditor){
246
        			Taxon accepted= ((ITaxonEditor)part).getTaxon();
247
        			Set<TaxonRelationship> rels =  taxon.getTaxonRelations(accepted);
248

    
249
        			 if (rels.iterator().hasNext()){
250
        				 TaxonRelationship rel = rels.iterator().next();
251
        				 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
252
	        				 getViewer().setInput(rel);
253
	        				  showViewer();
254
	        				  return;
255
        				 }
256
        			 }
257
        		}
258

    
259

    
260
        	}
261
        }
262
        getViewer().setInput(element);
263
        showViewer();
264
    }
265
}
266

    
267

    
(2-2/3)