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  import java.text.DecimalFormat;
18  
19  /**
20   * <small>
21   * Copyright (c)  2006  wbogaardt.
22   * This library is free software; you can redistribute it and/or
23   * modify it under the terms of the GNU Lesser General Public
24   * License as published by the Free Software Foundation; either
25   * version 2.1 of the License, or (at your option) any later version.
26   *
27   * This library is distributed in the hope that it will be useful,
28   * but WITHOUT ANY WARRANTY; without even the implied warranty of
29   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
30   * Lesser General Public License for more details.
31   *
32   * You should have received a copy of the GNU Lesser General Public
33   * License along with this library; if not, write to the Free Software
34   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
35   * <p/>
36   * $File:  $ <br>
37   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Apr 3, 2006 2:53:41 PM $ <br>
38   * </small>
39   *
40   * @author wbogaardt
41   * @version 1
42   *          Date: Apr 3, 2006
43   *          Time: 2:53:41 PM
44   */
45  
46  public class TotalTimeInstrumentReportForm extends JPanel implements ReportRefresh {
47  
48      private LogbookDelegate delegate;
49      private Double totalIMC;
50      final DecimalFormat formatter = new DecimalFormat("###,##0.0");
51      private ChartPanel chartPanel;
52  
53      public TotalTimeInstrumentReportForm() {
54          delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
55          initComponents();
56      }
57  
58      public void refresh() {
59           DefaultPieDataset dataSet = groupByIMC();
60          JFreeChart chart = ChartFactory.createPieChart("Total Time Instrument " + formatter.format(totalIMC), dataSet, true, true, false);
61          PiePlot plot = (PiePlot) chart.getPlot();
62          plot.setSectionOutlinesVisible(false);
63          plot.setLabelFont(new Font("SansSerif", Font.PLAIN,  12));
64          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
65          plot.setCircular(false);
66          plot.setLabelGap(0.02);
67         chartPanel.setChart(chart);
68      }
69  
70      private void initComponents() {
71          DefaultPieDataset dataSet = groupByIMC();
72          JFreeChart chart = ChartFactory.createPieChart("Total Time Instrument " + formatter.format(totalIMC), dataSet, true, true, false);
73          PiePlot plot = (PiePlot) chart.getPlot();
74          plot.setSectionOutlinesVisible(false);
75          plot.setLabelFont(new Font("SansSerif", Font.PLAIN,  12));
76          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
77          plot.setCircular(false);
78          plot.setLabelGap(0.02);
79          chartPanel = new ChartPanel(chart);
80          add(chartPanel);
81      }
82  
83      private DefaultPieDataset groupByIMC() {
84          java.util.List<Logbook> rows = delegate.getAllLogbookEntries();
85          Double totalFlight = 0.0;
86          Double actualIMC = 0.0;
87          Double simulatedIMC = 0.0;
88          Double flightSim = 0.0;
89          for(Logbook item : rows) {
90                  totalFlight = totalFlight + item.getFlightDuration();
91                  flightSim = flightSim + item.getConditionFlightSim();
92                  actualIMC = actualIMC + item.getConditionActualImc();
93                  simulatedIMC = simulatedIMC + item.getConditionSimulatedImc();
94          }
95          totalIMC = flightSim + actualIMC + simulatedIMC;
96          Double regularFlight = totalFlight - totalIMC;
97          DefaultPieDataset dataset = new DefaultPieDataset();
98          dataset.setValue("Non-IMC", regularFlight);
99          dataset.setValue("Actual IMC" , actualIMC);
100         dataset.setValue("Hood", simulatedIMC);
101         dataset.setValue("Flight Sim", flightSim);
102         return dataset;
103     }
104 }