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.HashSet;
|
12
|
import java.util.Set;
|
13
|
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
|
16
|
import org.eclipse.nebula.widgets.nattable.data.ListDataProvider;
|
17
|
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
|
18
|
import org.eclipse.nebula.widgets.nattable.summaryrow.DefaultSummaryRowConfiguration;
|
19
|
import org.eclipse.nebula.widgets.nattable.summaryrow.ISummaryProvider;
|
20
|
import org.eclipse.nebula.widgets.nattable.summaryrow.SummaryRowConfigAttributes;
|
21
|
import org.eclipse.nebula.widgets.nattable.summaryrow.SummaryRowLayer;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.dto.TaxonRowWrapperDTO;
|
24
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
25
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
26
|
import eu.etaxonomy.cdm.model.description.Feature;
|
27
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
28
|
|
29
|
/**
|
30
|
* Configuration for the aggregation algorithm for the columns
|
31
|
* of the character matrix
|
32
|
* @author pplitzner
|
33
|
* @since Jan 4, 2018
|
34
|
*
|
35
|
*/
|
36
|
public class AggregationConfiguration extends DefaultSummaryRowConfiguration {
|
37
|
|
38
|
private CharacterMatrix matrix;
|
39
|
|
40
|
public AggregationConfiguration(CharacterMatrix matrix) {
|
41
|
this.matrix = matrix;
|
42
|
}
|
43
|
|
44
|
@Override
|
45
|
public void addSummaryProviderConfig(IConfigRegistry configRegistry) {
|
46
|
for(int i=0;i<matrix.getFeatures().size();i++){
|
47
|
//no summary for the supplemental columns
|
48
|
if(i<CharacterMatrix.LEADING_COLUMN_COUNT){
|
49
|
int index = i;
|
50
|
configRegistry.registerConfigAttribute(
|
51
|
SummaryRowConfigAttributes.SUMMARY_PROVIDER,
|
52
|
columnIndex -> "",
|
53
|
DisplayMode.NORMAL,
|
54
|
SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+index);
|
55
|
}
|
56
|
//register aggregation configuration for each feature
|
57
|
Feature feature = matrix.getFeatures().get(i);
|
58
|
if(feature.isSupportsQuantitativeData()){
|
59
|
configRegistry.registerConfigAttribute(
|
60
|
SummaryRowConfigAttributes.SUMMARY_PROVIDER,
|
61
|
new QuantitativeSummaryProvider(),
|
62
|
DisplayMode.NORMAL,
|
63
|
SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+MatrixUtility.getProperty(feature));
|
64
|
}
|
65
|
if(feature.isSupportsCategoricalData()){
|
66
|
configRegistry.registerConfigAttribute(
|
67
|
SummaryRowConfigAttributes.SUMMARY_PROVIDER,
|
68
|
new CategoricalSummaryProvider(),
|
69
|
DisplayMode.NORMAL,
|
70
|
SummaryRowLayer.DEFAULT_SUMMARY_COLUMN_CONFIG_LABEL_PREFIX+MatrixUtility.getProperty(feature));
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
|
75
|
private class QuantitativeSummaryProvider implements ISummaryProvider {
|
76
|
@Override
|
77
|
public Object summarize(int columnIndex) {
|
78
|
Float minTotal = null;
|
79
|
Float maxTotal = null;
|
80
|
ListDataProvider<Object> bodyDataProvider = AggregationConfiguration.this.matrix.getBodyDataProvider();
|
81
|
int rowCount = bodyDataProvider.getRowCount();
|
82
|
|
83
|
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
|
84
|
Object rowObject = bodyDataProvider.getRowObject(rowIndex);
|
85
|
if(rowObject instanceof TaxonRowWrapperDTO){
|
86
|
// do not aggregate taxon descriptions
|
87
|
continue;
|
88
|
}
|
89
|
Object dataValue = bodyDataProvider.getDataValue(columnIndex,
|
90
|
rowIndex);
|
91
|
if(HibernateProxyHelper.isInstanceOf(dataValue, QuantitativeData.class)){
|
92
|
QuantitativeData quantitativeData = HibernateProxyHelper.deproxy(dataValue, QuantitativeData.class);
|
93
|
Float min = quantitativeData.getMin();
|
94
|
Float max = quantitativeData.getMax();
|
95
|
if(min!=null && (minTotal==null || min<minTotal)){
|
96
|
minTotal = min;
|
97
|
}
|
98
|
if(max!=null && (maxTotal==null || max>maxTotal)){
|
99
|
maxTotal = max;
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
String summaryString = MatrixUtility.getQuantitativeLabel(minTotal, null, maxTotal);
|
104
|
return summaryString;
|
105
|
}
|
106
|
|
107
|
}
|
108
|
|
109
|
private class CategoricalSummaryProvider implements ISummaryProvider {
|
110
|
@Override
|
111
|
public Object summarize(int columnIndex) {
|
112
|
int rowCount = AggregationConfiguration.this.matrix.getBodyDataProvider().getRowCount();
|
113
|
Set states = new HashSet<>();
|
114
|
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
|
115
|
Object dataValue = AggregationConfiguration.this.matrix.getBodyDataProvider().getDataValue(columnIndex,
|
116
|
rowIndex);
|
117
|
if(dataValue instanceof CategoricalData){
|
118
|
states.addAll(((CategoricalData) dataValue).getStatesOnly());
|
119
|
}
|
120
|
}
|
121
|
return StringUtils.join(states, ", "); //$NON-NLS-1$
|
122
|
}
|
123
|
}
|
124
|
}
|