Project

General

Profile

Download (5.62 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.workingSet.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.model.description.Feature;
28
import eu.etaxonomy.cdm.model.description.QuantitativeData;
29
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
30
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.CharacterMatrix;
31
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.RowWrapper;
32

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

    
40
    private Control editorControl;
41

    
42
    private Feature feature;
43

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

    
46
    private Object editorValue;
47

    
48
    private CharacterMatrix matrix;
49

    
50
    private boolean dirty = false;
51

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

    
58

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

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

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

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

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

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

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

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

    
128
    /**
129
     * {@inheritDoc}
130
     */
131
    @Override
132
    protected Control activateCell(Composite parent, Object originalCanonicalValue) {
133
        this.editorControl = createEditorControl(parent);
134
        if(originalCanonicalValue==null){
135
            QuantitativeData data = QuantitativeData.NewInstance(feature);
136
            RowWrapper rowObject = matrix.getBodyDataProvider().getRowObject(this.getRowIndex());
137
            rowObject.getSpecimenDescription().addElement(data);
138
            originalCanonicalValue = data;
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
        return this.editorControl;
167
    }
168

    
169
}
(1-1/2)