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.Observable;
23  import java.util.Observer;
24  
25  import javax.swing.table.AbstractTableModel;
26  
27  import ui.Messages;
28  import base.InternetCafeManager;
29  import base.service.Service;
30  import base.service.ServiceRateType;
31  
32  @SuppressWarnings("serial") //$NON-NLS-1$
33  public class ServiceTableModel extends AbstractTableModel implements Observer {
34  
35  	String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.name"), Messages.getString("common.description"), Messages.getString("common.ratetype"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
36  			Messages.getString("common.baserate"), Messages.getString("common.mindurationinminute"), Messages.getString("common.maxdurationinminute") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
37  
38  	public static final int SERVICE_ID_COLUMN = 0;
39  
40  	public static final int SERVICE_NAME_COLUMN = 1;
41  
42  	public static final int SERVICE_DESCRIPTION_COLUMN = 2;
43  
44  	public static final int SERVICE_RATE_TYPE_COLUMN = 3;
45  
46  	public static final int SERVICE_BASE_RATE_COLUMN = 4;
47  
48  	public static final int SERVICE_MIN_DURATION_IN_MINUTE_COLUMN = 5;
49  
50  	public static final int SERVICE_MAX_DURATION_IN_MINUTE_COLUMN = 6;
51  
52  	public ServiceTableModel() {
53  		super();
54  		InternetCafeManager.getInstance().addObserver(this);
55  	}
56  
57  	public int getColumnCount() {
58  		return columnNames.length;
59  	}
60  
61  	public int getRowCount() {
62  		return InternetCafeManager.getInstance().getService().length;
63  	}
64  
65  	public String getColumnName(int col) {
66  		return columnNames[col];
67  	}
68  
69  	public Object getValueAt(int row, int column) {
70  		if (row < 0 || column < 0)
71  			return null;
72  		Service[] service = InternetCafeManager.getInstance().getService();
73  		Object result = null;
74  		switch (column) {
75  		case SERVICE_ID_COLUMN:
76  			result = service[row].getId();
77  			break;
78  		case SERVICE_NAME_COLUMN:
79  			result = service[row].getName();
80  			break;
81  		case SERVICE_DESCRIPTION_COLUMN:
82  			result = service[row].getDescription();
83  			break;
84  		case SERVICE_RATE_TYPE_COLUMN:
85  			result = service[row].getRateType();
86  			break;
87  		case SERVICE_BASE_RATE_COLUMN:
88  			result = service[row].getBaseRate();
89  			break;
90  		case SERVICE_MIN_DURATION_IN_MINUTE_COLUMN:
91  			result = service[row].getMinDurationInMinute();
92  			break;
93  		case SERVICE_MAX_DURATION_IN_MINUTE_COLUMN:
94  			result = service[row].getMaxDurationInMinute();
95  			break;
96  		}
97  		return result;
98  	}
99  
100 	@SuppressWarnings("unchecked") //$NON-NLS-1$
101 	public Class getColumnClass(int column) {
102 		if (column < 0)
103 			return null;
104 		Class result = null;
105 		switch (column) {
106 		case SERVICE_ID_COLUMN:
107 			result = Integer.class;
108 			break;
109 		case SERVICE_NAME_COLUMN:
110 			result = String.class;
111 			break;
112 		case SERVICE_DESCRIPTION_COLUMN:
113 			result = String.class;
114 			break;
115 		case SERVICE_RATE_TYPE_COLUMN:
116 			result = ServiceRateType.class;
117 			break;
118 		case SERVICE_BASE_RATE_COLUMN:// Should be Double but with string will
119 			// be displayed the double's dot
120 			// notation
121 			result = String.class;
122 			break;
123 		case SERVICE_MIN_DURATION_IN_MINUTE_COLUMN:
124 			result = Integer.class;
125 			break;
126 		case SERVICE_MAX_DURATION_IN_MINUTE_COLUMN:
127 			result = Integer.class;
128 			break;
129 		}
130 		return result;
131 	}
132 
133 	/*
134 	 * Don't need to implement this method unless your table's editable.
135 	 */
136 	public boolean isCellEditable(int row, int column) {
137 		if (row < 0 || column < 0)
138 			return false;
139 		// Note that the data/cell address is constant,
140 		// no matter where the cell appears onscreen.
141 		boolean result = false;
142 		switch (column) {
143 		case SERVICE_ID_COLUMN:
144 			result = false;
145 			break;
146 		case SERVICE_NAME_COLUMN:
147 			result = true;
148 			break;
149 		case SERVICE_DESCRIPTION_COLUMN:
150 			result = true;
151 			break;
152 		case SERVICE_RATE_TYPE_COLUMN:
153 			result = true;
154 			break;
155 		case SERVICE_BASE_RATE_COLUMN:
156 			result = false;
157 			break;
158 		case SERVICE_MIN_DURATION_IN_MINUTE_COLUMN:
159 			result = true;
160 			break;
161 		case SERVICE_MAX_DURATION_IN_MINUTE_COLUMN:
162 			result = true;
163 			break;
164 		}
165 		return result;
166 	}
167 
168 	/*
169 	 * Don't need to implement this method unless your table's data can change.
170 	 */
171 	public void setValueAt(Object value, int row, int column) {
172 		if (row < 0 || column < 0)
173 			return;
174 		@SuppressWarnings("unused") //$NON-NLS-1$
175 		Service[] service = InternetCafeManager.getInstance().getService();
176 		switch (column) {
177 		case SERVICE_ID_COLUMN:
178 			break;
179 		case SERVICE_NAME_COLUMN:
180 			service[row].setName((String) value);
181 			break;
182 		case SERVICE_DESCRIPTION_COLUMN:
183 			service[row].setDescription((String) value);
184 			break;
185 		case SERVICE_RATE_TYPE_COLUMN:
186 			service[row].setRateType((String) value);
187 			break;
188 		case SERVICE_BASE_RATE_COLUMN:
189 			service[row].setBaseRate(Double.parseDouble(value.toString()));
190 			break;
191 		case SERVICE_MIN_DURATION_IN_MINUTE_COLUMN:
192 			service[row].setMinDurationInMinute(Integer.parseInt(value
193 					.toString()));
194 			break;
195 		case SERVICE_MAX_DURATION_IN_MINUTE_COLUMN:
196 			service[row].setMaxDurationInMinute(Integer.parseInt(value
197 					.toString()));
198 			break;
199 		}
200 		fireTableCellUpdated(row, column);
201 	}
202 
203 	/*
204 	 * (non-Javadoc)
205 	 * 
206 	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
207 	 */
208 	public void update(Observable observable, Object changedObject) {
209 		if (changedObject instanceof Service)
210 			this.fireTableDataChanged();
211 	}
212 }