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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
72
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
103
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
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
132
133
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 }