Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | Tag: | Revision:
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
import java.util.Set;
19

    
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.custom.ScrolledComposite;
22
import org.eclipse.swt.events.SelectionAdapter;
23
import org.eclipse.swt.events.SelectionEvent;
24
import org.eclipse.swt.layout.GridData;
25
import org.eclipse.swt.layout.GridLayout;
26
import org.eclipse.swt.widgets.Button;
27
import org.eclipse.swt.widgets.Combo;
28
import org.eclipse.swt.widgets.Composite;
29
import org.eclipse.swt.widgets.Label;
30
import org.eclipse.swt.widgets.Text;
31

    
32
import eu.etaxonomy.cdm.api.service.dto.QuantitativeDataDto;
33
import eu.etaxonomy.cdm.common.CdmUtils;
34
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
35
import eu.etaxonomy.cdm.persistence.dto.TermDto;
36
import eu.etaxonomy.taxeditor.model.ImageResources;
37

    
38
/**
39
 * @author pplitzner
40
 * @date 16.07.2018
41
 *
42
 */
43
public class QuantitativeDataDialogComposite extends Composite {
44

    
45
    private Map<TermDto, List<Text>> textFieldMap = new HashMap<>();
46
    QuantitativeDataDto editorValue;
47
    Combo unitCombo;
48
    Map<TermDto, Integer> unitMap = null;
49
    TermDto exactValueDto = TermDto.fromTerm(StatisticalMeasure.EXACT_VALUE());
50

    
51
    public QuantitativeDataDialogComposite(Character initialInput, QuantitativeDataDto editorVal, Composite parent, int style) {
52
        super(parent, style);
53
        this.editorValue = editorVal;
54
        if (editorValue.getFeatureDto().getRecommendedMeasurementUnits() != null && editorValue.getFeatureDto().getRecommendedMeasurementUnits().size()>0){
55
            unitMap = new HashMap<>();
56
            Integer i = editorValue.getFeatureDto().getRecommendedMeasurementUnits().size() -1;
57
            for (TermDto unit: editorValue.getFeatureDto().getRecommendedMeasurementUnits()){
58
                unitMap.put(unit, i);
59
                i--;
60
            }
61
        }
62

    
63
        setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
64

    
65
        setLayout(new GridLayout(1, false));
66

    
67
        ScrolledComposite scrolledComposite_1 = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL);
68
        scrolledComposite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
69
        scrolledComposite_1.setExpandHorizontal(true);
70
        scrolledComposite_1.setExpandVertical(true);
71

    
72
        Composite valuesComposite = new Composite(scrolledComposite_1, SWT.NONE);
73
        GridLayout gl_valuesComposite = new GridLayout();
74
        gl_valuesComposite.marginWidth = 0;
75
        gl_valuesComposite.marginHeight = 0;
76
        valuesComposite.setLayout(gl_valuesComposite);
77

    
78
        Composite composite_2 = new Composite(valuesComposite, SWT.NONE);
79
        composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
80
        GridLayout gl_composite_2 = new GridLayout(2, false);
81
        gl_composite_2.marginHeight = 0;
82
        gl_composite_2.marginWidth = 0;
83
        gl_composite_2.verticalSpacing = 0;
84
        gl_composite_2.horizontalSpacing = 2;
85
        composite_2.setLayout(gl_composite_2);
86

    
87
        Label lblNewLabel = new Label(composite_2, SWT.NONE);
88
        lblNewLabel.setText("Measurement Unit");
89
        lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
90
        if (editorValue.getFeatureDto().getRecommendedMeasurementUnits() != null && editorValue.getFeatureDto().getRecommendedMeasurementUnits().size() == 1){
91
            Label lblUnit = new Label(composite_2, SWT.BORDER);
92
            lblUnit.setText(editorValue.getFeatureDto().getRecommendedMeasurementUnits().iterator().next().getIdInVocabulary());
93
            lblUnit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
94
            editorValue.setMeasurementUnit(editorValue.getFeatureDto().getRecommendedMeasurementUnits().iterator().next());
95
        }else if (editorValue.getFeatureDto().getRecommendedMeasurementUnits() != null && editorValue.getFeatureDto().getRecommendedMeasurementUnits().size() > 1){
96
            unitCombo = new Combo(composite_2, SWT.NONE | SWT.READ_ONLY);
97
            unitCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
98

    
99
            Set<Entry<TermDto, Integer>> entries = unitMap.entrySet();
100
            TermDto[] terms = new TermDto[entries.size()];
101

    
102
            for (Entry<TermDto, Integer> unit: unitMap.entrySet()){
103
                if (unit.getValue() != null){
104
                    terms[unit.getValue()] = unit.getKey();
105
                }
106
            }
107
            int index = 0;
108
            for (TermDto term: terms){
109
                unitCombo.add(term.getTitleCache(), index);
110
                unitCombo.setData(term.getTitleCache(), term);
111
                index ++;
112
            }
113
            if (editorValue.getMeasurementUnit()!= null){
114
                unitCombo.select(unitMap.get(editorValue.getMeasurementUnit()));
115
            }else{
116
                unitCombo.select(0);
117
                String name = unitCombo.getText();
118
                editorValue.setMeasurementUnit((TermDto)unitCombo.getData(name));
119
            }
120

    
121
            unitCombo.addSelectionListener(new SelectionAdapter(){
122

    
123
                @Override
124
                public void widgetSelected(SelectionEvent e) {
125
                    String name = unitCombo.getText();
126
                    editorValue.setMeasurementUnit((TermDto)unitCombo.getData(name));
127
                }
128

    
129
            });
130
        }
131

    
132
        //add empty text field for exact value
133
        Text emptyTextField = addText(valuesComposite, exactValueDto, initialInput==null?null:initialInput.toString());
134
        emptyTextField.setFocus();
135
        emptyTextField.setSelection(emptyTextField.getText().length());
136

    
137
        //add existing exact values
138
        editorValue.getValues().stream()
139
        .filter(measure->measure.getType().getUuid().equals(StatisticalMeasure.EXACT_VALUE().getUuid()))
140
        .forEach(exact->addText(valuesComposite, exact.getType(), exact.getValue().toString()));
141

    
142

    
143

    
144
        //add aggregation values
145
        if (editorValue.getFeatureDto().getRecommendedMeasurementUnits() != null){
146
	        editorValue.getFeatureDto().getRecommendedStatisticalMeasures()
147
	        .stream()
148
	        .filter(sm->!sm.equals(exactValueDto))
149
	        .forEach(measure->{
150
	            BigDecimal specificStatisticalValue = editorValue.getSpecificStatisticalValue(measure.getUuid());
151
	            addText(valuesComposite, measure, specificStatisticalValue!=null?specificStatisticalValue.toString():null);
152
	        });
153
        }
154

    
155
        scrolledComposite_1.setContent(valuesComposite);
156
        scrolledComposite_1.setMinSize(valuesComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
157

    
158
//        if(initialInput!=null){
159
        	boolean hasExactValues = textFieldMap.entrySet().stream()
160
                    .filter(entry->entry.getKey().equals(exactValueDto))
161
            .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
162
            );
163
            
164
            boolean hasAggregatedValues = textFieldMap.entrySet().stream()
165
                    .filter(entry->!entry.getKey().equals(exactValueDto))
166
            .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
167
            );
168
            enableAggregationFields(hasExactValues, hasAggregatedValues);
169
//        }
170
    }
171

    
172
    private Text addText(Composite valuesComposite, TermDto type, String value){
173
        Composite composite_2 = new Composite(valuesComposite, SWT.NONE);
174
        composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
175
        GridLayout gl_composite_2 = new GridLayout(4, false);
176
        gl_composite_2.marginHeight = 0;
177
        gl_composite_2.marginWidth = 0;
178
        gl_composite_2.verticalSpacing = 0;
179
        gl_composite_2.horizontalSpacing = 2;
180
        composite_2.setLayout(gl_composite_2);
181

    
182
        Label lblNewLabel = new Label(composite_2, SWT.NONE);
183
        lblNewLabel.setText(type.getTitleCache());
184

    
185
        Text text = new Text(composite_2, SWT.BORDER);
186

    
187
        GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
188
        gd_text.widthHint = 15;
189
        text.setLayoutData(gd_text);
190
        if(value!=null){
191
            text.setText(value);
192
        }
193

    
194
        List<Text> list = textFieldMap.get(type);
195
        if(list==null){
196
            list = new ArrayList<>();
197
        }
198
        textFieldMap.put(type, list);
199
        list.add(text);
200

    
201
        Button btnRemove = new Button(composite_2, SWT.NONE);
202
        btnRemove.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
203
        new Label(composite_2, SWT.NONE);
204
        btnRemove.addSelectionListener(new SelectionAdapter() {
205
            @Override
206
            public void widgetSelected(SelectionEvent e) {
207
                text.setText("");
208
                boolean hasExactValues = textFieldMap.entrySet().stream()
209
                        .filter(entry->entry.getKey().equals(exactValueDto))
210
                .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
211
                );
212
                
213
                boolean hasAggregatedValues = textFieldMap.entrySet().stream()
214
                        .filter(entry->!entry.getKey().equals(exactValueDto))
215
                .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
216
                );
217

    
218
                enableAggregationFields(hasExactValues, hasAggregatedValues);
219
            }
220
        });
221
        text.addModifyListener(e->{
222
            boolean hasExactValues = textFieldMap.entrySet().stream()
223
                    .filter(entry->entry.getKey().equals(exactValueDto))
224
            .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
225
            );
226
            
227
            boolean hasAggregatedValues = textFieldMap.entrySet().stream()
228
                    .filter(entry->!entry.getKey().equals(exactValueDto))
229
            .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
230
            );
231

    
232
            enableAggregationFields(hasExactValues, hasAggregatedValues);
233
//            if(hasExactValues){
234
//                enableAggregationFields(false);
235
//            }
236
//            else{
237
//                enableAggregationFields(true);
238
//            }
239
        });
240
        return text;
241
    }
242

    
243
    private void enableAggregationFields(boolean hasExactValues, boolean hasAggregatedValues){
244
    	boolean enableAggregatedValues = hasAggregatedValues ==( hasAggregatedValues != hasExactValues);
245
        textFieldMap.entrySet().stream()
246
        .filter(entry->!entry.getKey().equals(exactValueDto))
247
        .forEach(aggEntry->aggEntry.getValue().forEach(aggText->aggText.setEnabled(enableAggregatedValues)));
248
        boolean enableExactValues = hasExactValues ==( hasAggregatedValues != hasExactValues);
249
        textFieldMap.entrySet().stream()
250
        .filter(entry->entry.getKey().equals(exactValueDto))
251
        .forEach(aggEntry->aggEntry.getValue().forEach(aggText->aggText.setEnabled(enableExactValues)));
252
    }
253

    
254
    public Map<TermDto, List<Text>> getTextFields() {
255
        return textFieldMap;
256
    }
257

    
258
    public QuantitativeDataDto getEditorValue() {
259
        return editorValue;
260
    }
261

    
262
}
(2-2/5)