1   package org.wcb.gui.table.model;
2   
3   import org.wcb.model.vo.hibernate.Logbook;
4   import org.wcb.model.vo.TotalLogEntriesVO;
5   
6   import javax.swing.table.TableModel;
7   import javax.swing.table.AbstractTableModel;
8   import javax.swing.event.TableModelListener;
9   import java.util.ArrayList;
10  import java.util.List;
11  import java.text.DateFormat;
12  
13  /**
14   * <small>
15   * <p/>
16   * Copyright (c)  2006  wbogaardt.
17   * This library is free software; you can redistribute it and/or
18   * modify it under the terms of the GNU Lesser General Public
19   * License as published by the Free Software Foundation; either
20   * version 2.1 of the License, or (at your option) any later version.
21   *
22   * This library is distributed in the hope that it will be useful,
23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25   * Lesser General Public License for more details.
26   *
27   * You should have received a copy of the GNU Lesser General Public
28   * License along with this library; if not, write to the Free Software
29   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
30   * <p/>
31   * $File:  $ <br>
32   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 12, 2006 12:03:08 PM $ <br>
33   * </small>
34   *
35   * @author wbogaardt
36   *         Allows printing of the entire logbook.
37   */
38  
39  public class PrintableLogbookModel extends AbstractTableModel implements TableModel {
40  
41         private Object[] columnNameFlat = new Object[] {
42              "Date", "Reg", "From", "Via", "To", "Duration", "PIC", "SIC", "X-country",
43              "Instructing", "Safety", "Dual", "Solo",
44              "TO/LDG", "TO/LDG","Inst Appr", "Night", "Actual IMC", "Simulated IMC",
45              "Sim"};
46  
47      private ArrayList row;
48      private DateFormat dateFormater = DateFormat.getDateInstance(DateFormat.MEDIUM);
49  
50      public PrintableLogbookModel(List<Logbook> logs) {
51          row = (ArrayList)logs;
52          row.add(new TotalLogEntriesVO());
53          reCalculate();
54      }
55  
56      public int getRowCount() {
57          return row.size();
58      }
59  
60      public int getColumnCount() {
61          return columnNameFlat.length;
62      }
63  
64      public String getColumnName(int i) {
65          return (String) columnNameFlat[i];
66      }
67  
68      public boolean isCellEditable(int i, int x) {
69          return false;
70      }
71  
72      public void setValueAt(Object obj, int i, int x) {
73  
74      }
75  
76       public Object getValueAt(int rowLine, int column) {
77          Object objValue = row.get(rowLine);
78          if (objValue instanceof Logbook) {
79              Logbook entry = (Logbook) objValue;
80              switch(column)
81              {
82                  case 0:
83                      return dateFormater.format(entry.getEntryDate());
84                  case 1:
85                      return entry.getRegistration();
86                  case 2:
87                      return entry.getFaaFrom();
88                  case 3:
89                      return entry.getFaaVia();
90                  case 4:
91                      return entry.getFaaTo();
92                  case 5:
93                      return entry.getFlightDuration();
94                  case 6:
95                      return entry.getPic();
96                  case 7:
97                      return entry.getSic();
98                  case 8:
99                      return entry.getCrossCountry();
100                 case 9:
101                     return entry.getFlightInstructing();
102                 case 10:
103                     return entry.getSafetyPilot();
104                 case 11:
105                     return entry.getDualReceived();
106                 case 12:
107                     return entry.getSolo();
108                 case 13:
109                     return entry.getDayTakeoffs() + " | " + entry.getDayLandings();
110                 case 14:
111                     return entry.getNightTakeoffs() + " | " + entry.getNightLandings();
112                 case 15:
113                     return entry.getInstrumentApproaches();
114                 case 16:
115                     return entry.getConditionNight();
116                 case 17:
117                     return entry.getConditionActualImc();
118                 case 18:
119                     return entry.getConditionSimulatedImc();
120                 case 19:
121                     return entry.getConditionFlightSim();
122                 default:
123                     return entry;
124             }
125         }
126         if (objValue instanceof TotalLogEntriesVO) {
127             TotalLogEntriesVO totalRow = (TotalLogEntriesVO) objValue;
128             switch(column) {
129                 case 0:
130                     return totalRow.getDescription();
131                 case 5:
132                     return totalRow.getTotalFlight();
133                 case 6:
134                     return totalRow.getTotalPIC();
135                  case 7:
136                     return totalRow.getTotalSIC();
137                  case 8:
138                     return totalRow.getTotalXCountry();
139                 case 9:
140                     return totalRow.getTotalFlightInstructing();
141                 case 10:
142                     return totalRow.getTotalSafety();
143                 case 11:
144                     return totalRow.getTotalDual();
145                 case 12:
146                     return totalRow.getTotalSolo();
147                 case 15:
148                     return totalRow.getTotalApproaches();
149                 case 16:
150                     return totalRow.getTotalNight();
151                 case 17:
152                     return totalRow.getTotalIMC();
153                 case 18:
154                     return totalRow.getTotalSimulatedIMC();
155                 case 19:
156                     return totalRow.getTotalSimulatorTime();
157                 default:
158                     return " ";
159             }
160         }
161         return "";
162     }
163 
164     /**
165      * This updates the totals row which displays at the bottom of the table.
166      * This method would be called in cases of the model change like a row
167      * being deleted or added.
168      */
169     private void reCalculate() {
170         double totalFlightDuration = 0.0;
171         double totalPIC = 0.0;
172         double totalSIC = 0.0;
173         double totalXCountry = 0.0;
174         double totalFlightInstructing = 0.0;
175         double totalSafety = 0.0;
176         double totalDual= 0.0;
177         double totalSolo = 0.0;
178         int totalApproaches = 0;
179         double totalNight = 0.0;
180         double totalIMC = 0.0;
181         double totalSimulatedIMC = 0.0;
182         double totalFlightSim = 0.0;
183         for (int i = 0; i < this.getRowCount() - 2; i++) {
184             totalFlightDuration += ((Double) this.getValueAt(i, 5)).doubleValue();
185             totalPIC += ((Double) this.getValueAt(i, 6)).doubleValue();
186             totalSIC += ((Double) this.getValueAt(i, 7)).doubleValue();
187             totalXCountry += ((Double) this.getValueAt(i, 8)).doubleValue();
188             totalFlightInstructing += ((Double) this.getValueAt(i, 9)).doubleValue();
189             totalSafety += ((Double) this.getValueAt(i, 10)).doubleValue();
190             totalDual += ((Double) this.getValueAt(i, 11)).doubleValue();
191             totalSolo += ((Double) this.getValueAt(i, 12)).doubleValue();
192             totalApproaches += ((Integer) this.getValueAt(i, 15)).intValue();
193             totalNight += ((Double) this.getValueAt(i, 16)).doubleValue();
194             totalIMC += ((Double) this.getValueAt(i, 17)).doubleValue();
195             totalSimulatedIMC += ((Double) this.getValueAt(i, 18)).doubleValue();
196             totalFlightSim += ((Double) this.getValueAt(i, 19)).doubleValue();
197         }
198         TotalLogEntriesVO voTotal = (TotalLogEntriesVO) row.get(getRowCount()-1);
199         voTotal.setDescription("Page Totals");
200         voTotal.setTotalFlight(totalFlightDuration);
201         voTotal.setTotalPIC(totalPIC);
202         voTotal.setTotalSIC(totalSIC);
203         voTotal.setTotalXCountry(totalXCountry);
204         voTotal.setTotalFlightInstructing(totalFlightInstructing);
205         voTotal.setTotalDual(totalDual);
206         voTotal.setTotalSafety(totalSafety);
207         voTotal.setTotalSolo(totalSolo);
208         voTotal.setTotalApproaches(totalApproaches);
209         voTotal.setTotalNight(totalNight);
210         voTotal.setTotalIMC(totalIMC);
211         voTotal.setTotalSimulatedIMC(totalSimulatedIMC);
212         voTotal.setTotalSimulatorTime(totalFlightSim);
213         row.set(getRowCount()-1, voTotal);
214         fireTableDataChanged();
215     }
216 
217     public Class getColumnClass(int columnIndex) {
218         try
219         {
220             return getValueAt(0, columnIndex).getClass();
221         }
222         catch (Exception e)
223         {
224             return String.class;
225         }
226     }
227 
228     public void addTableModelListener(TableModelListener listen) {
229 
230     }
231 
232     public void removeTableModelListener(TableModelListener listener) {
233 
234     }
235 }