1
|
/**
|
2
|
* Copyright (C) 2018 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.editor.workingSet.matrix;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Set;
|
16
|
|
17
|
import org.eclipse.jface.dialogs.Dialog;
|
18
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
19
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
20
|
import org.eclipse.jface.viewers.LabelProvider;
|
21
|
import org.eclipse.jface.viewers.TableViewer;
|
22
|
import org.eclipse.swt.graphics.Image;
|
23
|
import org.eclipse.swt.widgets.Composite;
|
24
|
import org.eclipse.swt.widgets.Control;
|
25
|
import org.eclipse.swt.widgets.Shell;
|
26
|
|
27
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
28
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
29
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
30
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
31
|
import eu.etaxonomy.cdm.model.description.WorkingSet;
|
32
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
35
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
36
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
37
|
|
38
|
/**
|
39
|
* @author pplitzner
|
40
|
* @since Jan 3, 2018
|
41
|
*
|
42
|
*/
|
43
|
public class DescriptionSelectionDialog extends Dialog {
|
44
|
|
45
|
private TableViewer list;
|
46
|
|
47
|
private WorkingSet workingSet;
|
48
|
|
49
|
Collection<DescriptionBase> selectedDescriptions = new ArrayList<>();
|
50
|
|
51
|
protected DescriptionSelectionDialog(Shell parentShell, WorkingSet workingSet) {
|
52
|
super(parentShell);
|
53
|
this.workingSet = workingSet;
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
protected Control createDialogArea(Composite parent) {
|
58
|
Composite composite = (Composite) super.createDialogArea(parent);
|
59
|
list = new TableViewer(composite);
|
60
|
list.setContentProvider(new ArrayContentProvider());
|
61
|
list.setLabelProvider(new DescriptionLabelProvider());
|
62
|
|
63
|
//gather descriptions
|
64
|
Set<TaxonNode> taxonNodes = workingSet.getTaxonSubtreeFilter();
|
65
|
Set<SpecimenDescription> specimenDescriptions = new HashSet<>();
|
66
|
for (TaxonNode node : taxonNodes) {
|
67
|
specimenDescriptions.addAll(addDescriptionsRecursively(node));
|
68
|
}
|
69
|
list.setInput(specimenDescriptions);
|
70
|
return composite;
|
71
|
}
|
72
|
|
73
|
private Set<SpecimenDescription> addDescriptionsRecursively(TaxonNode node){
|
74
|
Set<SpecimenDescription> descriptions = new HashSet<>();
|
75
|
if(node.getTaxon()!=null){
|
76
|
Taxon taxon = HibernateProxyHelper.deproxy(node.getTaxon(), Taxon.class);
|
77
|
Collection<SpecimenOrObservationBase> specimens = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(SpecimenOrObservationBase.class, null, taxon, null, null, null, null, null);
|
78
|
for (SpecimenOrObservationBase specimen : specimens) {
|
79
|
Set<SpecimenDescription> specimenDescriptions = specimen.getSpecimenDescriptions();
|
80
|
for (SpecimenDescription specimenDescription : specimenDescriptions) {
|
81
|
descriptions.add(specimenDescription);
|
82
|
}
|
83
|
}
|
84
|
}
|
85
|
List<TaxonNode> childNodes = node.getChildNodes();
|
86
|
for (TaxonNode childNode : childNodes) {
|
87
|
descriptions.addAll(addDescriptionsRecursively(childNode));
|
88
|
}
|
89
|
return descriptions;
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* {@inheritDoc}
|
94
|
*/
|
95
|
@Override
|
96
|
protected void okPressed() {
|
97
|
selectedDescriptions = ((IStructuredSelection)list.getSelection()).toList();
|
98
|
super.okPressed();
|
99
|
}
|
100
|
public Collection<DescriptionBase> getDescriptions(){
|
101
|
return selectedDescriptions;
|
102
|
}
|
103
|
|
104
|
private class DescriptionLabelProvider extends LabelProvider{
|
105
|
/**
|
106
|
* {@inheritDoc}
|
107
|
*/
|
108
|
@Override
|
109
|
public Image getImage(Object element) {
|
110
|
if(workingSet.getDescriptions().contains(element)){
|
111
|
return ImageResources.getImage(ImageResources.SYNCED);
|
112
|
}
|
113
|
return null;
|
114
|
}
|
115
|
}
|
116
|
}
|