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.ui.swing.util.TuttiNumberTickUnitSource;
28  import fr.ifremer.tutti.util.Units;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.jfree.chart.ChartFactory;
32  import org.jfree.chart.ChartPanel;
33  import org.jfree.chart.JFreeChart;
34  import org.jfree.chart.axis.NumberTickUnitSource;
35  import org.jfree.chart.axis.ValueAxis;
36  
37  import javax.swing.JFrame;
38  import java.awt.BorderLayout;
39  import java.awt.event.MouseAdapter;
40  import java.awt.event.MouseEvent;
41  import java.beans.PropertyChangeEvent;
42  import java.beans.PropertyChangeListener;
43  import java.io.Closeable;
44  
45  import static org.nuiton.i18n.I18n.t;
46  
47  /**
48   * To manage the frequencies histogram.
49   *
50   * Created on 14/04/16.
51   *
52   * @author Tony Chemit - chemit@codelutin.com
53   */
54  public class FrequenciesHistogramHandler implements Closeable {
55  
56      /** Logger. */
57      private static final Log log = LogFactory.getLog(FrequenciesHistogramHandler.class);
58  
59      private final FrequenciesHistogramModel model;
60  
61      private final JFreeChart chart;
62      private final JFrame popup;
63  
64      public FrequenciesHistogramHandler(SpeciesFrequencyUI ui) {
65  
66          SpeciesFrequencyUIModel uiModel = ui.getModel();
67          SpeciesFrequencyUIHandler uiHandler = ui.getHandler();
68  
69          this.model = uiModel.getFrequenciesHistogramModel();
70  
71          chart = ChartFactory.createXYBarChart(null,
72                                                t("tutti.editSpeciesFrequencies.table.header.lengthStep"),
73                                                false,
74                                                t("tutti.editSpeciesFrequencies.table.header.number"),
75                                                model.getDataset());
76          chart.clearSubtitles();
77  
78          ValueAxis rangeAxis = chart.getXYPlot().getRangeAxis();
79          rangeAxis.setAutoRange(true);
80          rangeAxis.setStandardTickUnits(new NumberTickUnitSource(true));
81  
82          ValueAxis domainAxis = chart.getXYPlot().getDomainAxis();
83          domainAxis.setAutoRange(true);
84          domainAxis.setStandardTickUnits(new TuttiNumberTickUnitSource(true));
85          domainAxis.setMinorTickMarksVisible(true);
86  
87          chart.getXYPlot().getRenderer().setSeriesPaint(0, uiHandler.getConfig().getColorComputedWeights());
88  
89          ChartPanel chartPanel = new ChartPanel(chart);
90          chartPanel.setDomainZoomable(false);
91          chartPanel.setMouseZoomable(false);
92          chartPanel.setPopupMenu(null);
93  
94          ui.getHistogramPanel().add(chartPanel, BorderLayout.CENTER);
95  
96          popup = new JFrame();
97          chartPanel.addMouseListener(new MouseAdapter() {
98  
99              @Override
100             public void mouseClicked(MouseEvent e) {
101                 super.mouseClicked(e);
102                 if (e.getClickCount() > 1) {
103 
104                     popup.getContentPane().removeAll();
105                     ChartPanel chartPanel = new ChartPanel(chart);
106                     popup.getContentPane().add(chartPanel);
107                     popup.pack();
108                     popup.setVisible(true);
109 
110                 }
111             }
112         });
113 
114         // when step has changed in model, update chart
115         uiModel.addPropertyChangeListener(SpeciesFrequencyUIModel.PROPERTY_STEP, new PropertyChangeListener() {
116             @Override
117             public void propertyChange(PropertyChangeEvent evt) {
118 
119                 Float step1 = (Float) evt.getNewValue();
120                 model.setStep(step1);
121 
122             }
123         });
124 
125         // when lengthStepCaracteristicUnit changed, let's updates the label of some fields
126         uiModel.addPropertyChangeListener(SpeciesFrequencyUIModel.PROPERTY_LENGTH_STEP_CARACTERISTIC_UNIT, new PropertyChangeListener() {
127             @Override
128             public void propertyChange(PropertyChangeEvent evt) {
129 
130                 String unit = (String) evt.getNewValue();
131 
132                 if (unit == null) {
133 
134                     unit = t("tutti.editSpeciesFrequencies.unkownStepUnit");
135                 }
136 
137                 String lengthStepLabelWithUnit = Units.getLabelWithUnit(t("tutti.editSpeciesFrequencies.table.header.lengthStep"), unit);
138                 model.setLengthStepLabelWithUnit(lengthStepLabelWithUnit);
139 
140             }
141 
142         });
143 
144         model.addPropertyChangeListener(AverageWeightsHistogramModel.PROPERTY_TITLE, new PropertyChangeListener() {
145             @Override
146             public void propertyChange(PropertyChangeEvent evt) {
147                 String title = (String) evt.getNewValue();
148                 if (log.isInfoEnabled()) {
149                     log.info("Frequencies graph title changed to: " + title);
150                 }
151                 popup.setTitle(title);
152             }
153         });
154 
155         model.addPropertyChangeListener(AverageWeightsHistogramModel.PROPERTY_LENGTH_STEP_LABEL_WITH_UNIT, new PropertyChangeListener() {
156             @Override
157             public void propertyChange(PropertyChangeEvent evt) {
158                 String lengthStepLabelWithUnit = (String) evt.getNewValue();
159                 if (log.isInfoEnabled()) {
160                     log.info("Frequencies graph lengthStepLabelWithUnit changed to: " + lengthStepLabelWithUnit);
161                 }
162                 chart.getXYPlot().getDomainAxis().setLabel(lengthStepLabelWithUnit);
163             }
164         });
165 
166         model.addPropertyChangeListener(AverageWeightsHistogramModel.PROPERTY_STEP, new PropertyChangeListener() {
167             @Override
168             public void propertyChange(PropertyChangeEvent evt) {
169 
170                 Float step = (Float) evt.getNewValue();
171                 if (log.isInfoEnabled()) {
172                     log.info("Frequencies graph step changed to: " + step);
173                 }
174 
175                 chart.getXYPlot().getDomainAxis().setStandardTickUnits(new TuttiNumberTickUnitSource(step == 1f));
176                 model.getDataset().setIntervalWidth(step);
177 
178             }
179         });
180 
181     }
182 
183     @Override
184     public void close() {
185         popup.dispose();
186     }
187 
188 }