2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.e4
;
11 import java
.util
.Arrays
;
12 import java
.util
.HashMap
;
13 import java
.util
.List
;
16 import javax
.annotation
.PostConstruct
;
17 import javax
.inject
.Inject
;
19 import org
.eclipse
.e4
.core
.contexts
.ContextInjectionFactory
;
20 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
21 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
22 import org
.eclipse
.e4
.ui
.di
.UIEventTopic
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
24 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
25 import org
.eclipse
.jface
.viewers
.AbstractTreeViewer
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
28 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.TreeNode
;
30 import org
.eclipse
.jface
.viewers
.TreeViewer
;
31 import org
.eclipse
.jface
.viewers
.Viewer
;
32 import org
.eclipse
.swt
.SWT
;
33 import org
.eclipse
.swt
.dnd
.DND
;
34 import org
.eclipse
.swt
.dnd
.Transfer
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Tree
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
42 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
43 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
45 import eu
.etaxonomy
.taxeditor
.bulkeditor
.e4
.BulkEditor
;
46 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
47 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
48 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.TaxonNameEditorE4
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.concept
.e4
.ConceptViewPartE4
;
50 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptionElementDragListener
;
51 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptionElementDropAdapter
;
52 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptionElementTransfer
;
53 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptiveContentProvider
;
54 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptiveLabelProvider
;
55 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptiveViewerSorter
;
56 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.NameDescriptionFilter
;
57 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.e4
.MediaViewPartE4
;
58 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
59 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
60 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
61 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
62 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
63 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
64 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
65 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
66 import eu
.etaxonomy
.taxeditor
.view
.e4
.AbstractCdmEditorPartE4
;
67 import eu
.etaxonomy
.taxeditor
.view
.e4
.details
.DetailsPartE4
;
68 import eu
.etaxonomy
.taxeditor
.view
.e4
.supplementaldata
.SupplementalDataPartE4
;
69 import eu
.etaxonomy
.taxeditor
.workbench
.part
.ICollapsableExpandable
;
75 public class FactualDataPartE4
extends AbstractCdmEditorPartE4
76 implements IPartContentHasDetails
, IPartContentHasSupplementalData
,
77 ICdmEntitySessionEnabled
<CdmBase
>, ICollapsableExpandable
{
80 * Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
81 * This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
83 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
= new HashMap
<>();
85 protected int dndOperations
= DND
.DROP_COPY
| DND
.DROP_MOVE
;
87 private DescriptiveContentProvider provider
;
90 public void create(Composite parent
, EMenuService menuService
, MPart thisPart
,
91 IEclipseContext context
) {
92 TreeViewer treeViewer
= new TreeViewer(new Tree(parent
, SWT
.H_SCROLL
93 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
| SWT
.MULTI
));
95 this.thisPart
= thisPart
;
97 provider
= new DescriptiveContentProvider(featureNodeContainerCache
);
98 treeViewer
.setContentProvider(provider
);
99 treeViewer
.setLabelProvider(new DescriptiveLabelProvider());
100 treeViewer
.setSorter(new DescriptiveViewerSorter());
101 treeViewer
.setFilters(new NameDescriptionFilter());
102 treeViewer
.setAutoExpandLevel(2);
103 Transfer
[] transfers
= new Transfer
[] { DescriptionElementTransfer
.getInstance() };
104 treeViewer
.addDragSupport(dndOperations
, transfers
, new DescriptionElementDragListener(
110 // Propagate selection from viewer
111 selectionChangedListener
= (event
-> selService
.setSelection(event
.getSelection()));
112 viewer
.addSelectionChangedListener(selectionChangedListener
);
113 DescriptionElementDropAdapter dropListener
= new DescriptionElementDropAdapter(viewer
);
114 ContextInjectionFactory
.inject(dropListener
, context
);
116 treeViewer
.addDropSupport(dndOperations
, transfers
,
118 //create context menu
119 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.factualDataView");
124 public void selectionChanged_internal(Object selection
, MPart activePart
, MPart thisPart
) {
125 if(activePart
==thisPart
){
129 Object partObject
= createPartObject(activePart
);
131 if ((partObject
instanceof DetailsPartE4
&& !(selection
instanceof TaxonName
))|| partObject
instanceof SupplementalDataPartE4
132 || partObject
instanceof MediaViewPartE4
|| partObject
instanceof ConceptViewPartE4
|| ((partObject
instanceof AbstractCdmEditorPartE4
) &&(((AbstractCdmEditorPartE4
)partObject
).getViewer().getInput() instanceof DescriptionElementBase
))) {
133 // do not show empty page as these views are also used to edit the
134 // description selected in this view
138 if(partObject
instanceof IPartContentHasFactualData
){
139 if (!viewer
.getControl().isDisposed()){
140 viewer
.getControl().setEnabled(isEnabled());
143 IStructuredSelection structuredSelection
= createSelection(selection
);
144 if(structuredSelection
==null || structuredSelection
.isEmpty()){
149 Object firstElement
= structuredSelection
.getFirstElement();
150 if (partObject
instanceof TaxonNameEditorE4
){
151 if (firstElement
!= ((TaxonNameEditorE4
)partObject
).getTaxon()){
157 if(firstElement
instanceof TreeNode
){
158 firstElement
= ((TreeNode
) firstElement
).getValue();
160 if(firstElement
instanceof IDescribable
<?
>
161 && partObject
instanceof IPartContentHasFactualData
162 && !(firstElement
instanceof SpecimenOrObservationBase
&& partObject
instanceof BulkEditor
)){
163 // && !(firstElement instanceof TaxonName && partObject instanceof BulkEditor)){
164 if (firstElement
instanceof TaxonName
){
165 thisPart
.setLabel(Messages
.NameFactsDataView_Lable
);
166 }else if (firstElement
instanceof TaxonBase
){
167 thisPart
.setLabel(Messages
.TaxaFactsDataView_Lable
);
168 }else if (firstElement
instanceof SpecimenOrObservationBase
) {
169 thisPart
.setLabel(Messages
.OccurrenceFactsDataView_Lable
);
171 thisPart
.setLabel(Messages
.FactualDataView_Lable
);
173 // featureNodeContainerCache.clear();
174 showViewer(structuredSelection
, activePart
, viewer
);
175 if ( viewer
.getControl() != null &&!this.viewer
.getControl().isDisposed()){
176 this.viewer
.getControl().setEnabled(isEnabled());
187 protected String
getViewName(){
188 return Messages
.DescriptiveViewPart_FACTUAL_DATA
;
192 public void changed(Object object
) {
193 Object
[] expandedObjects
= null;
194 if (!viewer
.getControl().isDisposed()){
195 expandedObjects
= ((TreeViewer
)viewer
).getExpandedElements();
199 for(FeatureNodeContainerTree containerTree
: featureNodeContainerCache
.values()){
200 containerTree
.buildTree();
204 super.changed(object
);
205 ((TreeViewer
) viewer
).setExpandedElements(expandedObjects
);
206 if(object
instanceof DescriptionElementBase
){
207 DescriptionElementBase descriptionElement
= (DescriptionElementBase
) object
;
208 DescriptionBase
<?
> description
= descriptionElement
.getInDescription();
209 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
210 if (containerTree
!= null){
211 FeatureNodeContainer featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
212 if (featureNodeContainer
== null){
213 containerTree
= new FeatureNodeContainerTree(description
, provider
.getFeatureTree(description
));
214 featureNodeContainerCache
.put(description
, containerTree
);
215 featureNodeContainer
= containerTree
.getFeatureNodeContainerForDescriptionElement(descriptionElement
);
217 ((AbstractTreeViewer
) viewer
).expandToLevel(featureNodeContainer
, 2);
220 StructuredSelection selection
= new StructuredSelection(object
);
221 viewer
.setSelection(selection
, true);
232 private void updateView(@UIEventTopic(WorkbenchEventConstants
.SAVE_TAXON
)boolean refresh
){
234 Object
[] expandedObjects
= ((TreeViewer
)viewer
).getExpandedElements();
236 ((TreeViewer
)viewer
).collapseAll();
237 if (expandedObjects
.length
>0){
238 ((TreeViewer
)viewer
).setExpandedElements(expandedObjects
);
246 private void updateView(@UIEventTopic(WorkbenchEventConstants
.CURRENT_ACTIVE_EDITOR
)ITaxonEditor editor
){
248 featureNodeContainerCache
.clear();
256 public void collapse() {
257 if(viewer
instanceof TreeViewer
) {
258 ((TreeViewer
) viewer
).collapseAll();
266 public void expand() {
267 if(viewer
instanceof TreeViewer
) {
268 ((TreeViewer
) viewer
).expandAll();
272 public void toggleShowOnlyIndividualAssociations(){
273 provider
.toggleShowOnlyIndividualAssociations();
278 public ICdmEntitySession
getCdmEntitySession() {
279 if(selectionProvidingPart
!= null && selectionProvidingPart
instanceof ICdmEntitySessionEnabled
) {
280 return ((ICdmEntitySessionEnabled
<?
>) selectionProvidingPart
).getCdmEntitySession();
286 public List
<CdmBase
> getRootEntities() {
287 return Arrays
.asList((CdmBase
)getViewer().getInput());
290 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
295 protected void showViewer(IStructuredSelection selection
, MPart activePart
, Viewer viewer
){
296 Object
[] expandedObjects
= null;
297 if (!((TreeViewer
)this.getViewer()).getControl().isDisposed()){
298 expandedObjects
= ((TreeViewer
)this.getViewer()).getExpandedElements();
300 super.showViewer(selection
, activePart
, viewer
);
301 if (expandedObjects
== null || expandedObjects
.length
== 0){
302 ((TreeViewer
)this.viewer
).expandToLevel(2);
304 ((TreeViewer
)this.viewer
).setExpandedElements(expandedObjects
);