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.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") //$NON-NLS-1$
33  public class ClientWorkstationTableModel extends AbstractTableModel implements
34  		Observer {
35  	/*
36  	 * private String name; private String description; private WorkstationType
37  	 * type; private InetAddress address; private boolean inUse = false;
38  	 */
39  	private String[] columnNames = { Messages.getString("common.id"), Messages.getString("common.name"), Messages.getString("common.location"), Messages.getString("common.description"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
40  			Messages.getString("common.type"), Messages.getString("common.address"), Messages.getString("common.port"), Messages.getString("common.inuse") }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
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 ClientWorkstationTableModel() {
59  		super();
60  		NetworkManager.getInstance().addObserver(this);
61  	}
62  
63  	public int getColumnCount() {
64  		return columnNames.length;
65  	}
66  
67  	public int getRowCount() {
68  		return NetworkManager.getInstance().getNetwork().getClient().length;
69  	}
70  
71  	public String getColumnName(int column) {
72  		return columnNames[column];
73  	}
74  
75  	public Object getValueAt(int row, int column) {
76  		if (row < 0 || column < 0)
77  			return null;
78  		Workstation[] client = NetworkManager.getInstance().getNetwork()
79  				.getClient();
80  		Object result = null;
81  		switch (column) {
82  		case WORKSTATION_ID_COLUMN:
83  			result = client[row].getId();
84  			break;
85  		case WORKSTATION_NAME_COLUMN:
86  			result = client[row].getName();
87  			break;
88  		case WORKSTATION_LOCATION_COLUMN:
89  			result = client[row].getLocation();
90  			break;
91  		case WORKSTATION_DESCRIPTION_COLUMN:
92  			result = client[row].getDescription();
93  			break;
94  		case WORKSTATION_TYPE_COLUMN:
95  			result = client[row].getType();
96  			break;
97  		case WORKSTATION_ADDRESS_COLUMN:
98  			result = client[row].getAddress().getHostAddress();
99  			break;
100 		case WORKSTATION_PORT_COLUMN:
101 			result = client[row].getPort();
102 			break;
103 		case WORKSTATION_IN_USE_COLUMN:
104 			result = client[row].isInUse();
105 			break;
106 		}
107 		return result;
108 	}
109 
110 	@SuppressWarnings("unchecked") //$NON-NLS-1$
111 	public Class getColumnClass(int column) {
112 		Class result = null;
113 		switch (column) {
114 		case WORKSTATION_ID_COLUMN:
115 			result = Integer.class;
116 			break;
117 		case WORKSTATION_NAME_COLUMN:
118 			result = String.class;
119 			break;
120 		case WORKSTATION_LOCATION_COLUMN:
121 			result = String.class;
122 			break;
123 		case WORKSTATION_DESCRIPTION_COLUMN:
124 			result = String.class;
125 			break;
126 		case WORKSTATION_TYPE_COLUMN:
127 			result = String.class;
128 			break;
129 		case WORKSTATION_ADDRESS_COLUMN:
130 			result = InetAddress.class;
131 			break;
132 		case WORKSTATION_PORT_COLUMN:
133 			result = Integer.class;
134 			break;
135 		case WORKSTATION_IN_USE_COLUMN:
136 			result = Boolean.class;
137 			break;
138 		}
139 		return result;
140 	}
141 
142 	/*
143 	 * Don't need to implement this method unless your table's editable.
144 	 */
145 	public boolean isCellEditable(int row, int column) {
146 		if (row < 0 || column < 0)
147 			return false;
148 		// Note that the data/cell address is constant,
149 		// no matter where the cell appears onscreen.
150 		boolean result = false;
151 		switch (column) {
152 		case WORKSTATION_ID_COLUMN:
153 			result = false;
154 			break;
155 		case WORKSTATION_NAME_COLUMN:
156 			result = true;
157 			break;
158 		case WORKSTATION_LOCATION_COLUMN:
159 			result = true;
160 			break;
161 		case WORKSTATION_DESCRIPTION_COLUMN:
162 			result = true;
163 			break;
164 		case WORKSTATION_TYPE_COLUMN:
165 			result = true;
166 			break;
167 		case WORKSTATION_ADDRESS_COLUMN:
168 			result = false;
169 			break;
170 		case WORKSTATION_PORT_COLUMN:
171 			result = true;
172 			break;
173 		case WORKSTATION_IN_USE_COLUMN:
174 			result = false;
175 			break;
176 		}
177 		return result;
178 	}
179 
180 	/*
181 	 * Don't need to implement this method unless your table's data can change.
182 	 */
183 	public void setValueAt(Object value, int row, int column) {
184 		if (row < 0 || column < 0)
185 			return;
186 		Workstation[] client = NetworkManager.getInstance().getNetwork()
187 				.getClient();
188 		switch (column) {
189 		case WORKSTATION_ID_COLUMN:
190 			break;
191 		case WORKSTATION_NAME_COLUMN:
192 			client[row].setName(value.toString());
193 			break;
194 		case WORKSTATION_LOCATION_COLUMN:
195 			client[row].setLocation(value.toString());
196 			break;
197 		case WORKSTATION_DESCRIPTION_COLUMN:
198 			client[row].setDescription(value.toString());
199 			break;
200 		case WORKSTATION_TYPE_COLUMN:
201 			client[row].setType(value.toString());
202 			break;
203 		case WORKSTATION_ADDRESS_COLUMN:
204 			break;
205 		case WORKSTATION_PORT_COLUMN:
206 			client[row].setPort(Integer.parseInt(value.toString()));
207 			break;
208 		case WORKSTATION_IN_USE_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 Workstation)
221 			this.fireTableDataChanged();
222 	}
223 
224 }