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.axis.DateAxis;
13  import org.jfree.chart.plot.XYPlot;
14  import org.jfree.chart.renderer.xy.XYItemRenderer;
15  import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
16  import org.jfree.data.time.Month;
17  import org.jfree.data.time.TimeSeries;
18  import org.jfree.data.time.TimeSeriesCollection;
19  import org.jfree.data.xy.XYDataset;
20  import org.jfree.ui.RectangleInsets;
21  
22  import javax.swing.*;
23  import java.awt.*;
24  import java.text.SimpleDateFormat;
25  import java.util.*;
26  
27  /**
28   * <small>
29   * Copyright (c)  2006  wbogaardt.
30   * This library is free software; you can redistribute it and/or
31   * modify it under the terms of the GNU Lesser General Public
32   * License as published by the Free Software Foundation; either
33   * version 2.1 of the License, or (at your option) any later version.
34   *
35   * This library is distributed in the hope that it will be useful,
36   * but WITHOUT ANY WARRANTY; without even the implied warranty of
37   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
38   * Lesser General Public License for more details.
39   *
40   * You should have received a copy of the GNU Lesser General Public
41   * License along with this library; if not, write to the Free Software
42   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
43   * <p/>
44   * $File:  $ <br>
45   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Apr 3, 2006 2:53:41 PM $ <br>
46   * </small>
47   *
48   * @author wbogaardt
49   * @version 1
50   *          Date: Apr 3, 2006
51   *          Time: 2:53:41 PM
52   */
53  
54  public class TotalFlightTimeByMonthReportForm extends JPanel implements ReportRefresh {
55  
56      private LogbookDelegate delegate;
57      private ChartPanel chartPanel;
58  
59      public TotalFlightTimeByMonthReportForm() {
60          delegate = (LogbookDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.LOGBOOK_DELEGATE);
61          initComponents();
62      }
63  
64      public void refresh() {
65           XYDataset data  = this.getData();
66          JFreeChart chart = ChartFactory.createTimeSeriesChart(
67                  "Time By Month",  // title
68                  "Date",             // x-axis label
69                  "Hours Flown",   // y-axis label
70                  data,            // data
71                  true,               // create legend?
72                  true,               // generate tooltips?
73                  false               // generate URLs?
74          );
75          chart.setBackgroundPaint(Color.white);
76          XYPlot plot = (XYPlot) chart.getPlot();
77          plot.setBackgroundPaint(Color.lightGray);
78          plot.setDomainGridlinePaint(Color.white);
79          plot.setRangeGridlinePaint(Color.white);
80          plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
81          plot.setDomainCrosshairVisible(true);
82          plot.setRangeCrosshairVisible(true);
83          plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
84  
85          XYItemRenderer r = plot.getRenderer();
86          if (r instanceof XYLineAndShapeRenderer) {
87              XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
88              renderer.setBaseShapesVisible(true);
89              renderer.setBaseShapesFilled(true);
90          }
91  
92          DateAxis axis = (DateAxis) plot.getDomainAxis();
93          axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
94          chartPanel.setChart(chart);
95      }
96  
97      private void initComponents() {
98          XYDataset data  = this.getData();
99          JFreeChart chart = ChartFactory.createTimeSeriesChart(
100                 "Time By Month",  // title
101                 "Date",             // x-axis label
102                 "Hours Flown",   // y-axis label
103                 data,            // data
104                 true,               // create legend?
105                 true,               // generate tooltips?
106                 false               // generate URLs?
107         );
108         chart.setBackgroundPaint(Color.white);
109         XYPlot plot = (XYPlot) chart.getPlot();
110         plot.setBackgroundPaint(Color.lightGray);
111         plot.setDomainGridlinePaint(Color.white);
112         plot.setRangeGridlinePaint(Color.white);
113         plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
114         plot.setDomainCrosshairVisible(true);
115         plot.setRangeCrosshairVisible(true);
116         plot.setNoDataMessage(MessageResourceRegister.getInstance().getValue(MessageKey.LABEL_NODATA));
117 
118         XYItemRenderer r = plot.getRenderer();
119         if (r instanceof XYLineAndShapeRenderer) {
120             XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
121             renderer.setBaseShapesVisible(true);
122             renderer.setBaseShapesFilled(true);
123         }
124 
125         DateAxis axis = (DateAxis) plot.getDomainAxis();
126         axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
127         chartPanel = new ChartPanel(chart);
128         add(chartPanel);
129     }
130 
131     private XYDataset getData() {
132         java.util.List<Logbook> rows = delegate.getAllLogbookEntries();
133         HashMap map = new HashMap();
134         Month tempMonth;
135         for(Logbook item : rows) {
136             int year;
137             if(item.getEntryDate().getYear() < 1900) {
138                 year = 1900 + item.getEntryDate().getYear();
139             } else {
140                 year = item.getEntryDate().getYear();
141             }
142 
143             GregorianCalendar cal = new GregorianCalendar(year, item.getEntryDate().getMonth(),1);
144 
145             tempMonth = new Month(cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR));
146             double value = getNotNullValue(map.get(tempMonth));
147             if (value == 0) {
148                 map.put(tempMonth, item.getFlightDuration());
149             } else {
150                 double flightDuration = value + item.getFlightDuration();
151                 map.put(tempMonth, flightDuration);
152             }
153         }
154 
155         TimeSeries s1 = new TimeSeries("Flight Hours", Month.class);
156 
157         Set<Month> itemkeys = map.keySet();
158         for(Month itemk : itemkeys) {
159             s1.add(itemk, (Double) map.get(itemk));
160         }
161 
162 
163         TimeSeriesCollection dataset = new TimeSeriesCollection();
164         dataset.addSeries(s1);
165         return dataset;
166     }
167 
168     private double getNotNullValue(Object val) {
169         try {
170             return (Double) val;
171         } catch (NullPointerException npe) {
172             return 0.0;
173         }
174 
175     }
176 }