1   package org.wcb.gui.forms.report;
2   
3   import org.jfree.chart.JFreeChart;
4   import org.jfree.chart.ChartFactory;
5   import org.jfree.chart.ChartPanel;
6   import org.jfree.chart.plot.PiePlot;
7   import org.jfree.data.general.DefaultPieDataset;
8   import org.wcb.model.vo.hibernate.Logbook;
9   import org.wcb.model.bd.LogbookDelegate;
10  import org.wcb.model.util.SpringUtil;
11  import org.wcb.model.service.IServicesConstants;
12  import org.wcb.resources.MessageResourceRegister;
13  import org.wcb.resources.MessageKey;
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 TotalTimeReportForm extends JPanel implements ReportRefresh {
47  
48      private LogbookDelegate delegate;
49      private Double totalTime = 0.0;
50      private final DecimalFormat formatter = new DecimalFormat("###,##0.0");
51      private ChartPanel chartPanel;
52  
53      public TotalTimeReportForm() {
54          delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
55          initComponents();
56      }
57  
58      public void refresh() {
59          totalTime = 0.0;
60          DefaultPieDataset dataset = getData();
61          JFreeChart chart = ChartFactory.createPieChart3D("Total Time " + formatter.format(totalTime) + " hours", dataset, true, true, false);
62          PiePlot plot = (PiePlot) chart.getPlot();
63          plot.setSectionOutlinesVisible(false);
64          plot.setLabelFont(new Font("SansSerif", Font.PLAIN,  12));
65          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
66          plot.setCircular(false);
67          plot.setForegroundAlpha(.5f);
68          plot.setLabelGap(0.02);
69          chartPanel.setChart(chart);
70      }
71  
72      private void initComponents() {
73          DefaultPieDataset dataset = getData();
74          JFreeChart chart = ChartFactory.createPieChart3D("Total Time " + formatter.format(totalTime) + " hours", dataset, true, true, false);
75          PiePlot plot = (PiePlot) chart.getPlot();
76          plot.setSectionOutlinesVisible(false);
77          plot.setLabelFont(new Font("SansSerif", Font.PLAIN,  12));
78          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
79          plot.setCircular(false);
80          plot.setForegroundAlpha(.5f);
81          plot.setLabelGap(0.02);
82          chartPanel = new ChartPanel(chart);
83          add(chartPanel);
84      }
85  
86      private DefaultPieDataset getData() {
87          java.util.List<Logbook> rows = delegate.getAllLogbookEntries();
88          Double pic = 0.0;
89          Double sic = 0.0;
90          Double xCountry = 0.0;
91          Double instructing = 0.0;
92          Double safety = 0.0;
93          Double dual = 0.0;
94          Double solo = 0.0;
95          Double actualIMC = 0.0;
96          Double simulatedIMC = 0.0;
97          Double flightSim = 0.0;
98          Double nightFlight = 0.0;
99          for(Logbook item : rows) {
100             totalTime = totalTime + item.getFlightDuration();
101             pic = pic + item.getPic();
102             sic = sic + item.getSic();
103             xCountry = xCountry + item.getCrossCountry();
104             instructing = instructing + item.getFlightInstructing();
105             safety = safety + item.getSafetyPilot();
106             dual = dual + item.getDualReceived();
107             solo = solo + item.getSolo();
108             nightFlight = nightFlight + item.getConditionNight();
109             actualIMC = actualIMC + item.getConditionActualImc();
110             simulatedIMC = simulatedIMC + item.getConditionSimulatedImc();
111             flightSim = flightSim + item.getConditionFlightSim();
112         }
113         DefaultPieDataset dataset = new DefaultPieDataset();
114         dataset.setValue("PIC", pic);
115         dataset.setValue("SIC", sic);
116         dataset.setValue("X-country", xCountry);
117         dataset.setValue("Instructing", instructing);
118         dataset.setValue("Safety Pilot", safety);
119         dataset.setValue("Dual", dual);
120         dataset.setValue("Solo", solo);
121         dataset.setValue("Actual IMC" , actualIMC);
122         dataset.setValue("Hood", simulatedIMC);
123         dataset.setValue("Simulator", flightSim);
124         dataset.setValue("Night", nightFlight);
125         return dataset;
126     }
127 }