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  
21  package base.jdbs.ui;
22  
23  import java.util.Observable;
24  import java.util.Observer;
25  
26  import javax.swing.table.AbstractTableModel;
27  
28  import org.apache.log4j.Logger;
29  
30  import base.jdbs.network.NetworkManager;
31  import base.jdbs.network.PeerDiscoveryThread;
32  
33  @SuppressWarnings("serial")
34  public class PeerTableModel extends AbstractTableModel implements Observer {
35  
36  	private static final transient Logger logger = Logger
37  			.getLogger(PeerTableModel.class.getName());
38  
39  	private String[] columnNames = { "Id", "Name", "Location", "Space (Mb)",
40  			"Ping (ms)" };
41  
42  	public static final int PEER_ID_COLUMN = 0;
43  
44  	public static final int PEER_NAME_COLUMN = 1;
45  
46  	public static final int PEER_LOCATION_COLUMN = 2;
47  
48  	public static final int PEER_SPACE_COLUMN = 3;
49  
50  	public static final int PEER_PING_COLUMN = 4;
51  
52  	public PeerTableModel() {
53  		super();
54  		logger.info("PeerTableModel: registering "
55  				+ PeerDiscoveryThread.THREAD_NAME + " as Observable...");
56  		NetworkManager.getInstance().getPeerDiscoveryThread().addObserver(this);
57  	}
58  
59  	public int getColumnCount() {
60  		return columnNames.length;
61  	}
62  
63  	public int getRowCount() {
64  		return NetworkManager.getInstance().getPeerDiscoveryThread().getPeer().length;
65  	}
66  
67  	public String getColumnName(int column) {
68  		return columnNames[column];
69  	}
70  
71  	public Object getValueAt(int row, int column) {
72  		if (row < 0 || column < 0)
73  			return null;
74  		Object result = null;
75  		switch (column) {
76  		case PEER_ID_COLUMN:
77  			result = NetworkManager.getInstance().getPeerDiscoveryThread()
78  					.getPeer()[row].getPeerId();
79  			break;
80  		case PEER_NAME_COLUMN:
81  			result = NetworkManager.getInstance().getPeerDiscoveryThread()
82  					.getPeer()[row].getPeerName();
83  			break;
84  		case PEER_LOCATION_COLUMN:
85  			result = NetworkManager.getInstance().getPeerDiscoveryThread()
86  					.getPeer()[row].getLocation();
87  			break;
88  		case PEER_SPACE_COLUMN:
89  			result = NetworkManager.getInstance().getPeerDiscoveryThread()
90  					.getPeer()[row].getAvailableSpace();
91  			break;
92  		case PEER_PING_COLUMN:
93  			result = NetworkManager.getInstance().getPeerDiscoveryThread()
94  					.getPeer()[row].getPing();
95  			break;
96  
97  		}
98  		return result;
99  	}
100 
101 	@SuppressWarnings("unchecked")
102 	public Class getColumnClass(int column) {
103 		Class result = null;
104 		switch (column) {
105 		case PEER_ID_COLUMN:
106 			result = String.class;
107 			break;
108 		case PEER_NAME_COLUMN:
109 			result = String.class;
110 			break;
111 		case PEER_LOCATION_COLUMN:
112 			result = String.class;
113 			break;
114 		case PEER_SPACE_COLUMN:
115 			result = Long.class;
116 			break;
117 		case PEER_PING_COLUMN:
118 			result = Integer.class;
119 			break;
120 
121 		}
122 		return result;
123 	}
124 
125 	public void update(Observable arg0, Object arg1) {
126 		this.fireTableDataChanged();
127 	}
128 }