Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GroupManager |
|
| 4.352941176470588;4.353 |
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 | 0 | private Logger logger = LoggerFactory.getLogger(getClass()); |
72 | 0 | 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 | 0 | groupList = new LinkedList<GroupOfPlayers>(); |
81 | GroupOfPlayers gop; | |
82 | 0 | gop = new GroupOfPlayers("All", Color.black); |
83 | 0 | gop.setActive(true); |
84 | 0 | allGroupSelected = true; |
85 | 0 | groupList.add(gop); |
86 | 0 | groupList.add(new GroupOfPlayers("Friends", Color.blue)); |
87 | 0 | gop = new GroupOfPlayers("Enemies", Color.red); |
88 | 0 | gop.setIgnore(true); |
89 | 0 | groupList.add(gop); |
90 | 0 | } |
91 | ||
92 | 0 | public GroupManager() { |
93 | 0 | groupList.add(new GroupOfPlayers("All", Color.black)); |
94 | 0 | } |
95 | ||
96 | public void setGroupSelectPane(GroupSelectPane groupSelectPane) { | |
97 | 0 | this.groupSelectPane = groupSelectPane; |
98 | 0 | } |
99 | ||
100 | public void setPlayerListPane(PlayerListPane playerListPane) { | |
101 | 0 | this.playerListPane = playerListPane; |
102 | 0 | } |
103 | ||
104 | /** Set the reference to the database. | |
105 | * @param database the database to set | |
106 | */ | |
107 | public void setDatabase(Database database) { | |
108 | 0 | this.database = database; |
109 | 0 | groupList = null; |
110 | try { | |
111 | 0 | groupList = this.database.getGroupsOfPlayers(); |
112 | 0 | } catch (Exception e) {} |
113 | 0 | if (groupList == null || groupList.size() == 0) |
114 | 0 | initDefaultGroups(); |
115 | 0 | this.allGroupSelected = groupList.get(0).isActive(); |
116 | 0 | } |
117 | ||
118 | public void save() { | |
119 | if (DEBUG) | |
120 | 0 | logger.info("save"); |
121 | 0 | this.database.store(groupList); |
122 | 0 | } |
123 | ||
124 | public void populatePane() { | |
125 | 0 | for (GroupOfPlayers gop : groupList) { |
126 | 0 | groupSelectPane.addGroup(gop); |
127 | 0 | } |
128 | 0 | groupSelectPane.populateFinish(); |
129 | 0 | } |
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 | 0 | logger.info("updateGroupOfPlayers: id=" + id + " name=" + groupName + " active=" + active); |
139 | 0 | if (id == 0) { |
140 | 0 | GroupOfPlayers gop = new GroupOfPlayers(groupName, Color.black); |
141 | 0 | gop.setActive(active); |
142 | 0 | groupList.add(gop); |
143 | 0 | } else { |
144 | 0 | for (GroupOfPlayers gop : groupList) { |
145 | 0 | if (gop.getId() == id) { |
146 | 0 | if (active != gop.isActive()) |
147 | 0 | gop.setDirty(true); |
148 | 0 | gop.setActive(active); |
149 | } | |
150 | 0 | } |
151 | } | |
152 | 0 | this.allGroupSelected = groupList.get(0).isActive(); |
153 | 0 | this.playerListPane.groupPaneChanged(); |
154 | 0 | } |
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 | 0 | for (GroupOfPlayers gop : groupList) { |
162 | 0 | if (gop.getGroupName().equals(groupName)) |
163 | 0 | return(gop); |
164 | 0 | } |
165 | 0 | return(null); |
166 | } | |
167 | ||
168 | public GroupOfPlayers getGroupOfPlayers(int groupId) { | |
169 | 0 | for (GroupOfPlayers gop : groupList) { |
170 | 0 | if (gop.getId() == groupId) |
171 | 0 | return(gop); |
172 | 0 | } |
173 | 0 | 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 | 0 | groupList.add(groupOfPlayers); |
181 | 0 | save(); |
182 | 0 | groupSelectPane.repopulate(); |
183 | 0 | populatePane(); |
184 | 0 | this.playerListPane.groupPaneChanged(); |
185 | 0 | } |
186 | ||
187 | public void editGroupFromUser(GroupOfPlayers groupOfPlayers) { | |
188 | 0 | GroupOfPlayers gop = getGroupOfPlayers(groupOfPlayers.getId()); |
189 | 0 | if (gop == null) |
190 | 0 | return; |
191 | 0 | gop.setDirty(true); |
192 | 0 | save(); |
193 | 0 | groupSelectPane.repopulate(); |
194 | 0 | populatePane(); |
195 | 0 | this.playerListPane.groupPaneChanged(); |
196 | 0 | } |
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 | 0 | Player p = this.database.getPlayer(playerName); |
204 | 0 | if (p == null) { |
205 | 0 | p = this.playerListPane.getPlayer(playerName); |
206 | 0 | if (p == null) |
207 | 0 | return; // We can't find the guy we clicked on?? |
208 | 0 | 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 | 0 | this.playerListPane.playerChanged(p); |
212 | if (DEBUG) | |
213 | 0 | logger.info("popupMenuItemSelected: name = '" + p.getName() + "' Now playerid =" + p.getId()); |
214 | } | |
215 | 0 | int pi = p.getId(); |
216 | 0 | GroupOfPlayers gop = getGroupOfPlayers(groupName); |
217 | 0 | if (this.isInGroup(groupName, pi)) { |
218 | 0 | for (PlayerGroup pg : gop.getPlayerGroups()) { |
219 | 0 | if (pg.getPlayerId() == pi) { |
220 | 0 | pg.setTagForDelete(); |
221 | 0 | gop.setDirty(true); |
222 | 0 | this.save(); |
223 | 0 | return; |
224 | } | |
225 | 0 | } |
226 | } else { | |
227 | 0 | gop.addPlayer(pi); |
228 | 0 | gop.setDirty(true); |
229 | 0 | this.save(); |
230 | } | |
231 | 0 | } |
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 | 0 | GroupOfPlayers gop = getGroupOfPlayers(groupName); |
240 | 0 | LinkedList<Integer> ll = gop.getPlayers(); |
241 | 0 | for (int i : ll) { |
242 | 0 | if (i == playerId) |
243 | 0 | return(true); |
244 | 0 | } |
245 | 0 | return(false); |
246 | } | |
247 | ||
248 | public LinkedList<PopupGroup> getGroupsForPlayer(GroupPopupSubmenu groupPopupSubmenu, int playerId) { | |
249 | 0 | LinkedList<PopupGroup> ll = new LinkedList<PopupGroup>(); |
250 | 0 | for (GroupOfPlayers gop : groupList) { |
251 | 0 | PopupGroup pg = groupPopupSubmenu.new PopupGroup(); |
252 | 0 | pg.name = gop.getGroupName(); |
253 | 0 | for (Integer i : gop.getPlayers()) { |
254 | 0 | if (playerId == i) { |
255 | 0 | pg.selected = true; |
256 | 0 | break; |
257 | } | |
258 | 0 | } |
259 | 0 | ll.add(pg); |
260 | 0 | } |
261 | 0 | 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 | 0 | if (player.getId() == 0) { |
271 | //System.out.println("filter: pid=0 " + player.getName()); | |
272 | 0 | if (this.allGroupSelected) |
273 | 0 | return(true); |
274 | else | |
275 | 0 | return(false); |
276 | } | |
277 | 0 | int playerId = player.getId(); |
278 | 0 | if (this.allGroupSelected) { |
279 | 0 | for (GroupOfPlayers gop : groupList) { |
280 | 0 | if (gop.isActive() && gop.isIgnore()) { |
281 | 0 | for (Integer i : gop.getPlayers()) { |
282 | 0 | if (playerId == i) { |
283 | //System.out.println("filter: " + i + " " + player.getName() + " - g:" + gop.getGroupName()); | |
284 | 0 | return(false); |
285 | } | |
286 | 0 | } |
287 | } | |
288 | 0 | } |
289 | 0 | return(true); |
290 | } else { | |
291 | 0 | for (GroupOfPlayers gop : groupList) { |
292 | 0 | if (gop.isActive()) { |
293 | 0 | for (Integer i : gop.getPlayers()) { |
294 | 0 | if (playerId == i) { |
295 | 0 | return(!gop.isIgnore()); |
296 | } | |
297 | 0 | } |
298 | } | |
299 | 0 | } |
300 | } | |
301 | 0 | 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 | 0 | StringBuffer sb = new StringBuffer(); |
309 | 0 | if (this.allGroupSelected) |
310 | 0 | sb.append("All"); |
311 | 0 | for (GroupOfPlayers gop : groupList) { |
312 | 0 | if (gop.isIgnore()) { |
313 | 0 | if (gop.isActive()) { |
314 | 0 | if (sb.length() != 0) |
315 | 0 | sb.append("; "); |
316 | 0 | sb.append("!"); |
317 | 0 | sb.append(gop.getGroupName()); |
318 | } | |
319 | } else { | |
320 | 0 | if (this.allGroupSelected || !gop.isActive()) |
321 | 0 | continue; |
322 | 0 | if (sb.length() != 0) |
323 | 0 | sb.append("; "); |
324 | 0 | sb.append(gop.getGroupName()); |
325 | } | |
326 | 0 | } |
327 | 0 | return(sb.toString()); |
328 | } | |
329 | } |