View Javadoc
1   /******************************************************************************
2    * GroupManager.java - Manage the Groups of Players
3    * $Id$
4    * 
5    * BuckoFIBS - Backgammon by BuckoSoft
6    * Copyright© 2010 - Dick Balaska - BuckoSoft, Corp.
7    * 
8    * $Log$
9    * Revision 1.6  2011/01/04 17:38:05  dick
10   * Flesh out the filter function.
11   *
12   * Revision 1.5  2011/01/01 20:47:07  dick
13   * Add edit group.  Add getGroupsSelectedString for the groups text box.
14   *
15   * Revision 1.4  2011/01/01 06:16:44  dick
16   * Filtering of players is functional.
17   *
18   * Revision 1.3  2011/01/01 02:33:11  dick
19   * Database for GroupOfPlayers is fully functional.
20   *
21   * Revision 1.2  2011/01/01 00:19:29  dick
22   * Lotso work.
23   *
24   * Revision 1.1  2010/12/31 05:28:38  dick
25   * Manage the Groups of Players
26   *
27   */
28  
29  /* 
30   * This program is free software: you can redistribute it and/or modify
31   * it under the terms of the GNU General Public License as published by
32   * the Free Software Foundation, either version 3 of the License, or
33   * (at your option) any later version.
34   *
35   * This program is distributed in the hope that it will be useful,
36   * but WITHOUT ANY WARRANTY; without even the implied warranty of
37   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38   * GNU General Public License for more details.
39   *
40   * You should have received a copy of the GNU General Public License
41   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
42   *
43   * The Original Code is BuckoFIBS, <http://www.buckosoft.com/BuckoFIBS/>.
44   * The Initial Developer of the Original Code is Dick Balaska and BuckoSoft, Corp.
45   * 
46   */
47  package com.buckosoft.fibs.BuckoFIBS;
48  
49  import java.awt.Color;
50  import java.util.LinkedList;
51  import java.util.List;
52  import org.slf4j.Logger;
53  import org.slf4j.LoggerFactory;
54  import com.buckosoft.fibs.BuckoFIBS.db.Database;
55  import com.buckosoft.fibs.BuckoFIBS.gui.playerList.PlayerListPane;
56  import com.buckosoft.fibs.BuckoFIBS.gui.playerList.group.GroupPopupSubmenu;
57  import com.buckosoft.fibs.BuckoFIBS.gui.playerList.group.GroupSelectPane;
58  import com.buckosoft.fibs.BuckoFIBS.gui.playerList.group.GroupPopupSubmenu.PopupGroup;
59  import com.buckosoft.fibs.domain.GroupOfPlayers;
60  import com.buckosoft.fibs.domain.Player;
61  import com.buckosoft.fibs.domain.PlayerGroup;
62  
63  /** Manage the GroupOfPlayers.
64   * @author Dick Balaska
65   * @since 2010/12/30
66   * @version $Revision$ <br> $Date$
67   * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/main/java/com/buckosoft/fibs/BuckoFIBS/GroupManager.java">cvs GroupManager.java</a>
68   */
69  public class GroupManager {
70  	private	final static boolean DEBUG = true;
71      private Logger logger = LoggerFactory.getLogger(getClass());
72  	private List<GroupOfPlayers> groupList = new LinkedList<GroupOfPlayers>();
73  	private GroupSelectPane groupSelectPane;
74  	private	PlayerListPane	playerListPane;
75  	private	boolean	allGroupSelected;			// convienence
76  	
77  	private	Database	database;
78  	
79  	public void initDefaultGroups() {
80  		groupList = new LinkedList<GroupOfPlayers>();
81  		GroupOfPlayers gop;
82  		gop = new GroupOfPlayers("All", Color.black);
83  		gop.setActive(true);
84  		allGroupSelected = true;
85  		groupList.add(gop);
86  		groupList.add(new GroupOfPlayers("Friends", Color.blue));
87  		gop = new GroupOfPlayers("Enemies", Color.red);
88  		gop.setIgnore(true);
89  		groupList.add(gop);
90  	}
91  	
92  	public GroupManager() {
93  		groupList.add(new GroupOfPlayers("All", Color.black));
94  	}
95  
96  	public void setGroupSelectPane(GroupSelectPane groupSelectPane) {
97  		this.groupSelectPane = groupSelectPane;
98  	}
99  
100 	public void setPlayerListPane(PlayerListPane playerListPane) {
101 		this.playerListPane = playerListPane;
102 	}
103 
104 	/** Set the reference to the database.
105 	 * @param database the database to set
106 	 */
107 	public void setDatabase(Database database) {
108 		this.database = database;
109 		groupList = null;
110 		try {
111 			groupList = this.database.getGroupsOfPlayers();
112 		} catch (Exception e) {}
113 		if (groupList == null || groupList.size() == 0)
114 			initDefaultGroups();
115 		this.allGroupSelected = groupList.get(0).isActive();
116 	}
117 	
118 	public void save() {
119 		if (DEBUG)
120 			logger.info("save");
121 		this.database.store(groupList);
122 	}
123 
124 	public void populatePane() {
125 		for (GroupOfPlayers gop : groupList) {
126 			groupSelectPane.addGroup(gop);
127 		}
128 		groupSelectPane.populateFinish();
129 	}
130 	
131 	/** Tag the active status for this GroupOfPlayers
132 	 * @param id
133 	 * @param groupName
134 	 * @param active
135 	 */
136 	public void updateGroupOfPlayers(int id, String groupName, boolean active) {
137 		if (DEBUG)
138 			logger.info("updateGroupOfPlayers: id=" + id + " name=" + groupName + " active=" + active);
139 		if (id == 0) {
140 			GroupOfPlayers gop = new GroupOfPlayers(groupName, Color.black);
141 			gop.setActive(active);
142 			groupList.add(gop);
143 		} else {
144 			for (GroupOfPlayers gop : groupList) {
145 				if (gop.getId() == id) {
146 					if (active != gop.isActive())
147 						gop.setDirty(true);
148 					gop.setActive(active);
149 				}
150 			}
151 		}
152 		this.allGroupSelected = groupList.get(0).isActive();
153 		this.playerListPane.groupPaneChanged();
154 	}
155 
156 	/** Get the GroupOfPlayers with this name
157 	 * @param groupName The name to search for
158 	 * @return The GroupOfPlayers or null if not found.
159 	 */
160 	public GroupOfPlayers getGroupOfPlayers(String groupName) {
161 		for (GroupOfPlayers gop : groupList) {
162 			if (gop.getGroupName().equals(groupName))
163 				return(gop);
164 		}
165 		return(null);
166 	}
167 
168 	public GroupOfPlayers getGroupOfPlayers(int groupId) {
169 		for (GroupOfPlayers gop : groupList) {
170 			if (gop.getId() == groupId)
171 				return(gop);
172 		}
173 		return(null);
174 		
175 	}
176 	/** The user has clicked OK in the add new group dialog.
177 	 * @param groupOfPlayers The new GroupOfPlayers created by the user.
178 	 */
179 	public void addNewGroupFromUser(GroupOfPlayers groupOfPlayers) {
180 		groupList.add(groupOfPlayers);
181 		save();
182 		groupSelectPane.repopulate();
183 		populatePane();
184 		this.playerListPane.groupPaneChanged();
185 	}
186 	
187 	public void editGroupFromUser(GroupOfPlayers groupOfPlayers) {
188 		GroupOfPlayers gop = getGroupOfPlayers(groupOfPlayers.getId());
189 		if (gop == null)
190 			return;
191 		gop.setDirty(true);
192 		save();
193 		groupSelectPane.repopulate();
194 		populatePane();
195 		this.playerListPane.groupPaneChanged();
196 	}
197 
198 	/** Handle an event from the popup menu
199 	 * @param groupName
200 	 * @param playerName
201 	 */
202 	public void popupMenuItemSelected(String groupName, String playerName) {
203 		Player p = this.database.getPlayer(playerName);
204 		if (p == null) {
205 			p = this.playerListPane.getPlayer(playerName);
206 			if (p == null)
207 				return;					// We can't find the guy we clicked on??
208 			this.database.store(p);		// Write him out so we get a playerId
209 			//System.out.println("New playerid =" + p.getId());
210 			//p = this.database.getPlayer(playerName);
211 			this.playerListPane.playerChanged(p);
212 			if (DEBUG)
213 				logger.info("popupMenuItemSelected: name = '" + p.getName() + "' Now playerid =" + p.getId());
214 		}
215 		int pi = p.getId();
216 		GroupOfPlayers gop = getGroupOfPlayers(groupName);
217 		if (this.isInGroup(groupName, pi)) {
218 			for (PlayerGroup pg : gop.getPlayerGroups()) {
219 				if (pg.getPlayerId() == pi) {
220 					pg.setTagForDelete();
221 					gop.setDirty(true);
222 					this.save();
223 					return;
224 				}
225 			}
226 		} else {
227 			gop.addPlayer(pi);
228 			gop.setDirty(true);
229 			this.save();
230 		}
231 	}
232 
233 	/** Is this playerId in this group?
234 	 * @param groupName The name of the group to check
235 	 * @param playerId The id of the player in question
236 	 * @return Success
237 	 */
238 	public boolean isInGroup(String groupName, int playerId) {
239 		GroupOfPlayers gop = getGroupOfPlayers(groupName);
240 		LinkedList<Integer> ll = gop.getPlayers();
241 		for (int i : ll) {
242 			if (i == playerId)
243 				return(true);
244 		}
245 		return(false);
246 	}
247 
248 	public LinkedList<PopupGroup> getGroupsForPlayer(GroupPopupSubmenu groupPopupSubmenu, int playerId) {
249 		LinkedList<PopupGroup> ll = new LinkedList<PopupGroup>();
250 		for (GroupOfPlayers gop : groupList) {
251 			PopupGroup pg = groupPopupSubmenu.new PopupGroup();
252 			pg.name = gop.getGroupName();
253 			for (Integer i : gop.getPlayers()) {
254 				if (playerId == i) {
255 					pg.selected = true;
256 					break;
257 				}
258 			}
259 			ll.add(pg);
260 		}
261 		return(ll);
262 	}
263 	/** Should this player be filtered?
264 	 * @param player The Player to test
265 	 * @return true = ok to use this player
266 	 */
267 	public boolean filter(Player player) {
268 //		if (this.allGroupSelected)
269 //			return(true);
270 		if (player.getId() == 0) {
271 			//System.out.println("filter: pid=0 " + player.getName());
272 			if (this.allGroupSelected)
273 				return(true);
274 			else
275 				return(false);
276 		}
277 		int playerId = player.getId();
278 		if (this.allGroupSelected) {
279 			for (GroupOfPlayers gop : groupList) {
280 				if (gop.isActive() && gop.isIgnore()) {
281 					for (Integer i : gop.getPlayers()) {
282 						if (playerId == i) {
283 							//System.out.println("filter: " + i + " " + player.getName() + " - g:" + gop.getGroupName());
284 							return(false);
285 						}
286 					}
287 				}
288 			}
289 			return(true);
290 		} else {
291 			for (GroupOfPlayers gop : groupList) {
292 				if (gop.isActive()) {
293 					for (Integer i : gop.getPlayers()) {
294 						if (playerId == i) {
295 							return(!gop.isIgnore());
296 						}
297 					}
298 				}
299 			}
300 		}
301 		return(false);
302 	}
303 	
304 	/** Get the String that is displayed at the top of the PlayerListPane
305 	 * @return A nice String
306 	 */
307 	public String getGroupsSelectedString() {
308 		StringBuffer sb = new StringBuffer();
309 		if (this.allGroupSelected)
310 			sb.append("All");
311 		for (GroupOfPlayers gop : groupList) {
312 			if (gop.isIgnore()) {
313 				if (gop.isActive()) {
314 					if (sb.length() != 0)
315 						sb.append("; ");
316 					sb.append("!");
317 					sb.append(gop.getGroupName());
318 				}
319 			} else {
320 				if (this.allGroupSelected || !gop.isActive())
321 					continue;
322 				if (sb.length() != 0)
323 					sb.append("; ");
324 				sb.append(gop.getGroupName());
325 			}
326 		}
327 		return(sb.toString());
328 	}
329 }