Project

General

Profile

Download (6.48 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.e4.supplementaldata;
11

    
12
import java.util.Set;
13

    
14
import javax.annotation.PostConstruct;
15
import javax.inject.Inject;
16

    
17
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
18
import org.eclipse.e4.core.contexts.IEclipseContext;
19
import org.eclipse.e4.core.di.annotations.Optional;
20
import org.eclipse.e4.core.services.log.Logger;
21
import org.eclipse.e4.ui.di.UIEventTopic;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.jface.viewers.StructuredSelection;
25
import org.eclipse.jface.viewers.TreeNode;
26
import org.eclipse.swt.SWTException;
27
import org.eclipse.swt.widgets.Composite;
28

    
29
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
30
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse;
31
import eu.etaxonomy.cdm.model.common.ICdmBase;
32
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
33
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
36
import eu.etaxonomy.cdm.persistence.dto.TermDto;
37
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
38
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
39
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
40
import eu.etaxonomy.taxeditor.l10n.Messages;
41
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
42
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
43
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
46

    
47

    
48
/**
49
 *
50
 * @author pplitzner
51
 * @since Aug 10, 2017
52
 *
53
 */
54
public class SupplementalDataPartE4 extends AbstractCdmEditorPartE4 {
55
	@Inject
56
	private Logger logger;
57

    
58
	@Inject
59
    public SupplementalDataPartE4() {
60
    }
61

    
62
	@PostConstruct
63
	public void create(Composite parent, MPart thisPart, IEclipseContext context) {
64

    
65
	    this.thisPart = thisPart;
66

    
67
	    viewer = ContextInjectionFactory.make(SupplementalDataViewerE4.class, context);
68
	    ((SupplementalDataViewerE4)viewer).init(parent, this);
69
	}
70

    
71
    @Override
72
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
73
	    if(activePart==thisPart){
74
	        return;
75
	    }
76

    
77
        Object partObject = createPartObject(activePart);
78

    
79
	    if(partObject instanceof IPartContentHasSupplementalData) {
80

    
81
	        IStructuredSelection structuredSelection = createSelection(selection);
82
	        if(structuredSelection==null || structuredSelection.isEmpty()){
83
	            showEmptyPage();
84
	            return;
85
	        }
86
	        if(structuredSelection.size() != 1){
87
	            showEmptyPage();
88
	            return;
89
	        }
90
	        if (partObject instanceof ITaxonEditor && structuredSelection.getFirstElement() instanceof Taxon ){
91
	            if (((ITaxonEditor)partObject).getTaxon() != structuredSelection.getFirstElement() && ((Taxon)structuredSelection.getFirstElement()).isMisapplication()){
92
	                Set<TaxonRelationship> rels =((Taxon)structuredSelection.getFirstElement()).getTaxonRelations(((ITaxonEditor)partObject).getTaxon());
93
	                if (rels != null && rels.size() == 1){
94
	                    structuredSelection = new StructuredSelection(rels.iterator().next());
95
	                }else{
96
	                    showEmptyPage();
97
	                    return;
98
	                }
99

    
100
	            }
101
	        }
102
	        // do not show supplemental data for feature nodes
103
	        if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
104
	            showEmptyPage();
105
	            return;
106
	        }
107
	        else if(structuredSelection.getFirstElement() instanceof DerivedUnitFacade){
108
	            return;
109
	        }
110
	        else if(structuredSelection.getFirstElement() instanceof PolytomousKeyNode){
111
	            structuredSelection = new StructuredSelection(((PolytomousKeyNode)structuredSelection.getFirstElement()).getKey());
112
	        }
113
	        else if(structuredSelection.getFirstElement() instanceof TreeNode){
114
	            structuredSelection = new StructuredSelection(((TreeNode)structuredSelection.getFirstElement()).getValue());
115
	        }
116
	        else if(structuredSelection.getFirstElement() instanceof GbifResponse){
117
	            structuredSelection = new StructuredSelection(((GbifResponse)structuredSelection.getFirstElement()).getDerivedUnitFacade().innerDerivedUnit());
118
	        }
119
	        else if(structuredSelection.getFirstElement() instanceof DescriptionElementBase ){
120
                structuredSelection = new StructuredSelection((structuredSelection.getFirstElement()));
121
            }
122
	        else if(structuredSelection.getFirstElement() instanceof TermDto
123
	                || structuredSelection.getFirstElement() instanceof TermVocabularyDto){
124
	            showViewer(structuredSelection, activePart, viewer);
125
	            return;
126
	        }
127

    
128

    
129
	        if(!(structuredSelection.getFirstElement() instanceof ICdmBase)){
130
	            showEmptyPage();
131
	            return;
132
	        }
133
	        showViewer(structuredSelection, activePart, viewer);
134
	        ((SupplementalDataViewerE4)viewer).setEnabled(isEnabled());
135

    
136

    
137
	        return;
138
	    }
139
        else{
140
	        showEmptyPage();
141
	        return;
142
	    }
143
	}
144

    
145
    /**
146
     * {@inheritDoc}
147
     */
148
    @Override
149
    protected void showEmptyPage() {
150
        super.showEmptyPage();
151
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
152
        	try{
153
        		((SupplementalDataViewerE4)viewer).destroySections();
154
        	}catch (SWTException e){
155
        		if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
156
                    MessagingUtils.errorDialog("Widget is disposed",
157
                            null,
158
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
159
                            null,
160
                            e,
161
                            true);
162

    
163
                }
164
        	}
165
        }
166
    }
167

    
168
	@Override
169
    protected String getViewName() {
170
		return Messages.SupplementalDataViewPart_VIEWER_NAME;
171
	}
172

    
173
	@Inject
174
    @Optional
175
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_SUPPLEMENTAL)boolean refresh){
176
        if(refresh){
177
            viewer.refresh();
178
        }
179
    }
180
}
(1-1/2)