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 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   * Created on 15/04/16.
37   *
38   * @author Tony Chemit - chemit@codelutin.com
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       * To store frequencies graph series.
48       *
49       * @since 3.11
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 }