View Javadoc
1   /******************************************************************************
2    * InviterTableModel.java - The Inviter's JList Table Model
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.3  2010/03/03 13:12:21  inim
10   * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8
11   *
12   * Revision 1.2  2010/03/03 12:19:49  inim
13   * 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.
14   *
15   * Revision 1.1  2010/02/04 05:57:53  inim
16   * Mavenized project folder layout
17   *
18   * Revision 1.5  2009/02/24 08:06:48  dick
19   * Add the hostName as a column.
20   *
21   * Revision 1.4  2009/02/14 15:46:36  dick
22   * BuckoFIBS is released under the GNU license.
23   *
24   * Revision 1.3  2009/02/11 09:07:30  dick
25   * playerChanged(Player) to update the changed player in the invter table.
26   * This is in case we got invited by an unknown player, and then figured him out.
27   *
28   * Revision 1.2  2009/02/02 08:39:25  dick
29   * Use a string for the match length, so we can say "" or "unlimited".
30   *
31   * Revision 1.1  2009/01/28 19:39:15  dick
32   * package com.buckosoft.fibs.gui.inviterList becomes com.buckosoft.fibs.BuckoFIBS.gui.inviterList.
33   *
34   * Revision 1.6  2009/01/28 08:32:07  dick
35   * Add removeAll to clean the list.
36   * Javadoc.
37   *
38   * Revision 1.5  2009/01/27 19:16:43  dick
39   * playerName gets a custom renderer so we can display warnings.
40   *
41   * Revision 1.4  2009/01/27 06:56:23  dick
42   * playerchanged() and playerGone() become invited() and uninvited().
43   *
44   * Revision 1.3  2009/01/09 07:19:28  dick
45   * Turn off debug.
46   *
47   * Revision 1.2  2008/12/10 18:07:20  dick
48   * Inviter contains a Player, not extends it.
49   *
50   * Revision 1.1  2008/12/09 19:40:15  dick
51   * The Inviter's JList Table Model.
52   */
53  
54  /* 
55   * This program is free software: you can redistribute it and/or modify
56   * it under the terms of the GNU General Public License as published by
57   * the Free Software Foundation, either version 3 of the License, or
58   * (at your option) any later version.
59   *
60   * This program is distributed in the hope that it will be useful,
61   * but WITHOUT ANY WARRANTY; without even the implied warranty of
62   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
63   * GNU General Public License for more details.
64   *
65   * You should have received a copy of the GNU General Public License
66   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
67   *
68   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
69   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
70   * 
71   */
72  package com.buckosoft.fibs.BuckoFIBS.gui.inviterList;
73  
74  import java.util.LinkedList;
75  import java.util.ListIterator;
76  
77  import javax.swing.table.AbstractTableModel;
78  
79  import com.buckosoft.fibs.BuckoFIBS.gui.playerList.Column;
80  import com.buckosoft.fibs.domain.Player;
81  
82  /** The Inviter's JList Table Model
83   * @author Dick Balaska
84   * @since 2008/12/08
85   * @version $Revision$ <br> $Date$
86   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/inviterList/InviterListPane.java">cvs InviterListPane.java</a>
87   */
88  public class InviterTableModel extends AbstractTableModel {
89  	private	final static boolean DEBUG = false;
90  	private static final long serialVersionUID = 1L;
91  
92  	public class Inviter {
93  		Player p;
94  		Inviter(Player p) {
95  			super();
96  			this.p = p;
97  		}
98  		String matchLength;
99  	}
100 
101 	private class InviterColumns {
102 		private	LinkedList<Column>	columns = new LinkedList<Column>();
103 		
104 		InviterColumns() {
105 			columns.add(new Column("Name", 20));
106 			columns.add(new Column("Match", 10));
107 			columns.add(new Column("Rating", 10));
108 			columns.add(new Column("Exp", 9));
109 			columns.add(new Column("Host", 300));
110 		}
111 
112 		/**
113 		 * @return the columns
114 		 */
115 		public LinkedList<Column> getColumns() {
116 			return columns;
117 		}
118 
119 	}
120 
121 	private	InviterColumns	_inviterColumns = new InviterColumns();
122 	private	LinkedList<Inviter>	inviters = new LinkedList<Inviter>();
123 	
124 	/** Method to add a Player to the inviter table
125 	 * @param player The Player that invited us
126 	 * @param matchLength How many games in the match
127 	 */
128 	public void invited(Player player, String matchLength) {
129 		ListIterator<Inviter> liter = inviters.listIterator();
130 		int row = -1;
131 		while (liter.hasNext()) {
132 			row++;
133 			Inviter p = liter.next();
134 			if (p.p.getName().equals(player.getName())) {
135 				Inviter pi = new Inviter(player);
136 				pi.matchLength = matchLength;
137 				liter.set(pi);
138 				this.fireTableRowsUpdated(row, row);
139 				if (DEBUG)
140 					System.out.println("inviterChanged: " + player.getName());
141 				return;
142 			}
143 		}
144 		Inviter pi = new Inviter(player);
145 		pi.matchLength = matchLength;	
146 		inviters.add(pi);
147 		if (DEBUG)
148 			System.out.println("inviterChanged: " + player.getName());
149 		this.fireTableRowsInserted(inviters.size()-1, inviters.size()-1);
150 	}
151 
152 	/** Method to remove a player from the inviter table.
153 	 * This is called i.e. when a player starts a match with someone else, or logs out.
154 	 * @param playerName The name of the player to remove
155 	 */
156 	public void uninvited(String playerName) {
157 		ListIterator<Inviter> liter = inviters.listIterator();
158 		int row = -1;
159 		while (liter.hasNext()) {
160 			row++;
161 			Inviter p = liter.next();
162 			if (p.p.getName().equals(playerName)) {
163 				liter.remove();
164 				this.fireTableRowsDeleted(row, row);
165 			}
166 		}
167 	}
168 	
169 	/** Remove all of the inviters in the list
170 	 */
171 	public void removeAll() {
172 		inviters.clear();
173 		this.fireTableDataChanged();		
174 	}
175 
176 	public void playerChanged(Player p) {
177 		Inviter i = getInviter(p.getName());
178 		if (i == null)
179 			return;
180 		i.p = p;
181 		this.fireTableDataChanged();
182 	}
183 	
184 	/** Return the Inviter that matches this playerName
185 	 * @param playerName The name to match
186 	 * @return the Inviter or null if not found
187 	 */
188 	public Inviter getInviter(String playerName) {
189 		for (Inviter p : inviters) {
190 			if (p.p.getName().equals(playerName))
191 				return(p);
192 		}
193 		return(null);
194 	}
195 
196 	/** Add a warning to this inviter. <br>
197 	 * Right now, we handle "Don't join if you want to resume a saved match with x".<br>
198 	 * Eventually, we will add MissManners and RepBot warnings.  
199 	 * @param playerName The name of the questionable player
200 	 * @param warning The warning message to display as a tooltip.
201 	 */
202 	public void setWarning(String playerName, String warning) {
203 		Inviter inv = getInviter(playerName);
204 		if (inv != null) {
205 			inv.p.setBfStatus(warning);
206 			this.fireTableDataChanged();
207 		}
208 	}
209 
210 	/* (non-Javadoc)
211 	 * @see javax.swing.table.TableModel#getColumnClass(int)
212 	 */
213 	public Class<?> getColumnClass(int arg0) {
214 		try {
215 			if (arg0 == 0)
216 				return(Class.forName("com.buckosoft.fibs.domain.Player"));
217 			return Class.forName("java.lang.String");
218 		} catch (ClassNotFoundException e) {
219 			System.out.println("Exception ignored...");
220 			e.printStackTrace();
221 		}
222 		return(null);
223 	}
224 
225 	/* (non-Javadoc)
226 	 * @see javax.swing.table.TableModel#getColumnCount()
227 	 */
228 	public int getColumnCount() {
229 		return _inviterColumns.getColumns().size();
230 	}
231 
232 	/* (non-Javadoc)
233 	 * @see javax.swing.table.TableModel#getColumnName(int)
234 	 */
235 	public String getColumnName(int arg0) {
236 		return _inviterColumns.getColumns().get(arg0).getName();
237 	}
238 
239 	/* (non-Javadoc)
240 	 * @see javax.swing.table.TableModel#getRowCount()
241 	 */
242 	public int getRowCount() {
243 		return this.inviters.size();
244 	}
245 
246 	/* (non-Javadoc)
247 	 * @see javax.swing.table.TableModel#getValueAt(int, int)
248 	 */
249 	public Object getValueAt(int arg0, int arg1) {
250 		Inviter p = inviters.get(arg0);
251 		switch (arg1) {
252 		case 0:
253 			return(p.p);
254 		
255 		case 1:
256 			return(p.matchLength);
257 		case 2:
258 			return(p.p.getRating());
259 		case 3:
260 			return(p.p.getExperience());
261 		case 4:
262 			return(p.p.getHostName());
263 		}
264 		return null;
265 	}
266 
267 	/** All cells are not editable
268 	 * @see javax.swing.table.TableModel#isCellEditable(int, int)
269 	 */
270 	public boolean isCellEditable(int arg0, int arg1) {
271 		return false;
272 	}
273 
274 
275 	/** We don't set no steenky values
276 	 */
277 	@Override
278 	public void setValueAt(Object arg0, int arg1, int arg2) {
279 	}
280 
281 }