| 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 net.sourceforge.hiveboard.AccountRole; | 
| 18 | import net.sourceforge.hiveboard.Board; | 
| 19 | import net.sourceforge.hiveboard.EventType; | 
| 20 | import net.sourceforge.hiveboard.model.BoardRepository; | 
| 21 | import net.sourceforge.hiveboard.util.GuiContext; | 
| 22 | import net.sourceforge.hiveboard.util.GuiContextListener; | 
| 23 | import net.sourceforge.hiveboard.util.TokenSetterHelper; | 
| 24 | import net.sourceforge.hivegui.application.AbstractPropertyBean; | 
| 25 |   | 
| 26 | public class GuiContextHandler | 
| 27 |         extends                AbstractPropertyBean  | 
| 28 |         implements        GuiContextListener | 
| 29 | { | 
| 30 |         static public final String        SELF_ADMIN                                        = "selfAdmin"; | 
| 31 |         static public final String        ACCOUNT_MODIFIABLE                        = "selectedAccountModifiable"; | 
| 32 |         static public final String        BOARD_SELECTED                                = "boardSelected"; | 
| 33 |         static public final String        DRAW_BOARD_SELECTED                        = "drawBoardSelected"; | 
| 34 |         static public final String        BOARD_INITIATOR                                = "boardInitiator"; | 
| 35 |         static public final String        DRAW_BOARD_INITIATOR                = "drawBoardInitiator"; | 
| 36 |         static public final String        ABSENT_FROM_BOARD                        = "absentFromBoard"; | 
| 37 |         static public final String        TOKEN_REQUESTABLE                        = "tokenRequestable"; | 
| 38 |         static public final String        BOARD_DESTROYABLE                        = "boardDestroyable"; | 
| 39 |         static public final String        SELF_INITIATOR                                = "selfInitiator"; | 
| 40 |         static public final String        NOTIFICATION_TOKEN_SETTABLE        = "notificationTokenSettable"; | 
| 41 |         static public final String        PARTICIPANT_REMOVABLE                = "participantRemovable"; | 
| 42 |         static public final String        PARTICIPANT_TOKEN_SETTABLE        = "participantTokenSettable"; | 
| 43 |   | 
| 44 |         public GuiContextHandler() | 
| 45 |         { | 
| 46 |                 setProperty(SELF_ADMIN, false); | 
| 47 |                 setProperty(ACCOUNT_MODIFIABLE, false); | 
| 48 |                 setProperty(SELF_INITIATOR, false); | 
| 49 |                 setProperty(BOARD_SELECTED, false); | 
| 50 |                 setProperty(DRAW_BOARD_SELECTED, false); | 
| 51 |                 setProperty(ABSENT_FROM_BOARD, false); | 
| 52 |                 setProperty(BOARD_INITIATOR, false); | 
| 53 |                 setProperty(DRAW_BOARD_INITIATOR, false); | 
| 54 |                 setProperty(TOKEN_REQUESTABLE, false); | 
| 55 |                 setProperty(BOARD_DESTROYABLE, false); | 
| 56 |                 setProperty(NOTIFICATION_TOKEN_SETTABLE, false); | 
| 57 |                 setProperty(PARTICIPANT_REMOVABLE, false); | 
| 58 |                 setProperty(PARTICIPANT_TOKEN_SETTABLE, false); | 
| 59 |         } | 
| 60 |          | 
| 61 |         public void        setBoardRepository(BoardRepository repository) | 
| 62 |         { | 
| 63 |                 _repository = repository; | 
| 64 |         } | 
| 65 |   | 
| 66 |         public void                setGuiContext(GuiContext context) | 
| 67 |         { | 
| 68 |                 _guiContext = context; | 
| 69 |                 context.addContextListener(this); | 
| 70 |         } | 
| 71 |   | 
| 72 |         public boolean        isSelfAdmin() | 
| 73 |         { | 
| 74 |                 return getBooleanProperty(SELF_ADMIN); | 
| 75 |         } | 
| 76 |          | 
| 77 |         public boolean        isSelectedAccountModifiable() | 
| 78 |         { | 
| 79 |                 return getBooleanProperty(ACCOUNT_MODIFIABLE); | 
| 80 |         } | 
| 81 |   | 
| 82 |         public boolean        isBoardSelected() | 
| 83 |         { | 
| 84 |                 return getBooleanProperty(BOARD_SELECTED); | 
| 85 |         } | 
| 86 |          | 
| 87 |         public boolean        isDrawBoardSelected() | 
| 88 |         { | 
| 89 |                 return getBooleanProperty(DRAW_BOARD_SELECTED); | 
| 90 |         } | 
| 91 |          | 
| 92 |         public boolean        isBoardInitiator() | 
| 93 |         { | 
| 94 |                 return getBooleanProperty(BOARD_INITIATOR); | 
| 95 |         } | 
| 96 |          | 
| 97 |         public boolean        isDrawBoardInitiator() | 
| 98 |         { | 
| 99 |                 return getBooleanProperty(DRAW_BOARD_INITIATOR); | 
| 100 |         } | 
| 101 |          | 
| 102 |         public boolean        isAbsentFromBoard() | 
| 103 |         { | 
| 104 |                 return getBooleanProperty(ABSENT_FROM_BOARD); | 
| 105 |         } | 
| 106 |          | 
| 107 |         public boolean        isTokenRequestable() | 
| 108 |         { | 
| 109 |                 return getBooleanProperty(TOKEN_REQUESTABLE); | 
| 110 |         } | 
| 111 |          | 
| 112 |         public boolean        isSelfInitiator() | 
| 113 |         { | 
| 114 |                 return getBooleanProperty(SELF_INITIATOR); | 
| 115 |         } | 
| 116 |          | 
| 117 |         public boolean        isBoardDestroyable() | 
| 118 |         { | 
| 119 |                 return getBooleanProperty(BOARD_DESTROYABLE); | 
| 120 |         } | 
| 121 |   | 
| 122 |         public boolean        isNotificationTokenSettable() | 
| 123 |         { | 
| 124 |                 return getBooleanProperty(NOTIFICATION_TOKEN_SETTABLE); | 
| 125 |         } | 
| 126 |   | 
| 127 |         public boolean        isParticipantRemovable() | 
| 128 |         { | 
| 129 |                 return getBooleanProperty(PARTICIPANT_REMOVABLE); | 
| 130 |         } | 
| 131 |   | 
| 132 |         public boolean        isParticipantTokenSettable() | 
| 133 |         { | 
| 134 |                 return getBooleanProperty(PARTICIPANT_TOKEN_SETTABLE); | 
| 135 |         } | 
| 136 |          | 
| 137 |         public void contextChanged(GuiContext context) | 
| 138 |         { | 
| 139 |                 if (context.getIdentity() == null) | 
| 140 |                 { | 
| 141 |                         return; | 
| 142 |                 } | 
| 143 |                 updateAccountProperties(context); | 
| 144 |                 updateBoardProperties(context); | 
| 145 |                 updateParticipantProperties(context); | 
| 146 |                 updateNotificationProperties(context); | 
| 147 |         } | 
| 148 |   | 
| 149 |         protected void updateAccountProperties(GuiContext context) | 
| 150 |         { | 
| 151 |                 boolean enabled; | 
| 152 |   | 
| 153 |                 enabled = context.getIdentity().isAdmin(); | 
| 154 |                 setProperty(SELF_ADMIN, enabled); | 
| 155 |   | 
| 156 |                 enabled =                context.getAccount() != null | 
| 157 |                                         &&        context.getIdentity().isAdmin(); | 
| 158 |                 setProperty(ACCOUNT_MODIFIABLE, enabled); | 
| 159 |   | 
| 160 |                 enabled = context.getIdentity().getRole() == AccountRole.INITIATOR; | 
| 161 |                 setProperty(SELF_INITIATOR, enabled); | 
| 162 |         } | 
| 163 |   | 
| 164 |         protected void updateBoardProperties(GuiContext context) | 
| 165 |         { | 
| 166 |                 boolean enabled; | 
| 167 |                 int id = context.getIdentity().getId(); | 
| 168 |   | 
| 169 |                 enabled = context.getBoard() != null; | 
| 170 |                 setProperty(BOARD_SELECTED, enabled); | 
| 171 |   | 
| 172 |                 enabled = context.getDrawBoard() != null; | 
| 173 |                 setProperty(DRAW_BOARD_SELECTED, enabled); | 
| 174 |   | 
| 175 |                 enabled =                context.getBoard() != null  | 
| 176 |                                         &&        !context.getBoard().getPresents().contains(id); | 
| 177 |                 setProperty(ABSENT_FROM_BOARD, enabled); | 
| 178 |   | 
| 179 |                 enabled =                context.getBoard() != null | 
| 180 |                                         &&        context.getBoard().getInitiator() == id; | 
| 181 |                 setProperty(BOARD_INITIATOR, enabled); | 
| 182 |                  | 
| 183 |                 enabled =                context.getDrawBoard() != null | 
| 184 |                                         &&        context.getDrawBoard().getInitiator() == id; | 
| 185 |                 setProperty(DRAW_BOARD_INITIATOR, enabled); | 
| 186 |                  | 
| 187 |                 enabled =                context.getDrawBoard() != null | 
| 188 |                                         &&        context.getDrawBoard().getInitiator() != id | 
| 189 |                                         &&        context.getDrawBoard().getWriter() != id; | 
| 190 |                 setProperty(TOKEN_REQUESTABLE, enabled); | 
| 191 |                  | 
| 192 |                 enabled =                context.getBoard() != null | 
| 193 |                                         &&        context.getBoard().getInitiator() == id | 
| 194 |                                         &&        context.getBoard().getPresents().isEmpty(); | 
| 195 |                 setProperty(BOARD_DESTROYABLE, enabled); | 
| 196 |         } | 
| 197 |   | 
| 198 |         protected void updateParticipantProperties(GuiContext context) | 
| 199 |         { | 
| 200 |                 boolean enabled; | 
| 201 |                 int id = context.getIdentity().getId(); | 
| 202 |   | 
| 203 |                 enabled =                 context.getBoard() != null  | 
| 204 |                                         &&        context.getParticipant() != null | 
| 205 |                                         &&        context.getBoard().getInitiator() == id | 
| 206 |                                         &&        !context.getParticipant().isInitiator(); | 
| 207 |                 setProperty(PARTICIPANT_REMOVABLE, enabled); | 
| 208 |   | 
| 209 |                 enabled =                 context.getBoard() != null  | 
| 210 |                                         &&        context.getParticipant() != null | 
| 211 |                                         &&        context.getParticipant().isPresent() | 
| 212 |                                         &&        !context.getParticipant().isWriter() | 
| 213 |                                         &&        TokenSetterHelper.canGiveToken(context.getBoard(), id); | 
| 214 |                 setProperty(PARTICIPANT_TOKEN_SETTABLE, enabled); | 
| 215 |         } | 
| 216 |   | 
| 217 |         protected void updateNotificationProperties(GuiContext context) | 
| 218 |         { | 
| 219 |                 boolean enabled; | 
| 220 |                 int id = context.getIdentity().getId(); | 
| 221 |   | 
| 222 |                 enabled = false; | 
| 223 |                 if (        context.getNotification() != null | 
| 224 |                         &&        context.getNotification().getType() == EventType.EVT_REQUEST_TOKEN) | 
| 225 |                 { | 
| 226 |                         Board board = _repository.getBoard(context.getNotification().getWhere()); | 
| 227 |                         int who = context.getNotification().getWho(); | 
| 228 |                         if (        board != null | 
| 229 |                                 &&        board.getPresents().contains(who) | 
| 230 |                                 &&        who != board.getWriter() | 
| 231 |                                 &&        TokenSetterHelper.canGiveToken(board, id)) | 
| 232 |                         { | 
| 233 |                                 enabled = true; | 
| 234 |                         } | 
| 235 |                 } | 
| 236 |                 setProperty(NOTIFICATION_TOKEN_SETTABLE, enabled); | 
| 237 |         } | 
| 238 |   | 
| 239 |         protected GuiContext                _guiContext; | 
| 240 |         protected BoardRepository        _repository; | 
| 241 | } |