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 TotalTimeAircraftReportForm extends JPanel {
47  
48      private LogbookDelegate delegate;
49      private String registration;
50      private final DecimalFormat formatter = new DecimalFormat("###,##0.0");
51      private Double totalTime = 0.0;
52  
53      /**
54       * This report shows the total time in a selected aircraft.
55       * @param reg The registration number of the aircraft.
56       */
57      public TotalTimeAircraftReportForm(String reg) {
58          registration = reg;
59          delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
60          initComponents();
61      }
62  
63      /**
64       * Sets the registration number of the aircraft
65       * @param reg The registration number of the aircraft.
66       */
67      public void setRegistration(String reg) {
68          this.registration = reg;
69      }
70  
71      private void initComponents() {
72          DefaultPieDataset dataset = groupByAircraft(this.registration);
73          JFreeChart chart = ChartFactory.createPieChart("Total Time in " + registration + " - " + formatter.format(totalTime) + " hours", dataset, true, true, false);
74          PiePlot plot = (PiePlot) chart.getPlot();
75          plot.setSectionOutlinesVisible(false);
76          plot.setLabelFont(new Font("SansSerif", Font.PLAIN,  12));
77          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
78          plot.setCircular(false);
79          plot.setLabelGap(0.02);
80          add(new ChartPanel(chart));
81      }
82  
83      private DefaultPieDataset groupByAircraft(String nNumber) {
84          java.util.List<Logbook> rows = delegate.findByRegistration(nNumber);
85          Double pic = 0.0;
86          Double sic = 0.0;
87          Double xCountry = 0.0;
88          Double instructing = 0.0;
89          Double safety = 0.0;
90          Double dual = 0.0;
91          Double solo = 0.0;
92          Double actualIMC = 0.0;
93          Double simulatedIMC = 0.0;
94          Double nightFlight = 0.0;
95          totalTime = 0.0;
96          for(Logbook item : rows) {
97              totalTime = totalTime + item.getFlightDuration();
98              pic = pic + item.getPic();
99              sic = sic + item.getSic();
100             xCountry = xCountry + item.getCrossCountry();
101             instructing = instructing + item.getFlightInstructing();
102             safety = safety + item.getSafetyPilot();
103             dual = dual + item.getDualReceived();
104             solo = solo + item.getSolo();
105             nightFlight = nightFlight + item.getConditionNight();
106             actualIMC = actualIMC + item.getConditionActualImc();
107             simulatedIMC = simulatedIMC + item.getConditionSimulatedImc();
108         }
109         DefaultPieDataset dataset = new DefaultPieDataset();
110         dataset.setValue("PIC", pic);
111         dataset.setValue("SIC", sic);
112         dataset.setValue("X-country", xCountry);
113         dataset.setValue("Instructing", instructing);
114         dataset.setValue("Safety Pilot", safety);
115         dataset.setValue("Dual", dual);
116         dataset.setValue("Solo", solo);
117         dataset.setValue("Actual IMC" , actualIMC);
118         dataset.setValue("Hood", simulatedIMC);
119         dataset.setValue("Night", nightFlight);
120         return dataset;
121     }
122 }