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