1   package org.wcb.gui.forms.report;
2   
3   import org.wcb.model.bd.LogbookDelegate;
4   import org.wcb.model.vo.hibernate.Logbook;
5   import org.wcb.model.util.SpringUtil;
6   import org.wcb.model.service.IServicesConstants;
7   import org.wcb.resources.MessageResourceRegister;
8   import org.wcb.resources.MessageKey;
9   import org.jfree.chart.JFreeChart;
10  import org.jfree.chart.ChartFactory;
11  import org.jfree.chart.ChartPanel;
12  import org.jfree.chart.plot.PiePlot;
13  import org.jfree.data.general.DefaultPieDataset;
14  
15  import javax.swing.*;
16  import java.awt.*;
17  
18  /**
19   * <small>
20   * Copyright (c)  2006  wbogaardt.
21   * This library is free software; you can redistribute it and/or
22   * modify it under the terms of the GNU Lesser General Public
23   * License as published by the Free Software Foundation; either
24   * version 2.1 of the License, or (at your option) any later version.
25   *
26   * This library is distributed in the hope that it will be useful,
27   * but WITHOUT ANY WARRANTY; without even the implied warranty of
28   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29   * Lesser General Public License for more details.
30   *
31   * You should have received a copy of the GNU Lesser General Public
32   * License along with this library; if not, write to the Free Software
33   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34   * <p/>
35   * $File:  $ <br>
36   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Apr 3, 2006 2:53:41 PM $ <br>
37   * </small>
38   *
39   * @author wbogaardt
40   * @version 1
41   *          Date: Apr 3, 2006
42   *          Time: 2:53:41 PM
43   */
44  
45  public class TotalTakeoffLandingsReportForm extends JPanel implements ReportRefresh {
46  
47      private LogbookDelegate delegate;
48      private ChartPanel chartPanel;
49      private int totalAllLandings;
50      private int totalAllTakeoffs;
51  
52      public TotalTakeoffLandingsReportForm() {
53          delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
54          initComponents();
55      }
56  
57      public void refresh() {
58          JFreeChart chart = ChartFactory.createPieChart("Takeoff/Landing Report - " + totalAllTakeoffs + "/" + totalAllLandings, getData(), true, true, false);
59          PiePlot plot = (PiePlot) chart.getPlot();
60          plot.setSectionOutlinesVisible(false);
61          plot.setLabelFont(new Font("SansSerif", Font.PLAIN,  12));
62          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
63          plot.setCircular(false);
64          plot.setLabelGap(0.02);
65          chartPanel.setChart(chart); 
66      }
67  
68      private void initComponents() {
69          JFreeChart chart = ChartFactory.createPieChart("Takeoff/Landing Report", getData(), true, true, false);
70          PiePlot plot = (PiePlot) chart.getPlot();
71          plot.setSectionOutlinesVisible(false);
72          plot.setLabelFont(new Font("SansSerif", Font.PLAIN,  12));
73          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
74          plot.setCircular(false);
75          plot.setLabelGap(0.02);
76          chartPanel = new ChartPanel(chart);
77          add(chartPanel);
78      }
79  
80      private DefaultPieDataset getData() {
81          java.util.List<Logbook> rows = delegate.getAllLogbookEntries();
82          int dayTakeoff = 0;
83          int dayLandings = 0;
84          int nightTakeoff = 0;
85          int nightLandings = 0;
86          for(Logbook item : rows) {
87                  dayTakeoff = dayTakeoff + item.getDayTakeoffs();
88                  dayLandings = dayLandings + item.getDayLandings();
89                  nightTakeoff = nightTakeoff + item.getNightTakeoffs();
90                  nightLandings = nightLandings + item.getNightLandings();
91          }
92          totalAllLandings = dayLandings + nightLandings;
93          totalAllTakeoffs = dayTakeoff + nightTakeoff;
94          DefaultPieDataset dataset = new DefaultPieDataset();
95          dataset.setValue("Day Takeoff", dayTakeoff);
96          dataset.setValue("Day Landing", dayLandings);
97          dataset.setValue("Night Takeoff", nightTakeoff);
98          dataset.setValue("Night Landing", nightLandings);
99          return dataset;
100     }
101 }