Project

General

Profile

Download (6.35 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
package eu.etaxonomy.taxeditor.view.e4.supplementaldata;
10

    
11
import java.util.Set;
12

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

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

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

    
45
/**
46
 * @author pplitzner
47
 * @since Aug 10, 2017
48
 */
49
public class SupplementalDataPartE4 extends AbstractCdmEditorPartE4 {
50

    
51
	@Inject
52
    public SupplementalDataPartE4() {}
53

    
54
	@PostConstruct
55
	public void create(Composite parent, MPart thisPart, IEclipseContext context) {
56

    
57
	    this.thisPart = thisPart;
58

    
59
	    viewer = ContextInjectionFactory.make(SupplementalDataViewerE4.class, context);
60
	    ((SupplementalDataViewerE4)viewer).init(parent, this);
61
	}
62

    
63
    @Override
64
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
65
	    if(activePart==thisPart){
66
	        return;
67
	    }
68

    
69
        Object partObject = createPartObject(activePart);
70

    
71
	    if(partObject instanceof IPartContentHasSupplementalData) {
72

    
73
	        IStructuredSelection structuredSelection = createSelection(selection);
74
	        if(structuredSelection==null || structuredSelection.isEmpty()){
75
	            showEmptyPage();
76
	            return;
77
	        }
78
	        if(structuredSelection.size() != 1){
79
	            showEmptyPage();
80
	            return;
81
	        }
82
	        if (partObject instanceof ITaxonEditor && structuredSelection.getFirstElement() instanceof Taxon ){
83
	            if (((ITaxonEditor)partObject).getTaxon() != structuredSelection.getFirstElement() && ((Taxon)structuredSelection.getFirstElement()).isMisapplication()){
84
	                Set<TaxonRelationship> rels =((Taxon)structuredSelection.getFirstElement()).getTaxonRelations(((ITaxonEditor)partObject).getTaxon());
85
	                if (rels != null && rels.size() == 1){
86
	                    structuredSelection = new StructuredSelection(rels.iterator().next());
87
	                }else{
88
	                    showEmptyPage();
89
	                    return;
90
	                }
91

    
92
	            }
93
	        }
94
	        // do not show supplemental data for feature nodes
95
	        if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
96
	            showEmptyPage();
97
	            return;
98
	        }
99

    
100
	        else if(structuredSelection.getFirstElement() instanceof DerivedUnitFacade){
101
	            return;
102
	        }
103
	        else if(structuredSelection.getFirstElement() instanceof PolytomousKeyNode){
104
	            structuredSelection = new StructuredSelection(((PolytomousKeyNode)structuredSelection.getFirstElement()).getKey());
105
	        }
106
	        else if(structuredSelection.getFirstElement() instanceof TreeNode){
107
	            structuredSelection = new StructuredSelection(((TreeNode)structuredSelection.getFirstElement()).getValue());
108
	        }
109
	        else if(structuredSelection.getFirstElement() instanceof GbifResponse){
110
	            structuredSelection = new StructuredSelection(((GbifResponse)structuredSelection.getFirstElement()).getDerivedUnitFacade().innerDerivedUnit());
111
	        }
112
	        else if(structuredSelection.getFirstElement() instanceof DescriptionElementBase ){
113
                structuredSelection = new StructuredSelection((structuredSelection.getFirstElement()));
114
            }
115
	        else if(structuredSelection.getFirstElement() instanceof TermDto
116
	                || structuredSelection.getFirstElement() instanceof TermVocabularyDto){
117
	            showViewer(structuredSelection, activePart, viewer);
118
	            return;
119
	        }
120

    
121

    
122
	        if(!(structuredSelection.getFirstElement() instanceof ICdmBase)){
123
	            showEmptyPage();
124
	            return;
125
	        }
126
	        showViewer(structuredSelection, activePart, viewer);
127
	        ((SupplementalDataViewerE4)viewer).setEnabled(isEnabled());
128

    
129
	        return;
130
	    }
131
        else{
132
	        showEmptyPage();
133
	        return;
134
	    }
135
	}
136

    
137
    @Override
138
    protected void showEmptyPage() {
139
        super.showEmptyPage();
140
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
141
        	try{
142
        		((SupplementalDataViewerE4)viewer).destroySections();
143
        	}catch (SWTException e){
144
        		if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
145
                    MessagingUtils.errorDialog("Widget is disposed",
146
                            null,
147
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
148
                            null,
149
                            e,
150
                            true);
151

    
152
                }
153
        	}
154
        }
155
    }
156

    
157
	@Override
158
    protected String getViewName() {
159
		return Messages.SupplementalDataViewPart_VIEWER_NAME;
160
	}
161

    
162
	@Inject
163
    @Optional
164
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_SUPPLEMENTAL)boolean refresh){
165
        if(refresh){
166
            viewer.refresh();
167
        }
168
    }
169
}
(1-1/2)