Project

General

Profile

Download (8.52 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.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Map.Entry;
17

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

    
30
import eu.etaxonomy.cdm.common.CdmUtils;
31
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
32
import eu.etaxonomy.cdm.model.description.QuantitativeData;
33
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
34
import eu.etaxonomy.taxeditor.model.ImageResources;
35

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

    
43
    private Map<StatisticalMeasure, List<Text>> textFieldMap = new HashMap<>();
44
    QuantitativeData editorValue;
45
    Combo unitCombo;
46
    Map<MeasurementUnit, Integer> unitMap = null;
47

    
48
    public QuantitativeDataDialogComposite(Character initialInput, QuantitativeData editorVal, Composite parent, int style) {
49
        super(parent, style);
50
        this.editorValue = editorVal;
51
        if (editorValue.getFeature().getRecommendedMeasurementUnits() != null && editorValue.getFeature().getRecommendedMeasurementUnits().size()>0){
52
            unitMap = new HashMap<>();
53
            Integer i = 0;
54
            for (MeasurementUnit unit: editorValue.getFeature().getRecommendedMeasurementUnits()){
55
                unitMap.put(unit, i);
56
                i++;
57
            }
58
        }
59

    
60
        setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
61

    
62
        setLayout(new GridLayout(1, false));
63

    
64
        ScrolledComposite scrolledComposite_1 = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL);
65
        scrolledComposite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
66
        scrolledComposite_1.setExpandHorizontal(true);
67
        scrolledComposite_1.setExpandVertical(true);
68

    
69
        Composite valuesComposite = new Composite(scrolledComposite_1, SWT.NONE);
70
        GridLayout gl_valuesComposite = new GridLayout();
71
        gl_valuesComposite.marginWidth = 0;
72
        gl_valuesComposite.marginHeight = 0;
73
        valuesComposite.setLayout(gl_valuesComposite);
74

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

    
84
        Label lblNewLabel = new Label(composite_2, SWT.NONE);
85
        lblNewLabel.setText("Measurement Unit");
86
        lblNewLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
87
        if (editorValue.getFeature().getRecommendedMeasurementUnits() != null && editorValue.getFeature().getRecommendedMeasurementUnits().size() == 1){
88
            Label lblUnit = new Label(composite_2, SWT.BORDER);
89
            lblUnit.setText(editorValue.getFeature().getRecommendedMeasurementUnits().iterator().next().getLabel());
90
            lblUnit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
91
        }else if (editorValue.getFeature().getRecommendedMeasurementUnits() != null && editorValue.getFeature().getRecommendedMeasurementUnits().size() > 1){
92
            unitCombo = new Combo(composite_2, SWT.NONE | SWT.READ_ONLY);
93
            unitCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
94
            for (Entry<MeasurementUnit, Integer> unit: unitMap.entrySet()){
95
                unitCombo.add(unit.getKey().getTitleCache(), unit.getValue());
96
                unitCombo.setData(unit.getKey().getTitleCache(), unit.getKey());
97
            }
98
            if (editorValue.getUnit()!= null){
99
                unitCombo.select(unitMap.get(editorValue.getUnit()));
100
            }
101

    
102
            unitCombo.addSelectionListener(new SelectionAdapter(){
103

    
104
                @Override
105
                public void widgetSelected(SelectionEvent e) {
106
                    String name = unitCombo.getText();
107
                    editorValue.setUnit((MeasurementUnit)unitCombo.getData(name));
108
                }
109

    
110
            });
111
        }
112

    
113
        //add empty text field for exact value
114
        Text emptyTextField = addText(valuesComposite, StatisticalMeasure.EXACT_VALUE(), initialInput==null?null:initialInput.toString());
115
        emptyTextField.setFocus();
116
        emptyTextField.setSelection(emptyTextField.getText().length());
117

    
118
        //add existing exact values
119
        editorValue.getStatisticalValues().stream()
120
        .filter(measure->measure.getType().equals(StatisticalMeasure.EXACT_VALUE()))
121
        .forEach(exact->addText(valuesComposite, exact.getType(), Float.toString(exact.getValue())));
122

    
123
        //add aggregation values
124
        editorValue.getFeature().getRecommendedStatisticalMeasures()
125
        .stream()
126
        .filter(sm->!sm.equals(StatisticalMeasure.EXACT_VALUE()))
127
        .forEach(measure->{
128
            Float specificStatisticalValue = editorValue.getSpecificStatisticalValue(measure);
129
            addText(valuesComposite, measure, specificStatisticalValue!=null?Float.toString(specificStatisticalValue):null);
130
        });
131

    
132
        scrolledComposite_1.setContent(valuesComposite);
133
        scrolledComposite_1.setMinSize(valuesComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
134

    
135
        if(initialInput!=null){
136
            enableAggregationFields(false);
137
        }
138
    }
139

    
140
    private Text addText(Composite valuesComposite, StatisticalMeasure type, String value){
141
        Composite composite_2 = new Composite(valuesComposite, SWT.NONE);
142
        composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
143
        GridLayout gl_composite_2 = new GridLayout(4, false);
144
        gl_composite_2.marginHeight = 0;
145
        gl_composite_2.marginWidth = 0;
146
        gl_composite_2.verticalSpacing = 0;
147
        gl_composite_2.horizontalSpacing = 2;
148
        composite_2.setLayout(gl_composite_2);
149

    
150
        Label lblNewLabel = new Label(composite_2, SWT.NONE);
151
        lblNewLabel.setText(type.getLabel());
152

    
153
        Text text = new Text(composite_2, SWT.BORDER);
154

    
155
        GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
156
        gd_text.widthHint = 15;
157
        text.setLayoutData(gd_text);
158
        if(value!=null){
159
            text.setText(value);
160
        }
161

    
162
        List<Text> list = textFieldMap.get(type);
163
        if(list==null){
164
            list = new ArrayList<>();
165
        }
166
        textFieldMap.put(type, list);
167
        list.add(text);
168

    
169
        Button btnNewButton_2 = new Button(composite_2, SWT.NONE);
170
        btnNewButton_2.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
171
        new Label(composite_2, SWT.NONE);
172
        btnNewButton_2.addSelectionListener(new SelectionAdapter() {
173
            @Override
174
            public void widgetSelected(SelectionEvent e) {
175
                text.setText("");
176
            }
177
        });
178
        text.addModifyListener(e->{
179
            boolean hasExactValues = textFieldMap.entrySet().stream()
180
                    .filter(entry->entry.getKey().equals(StatisticalMeasure.EXACT_VALUE()))
181
            .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
182
            );
183
            if(hasExactValues){
184
                enableAggregationFields(false);
185
            }
186
            else{
187
                enableAggregationFields(true);
188
            }
189
        });
190
        return text;
191
    }
192

    
193
    private void enableAggregationFields(boolean enabled){
194
        textFieldMap.entrySet().stream()
195
        .filter(entry->!entry.getKey().equals(StatisticalMeasure.EXACT_VALUE()))
196
        .forEach(aggEntry->aggEntry.getValue().forEach(aggText->aggText.setEnabled(enabled)));
197
    }
198

    
199
    public Map<StatisticalMeasure, List<Text>> getTextFields() {
200
        return textFieldMap;
201
    }
202

    
203
    public QuantitativeData getEditorValue() {
204
        return editorValue;
205
    }
206

    
207
}
(2-2/5)