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 org.jdesktop.beans.AbstractSerializableBean;
28 import org.jfree.data.xy.XYSeries;
29 import org.jfree.data.xy.XYSeriesCollection;
30
31 import java.util.List;
32
33 import static org.nuiton.i18n.I18n.t;
34
35
36
37
38
39
40 public class FrequenciesHistogramModel extends AbstractSerializableBean {
41
42 public static final String PROPERTY_TITLE = "title";
43 public static final String PROPERTY_LENGTH_STEP_LABEL_WITH_UNIT = "lengthStepLabelWithUnit";
44 public static final String PROPERTY_STEP = "step";
45
46
47
48
49
50
51 private final XYSeriesCollection dataset;
52 protected final XYSeries series;
53
54 private String title;
55 private String lengthStepLabelWithUnit;
56 private Float step;
57
58 public FrequenciesHistogramModel() {
59
60 series = new XYSeries("", true, false);
61
62 dataset = new XYSeriesCollection(series);
63 dataset.setIntervalPositionFactor(0);
64 dataset.setIntervalWidth(0);
65
66 }
67
68 public XYSeriesCollection getDataset() {
69 return dataset;
70 }
71
72 public String getTitle() {
73 return title;
74 }
75
76 public void setTitle(String title) {
77
78 title = title + t("tutti.editSpeciesFrequencies.title");
79
80 Object oldValue = getTitle();
81 this.title = title;
82 firePropertyChange(PROPERTY_TITLE, oldValue, title);
83 }
84
85 public String getLengthStepLabelWithUnit() {
86 return lengthStepLabelWithUnit;
87 }
88
89 public void setLengthStepLabelWithUnit(String lengthStepLabelWithUnit) {
90 Object oldValue = getLengthStepLabelWithUnit();
91 this.lengthStepLabelWithUnit = lengthStepLabelWithUnit;
92 firePropertyChange(PROPERTY_LENGTH_STEP_LABEL_WITH_UNIT, oldValue, lengthStepLabelWithUnit);
93 }
94
95 public Float getStep() {
96 return step;
97 }
98
99 public void setStep(Float step) {
100 Object oldValue = getStep();
101 this.step = step;
102 firePropertyChange(PROPERTY_STEP, oldValue, step);
103 }
104
105 public void reloadRows(List<SpeciesFrequencyRowModel> rows) {
106
107 series.clear();
108
109 if (rows != null) {
110
111 rows.stream().filter(SpeciesFrequencyRowModel::isValid).forEach(this::addOrUpdate);
112
113 }
114
115 }
116
117 public void addOrUpdate(SpeciesFrequencyRowModel row) {
118 series.addOrUpdate(row.getLengthStep(), row.getNumber());
119 }
120
121 public void removeValue(Float lengthStep) {
122 if (series.indexOf(lengthStep) >= 0) {
123 if (series.getItemCount() > 1) {
124 series.remove(lengthStep);
125 } else {
126 series.clear();
127 }
128 }
129 }
130
131 }