1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.command; |
16 | |
17 | import java.util.EventObject; |
18 | |
19 | import org.jdesktop.application.Application.ExitListener; |
20 | |
21 | import net.sourceforge.hiveboard.Board; |
22 | import net.sourceforge.hiveboard.util.TokenSetterHelper; |
23 | import net.sourceforge.hivegui.message.UserChoice; |
24 | |
25 | /** |
26 | * @author Jean-Francois Poilpret |
27 | */ |
28 | public class Quit extends AbstractCommandHolder implements ExitListener |
29 | { |
30 | public boolean canExit(EventObject event) |
31 | { |
32 | // First check if current user should first give board token to someone else |
33 | int self = _guiContext.getIdentity().getId(); |
34 | int numOpenBoards = 0; |
35 | int numGiveTokenBoards = 0; |
36 | int numInitializerBoards = 0; |
37 | for (Board board: _repository.getBoards()) |
38 | { |
39 | if (board.getPresents().contains(self)) |
40 | { |
41 | numOpenBoards++; |
42 | if (TokenSetterHelper.shouldGiveToken(board, self)) |
43 | { |
44 | numGiveTokenBoards++; |
45 | if (board.getInitiator() == self) |
46 | { |
47 | numInitializerBoards++; |
48 | } |
49 | } |
50 | } |
51 | } |
52 | if (numGiveTokenBoards > 0) |
53 | { |
54 | return (showMessage("confirm-quit", |
55 | numOpenBoards, |
56 | numGiveTokenBoards, |
57 | numInitializerBoards) == UserChoice.YES); |
58 | } |
59 | else |
60 | { |
61 | return true; |
62 | } |
63 | } |
64 | |
65 | public void willExit(EventObject event) |
66 | { |
67 | } |
68 | } |