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.net.InetAddress;
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.network.NetworkManager;
30 import base.network.Workstation;
31
32 @SuppressWarnings("serial")
33 public class ServerWorkstationTableModel extends AbstractTableModel implements
34 Observer {
35
36
37
38
39 private String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.name"), Messages.getString("common.location"), Messages.getString("common.description"),
40 Messages.getString("common.type"), Messages.getString("common.address"), Messages.getString("common.port"), Messages.getString("common.inuse"), Messages.getString("common.role") };
41
42 public static final int WORKSTATION_ID_COLUMN = 0;
43
44 public static final int WORKSTATION_NAME_COLUMN = 1;
45
46 public static final int WORKSTATION_LOCATION_COLUMN = 2;
47
48 public static final int WORKSTATION_DESCRIPTION_COLUMN = 3;
49
50 public static final int WORKSTATION_TYPE_COLUMN = 4;
51
52 public static final int WORKSTATION_ADDRESS_COLUMN = 5;
53
54 public static final int WORKSTATION_PORT_COLUMN = 6;
55
56 public static final int WORKSTATION_IN_USE_COLUMN = 7;
57
58 public static final int WORKSTATION_ROLE_COLUMN = 8;
59
60 public ServerWorkstationTableModel() {
61 super();
62 NetworkManager.getInstance().addObserver(this);
63 }
64
65 public int getColumnCount() {
66 return columnNames.length;
67 }
68
69 public int getRowCount() {
70 return NetworkManager.getInstance().getNetwork().getServer().length;
71 }
72
73 public String getColumnName(int column) {
74 return columnNames[column];
75 }
76
77 public Object getValueAt(int row, int column) {
78 if (row < 0 || column < 0)
79 return null;
80 Workstation[] server = NetworkManager.getInstance().getNetwork()
81 .getServer();
82 Object result = null;
83 switch (column) {
84 case WORKSTATION_ID_COLUMN:
85 result = server[row].getId();
86 break;
87 case WORKSTATION_NAME_COLUMN:
88 result = server[row].getName();
89 break;
90 case WORKSTATION_LOCATION_COLUMN:
91 result = server[row].getLocation();
92 break;
93 case WORKSTATION_DESCRIPTION_COLUMN:
94 result = server[row].getDescription();
95 break;
96 case WORKSTATION_TYPE_COLUMN:
97 result = server[row].getType();
98 break;
99 case WORKSTATION_ADDRESS_COLUMN:
100 result = server[row].getAddress().getHostAddress();
101 break;
102 case WORKSTATION_PORT_COLUMN:
103 result = server[row].getPort();
104 break;
105 case WORKSTATION_IN_USE_COLUMN:
106 result = server[row].isInUse();
107 break;
108 case WORKSTATION_ROLE_COLUMN:
109
110 break;
111 }
112 return result;
113 }
114
115 @SuppressWarnings("unchecked")
116 public Class getColumnClass(int column) {
117 Class result = null;
118 switch (column) {
119 case WORKSTATION_ID_COLUMN:
120 result = Integer.class;
121 break;
122 case WORKSTATION_NAME_COLUMN:
123 result = String.class;
124 break;
125 case WORKSTATION_LOCATION_COLUMN:
126 result = String.class;
127 break;
128 case WORKSTATION_DESCRIPTION_COLUMN:
129 result = String.class;
130 break;
131 case WORKSTATION_TYPE_COLUMN:
132 result = String.class;
133 break;
134 case WORKSTATION_ADDRESS_COLUMN:
135 result = InetAddress.class;
136 break;
137 case WORKSTATION_PORT_COLUMN:
138 result = Integer.class;
139 break;
140 case WORKSTATION_IN_USE_COLUMN:
141 result = Boolean.class;
142 break;
143 case WORKSTATION_ROLE_COLUMN:
144 result = String.class;
145 break;
146 }
147 return result;
148 }
149
150
151
152
153 public boolean isCellEditable(int row, int column) {
154 if (row < 0 || column < 0)
155 return false;
156
157
158 boolean result = false;
159 switch (column) {
160 case WORKSTATION_ID_COLUMN:
161 result = false;
162 break;
163 case WORKSTATION_NAME_COLUMN:
164 result = true;
165 break;
166 case WORKSTATION_LOCATION_COLUMN:
167 result = true;
168 break;
169 case WORKSTATION_DESCRIPTION_COLUMN:
170 result = true;
171 break;
172 case WORKSTATION_TYPE_COLUMN:
173 result = true;
174 break;
175 case WORKSTATION_ADDRESS_COLUMN:
176 result = false;
177 break;
178 case WORKSTATION_PORT_COLUMN:
179 result = true;
180 break;
181 case WORKSTATION_IN_USE_COLUMN:
182 result = false;
183 break;
184 case WORKSTATION_ROLE_COLUMN:
185 result = true;
186 break;
187 }
188 return result;
189 }
190
191
192
193
194 public void setValueAt(Object value, int row, int column) {
195 if (row < 0 || column < 0)
196 return;
197 Workstation[] server = NetworkManager.getInstance().getNetwork()
198 .getServer();
199 switch (column) {
200 case WORKSTATION_ID_COLUMN:
201 break;
202 case WORKSTATION_NAME_COLUMN:
203 server[row].setName(value.toString());
204 break;
205 case WORKSTATION_LOCATION_COLUMN:
206 server[row].setLocation(value.toString());
207 break;
208 case WORKSTATION_DESCRIPTION_COLUMN:
209 server[row].setDescription(value.toString());
210 break;
211 case WORKSTATION_TYPE_COLUMN:
212 server[row].setType(value.toString());
213 break;
214 case WORKSTATION_ADDRESS_COLUMN:
215 break;
216 case WORKSTATION_PORT_COLUMN:
217 server[row].setPort(Integer.parseInt(value.toString()));
218 break;
219 case WORKSTATION_IN_USE_COLUMN:
220 break;
221 case WORKSTATION_ROLE_COLUMN:
222 break;
223 }
224 fireTableCellUpdated(row, column);
225 }
226
227
228
229
230
231
232 public void update(Observable observable, Object changedObject) {
233 if (changedObject instanceof Workstation)
234 this.fireTableDataChanged();
235 }
236
237 }