1 /********************************************************************************
2 * InternetCafe is a software solution that helps the management of Cybercafes
3 * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4 * Copyright (C) 2006 Guido Angelo Ingenito
5
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 package ui.table.model;
21
22 import java.util.Date;
23 import java.util.Observable;
24 import java.util.Observer;
25
26 import javax.swing.table.AbstractTableModel;
27
28 import ui.Messages;
29 import base.Control;
30 import base.InternetCafeManager;
31 import base.user.User;
32 import base.user.UserCredential;
33 import base.user.UserGender;
34
35 @SuppressWarnings("serial")
36 public class UserTableModel extends AbstractTableModel implements Observer {
37
38 String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.name"), Messages.getString("common.surname"), Messages.getString("common.gender"), Messages.getString("common.birthday"),
39 Messages.getString("common.nickname"), Messages.getString("common.credential"), Messages.getString("common.validdocument"), Messages.getString("common.creationdate") };
40
41 public static final int USER_ID_COLUMN = 0;
42
43 public static final int USER_NAME_COLUMN = 1;
44
45 public static final int USER_SURNAME_COLUMN = 2;
46
47 public static final int USER_GENDER_COLUMN = 3;
48
49 public static final int USER_BIRTHDAY_COLUMN = 4;
50
51 public static final int USER_NICKNAME_COLUMN = 5;
52
53 public static final int USER_CREDENTIAL_COLUMN = 6;
54
55 public static final int USER_VALID_DOCUMENT_COLUMN = 7;
56
57 public static final int USER_CREATION_DATE_COLUMN = 8;
58
59 public UserTableModel() {
60 super();
61 InternetCafeManager.getInstance().addObserver(this);
62 }
63
64 public int getColumnCount() {
65 return columnNames.length;
66 }
67
68 public int getRowCount() {
69 return InternetCafeManager.getInstance().getUser().length;
70 }
71
72 public String getColumnName(int col) {
73 return columnNames[col];
74 }
75
76 public Object getValueAt(int row, int column) {
77 if (row < 0 || column < 0)
78 return null;
79 User[] user = InternetCafeManager.getInstance().getUser();
80 Object result = null;
81 switch (column) {
82 case USER_ID_COLUMN:
83 result = user[row].getId();
84 break;
85 case USER_NAME_COLUMN:
86 result = user[row].getName();
87 break;
88 case USER_SURNAME_COLUMN:
89 result = user[row].getSurname();
90 break;
91 case USER_GENDER_COLUMN:
92 result = user[row].getGender();
93 break;
94 case USER_BIRTHDAY_COLUMN:
95 result = user[row].getBirthday();
96 break;
97 case USER_NICKNAME_COLUMN:
98 result = user[row].getNickname();
99 break;
100 case USER_CREDENTIAL_COLUMN:
101 result = user[row].getCredential();
102 break;
103 case USER_VALID_DOCUMENT_COLUMN:
104
105 result = Control.isValidUserDocument(user[row].getDocument())
106 && Control.isValidDocumentImage(user[row].getDocument()
107 .getImagePath());
108 break;
109 case USER_CREATION_DATE_COLUMN:
110 result = user[row].getCreationDate();
111 break;
112 }
113 return result;
114 }
115
116 @SuppressWarnings("unchecked")
117 public Class getColumnClass(int column) {
118 if (column < 0)
119 return null;
120 Class result = null;
121 switch (column) {
122 case USER_ID_COLUMN:
123 result = Integer.class;
124 break;
125 case USER_NAME_COLUMN:
126 result = String.class;
127 break;
128 case USER_SURNAME_COLUMN:
129 result = String.class;
130 break;
131 case USER_GENDER_COLUMN:
132 result = UserGender.class;
133 break;
134 case USER_BIRTHDAY_COLUMN:
135 result = Date.class;
136 break;
137 case USER_NICKNAME_COLUMN:
138 result = String.class;
139 break;
140 case USER_CREDENTIAL_COLUMN:
141 result = UserCredential.class;
142 break;
143 case USER_VALID_DOCUMENT_COLUMN:
144 result = Boolean.class;
145 break;
146 case USER_CREATION_DATE_COLUMN:
147 result = Date.class;
148 break;
149 }
150 return result;
151 }
152
153
154
155
156 public boolean isCellEditable(int row, int column) {
157 if (row < 0 || column < 0)
158 return false;
159
160
161 boolean result = false;
162 switch (column) {
163 case USER_ID_COLUMN:
164 result = false;
165 break;
166 case USER_NAME_COLUMN:
167 result = false;
168 break;
169 case USER_SURNAME_COLUMN:
170 result = false;
171 break;
172 case USER_GENDER_COLUMN:
173 result = true;
174 break;
175 case USER_BIRTHDAY_COLUMN:
176 result = false;
177 break;
178 case USER_NICKNAME_COLUMN:
179 result = false;
180 break;
181 case USER_CREDENTIAL_COLUMN:
182 result = true;
183 break;
184 case USER_VALID_DOCUMENT_COLUMN:
185 result = false;
186 break;
187 case USER_CREATION_DATE_COLUMN:
188 result = false;
189 break;
190 }
191 return result;
192 }
193
194
195
196
197 public void setValueAt(Object value, int row, int column) {
198 if (row < 0 || column < 0)
199 return;
200
201 User[] user = InternetCafeManager.getInstance().getUser();
202 switch (column) {
203 case USER_ID_COLUMN:
204 break;
205 case USER_NAME_COLUMN:
206 user[row].setName((String) value);
207 break;
208 case USER_SURNAME_COLUMN:
209 user[row].setSurname((String) value);
210 break;
211 case USER_GENDER_COLUMN:
212 user[row].setGender((String) value);
213 break;
214 case USER_BIRTHDAY_COLUMN:
215 break;
216 case USER_NICKNAME_COLUMN:
217 break;
218 case USER_CREDENTIAL_COLUMN:
219 user[row].setCredential((String) value);
220 break;
221 case USER_VALID_DOCUMENT_COLUMN:
222 break;
223 case USER_CREATION_DATE_COLUMN:
224 break;
225 }
226
227 fireTableCellUpdated(row, column);
228 }
229
230
231
232
233
234
235 public void update(Observable observable, Object changedObject) {
236 if (changedObject instanceof User)
237 this.fireTableDataChanged();
238 }
239
240 }