Project

General

Profile

Download (5.68 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

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

    
28
import eu.etaxonomy.cdm.common.CdmUtils;
29
import eu.etaxonomy.cdm.model.description.QuantitativeData;
30
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
31
import eu.etaxonomy.taxeditor.model.ImageResources;
32

    
33
/**
34
 * @author pplitzner
35
 * @date 16.07.2018
36
 *
37
 */
38
public class QuantitativeDataDialogComposite extends Composite {
39

    
40
    private Map<StatisticalMeasure, List<Text>> textFieldMap = new HashMap<>();
41

    
42
    public QuantitativeDataDialogComposite(Character initialInput, QuantitativeData editorValue, Composite parent, int style) {
43
        super(parent, style);
44
        setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
45

    
46
        setLayout(new GridLayout(1, false));
47

    
48
        ScrolledComposite scrolledComposite_1 = new ScrolledComposite(this, SWT.BORDER | SWT.V_SCROLL);
49
        scrolledComposite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
50
        scrolledComposite_1.setExpandHorizontal(true);
51
        scrolledComposite_1.setExpandVertical(true);
52

    
53
        Composite valuesComposite = new Composite(scrolledComposite_1, SWT.NONE);
54
        GridLayout gl_valuesComposite = new GridLayout();
55
        gl_valuesComposite.marginWidth = 0;
56
        gl_valuesComposite.marginHeight = 0;
57
        valuesComposite.setLayout(gl_valuesComposite);
58

    
59
        //add empty text field for exact value
60
        Text emptyTextField = addText(valuesComposite, StatisticalMeasure.EXACT_VALUE(), initialInput==null?null:initialInput.toString());
61
        emptyTextField.setFocus();
62
        emptyTextField.setSelection(emptyTextField.getText().length());
63

    
64
        //add existing exact values
65
        editorValue.getStatisticalValues().stream()
66
        .filter(measure->measure.getType().equals(StatisticalMeasure.EXACT_VALUE()))
67
        .forEach(exact->addText(valuesComposite, exact.getType(), Float.toString(exact.getValue())));
68

    
69
        //add aggregation values
70
        editorValue.getFeature().getRecommendedStatisticalMeasures()
71
        .stream()
72
        .filter(sm->!sm.equals(StatisticalMeasure.EXACT_VALUE()))
73
        .forEach(measure->{
74
            Float specificStatisticalValue = editorValue.getSpecificStatisticalValue(measure);
75
            addText(valuesComposite, measure, specificStatisticalValue!=null?Float.toString(specificStatisticalValue):null);
76
        });
77

    
78
        scrolledComposite_1.setContent(valuesComposite);
79
        scrolledComposite_1.setMinSize(valuesComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
80

    
81
        if(initialInput!=null){
82
            enableAggregationFields(false);
83
        }
84
    }
85

    
86
    private Text addText(Composite valuesComposite, StatisticalMeasure type, String value){
87
        Composite composite_2 = new Composite(valuesComposite, SWT.NONE);
88
        composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
89
        GridLayout gl_composite_2 = new GridLayout(4, false);
90
        gl_composite_2.marginHeight = 0;
91
        gl_composite_2.marginWidth = 0;
92
        gl_composite_2.verticalSpacing = 0;
93
        gl_composite_2.horizontalSpacing = 2;
94
        composite_2.setLayout(gl_composite_2);
95

    
96
        Label lblNewLabel = new Label(composite_2, SWT.NONE);
97
        lblNewLabel.setText(type.getLabel().substring(0, 3));
98

    
99
        Text text = new Text(composite_2, SWT.BORDER);
100
        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
101
        if(value!=null){
102
            text.setText(value);
103
        }
104

    
105
        List<Text> list = textFieldMap.get(type);
106
        if(list==null){
107
            list = new ArrayList<>();
108
        }
109
        textFieldMap.put(type, list);
110
        list.add(text);
111

    
112
        Button btnNewButton_2 = new Button(composite_2, SWT.NONE);
113
        btnNewButton_2.setImage(ImageResources.getImage(ImageResources.TRASH_ICON));
114
        new Label(composite_2, SWT.NONE);
115
        btnNewButton_2.addSelectionListener(new SelectionAdapter() {
116
            @Override
117
            public void widgetSelected(SelectionEvent e) {
118
                text.setText("");
119
            }
120
        });
121
        text.addModifyListener(e->{
122
            boolean hasExactValues = textFieldMap.entrySet().stream()
123
                    .filter(entry->entry.getKey().equals(StatisticalMeasure.EXACT_VALUE()))
124
            .anyMatch(exactEntry->exactEntry.getValue().stream().anyMatch(exactText->CdmUtils.isNotBlank(exactText.getText()))
125
            );
126
            if(hasExactValues){
127
                enableAggregationFields(false);
128
            }
129
            else{
130
                enableAggregationFields(true);
131
            }
132
        });
133
        return text;
134
    }
135

    
136
    private void enableAggregationFields(boolean enabled){
137
        textFieldMap.entrySet().stream()
138
        .filter(entry->!entry.getKey().equals(StatisticalMeasure.EXACT_VALUE()))
139
        .forEach(aggEntry->aggEntry.getValue().forEach(aggText->aggText.setEnabled(enabled)));
140
    }
141

    
142
    public Map<StatisticalMeasure, List<Text>> getTextFields() {
143
        return textFieldMap;
144
    }
145

    
146
}
(2-2/5)