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 javax.swing.AbstractCellEditor;
|
16
|
|
17
|
import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.MoveDirectionEnum;
|
18
|
import org.eclipse.swt.SWT;
|
19
|
import org.eclipse.swt.layout.FillLayout;
|
20
|
import org.eclipse.swt.layout.GridData;
|
21
|
import org.eclipse.swt.layout.GridLayout;
|
22
|
import org.eclipse.swt.widgets.Composite;
|
23
|
import org.eclipse.swt.widgets.Control;
|
24
|
import org.eclipse.swt.widgets.Label;
|
25
|
import org.eclipse.swt.widgets.Listener;
|
26
|
import org.eclipse.swt.widgets.Text;
|
27
|
|
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.workingSet.matrix.CharacterMatrix;
|
32
|
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.RowWrapper;
|
33
|
|
34
|
/**
|
35
|
* Cell editor for QuantitativeData
|
36
|
* @author pplitzner
|
37
|
* @since Dec 7, 2017
|
38
|
*
|
39
|
*/
|
40
|
public class QuantitativeDataCellEditor extends AbstractCellEditor{
|
41
|
|
42
|
private Control editorControl;
|
43
|
|
44
|
private Feature feature;
|
45
|
|
46
|
private Map<StatisticalMeasure, Text> measureToTextMap = new HashMap<>();
|
47
|
|
48
|
private Object editorValue;
|
49
|
|
50
|
private CharacterMatrix matrix;
|
51
|
|
52
|
private boolean dirty = false;
|
53
|
|
54
|
public QuantitativeDataCellEditor(Feature feature, CharacterMatrix matrix) {
|
55
|
super();
|
56
|
this.matrix = matrix;
|
57
|
this.feature = feature;
|
58
|
}
|
59
|
|
60
|
|
61
|
/**
|
62
|
* {@inheritDoc}
|
63
|
*/
|
64
|
@Override
|
65
|
public Object getEditorValue() {
|
66
|
return this.editorValue;
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* {@inheritDoc}
|
71
|
*/
|
72
|
@Override
|
73
|
public void setEditorValue(Object value) {
|
74
|
this.editorValue = value;
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* {@inheritDoc}
|
79
|
*/
|
80
|
@Override
|
81
|
public Control getEditorControl() {
|
82
|
return editorControl;
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* {@inheritDoc}
|
87
|
*/
|
88
|
@Override
|
89
|
public Control createEditorControl(Composite parent) {
|
90
|
Composite composite = new Composite(parent, SWT.NONE);
|
91
|
composite.setLayout(new FillLayout(SWT.HORIZONTAL));
|
92
|
|
93
|
for (StatisticalMeasure measure: feature.getRecommendedStatisticalMeasures()) {
|
94
|
Composite measureComposite = new Composite(composite, SWT.NONE);
|
95
|
GridLayout layout = new GridLayout(2, false);
|
96
|
layout.horizontalSpacing = 0;
|
97
|
layout.marginBottom = 0;
|
98
|
layout.marginHeight = 0;
|
99
|
layout.marginLeft = 0;
|
100
|
layout.marginRight = 0;
|
101
|
layout.marginTop = 0;
|
102
|
layout.marginWidth = 0;
|
103
|
layout.verticalSpacing = 0;
|
104
|
measureComposite.setLayout(layout);
|
105
|
Label label = new Label(measureComposite, SWT.NONE);
|
106
|
GridData layoutData = new GridData(SWT.CENTER, SWT.TOP, false, false);
|
107
|
label.setLayoutData(layoutData);
|
108
|
label.setText(measure.getLabel().substring(0, 3));
|
109
|
|
110
|
Text text = new Text(measureComposite, SWT.NONE);
|
111
|
label.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false));
|
112
|
|
113
|
measureToTextMap.put(measure, text);
|
114
|
}
|
115
|
return composite;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* {@inheritDoc}
|
120
|
*/
|
121
|
@Override
|
122
|
public boolean commit(MoveDirectionEnum direction, boolean closeAfterCommit, boolean skipValidation) {
|
123
|
if(dirty){
|
124
|
matrix.setDirty();
|
125
|
dirty = false;
|
126
|
}
|
127
|
return super.commit(direction, closeAfterCommit, skipValidation);
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* {@inheritDoc}
|
132
|
*/
|
133
|
@Override
|
134
|
protected Control activateCell(Composite parent, Object originalCanonicalValue) {
|
135
|
Object rowObject = matrix.getBodyDataProvider().getRowObject(this.getRowIndex());
|
136
|
if(rowObject instanceof RowWrapper){
|
137
|
this.editorControl = createEditorControl(parent);
|
138
|
if(originalCanonicalValue==null){
|
139
|
originalCanonicalValue = ((RowWrapper) rowObject).addQuantitativeData(feature);
|
140
|
}
|
141
|
this.setEditorValue(originalCanonicalValue);
|
142
|
|
143
|
if(originalCanonicalValue instanceof QuantitativeData){
|
144
|
QuantitativeData quantitativeData = (QuantitativeData)originalCanonicalValue;
|
145
|
Set<StatisticalMeasure> statisticalValues = quantitativeData.getFeature().getRecommendedStatisticalMeasures();
|
146
|
//Fill text controls with actual values and add modify listeners for editing
|
147
|
for (StatisticalMeasure value : statisticalValues) {
|
148
|
Text text = measureToTextMap.get(value);
|
149
|
text.addModifyListener(e->{
|
150
|
quantitativeData.setSpecificStatisticalValue(Float.parseFloat(text.getText()), null, value);
|
151
|
dirty = true;
|
152
|
});
|
153
|
|
154
|
Float specificStatisticalValue = quantitativeData.getSpecificStatisticalValue(value);
|
155
|
if(specificStatisticalValue!=null){
|
156
|
Listener[] listeners = text.getListeners(SWT.Modify);
|
157
|
for (Listener listener : listeners) {
|
158
|
text.removeListener(SWT.Modify, listener);
|
159
|
}
|
160
|
text.setText(Float.toString(specificStatisticalValue));
|
161
|
for (Listener listener : listeners) {
|
162
|
text.addListener(SWT.Modify, listener);
|
163
|
}
|
164
|
}
|
165
|
}
|
166
|
}
|
167
|
}
|
168
|
return this.editorControl;
|
169
|
}
|
170
|
|
171
|
}
|