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.InternetCafeManager;
30  import base.session.Session;
31  
32  @SuppressWarnings("serial") //$NON-NLS-1$
33  public class SessionTableModel extends AbstractTableModel implements Observer {
34  
35  	/*
36  	 * private User user; private Workstation workstation; private String
37  	 * description; private Date startTime; private Date endTime;
38  	 */
39  	String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.nickname"), Messages.getString("common.workstation"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
40  			Messages.getString("common.description"), Messages.getString("common.starttime"), Messages.getString("common.endtime"), Messages.getString("common.service"), Messages.getString("common.finished") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
41  
42  	public static final int SESSION_ID_COLUMN = 0;
43  
44  	public static final int SESSION_USER_NICKNAME_COLUMN = 1;
45  
46  	public static final int SESSION_WORKSTATION_NAME_COLUMN = 2;
47  
48  	public static final int SESSION_DESCRIPTION_COLUMN = 3;
49  
50  	public static final int SESSION_START_TIME_COLUMN = 4;
51  
52  	public static final int SESSION_END_TIME_COLUMN = 5;
53  
54  	public static final int SESSION_SERVICE_COLUMN = 6;
55  
56  	public static final int SESSION_FINISHED_COLUMN = 7;
57  
58  	private volatile Session[] data = null;
59  
60  	public SessionTableModel() {
61  		super();
62  		InternetCafeManager.getInstance().addObserver(this);
63  	}
64  
65  	public int getColumnCount() {
66  		return columnNames.length;
67  	}
68  
69  	public int getRowCount() {
70  		return data != null ? data.length : InternetCafeManager.getInstance()
71  				.getSession().length;
72  	}
73  
74  	public String getColumnName(int col) {
75  		return columnNames[col];
76  	}
77  
78  	public Object getValueAt(int row, int column) {
79  		if (row < 0 || column < 0)
80  			return null;
81  		Session[] session = data != null ? data : InternetCafeManager
82  				.getInstance().getSession();
83  		Object result = null;
84  		switch (column) {
85  		case SESSION_ID_COLUMN:
86  			result = session[row].getId();
87  			break;
88  		case SESSION_USER_NICKNAME_COLUMN:
89  			result = session[row].getUser().getNickname();
90  			break;
91  		case SESSION_WORKSTATION_NAME_COLUMN:
92  			result = session[row].getWorkstation().getName();
93  			break;
94  		case SESSION_DESCRIPTION_COLUMN:
95  			result = session[row].getDescription();
96  			break;
97  		case SESSION_START_TIME_COLUMN:
98  			result = session[row].getStartTime();
99  			break;
100 		case SESSION_END_TIME_COLUMN:
101 			result = session[row].getEndTime();
102 			break;
103 		case SESSION_SERVICE_COLUMN:
104 			result = session[row].getService().getName();
105 			break;
106 		case SESSION_FINISHED_COLUMN:
107 			result = session[row].isFinished();
108 			break;
109 		}
110 		return result;
111 	}
112 
113 	@SuppressWarnings("unchecked") //$NON-NLS-1$
114 	public Class getColumnClass(int column) {
115 		if (column < 0)
116 			return null;
117 		Class result = null;
118 		switch (column) {
119 		case SESSION_ID_COLUMN:
120 			result = Integer.class;
121 			break;
122 		case SESSION_USER_NICKNAME_COLUMN:
123 			result = String.class;
124 			break;
125 		case SESSION_WORKSTATION_NAME_COLUMN:
126 			result = String.class;
127 			break;
128 		case SESSION_DESCRIPTION_COLUMN:
129 			result = String.class;
130 			break;
131 		case SESSION_START_TIME_COLUMN:
132 			result = Date.class;
133 			break;
134 		case SESSION_END_TIME_COLUMN:
135 			result = Date.class;
136 			break;
137 		case SESSION_SERVICE_COLUMN:
138 			result = String.class;
139 			break;
140 		case SESSION_FINISHED_COLUMN:
141 			result = Boolean.class;
142 			break;
143 		}
144 		return result;
145 	}
146 
147 	/*
148 	 * Don't need to implement this method unless your table's editable.
149 	 */
150 	public boolean isCellEditable(int row, int column) {
151 		if (row < 0 || column < 0)
152 			return false;
153 		// Note that the data/cell address is constant,
154 		// no matter where the cell appears onscreen.
155 		boolean result = false;
156 		switch (column) {
157 		case SESSION_ID_COLUMN:
158 			result = false;
159 			break;
160 		case SESSION_USER_NICKNAME_COLUMN:
161 			result = false;
162 			break;
163 		case SESSION_WORKSTATION_NAME_COLUMN:
164 			result = false;
165 			break;
166 		case SESSION_DESCRIPTION_COLUMN:
167 			result = true;
168 			break;
169 		case SESSION_START_TIME_COLUMN:
170 			result = false;
171 			break;
172 		case SESSION_END_TIME_COLUMN:
173 			result = false;
174 			break;
175 		case SESSION_SERVICE_COLUMN:
176 			result = false;
177 			break;
178 		case SESSION_FINISHED_COLUMN:
179 			result = false;
180 			break;
181 		}
182 		return result;
183 	}
184 
185 	/*
186 	 * Don't need to implement this method unless your table's data can change.
187 	 */
188 	public void setValueAt(Object value, int row, int column) {
189 		if (row < 0 || column < 0)
190 			return;
191 		Session[] session = InternetCafeManager.getInstance().getSession();
192 		switch (column) {
193 		case SESSION_ID_COLUMN:
194 			break;
195 		case SESSION_USER_NICKNAME_COLUMN:
196 			break;
197 		case SESSION_WORKSTATION_NAME_COLUMN:
198 			break;
199 		case SESSION_DESCRIPTION_COLUMN:
200 			session[row].setDescription((String) value);
201 			break;
202 		case SESSION_START_TIME_COLUMN:
203 			break;
204 		case SESSION_END_TIME_COLUMN:
205 			break;
206 		case SESSION_SERVICE_COLUMN:
207 			break;
208 		case SESSION_FINISHED_COLUMN:
209 			break;
210 		}
211 		fireTableCellUpdated(row, column);
212 	}
213 
214 	/*
215 	 * (non-Javadoc)
216 	 * 
217 	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
218 	 */
219 	public void update(Observable observable, Object changedObject) {
220 		if (changedObject instanceof Session)
221 			this.fireTableDataChanged();
222 	}
223 
224 	/***
225 	 * @param sessions
226 	 */
227 	public void setData(Session[] sessions) {
228 		data = sessions;
229 	}
230 }