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.descriptiveDataSet.matrix;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
import java.util.UUID;
|
18
|
import java.util.stream.Collectors;
|
19
|
|
20
|
import org.eclipse.jface.layout.GridDataFactory;
|
21
|
import org.eclipse.jface.window.Window;
|
22
|
import org.eclipse.swt.SWT;
|
23
|
import org.eclipse.swt.events.SelectionAdapter;
|
24
|
import org.eclipse.swt.events.SelectionEvent;
|
25
|
import org.eclipse.swt.layout.RowLayout;
|
26
|
import org.eclipse.swt.widgets.Button;
|
27
|
import org.eclipse.swt.widgets.Composite;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
30
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
31
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
32
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
33
|
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
|
34
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
35
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
36
|
import eu.etaxonomy.cdm.model.description.Feature;
|
37
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
38
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
41
|
import eu.etaxonomy.cdm.persistence.dto.SpecimenNodeWrapper;
|
42
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
43
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
44
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
45
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
46
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonSelectionDialog;
|
47
|
|
48
|
/**
|
49
|
* @author pplitzner
|
50
|
* @since Jul 9, 2018
|
51
|
*
|
52
|
*/
|
53
|
public class CharacterMatrixBottomToolbar extends Composite{
|
54
|
|
55
|
private CharacterMatrix matrix;
|
56
|
|
57
|
public CharacterMatrixBottomToolbar(CharacterMatrix matrix, int style) {
|
58
|
super(matrix, style);
|
59
|
this.matrix = matrix;
|
60
|
|
61
|
init();
|
62
|
}
|
63
|
|
64
|
private void init() {
|
65
|
|
66
|
setLayout(new RowLayout());
|
67
|
GridDataFactory.fillDefaults().grab(true, false).applyTo(this);
|
68
|
|
69
|
/**
|
70
|
* Add description button
|
71
|
*/
|
72
|
Button btnAddDescription = new Button(this, SWT.PUSH);
|
73
|
btnAddDescription.setImage(ImageResources.getImage(ImageResources.ADD_ICON_GREEN));
|
74
|
btnAddDescription.addSelectionListener(new SelectionAdapter() {
|
75
|
@Override
|
76
|
public void widgetSelected(SelectionEvent e) {
|
77
|
SpecimenSelectionDialog dialog = new SpecimenSelectionDialog(matrix.getShell(), matrix);
|
78
|
if(dialog.open()==Window.OK){
|
79
|
Collection<SpecimenNodeWrapper> wrappers = dialog.getSpecimen();
|
80
|
for (SpecimenNodeWrapper wrapper : wrappers) {
|
81
|
SpecimenDescription description = CdmStore.getService(IDescriptiveDataSetService.class)
|
82
|
.findDescriptionForDescriptiveDataSet(matrix.getDescriptiveDataSet().getUuid(),
|
83
|
wrapper.getUuidAndTitleCache().getUuid());
|
84
|
// description elements
|
85
|
Map<Feature, DescriptionElementBase> featureToElementMap = new HashMap<>();
|
86
|
Set<DescriptionElementBase> elements = description.getElements();
|
87
|
for (DescriptionElementBase descriptionElementBase : elements) {
|
88
|
Feature feature = descriptionElementBase.getFeature();
|
89
|
featureToElementMap.put(feature, descriptionElementBase);
|
90
|
}
|
91
|
RowWrapperDTO rowWrapper = CdmStore.getService(IDescriptiveDataSetService.class).createRowWrapper(description, matrix.getDescriptiveDataSet());
|
92
|
matrix.getDescriptions().add(rowWrapper);
|
93
|
matrix.getDescriptiveDataSet().addDescription(description);
|
94
|
matrix.setDirty();
|
95
|
matrix.getSpecimenCache().remove(wrapper);
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
});
|
100
|
/**
|
101
|
* Remove description button
|
102
|
*/
|
103
|
Button btnRemoveDescription = new Button(this, SWT.PUSH);
|
104
|
btnRemoveDescription.setImage(ImageResources.getImage(ImageResources.ACTIVE_DELETE_ICON));
|
105
|
btnRemoveDescription.addSelectionListener(new SelectionAdapter() {
|
106
|
@Override
|
107
|
public void widgetSelected(SelectionEvent e) {
|
108
|
int[] fullySelectedRowPositions = matrix.getBodyLayer().getSelectionLayer().getFullySelectedRowPositions();
|
109
|
List<RowWrapperDTO> toRemove = new ArrayList<>();
|
110
|
for (int i : fullySelectedRowPositions) {
|
111
|
Object rowObject = matrix.getBodyDataProvider().getRowObject(i);
|
112
|
if(rowObject instanceof RowWrapperDTO){
|
113
|
toRemove.add((RowWrapperDTO) rowObject);
|
114
|
}
|
115
|
}
|
116
|
toRemove.forEach(rowToRemove->{
|
117
|
matrix.getDescriptions().remove(rowToRemove);
|
118
|
matrix.getDescriptiveDataSet().removeDescription(rowToRemove.getSpecimenDescription());
|
119
|
matrix.setDirty();
|
120
|
});
|
121
|
}
|
122
|
});
|
123
|
/**
|
124
|
* Aggregate button
|
125
|
*/
|
126
|
Button btnAggregate = new Button(this, SWT.PUSH);
|
127
|
btnAggregate.setText("Aggregate");
|
128
|
btnAggregate.addSelectionListener(new SelectionAdapter() {
|
129
|
@Override
|
130
|
public void widgetSelected(SelectionEvent e) {
|
131
|
Set<TaxonNode> taxonSubtreeFilter = matrix.getDescriptiveDataSet().getTaxonSubtreeFilter();
|
132
|
List<TaxonNodeDto> nodeDtos = taxonSubtreeFilter.stream()
|
133
|
.map(node -> new TaxonNodeDto(node)).collect(Collectors.toList());
|
134
|
TaxonNodeDto parentDto = CdmStore.getService(ITaxonNodeService.class).findCommonParentDto(nodeDtos);
|
135
|
UUID taxonUuid = parentDto.getTaxonUuid();
|
136
|
int response = MessagingUtils.confirmDialog(
|
137
|
"Choose location for the aggregated description",
|
138
|
String.format("The aggregated description will be stored at "
|
139
|
+ "the common parent taxon of this data set:\n%s\n\n"
|
140
|
+ "Do you want to use this taxon?"
|
141
|
, parentDto.getTaxonTitleCache()), "Yes", "Choose taxon", "Cancel");
|
142
|
if(response==2){
|
143
|
return;
|
144
|
}
|
145
|
else if(response==1){
|
146
|
Taxon taxon = TaxonSelectionDialog.selectTaxon(getShell(), null);
|
147
|
if(taxon==null){
|
148
|
return;
|
149
|
}
|
150
|
taxonUuid = taxon.getUuid();
|
151
|
}
|
152
|
UpdateResult result = CdmStore.getService(IDescriptionService.class).aggregateDescription(taxonUuid, null, matrix.getDescriptiveDataSet().getUuid());
|
153
|
matrix.addUpdateResult(result);
|
154
|
matrix.setDirty();
|
155
|
|
156
|
aggregateCategorcialHistogram(matrix.getFeatureToHistogramMap());
|
157
|
aggregateQuantitativeSummary(matrix.getFeatureToQuantDataStatisticsMap());
|
158
|
}
|
159
|
|
160
|
});
|
161
|
}
|
162
|
|
163
|
@SuppressWarnings("unchecked")
|
164
|
private void aggregateCategorcialHistogram(Map<Feature, CategoricalDataHistogram> featureToHistogramMap) {
|
165
|
featureToHistogramMap.clear();
|
166
|
matrix.getDescriptions()
|
167
|
.forEach(o -> ((RowWrapperDTO) o).getSpecimenDescription().getElements().stream()
|
168
|
.filter(descriptionElement -> descriptionElement instanceof CategoricalData)
|
169
|
.forEach(categoricalData -> {
|
170
|
Feature feature = ((CategoricalData) categoricalData).getFeature();
|
171
|
CategoricalDataHistogram dataHistogram = featureToHistogramMap.get(feature);
|
172
|
if(dataHistogram==null){
|
173
|
dataHistogram = new CategoricalDataHistogram(feature);
|
174
|
}
|
175
|
featureToHistogramMap.put(feature, dataHistogram);
|
176
|
((CategoricalData) categoricalData).getStateData()
|
177
|
.forEach(stateData -> featureToHistogramMap.get(feature).addState(stateData.getState()));
|
178
|
}));
|
179
|
}
|
180
|
|
181
|
@SuppressWarnings("unchecked")
|
182
|
private void aggregateQuantitativeSummary(Map<Feature, QuantitativeDataStatistics> featureToQuantDataStatisticsMap) {
|
183
|
featureToQuantDataStatisticsMap.clear();
|
184
|
matrix.getDescriptions()
|
185
|
.forEach(o -> ((RowWrapperDTO) o).getSpecimenDescription().getElements().stream()
|
186
|
.filter(descriptionElement -> descriptionElement instanceof QuantitativeData)
|
187
|
.forEach(quantData -> {
|
188
|
Feature feature = ((QuantitativeData) quantData).getFeature();
|
189
|
QuantitativeDataStatistics dataStatistics = featureToQuantDataStatisticsMap.get(feature);
|
190
|
if(dataStatistics==null){
|
191
|
dataStatistics = new QuantitativeDataStatistics();
|
192
|
}
|
193
|
featureToQuantDataStatisticsMap.put(feature, dataStatistics);
|
194
|
dataStatistics.addQuantitativeData((QuantitativeData) quantData);
|
195
|
}));
|
196
|
}
|
197
|
|
198
|
}
|