Project

General

Profile

Download (5.73 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
import java.util.Set;
14

    
15
import org.eclipse.nebula.widgets.nattable.edit.editor.AbstractCellEditor;
16
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.MoveDirectionEnum;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.layout.FillLayout;
19
import org.eclipse.swt.layout.GridData;
20
import org.eclipse.swt.layout.GridLayout;
21
import org.eclipse.swt.widgets.Composite;
22
import org.eclipse.swt.widgets.Control;
23
import org.eclipse.swt.widgets.Label;
24
import org.eclipse.swt.widgets.Listener;
25
import org.eclipse.swt.widgets.Text;
26

    
27
import eu.etaxonomy.cdm.api.service.dto.RowWrapperDTO;
28
import eu.etaxonomy.cdm.model.description.Feature;
29
import eu.etaxonomy.cdm.model.description.QuantitativeData;
30
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
31
import eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix.CharacterMatrix;
32

    
33
/**
34
 * Cell editor for QuantitativeData
35
 * @author pplitzner
36
 * @since Dec 7, 2017
37
 *
38
 */
39
public class QuantitativeDataCellEditor extends AbstractCellEditor{
40

    
41
    private Control editorControl;
42

    
43
    private Feature feature;
44

    
45
    private Map<StatisticalMeasure, Text> measureToTextMap = new HashMap<>();
46

    
47
    private Object editorValue;
48

    
49
    private CharacterMatrix matrix;
50

    
51
    private boolean dirty = false;
52

    
53
    public QuantitativeDataCellEditor(Feature feature, CharacterMatrix matrix) {
54
        super();
55
        this.matrix = matrix;
56
        this.feature = feature;
57
    }
58

    
59

    
60
    /**
61
     * {@inheritDoc}
62
     */
63
    @Override
64
    public Object getEditorValue() {
65
        return this.editorValue;
66
    }
67

    
68
    /**
69
     * {@inheritDoc}
70
     */
71
    @Override
72
    public void setEditorValue(Object value) {
73
        this.editorValue = value;
74
    }
75

    
76
    /**
77
     * {@inheritDoc}
78
     */
79
    @Override
80
    public Control getEditorControl() {
81
        return editorControl;
82
    }
83

    
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    public Control createEditorControl(Composite parent) {
89
        Composite composite = new Composite(parent, SWT.NONE);
90
        composite.setLayout(new FillLayout(SWT.HORIZONTAL));
91

    
92
        for (StatisticalMeasure measure: feature.getRecommendedStatisticalMeasures()) {
93
            Composite measureComposite = new Composite(composite, SWT.NONE);
94
            GridLayout layout = new GridLayout(2, false);
95
            layout.horizontalSpacing = 0;
96
            layout.marginBottom = 0;
97
            layout.marginHeight = 0;
98
            layout.marginLeft = 0;
99
            layout.marginRight = 0;
100
            layout.marginTop = 0;
101
            layout.marginWidth = 0;
102
            layout.verticalSpacing = 0;
103
            measureComposite.setLayout(layout);
104
            Label label = new Label(measureComposite, SWT.NONE);
105
            GridData layoutData = new GridData(SWT.CENTER, SWT.TOP, false, false);
106
            label.setLayoutData(layoutData);
107
            label.setText(measure.getLabel().substring(0, 3));
108

    
109
            Text text = new Text(measureComposite, SWT.NONE);
110
            label.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false));
111

    
112
            measureToTextMap.put(measure, text);
113
        }
114
        return composite;
115
    }
116

    
117
    /**
118
     * {@inheritDoc}
119
     */
120
    @Override
121
    public boolean commit(MoveDirectionEnum direction, boolean closeAfterCommit, boolean skipValidation) {
122
        if(dirty){
123
            matrix.setDirty();
124
            dirty = false;
125
        }
126
        return super.commit(direction, closeAfterCommit, skipValidation);
127
    }
128

    
129
    /**
130
     * {@inheritDoc}
131
     */
132
    @Override
133
    protected Control activateCell(Composite parent, Object originalCanonicalValue) {
134
        Object rowObject = matrix.getBodyDataProvider().getRowObject(this.getRowIndex());
135
        if(rowObject instanceof RowWrapperDTO){
136
            this.editorControl = createEditorControl(parent);
137
            if(originalCanonicalValue==null){
138
                originalCanonicalValue = ((RowWrapperDTO) rowObject).addQuantitativeData(feature);
139
            }
140
            this.setEditorValue(originalCanonicalValue);
141

    
142
            if(originalCanonicalValue instanceof QuantitativeData){
143
                QuantitativeData quantitativeData = (QuantitativeData)originalCanonicalValue;
144
                Set<StatisticalMeasure> statisticalValues = quantitativeData.getFeature().getRecommendedStatisticalMeasures();
145
                //Fill text controls with actual values and add modify listeners for editing
146
                for (StatisticalMeasure value : statisticalValues) {
147
                    Text text = measureToTextMap.get(value);
148
                    text.addModifyListener(e->{
149
                        quantitativeData.setSpecificStatisticalValue(Float.parseFloat(text.getText()), null, value);
150
                        dirty = true;
151
                    });
152

    
153
                    Float specificStatisticalValue = quantitativeData.getSpecificStatisticalValue(value);
154
                    if(specificStatisticalValue!=null){
155
                        Listener[] listeners = text.getListeners(SWT.Modify);
156
                        for (Listener listener : listeners) {
157
                            text.removeListener(SWT.Modify, listener);
158
                        }
159
                        text.setText(Float.toString(specificStatisticalValue));
160
                        for (Listener listener : listeners) {
161
                            text.addListener(SWT.Modify, listener);
162
                        }
163
                    }
164
                }
165
            }
166
        }
167
        return this.editorControl;
168
    }
169

    
170
}
(1-1/2)