Project

General

Profile

Download (7.41 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.DescriptionBase;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
35
import eu.etaxonomy.cdm.persistence.dto.TermDto;
36
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
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
 * @author pplitzner
49
 * @since Aug 10, 2017
50
 */
51
public class SupplementalDataPartE4 extends AbstractCdmEditorPartE4 {
52

    
53
	@Inject
54
    public SupplementalDataPartE4() {}
55

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

    
59
	    this.thisPart = thisPart;
60

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

    
65
    @Override
66
    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
67
	    if(activePart==thisPart){
68
	        return;
69
	    }
70
	    setEnabled(true);
71
        Object partObject = createPartObject(activePart);
72

    
73
	    if(partObject instanceof IPartContentHasSupplementalData) {
74

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

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

    
102
	        else if(structuredSelection.getFirstElement() instanceof DerivedUnitFacade){
103
	            return;
104
	        }
105
	        else if(structuredSelection.getFirstElement() instanceof PolytomousKeyNode){
106
	            structuredSelection = new StructuredSelection(((PolytomousKeyNode)structuredSelection.getFirstElement()).getKey());
107
	        }
108
	        else if(structuredSelection.getFirstElement() instanceof TreeNode){
109
	            structuredSelection = new StructuredSelection(((TreeNode)structuredSelection.getFirstElement()).getValue());
110
	        }
111
	        else if(structuredSelection.getFirstElement() instanceof GbifResponse){
112
	            structuredSelection = new StructuredSelection(((GbifResponse)structuredSelection.getFirstElement()).getDerivedUnitFacade().innerDerivedUnit());
113
	        }
114
	        else if(structuredSelection.getFirstElement() instanceof DescriptionElementBase ){
115
	            structuredSelection = new StructuredSelection((structuredSelection.getFirstElement()));
116

    
117
	            if (((DescriptionElementBase)structuredSelection.getFirstElement()).getInDescription() != null && ((DescriptionElementBase)structuredSelection.getFirstElement()).getInDescription().isComputed()){
118
                    if (PreferencesUtil.isComputedDesciptionHandlingDisabled()){
119
                        setEnabled(false);
120
                    }else{
121
                        setEnabled(true);
122
                    }
123
                }
124
            }
125
	        else if(structuredSelection.getFirstElement() instanceof DescriptionBase ){
126

    
127
                if (((DescriptionBase)structuredSelection.getFirstElement()).isComputed()){
128
                    if (PreferencesUtil.isComputedDesciptionHandlingDisabled()){
129
                        setEnabled(false);
130
                    }else{
131
                        setEnabled(true);
132
                    }
133
                }
134
            }
135
	        else if(structuredSelection.getFirstElement() instanceof TermDto
136
	                || structuredSelection.getFirstElement() instanceof TermVocabularyDto
137
	                || structuredSelection.getFirstElement() instanceof TermNodeDto){
138
	            showViewer(structuredSelection, activePart, viewer);
139
	            return;
140
	        }
141

    
142

    
143
	        if(!(structuredSelection.getFirstElement() instanceof ICdmBase)){
144
	            showEmptyPage();
145
	            return;
146
	        }
147
	        showViewer(structuredSelection, activePart, viewer);
148
	        ((SupplementalDataViewerE4)viewer).setEnabled(isEnabled());
149

    
150
	        return;
151
	    }
152
        else{
153
	        showEmptyPage();
154
	        return;
155
	    }
156
	}
157

    
158
    @Override
159
    protected void showEmptyPage() {
160
        super.showEmptyPage();
161
        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
162
        	try{
163
        		((SupplementalDataViewerE4)viewer).destroySections();
164
        	}catch (SWTException e){
165
        		if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
166
                    MessagingUtils.errorDialog("Widget is disposed",
167
                            null,
168
                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
169
                            null,
170
                            e,
171
                            true);
172

    
173
                }
174
        	}
175
        }
176
    }
177

    
178
	@Override
179
    protected String getViewName() {
180
		return Messages.SupplementalDataViewPart_VIEWER_NAME;
181
	}
182

    
183
	@Inject
184
    @Optional
185
    private void updateView(@UIEventTopic(WorkbenchEventConstants.REFRESH_SUPPLEMENTAL)boolean refresh){
186
        if(refresh){
187
            viewer.refresh();
188
        }
189
    }
190
}
(1-1/2)