1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2018 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.quantitative;
|
11
|
|
12
|
import java.math.BigDecimal;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Map.Entry;
|
18
|
|
19
|
import org.eclipse.swt.SWT;
|
20
|
import org.eclipse.swt.custom.ScrolledComposite;
|
21
|
import org.eclipse.swt.events.SelectionAdapter;
|
22
|
import org.eclipse.swt.events.SelectionEvent;
|
23
|
import org.eclipse.swt.layout.GridData;
|
24
|
import org.eclipse.swt.layout.GridLayout;
|
25
|
import org.eclipse.swt.widgets.Button;
|
26
|
import org.eclipse.swt.widgets.Combo;
|
27
|
import org.eclipse.swt.widgets.Composite;
|
28
|
import org.eclipse.swt.widgets.Label;
|
29
|
import org.eclipse.swt.widgets.Text;
|
30
|
|
31
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
32
|
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
|
33
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
34
|
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
|
35
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
36
|
|
37
|
/**
|
38
|
* @author pplitzner
|
39
|
* @date 16.07.2018
|
40
|
*
|
41
|
*/
|
42
|
public class QuantitativeDataDialogComposite extends Composite {
|
43
|
|
44
|
private Map<StatisticalMeasure, List<Text>> textFieldMap = new HashMap<>();
|
45
|
QuantitativeData editorValue;
|
46
|
Combo unitCombo;
|
47
|
Map<MeasurementUnit, Integer> unitMap = null;
|
48
|
|
49
|
public QuantitativeDataDialogComposite(Character initialInput, QuantitativeData editorVal, Composite parent, int style) {
|
50
|
super(parent, style);
|
51
|
this.editorValue = editorVal;
|
52
|
if (editorValue.getFeature().getRecommendedMeasurementUnits() != null && editorValue.getFeature().getRecommendedMeasurementUnits().size()>0){
|
53
|
unitMap = new HashMap<>();
|
54
|
Integer i = 0;
|
55
|
for (MeasurementUnit unit: editorValue.getFeature().getRecommendedMeasurementUnits()){
|
56
|
unitMap.put(unit, i);
|
57
|
i++;
|
58
|
}
|
59
|
}
|
60
|
|
61
|
setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
|
62
|
|
63
|
setLayout(new GridLayout(1, false));
|
64
|
|
65
|
ScrolledComposite scrolledComposite_1 = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL);
|
66
|
scrolledComposite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
|
67
|
scrolledComposite_1.setExpandHorizontal(true);
|
68
|
scrolledComposite_1.setExpandVertical(true);
|
69
|
|
70
|
Composite valuesComposite = new Composite(scrolledComposite_1, SWT.NONE);
|
71
|
GridLayout gl_valuesComposite = new GridLayout();
|
72
|
gl_valuesComposite.marginWidth = 0;
|
73
|
gl_valuesComposite.marginHeight = 0;
|
74
|
valuesComposite.setLayout(gl_valuesComposite);
|
75
|
|
76
|
Composite composite_2 = new Composite(valuesComposite, SWT.NONE);
|
77
|
composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
|
78
|
GridLayout gl_composite_2 = new GridLayout(2, false);
|
79
|
gl_composite_2.marginHeight = 0;
|
80
|
gl_composite_2.marginWidth = 0;
|
81
|
gl_composite_2.verticalSpacing = 0;
|
82
|
gl_composite_2.horizontalSpacing = 2;
|
83
|
composite_2.setLayout(gl_composite_2);
|
84
|
|
85
|
Label lblNewLabel = new Label(composite_2, SWT.NONE);
|
86
|
lblNewLabel.setText("Measurement Unit");
|
87
|
lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
88
|
if (editorValue.getFeature().getRecommendedMeasurementUnits() != null && editorValue.getFeature().getRecommendedMeasurementUnits().size() == 1){
|
89
|
Label lblUnit = new Label(composite_2, SWT.BORDER);
|
90
|
lblUnit.setText(editorValue.getFeature().getRecommendedMeasurementUnits().iterator().next().getLabel());
|
91
|
lblUnit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
92
|
editorValue.setUnit(editorValue.getFeature().getRecommendedMeasurementUnits().iterator().next());
|
93
|
}else if (editorValue.getFeature().getRecommendedMeasurementUnits() != null && editorValue.getFeature().getRecommendedMeasurementUnits().size() > 1){
|
94
|
unitCombo = new Combo(composite_2, SWT.NONE | SWT.READ_ONLY);
|
95
|
unitCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
96
|
for (Entry<MeasurementUnit, Integer> unit: unitMap.entrySet()){
|
97
|
unitCombo.add(unit.getKey().getTitleCache(), unit.getValue());
|
98
|
unitCombo.setData(unit.getKey().getTitleCache(), unit.getKey());
|
99
|
}
|
100
|
if (editorValue.getUnit()!= null){
|
101
|
unitCombo.select(unitMap.get(editorValue.getUnit()));
|
102
|
}
|
103
|
|
104
|
unitCombo.addSelectionListener(new SelectionAdapter(){
|
105
|
|
106
|
@Override
|
107
|
public void widgetSelected(SelectionEvent e) {
|
108
|
String name = unitCombo.getText();
|
109
|
editorValue.setUnit((MeasurementUnit)unitCombo.getData(name));
|
110
|
}
|
111
|
|
112
|
});
|
113
|
}
|
114
|
|
115
|
//add empty text field for exact value
|
116
|
Text emptyTextField = addText(valuesComposite, StatisticalMeasure.EXACT_VALUE(), initialInput==null?null:initialInput.toString());
|
117
|
emptyTextField.setFocus();
|
118
|
emptyTextField.setSelection(emptyTextField.getText().length());
|
119
|
|
120
|
//add existing exact values
|
121
|
editorValue.getStatisticalValues().stream()
|
122
|
.filter(measure->measure.getType().equals(StatisticalMeasure.EXACT_VALUE()))
|
123
|
.forEach(exact->addText(valuesComposite, exact.getType(), exact.getValue().toString()));
|
124
|
|
125
|
|
126
|
|
127
|
//add aggregation values
|
128
|
editorValue.getFeature().getRecommendedStatisticalMeasures()
|
129
|
.stream()
|
130
|
.filter(sm->!sm.equals(StatisticalMeasure.EXACT_VALUE()))
|
131
|
.forEach(measure->{
|
132
|
BigDecimal specificStatisticalValue = editorValue.getSpecificStatisticalValue(measure);
|
133
|
addText(valuesComposite, measure, specificStatisticalValue!=null?specificStatisticalValue.toString():null);
|
134
|
});
|
135
|
|
136
|
scrolledComposite_1.setContent(valuesComposite);
|
137
|
scrolledComposite_1.setMinSize(valuesComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
|
138
|
|
139
|
if(initialInput!=null){
|
140
|
enableAggregationFields(false);
|
141
|
}
|
142
|
}
|
143
|
|
144
|
private Text addText(Composite valuesComposite, StatisticalMeasure type, String value){
|
145
|
Composite composite_2 = new Composite(valuesComposite, SWT.NONE);
|
146
|
composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
|
147
|
GridLayout gl_composite_2 = new GridLayout(4, false);
|
148
|
gl_composite_2.marginHeight = 0;
|
149
|
gl_composite_2.marginWidth = 0;
|
150
|
gl_composite_2.verticalSpacing = 0;
|
151
|
gl_composite_2.horizontalSpacing = 2;
|
152
|
composite_2.setLayout(gl_composite_2);
|
153
|
|
154
|
Label lblNewLabel = new Label(composite_2, SWT.NONE);
|
155
|
lblNewLabel.setText(type.getLabel());
|
156
|
|
157
|
Text text = new Text(composite_2, SWT.BORDER);
|
158
|
|
159
|
GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
|
160
|
gd_text.widthHint = 15;
|
161
|
text.setLayoutData(gd_text);
|
162
|
if(value!=null){
|
163
|
text.setText(value);
|
164
|
}
|
165
|
|
166
|
List<Text> list = textFieldMap.get(type);
|
167
|
if(list==null){
|
168
|
list = new ArrayList<>();
|
169
|
}
|
170
|
textFieldMap.put(type, list);
|
171
|
list.add(text);
|
172
|
|
173
|
Button btnNewButton_2 = new Button(composite_2, SWT.NONE);
|
174
|
btnNewButton_2.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
|
175
|
new Label(composite_2, SWT.NONE);
|
176
|
btnNewButton_2.addSelectionListener(new SelectionAdapter() {
|
177
|
@Override
|
178
|
public void widgetSelected(SelectionEvent e) {
|
179
|
text.setText("");
|
180
|
}
|
181
|
});
|
182
|
text.addModifyListener(e->{
|
183
|
boolean hasExactValues = textFieldMap.entrySet().stream()
|
184
|
.filter(entry->entry.getKey().equals(StatisticalMeasure.EXACT_VALUE()))
|
185
|
.anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
|
186
|
);
|
187
|
if(hasExactValues){
|
188
|
enableAggregationFields(false);
|
189
|
}
|
190
|
else{
|
191
|
enableAggregationFields(true);
|
192
|
}
|
193
|
});
|
194
|
return text;
|
195
|
}
|
196
|
|
197
|
private void enableAggregationFields(boolean enabled){
|
198
|
textFieldMap.entrySet().stream()
|
199
|
.filter(entry->!entry.getKey().equals(StatisticalMeasure.EXACT_VALUE()))
|
200
|
.forEach(aggEntry->aggEntry.getValue().forEach(aggText->aggText.setEnabled(enabled)));
|
201
|
}
|
202
|
|
203
|
public Map<StatisticalMeasure, List<Text>> getTextFields() {
|
204
|
return textFieldMap;
|
205
|
}
|
206
|
|
207
|
public QuantitativeData getEditorValue() {
|
208
|
return editorValue;
|
209
|
}
|
210
|
|
211
|
}
|