View Javadoc

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") //$NON-NLS-1$
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"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
39  			Messages.getString("common.nickname"), Messages.getString("common.credential"), Messages.getString("common.validdocument"), Messages.getString("common.creationdate") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
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:// Id
83  			result = user[row].getId();
84  			break;
85  		case USER_NAME_COLUMN:// Name
86  			result = user[row].getName();
87  			break;
88  		case USER_SURNAME_COLUMN:// Surname
89  			result = user[row].getSurname();
90  			break;
91  		case USER_GENDER_COLUMN:// Gender
92  			result = user[row].getGender();
93  			break;
94  		case USER_BIRTHDAY_COLUMN:// Birthday
95  			result = user[row].getBirthday();
96  			break;
97  		case USER_NICKNAME_COLUMN:// Nickname
98  			result = user[row].getNickname();
99  			break;
100 		case USER_CREDENTIAL_COLUMN:// Credential
101 			result = user[row].getCredential();
102 			break;
103 		case USER_VALID_DOCUMENT_COLUMN:// Has Document associated with a valid
104 			// image
105 			result = Control.isValidUserDocument(user[row].getDocument())
106 					&& Control.isValidDocumentImage(user[row].getDocument()
107 							.getImagePath());
108 			break;
109 		case USER_CREATION_DATE_COLUMN:// Creation Date
110 			result = user[row].getCreationDate();
111 			break;
112 		}
113 		return result;
114 	}
115 
116 	@SuppressWarnings("unchecked") //$NON-NLS-1$
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:// Id
123 			result = Integer.class;
124 			break;
125 		case USER_NAME_COLUMN:// Name
126 			result = String.class;
127 			break;
128 		case USER_SURNAME_COLUMN:// Surname
129 			result = String.class;
130 			break;
131 		case USER_GENDER_COLUMN:// Gender
132 			result = UserGender.class;
133 			break;
134 		case USER_BIRTHDAY_COLUMN:// Birthday
135 			result = Date.class;
136 			break;
137 		case USER_NICKNAME_COLUMN:// Nickname
138 			result = String.class;
139 			break;
140 		case USER_CREDENTIAL_COLUMN:// Credential
141 			result = UserCredential.class;
142 			break;
143 		case USER_VALID_DOCUMENT_COLUMN:// Has Document
144 			result = Boolean.class;
145 			break;
146 		case USER_CREATION_DATE_COLUMN:// Creation Date
147 			result = Date.class;
148 			break;
149 		}
150 		return result;
151 	}
152 
153 	/*
154 	 * Don't need to implement this method unless your table's editable.
155 	 */
156 	public boolean isCellEditable(int row, int column) {
157 		if (row < 0 || column < 0)
158 			return false;
159 		// Note that the data/cell address is constant,
160 		// no matter where the cell appears onscreen.
161 		boolean result = false;
162 		switch (column) {
163 		case USER_ID_COLUMN:// Id
164 			result = false;
165 			break;
166 		case USER_NAME_COLUMN:// Name
167 			result = false;
168 			break;
169 		case USER_SURNAME_COLUMN:// Surname
170 			result = false;
171 			break;
172 		case USER_GENDER_COLUMN:// Gender
173 			result = true;
174 			break;
175 		case USER_BIRTHDAY_COLUMN:// Birthday
176 			result = false;
177 			break;
178 		case USER_NICKNAME_COLUMN:// Nickname
179 			result = false;
180 			break;
181 		case USER_CREDENTIAL_COLUMN:// Credential
182 			result = true;
183 			break;
184 		case USER_VALID_DOCUMENT_COLUMN:// Has Document
185 			result = false;
186 			break;
187 		case USER_CREATION_DATE_COLUMN:// Creation Date
188 			result = false;
189 			break;
190 		}
191 		return result;
192 	}
193 
194 	/*
195 	 * Don't need to implement this method unless your table's data can change.
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:// Id
204 			break;
205 		case USER_NAME_COLUMN:// Name
206 			user[row].setName((String) value);
207 			break;
208 		case USER_SURNAME_COLUMN:// Surname
209 			user[row].setSurname((String) value);
210 			break;
211 		case USER_GENDER_COLUMN:// Gender
212 			user[row].setGender((String) value);
213 			break;
214 		case USER_BIRTHDAY_COLUMN:// Birthday
215 			break;
216 		case USER_NICKNAME_COLUMN:// Nickname
217 			break;
218 		case USER_CREDENTIAL_COLUMN:// Credential
219 			user[row].setCredential((String) value);
220 			break;
221 		case USER_VALID_DOCUMENT_COLUMN:// Has Document
222 			break;
223 		case USER_CREATION_DATE_COLUMN:// Creation Date
224 			break;
225 		}
226 
227 		fireTableCellUpdated(row, column);
228 	}
229 
230 	/*
231 	 * (non-Javadoc)
232 	 * 
233 	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
234 	 */
235 	public void update(Observable observable, Object changedObject) {
236 		if (changedObject instanceof User)
237 			this.fireTableDataChanged();
238 	}
239 
240 }