Project

General

Profile

Download (4.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.quantitative;
10

    
11
import java.util.HashMap;
12
import java.util.Map;
13

    
14
import org.eclipse.nebula.widgets.nattable.edit.editor.AbstractCellEditor;
15
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.MoveDirectionEnum;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.widgets.Composite;
18
import org.eclipse.swt.widgets.Control;
19
import org.eclipse.swt.widgets.Listener;
20
import org.eclipse.swt.widgets.Text;
21

    
22
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
23
import eu.etaxonomy.cdm.model.description.Feature;
24
import eu.etaxonomy.cdm.model.description.QuantitativeData;
25
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
26
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
27

    
28
/**
29
 *
30
 * Cell editor for QuantitativeData
31
 * @author pplitzner
32
 * @date 17.07.2018
33
 *
34
 */
35
public class QuantitativeDataExactValueCellEditor extends AbstractCellEditor{
36

    
37
    private Control editorControl;
38

    
39
    private Feature feature;
40

    
41
    private Map<StatisticalMeasure, Text> measureToTextMap = new HashMap<>();
42

    
43
    private Object editorValue;
44

    
45
    private CharacterMatrix matrix;
46

    
47
    private boolean dirty = false;
48

    
49
    private QuantitativeDataComposite dataComposite;
50

    
51
    public QuantitativeDataExactValueCellEditor(Feature feature, CharacterMatrix matrix) {
52
        super();
53
        this.matrix = matrix;
54
        this.feature = feature;
55
    }
56

    
57

    
58
    @Override
59
    public Object getEditorValue() {
60
        return this.editorValue;
61
    }
62

    
63
    @Override
64
    public void setEditorValue(Object value) {
65
        this.editorValue = value;
66
    }
67

    
68
    @Override
69
    public Control getEditorControl() {
70
        return editorControl;
71
    }
72

    
73
    @Override
74
    public Control createEditorControl(Composite parent) {
75
        dataComposite = new QuantitativeDataComposite(parent, SWT.NONE);
76
        return dataComposite;
77
    }
78

    
79
    @Override
80
    public boolean commit(MoveDirectionEnum direction, boolean closeAfterCommit, boolean skipValidation) {
81
        if(dirty){
82
            matrix.setDirty();
83
            dirty = false;
84
        }
85
        return super.commit(direction, closeAfterCommit, skipValidation);
86
    }
87

    
88
    @Override
89
    protected Control activateCell(Composite parent, Object originalCanonicalValue) {
90
        Object rowObject = matrix.getBodyDataProvider().getRowObject(this.getRowIndex());
91
        if(rowObject instanceof RowWrapperDTO){
92
            this.editorControl = createEditorControl(parent);
93
            if(originalCanonicalValue==null){
94
                originalCanonicalValue = ((RowWrapperDTO) rowObject).addQuantitativeData(feature);
95
            }
96
            this.setEditorValue(originalCanonicalValue);
97

    
98
                //Fill text controls with actual values and add modify listeners for editing
99
            if(originalCanonicalValue instanceof QuantitativeData){
100
                QuantitativeData quantitativeData = (QuantitativeData)originalCanonicalValue;
101
                long exactValuesCount = quantitativeData.getStatisticalValues().stream()
102
                .filter(val->val.getType().equals(StatisticalMeasure.EXACT_VALUE()))
103
                .count();
104
                if(exactValuesCount>1){
105
                    dataComposite.getText().setText("...");
106
                    dataComposite.getText().setEnabled(false);
107
                }
108
                else if(exactValuesCount==1){
109
                    Float exactValue = quantitativeData.getSpecificStatisticalValue(StatisticalMeasure.EXACT_VALUE());
110
                    dataComposite.getText().setText(exactValue.toString());
111
                    dataComposite.getText().addModifyListener(e->{
112
                        try {
113
                            float parseFloat = Float.parseFloat(dataComposite.getText().getText());
114
                            quantitativeData.setSpecificStatisticalValue(parseFloat, null, StatisticalMeasure.EXACT_VALUE());
115
                            dirty = true;
116
                        } catch (NumberFormatException|NullPointerException ex) {
117
                            // ignore exception for wrong number format
118
                        }
119
                    });
120
                }
121
            }
122
        }
123
        return this.editorControl;
124
    }
125

    
126
    private void setText(Float specificStatisticalValue, Text text){
127
        if(specificStatisticalValue!=null){
128
            Listener[] listeners = text.getListeners(SWT.Modify);
129
            for (Listener listener : listeners) {
130
                text.removeListener(SWT.Modify, listener);
131
            }
132
            text.setText(Float.toString(specificStatisticalValue));
133
            for (Listener listener : listeners) {
134
                text.addListener(SWT.Modify, listener);
135
            }
136
        }
137
    }
138

    
139
}
(5-5/5)