View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.species.frequency;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
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   * Created on 15/04/16.
38   *
39   * @author Tony Chemit - chemit@codelutin.com
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       * To store average weights graph series.
49       *
50       * @since 4.5
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 }