1   package org.wcb.gui.table.model;
2   
3   import org.wcb.model.vo.hibernate.AircraftBO;
4   import org.wcb.model.vo.hibernate.AircraftTypeBO;
5   import org.wcb.model.bd.AircraftDelegate;
6   import org.wcb.model.util.SpringUtil;
7   import org.wcb.model.service.IServicesConstants;
8   
9   import javax.swing.table.AbstractTableModel;
10  import java.util.List;
11  import java.util.ArrayList;
12  
13  /**
14   * <small>
15   * Copyright (c)  2006  wbogaardt.
16  * This library is free software; you can redistribute it and/or
17   * modify it under the terms of the GNU Lesser General Public
18   * License as published by the Free Software Foundation; either
19   * version 2.1 of the License, or (at your option) any later version.
20   *
21   * This library is distributed in the hope that it will be useful,
22   * but WITHOUT ANY WARRANTY; without even the implied warranty of
23   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24   * Lesser General Public License for more details.
25   *
26   * You should have received a copy of the GNU Lesser General Public
27   * License along with this library; if not, write to the Free Software
28   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
29   * <p/>
30   * $File:  $ <br>
31   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Mar 28, 2006 8:51:59 AM $ <br>
32   * </small>
33   *
34   * @author wbogaardt
35   * @version 1
36   *          Date: Mar 28, 2006
37   *          Time: 8:51:59 AM
38   */
39  
40  public class AircraftSelectModel extends AbstractTableModel {
41  
42      private String[] columnName;
43      private ArrayList row;
44      private Class[] columnClassTypes = new Class [] {
45                  Object.class, String.class, String.class, Object.class, Object.class
46              };
47  
48      private AircraftDelegate delegate;
49      private List<AircraftTypeBO> aircraftTypes;
50  
51      public AircraftSelectModel() {
52          this(new String[] {"Select", "Registration", "type", "",""});
53      }
54  
55      public AircraftSelectModel(String[] colNames) {
56          columnName = colNames;
57      }
58  
59       public AircraftDelegate getDelegate() {
60          if (delegate == null) {
61               delegate = (AircraftDelegate) SpringUtil.getApplicationContext().getBean(IServicesConstants.AIRCRAFT_DELEGATE);
62          }
63          return delegate;
64      }
65  
66      public void setDelegate(AircraftDelegate delegate) {
67          this.delegate = delegate;
68      }
69  
70      /**
71       * this gets the data from the database and marshalls it into
72       * a table model for display.
73       */
74      public void refreshModel() {
75          List<AircraftBO> aircraft = getDelegate().getAllAircraft();
76          row = (ArrayList) aircraft;
77          aircraftTypes = getDelegate().getAircraftTypes();
78          fireTableDataChanged();
79      }
80  
81      public String getColumnName(int column) {
82          return columnName[column];
83      }
84  
85      public boolean isCellEditable(int row, int column) {
86          return true;
87      }
88  
89      public int getColumnCount() {
90          return columnName.length;
91      }
92  
93      public int getRowCount() {
94          if (row == null) {
95              this.refreshModel();
96          }
97          return row.size();
98      }
99  
100     public Object getValueAt(int rowLine, int column) {
101         AircraftBO rowitem = (AircraftBO)row.get(rowLine);
102         //If the specific table has select as the option then the
103         //placement of columns is such.
104         if (getColumnCount() == 5) {
105             switch(column) {
106                 case 1:
107                     return rowitem.getRegistrationNumber();
108                 case 2:
109                     return this.getAircraftType(rowitem.getTypeId()).getAbbreviation();
110                 default:
111                     return rowitem;
112             }
113         } else {
114             //Else the first two columns should always return a registration number, and type of aircraft.
115              switch(column) {
116                 case 0:
117                     return rowitem.getRegistrationNumber();
118                 case 1:
119                     return this.getAircraftType(rowitem.getTypeId()).getAbbreviation();
120                 default:
121                     return rowitem;
122             }
123         }
124     }
125 
126     public AircraftBO getItemAt(int rowId) {
127         return (AircraftBO) row.get(rowId);
128     }
129 
130     /**
131      * Remove the identified row from the
132      * model and fire a data table change event.
133      * @param rowId Row number to remove
134      */
135     public void removeRow(int rowId)
136     {
137         row.remove(rowId);
138         fireTableDataChanged();
139     }
140 
141     public Class getColumnClass(int columnIndex) {
142                 return columnClassTypes [columnIndex];
143     }
144 
145     private AircraftTypeBO getAircraftType(Integer id) {
146         for(AircraftTypeBO item : aircraftTypes) {
147             if(item.getId().equals(id)) {
148                 return item;
149             }
150         }
151         return null;
152     }
153 }