View Javadoc
1   /******************************************************************************
2    * PlayerTableModel.java - The data model behind the list of Players
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.5  2011/05/31 19:41:06  dick
10   * Eliminate a to do marker.
11   *
12   * Revision 1.4  2010/12/24 02:59:32  dick
13   * WinLoss becomes a light object instead of a String.
14   *
15   * Revision 1.3  2010/03/03 13:12:21  inim
16   * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
17   *
18   * Revision 1.2  2010/03/03 12:19:49  inim
19   * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010.
20   *
21   * Revision 1.1  2010/02/04 05:57:53  inim
22   * Mavenized project folder layout
23   *
24   * Revision 1.11  2010/01/23 07:48:49  dick
25   * Turn off youInvited when a player's state changed.
26   *
27   * Revision 1.10  2010/01/23 06:26:42  dick
28   * handle playerInvited().
29   *
30   * Revision 1.9  2009/03/12 15:46:20  dick
31   * Add the client type.
32   *
33   * Revision 1.8  2009/03/04 19:02:34  dick
34   * Add the win/loss column.
35   *
36   * Revision 1.7  2009/03/01 09:11:56  dick
37   * ROP filtering is functional.
38   *
39   * Revision 1.6  2009/02/24 08:04:53  dick
40   * Add the hostName as a column.  Javadoc.
41   *
42   * Revision 1.5  2009/02/14 15:46:36  dick
43   * BuckoFIBS is released under the GNU license.
44   *
45   * Revision 1.4  2009/02/05 05:58:18  dick
46   * Get rid of the toodoo markers.
47   *
48   * Revision 1.3  2009/02/01 21:29:23  dick
49   * Column 0 is now a Player, not a name.
50   *
51   * Revision 1.2  2009/01/28 22:32:13  dick
52   * Javadoc.
53   *
54   * Revision 1.1  2009/01/28 19:39:44  dick
55   * package com.buckosoft.fibs.gui.playerList becomes com.buckosoft.fibs.BuckoFIBS.gui.playerList.
56   *
57   * Revision 1.7  2009/01/28 08:31:33  dick
58   * Add removeAll to clean the list.
59   *
60   * Revision 1.6  2009/01/22 05:07:16  dick
61   * Rating and exp are class Double and Integer.
62   *
63   * Revision 1.5  2009/01/22 04:42:58  dick
64   * Move the "playing" test up top, it is the highest priority in the test.
65   * Add getColumnWidth().
66   *
67   * Revision 1.4  2009/01/09 07:19:39  dick
68   * Turn off debug.
69   *
70   * Revision 1.3  2008/12/11 10:01:40  dick
71   * Display the correct status.
72   *
73   * Revision 1.2  2008/12/09 19:41:34  dick
74   * getPlayer(String) searches the list for this player.
75   *
76   * Revision 1.1  2008/04/02 23:22:53  dick
77   * Move playerList to it's own package.
78   *
79   * Revision 1.3  2008/04/01 04:22:15  dick
80   * Handle player logging out.
81   * Fire event when player changes.
82   *
83   * Revision 1.2  2008/03/31 07:11:39  dick
84   * Start fleshing out the table.
85   *
86   * Revision 1.1  2008/03/29 08:57:43  dick
87   * BuckoFIBS skeleton checkin.
88   */
89  
90  /* 
91   * This program is free software: you can redistribute it and/or modify
92   * it under the terms of the GNU General Public License as published by
93   * the Free Software Foundation, either version 3 of the License, or
94   * (at your option) any later version.
95   *
96   * This program is distributed in the hope that it will be useful,
97   * but WITHOUT ANY WARRANTY; without even the implied warranty of
98   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
99   * GNU General Public License for more details.
100  *
101  * You should have received a copy of the GNU General Public License
102  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
103  *
104  * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
105  * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
106  * 
107  */
108 package com.buckosoft.fibs.BuckoFIBS.gui.playerList;
109 
110 import java.util.LinkedList;
111 import java.util.ListIterator;
112 
113 import javax.swing.table.AbstractTableModel;
114 
115 import com.buckosoft.fibs.domain.Player;
116 
117 /** The data model behind the list of Players
118  * @author Dick Balaska
119  * @since 2008/03/29
120  * @version $Revision$ <br> $Date$
121  * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/playerList/PlayerTableModel.java">cvs PlayerTableModel.java</a>
122  */
123 public class PlayerTableModel extends AbstractTableModel {
124 	private	final static boolean DEBUG = false;
125 	private static final long serialVersionUID = 1L;
126 
127 	private	PlayerColumns	_playerColumns = new PlayerColumns();
128 	private	LinkedList<Player>	players = new LinkedList<Player>();
129 	
130 
131 	/** Update or add this player to the table.
132 	 * @param player The player who's state has changed.
133 	 */
134 	public void playerChanged(Player player) {
135 		ListIterator<Player> liter = players.listIterator();
136 //		int row = -1;
137 		while (liter.hasNext()) {
138 //			row++;
139 			Player p = liter.next();
140 			if (p.getName().equals(player.getName())) {
141 				liter.set(player);
142 				//this.fireTableRowsUpdated(row, row);
143 				if (DEBUG)
144 					System.out.println("playerChanged: " + player.getName() + " - " + player.isReady());
145 				return;
146 			}
147 		}
148 		players.add(player);
149 		if (DEBUG)
150 			System.out.println("playerChanged: " + player.getName());
151 //		if (players.size()>1)
152 //			this.fireTableRowsInserted(players.size()-1, players.size()-1);
153 	}
154 
155 	/** Remove this player from the table.
156 	 * @param playerName The name of the player who left.
157 	 */
158 	public void playerGone(String playerName) {
159 		ListIterator<Player> liter = players.listIterator();
160 //		int row = -1;
161 		while (liter.hasNext()) {
162 //			row++;
163 			Player p = liter.next();
164 			if (p.getName().equals(playerName)) {
165 				liter.remove();
166 				//this.fireTableRowsDeleted(row, row);
167 			}
168 		}
169 	}
170 	/** You invited this player to play a match.
171 	 * You only get one open invitation, so remove old values.
172 	 * @param playerName The name of the player invited.
173 	 */ 
174 	public void playerInvited(String playerName) {
175 		for (Player p : players) {
176 			if (p.getName().equals(playerName))
177 				p.setInvited(true);
178 			else
179 				p.setInvited(false);
180 		}
181 	}
182 
183 	/** Clear the invited flag from any players.
184 	 * @return if anyone had the flag (triggers redraw).
185 	 */
186 	public boolean playerUninvited() {
187 		boolean ret = false;
188 		for (Player p : players) {
189 			if (p.isInvited())
190 				ret = true;
191 			p.setInvited(false);
192 		}
193 		return(ret);
194 	}
195 
196 	/** Remove all of the Players in the list.
197 	 */
198 	public void removeAll() {
199 		players.clear();
200 		this.fireTableDataChanged();		
201 	}
202 
203 	/** Return the Player in the table that matches this name.
204 	 * @param playerName The name to query
205 	 * @return The Player found or null if no match
206 	 */
207 	public Player getPlayer(String playerName) {
208 		for (Player p : players) {
209 			if (p.getName().equals(playerName))
210 				return(p);
211 		}
212 		return(null);
213 	}
214 
215 	/* (non-Javadoc)
216 	 * @see javax.swing.table.TableModel#getColumnClass(int)
217 	 */
218 	public Class<?> getColumnClass(int arg0) {
219 		try {
220 			if (arg0 == 0)
221 				return(Class.forName("com.buckosoft.fibs.domain.Player"));
222 			if (arg0 == 2)
223 				return Class.forName("java.lang.Double");
224 			if (arg0 == 3)
225 				return Class.forName("java.lang.Integer");
226 			if (arg0 == 4)
227 				return(Class.forName("com.buckosoft.fibs.domain.WinLoss"));				
228 			return Class.forName("java.lang.String");
229 		} catch (ClassNotFoundException e) {
230 			e.printStackTrace();
231 		}
232 		return(null);
233 	}
234 
235 	/* (non-Javadoc)
236 	 * @see javax.swing.table.TableModel#getColumnCount()
237 	 */
238 	public int getColumnCount() {
239 		return _playerColumns.getColumns().size();
240 	}
241 
242 	/* (non-Javadoc)
243 	 * @see javax.swing.table.TableModel#getColumnName(int)
244 	 */
245 	public String getColumnName(int arg0) {
246 		return _playerColumns.getColumns().get(arg0).getName();
247 	}
248 
249 	public int getColumnWidth(int arg0) {
250 		return _playerColumns.getColumns().get(arg0).getWidth();
251 	}
252 
253 	/* (non-Javadoc)
254 	 * @see javax.swing.table.TableModel#getRowCount()
255 	 */
256 	public int getRowCount() {
257 		return this.players.size();
258 	}
259 
260 	/* (non-Javadoc)
261 	 * @see javax.swing.table.TableModel#getValueAt(int, int)
262 	 */
263 	public Object getValueAt(int row, int col) {
264 		if (row >= players.size())
265 			return(null);
266 		Player p = players.get(row);
267 		switch (col) {
268 		case 0:
269 			return(p);
270 		case 1:
271 			if (!p.getOpponent().isEmpty())
272 				return("Playing");
273 			if (p.isReady())
274 				return("Ready");
275 			if (p.isAway())
276 				return("Away");
277 			return("Online");
278 		case 2:
279 			return(p.getRating());
280 		case 3:
281 			return(p.getExperience());
282 		case 4:
283 			return(p.getWinLoss());
284 		case 5:
285 			return(p.getClient());
286 		case 6:
287 			return(p.getHostName());
288 		}
289 		return null;
290 	}
291 
292 	/** All cells are non-editable.
293 	 * @return false
294 	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
295 	 */
296 	public boolean isCellEditable(int arg0, int arg1) {
297 		return false;
298 	}
299 
300 	/* (non-Javadoc)
301 	 * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
302 	 */
303 	public void setValueAt(Object arg0, int arg1, int arg2) {
304 	}
305 
306 }