1 package fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.tutti.type.WeightUnit;
28 import org.jdesktop.beans.AbstractSerializableBean;
29 import org.jfree.data.xy.XYSeries;
30 import org.jfree.data.xy.XYSeriesCollection;
31
32 import java.util.List;
33
34 import static org.nuiton.i18n.I18n.t;
35
36
37
38
39
40
41 public class AverageWeightsHistogramModel extends AbstractSerializableBean {
42
43 public static final String PROPERTY_TITLE = "title";
44 public static final String PROPERTY_LENGTH_STEP_LABEL_WITH_UNIT = "lengthStepLabelWithUnit";
45 public static final String PROPERTY_STEP = "step";
46
47
48
49
50
51
52 private final XYSeriesCollection dataset;
53 protected final XYSeries series;
54
55 private final WeightUnit frequencyWeightUnit;
56
57 private String title;
58 private String lengthStepLabelWithUnit;
59 private Float step;
60
61 public AverageWeightsHistogramModel(WeightUnit frequencyWeightUnit) {
62
63 this.frequencyWeightUnit = frequencyWeightUnit;
64
65 this.series = new XYSeries("", true, false);
66 this.dataset = new XYSeriesCollection(series);
67 this.dataset.setIntervalPositionFactor(0);
68 this.dataset.setIntervalWidth(0);
69
70 }
71
72 public WeightUnit getFrequencyWeightUnit() {
73 return frequencyWeightUnit;
74 }
75
76 public XYSeriesCollection getDataset() {
77 return dataset;
78 }
79
80 public String getTitle() {
81 return title;
82 }
83
84 public void setTitle(String title) {
85
86 title = title + frequencyWeightUnit.decorateLabel(t("tutti.editSpeciesFrequencies.field.graphAverageWeight"));
87
88 Object oldValue = getTitle();
89 this.title = title;
90 firePropertyChange(PROPERTY_TITLE, oldValue, title);
91 }
92
93 public String getLengthStepLabelWithUnit() {
94 return lengthStepLabelWithUnit;
95 }
96
97 public void setLengthStepLabelWithUnit(String lengthStepLabelWithUnit) {
98 Object oldValue = getLengthStepLabelWithUnit();
99 this.lengthStepLabelWithUnit = lengthStepLabelWithUnit;
100 firePropertyChange(PROPERTY_LENGTH_STEP_LABEL_WITH_UNIT, oldValue, lengthStepLabelWithUnit);
101 }
102
103 public Float getStep() {
104 return step;
105 }
106
107 public void setStep(Float step) {
108 Object oldValue = getStep();
109 this.step = step;
110 firePropertyChange(PROPERTY_STEP, oldValue, step);
111 }
112
113 public void reloadRows(List<SpeciesFrequencyRowModel> rows) {
114
115 series.clear();
116
117 if (rows != null) {
118
119 rows.stream().filter(SpeciesFrequencyRowModel::isValid).forEach(this::addOrUpdate);
120
121 }
122
123 }
124
125 public void addOrUpdate(SpeciesFrequencyRowModel row) {
126
127 series.addOrUpdate(row.getLengthStep(), row.computeAverageWeight());
128
129 }
130
131 public void removeValue(Float lengthStep) {
132 if (series.indexOf(lengthStep) >= 0) {
133 if (series.getItemCount() > 1) {
134 series.remove(lengthStep);
135 } else {
136 series.clear();
137 }
138 }
139 }
140
141 }