1   package org.wcb.gui.forms.report;
2   
3   import org.wcb.model.bd.LogbookDelegate;
4   import org.wcb.model.bd.AircraftDelegate;
5   import org.wcb.model.util.SpringUtil;
6   import org.wcb.model.service.IServicesConstants;
7   import org.wcb.model.vo.hibernate.Logbook;
8   import org.wcb.model.vo.hibernate.AircraftTypeBO;
9   import org.wcb.model.vo.hibernate.AircraftBO;
10  import org.jfree.chart.ChartPanel;
11  import org.jfree.chart.JFreeChart;
12  import org.jfree.chart.ChartFactory;
13  import org.jfree.chart.renderer.category.BarRenderer;
14  import org.jfree.chart.axis.NumberAxis;
15  import org.jfree.chart.axis.CategoryLabelPositions;
16  import org.jfree.chart.axis.CategoryAxis;
17  import org.jfree.chart.plot.PlotOrientation;
18  import org.jfree.chart.plot.CategoryPlot;
19  import org.jfree.data.category.CategoryDataset;
20  import org.jfree.data.category.DefaultCategoryDataset;
21  import org.jfree.data.UnknownKeyException;
22  
23  import javax.swing.*;
24  import java.awt.*;
25  
26  
27  /**
28   * <small>
29   * <p/>
30   * Copyright (C)  2006  wbogaardt.
31   * This library is free software; you can redistribute it and/or
32   * modify it under the terms of the GNU Lesser General Public
33   * License as published by the Free Software Foundation; either
34   * version 2.1 of the License, or (at your option) any later version.
35   * <p/>
36   * This library is distributed in the hope that it will be useful,
37   * but WITHOUT ANY WARRANTY; without even the implied warranty of
38   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39   * Lesser General Public License for more details.
40   * <p/>
41   * You should have received a copy of the GNU Lesser General Public
42   * License along with this library; if not, write to the Free Software
43   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
44   * <p/>
45   * $File:  $ <br>
46   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Nov 21, 2006 12:28:22 PM $ <br>
47   * </small>
48   *
49   * @author wbogaardt
50   *         This is to display a report that shows all the makes and models flown
51   * broken down by total time and pilot in command time for each make and model. This is
52   * a bar chart for easy readability.
53   */
54  
55  public class AircraftMakeModelReportForm extends JPanel implements ReportRefresh {
56  
57      private LogbookDelegate delegate;
58      private AircraftDelegate aircrafts;
59      private ChartPanel chartPanel;
60  
61      public AircraftMakeModelReportForm() {
62          delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
63          aircrafts = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
64          initComponents();
65      }
66  
67      public void refresh() {
68          CategoryDataset dataset = createDataSet();
69  
70  // create the chart...
71          JFreeChart chart = ChartFactory.createBarChart(
72                  "AircraftBO by Make/Model",       // chart title
73                  "Make/Model",               // domain axis label
74                  "Time",                  // range axis label
75                  dataset,                  // data
76                  PlotOrientation.VERTICAL, // orientation
77                  true,                     // include legend
78                  true,                     // tooltips?
79                  false                     // URLs?
80          );
81  
82          chart.setBackgroundPaint(Color.white);
83          // get a reference to the plot for further customisation...
84          CategoryPlot plot = chart.getCategoryPlot();
85          plot.setBackgroundPaint(Color.lightGray);
86          plot.setDomainGridlinePaint(Color.white);
87          plot.setDomainGridlinesVisible(true);
88          plot.setRangeGridlinePaint(Color.white);
89  
90          // set the range axis to display integers only...
91          final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
92          rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
93  
94          // disable bar outlines...
95          BarRenderer renderer = (BarRenderer) plot.getRenderer();
96          renderer.setDrawBarOutline(false);
97  
98          // set up gradient paints for series...
99          GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
100                 0.0f, 0.0f, new Color(0, 0, 64));
101         GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green,
102                 0.0f, 0.0f, new Color(0, 64, 0));
103         GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red,
104                 0.0f, 0.0f, new Color(64, 0, 0));
105         renderer.setSeriesPaint(0, gp0);
106         renderer.setSeriesPaint(1, gp1);
107         renderer.setSeriesPaint(2, gp2);
108 
109         CategoryAxis domainAxis = plot.getDomainAxis();
110         domainAxis.setCategoryLabelPositions(
111                 CategoryLabelPositions.createUpRotationLabelPositions(
112                         Math.PI / 6.0));
113         chartPanel.setChart(chart);
114 
115     }
116 
117     private CategoryDataset createDataSet() {
118         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
119 
120         java.util.List<AircraftBO> aircraftsList = aircrafts.getAllAircraft();
121         for(AircraftBO item: aircraftsList) {
122             AircraftTypeBO type = aircrafts.getAircraftType(item.getTypeId());
123             this.getTotalForAircraft(dataset,type.getManufacturer() + "/" +type.getModel(), item.getRegistrationNumber());
124 
125         }
126         return dataset;
127     }
128 
129     private void getTotalForAircraft(DefaultCategoryDataset dataset, String makeModel, String registration) {
130         java.util.List<Logbook> rows = delegate.findByRegistration(registration);
131         Double pic = 0.0;
132         Double totalTime = 0.0;
133         for(Logbook item : rows) {
134             if(item.getFlightDuration() > 0) {
135                 totalTime = totalTime + item.getFlightDuration();
136                 pic = pic + item.getPic();
137             } else {
138                 //must be a PCATD device
139                 totalTime = totalTime + item.getConditionFlightSim();
140                 pic = pic + item.getConditionFlightSim();
141             }
142         }
143         try {
144             Number numTotal = dataset.getValue("Total Time", makeModel);
145             Number numPIC = dataset.getValue("PIC Time", makeModel);
146             totalTime = numTotal.doubleValue() + totalTime;
147             pic = numPIC.doubleValue() + pic;
148         } catch (UnknownKeyException uke) {
149             //unknown key problem is because it hasn't been set so
150             //we need to just create default values;
151             dataset.addValue(totalTime, "Total Time", makeModel);
152             dataset.addValue(pic, "PIC Time", makeModel);
153         }
154         dataset.setValue(totalTime, "Total Time", makeModel);
155         dataset.setValue(pic, "PIC Time", makeModel);
156     }
157 
158     private void initComponents() {
159         CategoryDataset dataset = createDataSet();
160 
161 // create the chart...
162         JFreeChart chart = ChartFactory.createBarChart(
163                 "Bar Chart Demo 1",       // chart title
164                 "Category",               // domain axis label
165                 "Value",                  // range axis label
166                 dataset,                  // data
167                 PlotOrientation.VERTICAL, // orientation
168                 true,                     // include legend
169                 true,                     // tooltips?
170                 false                     // URLs?
171         );
172 
173         chart.setBackgroundPaint(Color.white);
174         // get a reference to the plot for further customisation...
175         CategoryPlot plot = chart.getCategoryPlot();
176         plot.setBackgroundPaint(Color.lightGray);
177         plot.setDomainGridlinePaint(Color.white);
178         plot.setDomainGridlinesVisible(true);
179         plot.setRangeGridlinePaint(Color.white);
180 
181 // set the range axis to display integers only...
182         final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
183         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
184 
185         CategoryAxis domainAxis = plot.getDomainAxis();
186         domainAxis.setCategoryLabelPositions(
187                 CategoryLabelPositions.createUpRotationLabelPositions(
188                         Math.PI / 6.0));
189         chartPanel = new ChartPanel(chart);
190         add(chartPanel);
191     }
192 }