| 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.ArrayList; | 
| 18 | import java.util.Collections; | 
| 19 | import java.util.Date; | 
| 20 | import java.util.List; | 
| 21 |   | 
| 22 | import org.jdesktop.application.Action; | 
| 23 | import org.jdesktop.application.Task; | 
| 24 |   | 
| 25 | import net.sourceforge.hiveboard.Board; | 
| 26 | import net.sourceforge.hiveboard.Event; | 
| 27 | import net.sourceforge.hiveboard.Snapshot; | 
| 28 | import net.sourceforge.hiveboard.dialog.LoadCommentsParamsPanel; | 
| 29 | import net.sourceforge.hiveboard.docking.Views; | 
| 30 | import net.sourceforge.hiveboard.util.TokenSetterHelper; | 
| 31 | import net.sourceforge.hiveboard.view.CommentListPanel; | 
| 32 | import net.sourceforge.hivegui.message.UserChoice; | 
| 33 | import net.sourceforge.hivegui.table.DataListModel; | 
| 34 |   | 
| 35 | public class BoardCommands extends AbstractCommandHolder | 
| 36 | { | 
| 37 |         @Action(enabledProperty = SELF_INITIATOR) | 
| 38 |         public void        createBoard() | 
| 39 |         { | 
| 40 |                 showDialog("create-board-panel"); | 
| 41 |         } | 
| 42 |          | 
| 43 |         @Action(enabledProperty = BOARD_INITIATOR) | 
| 44 |         public void        modifyBoard() | 
| 45 |         { | 
| 46 |                 showDialog("modify-board-panel", _guiContext.getBoard()); | 
| 47 |         } | 
| 48 |          | 
| 49 |         @Action(enabledProperty = BOARD_INITIATOR) | 
| 50 |         public void        modifyParticipants() | 
| 51 |         { | 
| 52 |                 showDialog("add-participant-panel", _guiContext.getBoard()); | 
| 53 |         } | 
| 54 |   | 
| 55 |         @Action(enabledProperty = BOARD_DESTROYABLE, block = Task.BlockingScope.WINDOW) | 
| 56 |         public Task        destroyBoard() | 
| 57 |         { | 
| 58 |                 if (showMessage("confirm-destroy-board",  | 
| 59 |                                                 _guiContext.getBoard().getName()) == UserChoice.YES) | 
| 60 |                 { | 
| 61 |                         final int id = _guiContext.getBoard().getId(); | 
| 62 |                         return new AbstractTask<Void, Void>(_application) | 
| 63 |                         { | 
| 64 |                                 @Override protected Void doInBackground() throws Exception | 
| 65 |                                 { | 
| 66 |                                         _service.closeBoard(id); | 
| 67 |                                         return null; | 
| 68 |                                 } | 
| 69 |                         }; | 
| 70 |                 } | 
| 71 |                 else | 
| 72 |                 { | 
| 73 |                         return null; | 
| 74 |                 } | 
| 75 |         } | 
| 76 |   | 
| 77 |         @Action(enabledProperty = BOARD_SELECTED, block = Task.BlockingScope.WINDOW) | 
| 78 |         public Task        listSnapshots() | 
| 79 |         { | 
| 80 |                 final Board board = _guiContext.getBoard(); | 
| 81 |                 return new AbstractTask<Snapshot[], Void>(_application) | 
| 82 |                 { | 
| 83 |                         @Override protected Snapshot[] doInBackground() throws Exception | 
| 84 |                         { | 
| 85 |                                 return _service.getSnapshots(board.getId()); | 
| 86 |                         } | 
| 87 |   | 
| 88 |                         @Override protected void succeeded(Snapshot[] snapshots) | 
| 89 |                         { | 
| 90 |                                 showDialog("list-snapshots-panel", _guiContext.getBoard(), snapshots); | 
| 91 |                         } | 
| 92 |                 }; | 
| 93 |         } | 
| 94 |          | 
| 95 |         @Action(enabledProperty = ABSENT_FROM_BOARD, block = Task.BlockingScope.WINDOW) | 
| 96 |         public Task        joinBoard() | 
| 97 |         { | 
| 98 |                 final int id = _guiContext.getBoard().getId(); | 
| 99 |                 return new AbstractTask<Void, Void>(_application) | 
| 100 |                 { | 
| 101 |                         @Override protected Void doInBackground() throws Exception | 
| 102 |                         { | 
| 103 |                                 _service.join(id); | 
| 104 |                                 return null; | 
| 105 |                         } | 
| 106 |                 }; | 
| 107 |         } | 
| 108 |          | 
| 109 |         @Action(enabledProperty = TOKEN_REQUESTABLE, block = Task.BlockingScope.WINDOW) | 
| 110 |         public Task        requestToken() | 
| 111 |         { | 
| 112 |                 final int id = _guiContext.getDrawBoard().getId(); | 
| 113 |                 return new AbstractTask<Void, Void>(_application) | 
| 114 |                 { | 
| 115 |                         @Override protected Void doInBackground() throws Exception | 
| 116 |                         { | 
| 117 |                                 _service.requestToken(id); | 
| 118 |                                 return null; | 
| 119 |                         } | 
| 120 |                 }; | 
| 121 |         } | 
| 122 |          | 
| 123 |         @Action(enabledProperty = DRAW_BOARD_INITIATOR, block = Task.BlockingScope.WINDOW) | 
| 124 |         public Task        takeSnapshot() | 
| 125 |         { | 
| 126 |                 final int id = _guiContext.getDrawBoard().getId(); | 
| 127 |                 return new AbstractTask<Void, Void>(_application) | 
| 128 |                 { | 
| 129 |                         @Override protected Void doInBackground() throws Exception | 
| 130 |                         { | 
| 131 |                                 _service.takeSnapshot(id); | 
| 132 |                                 return null; | 
| 133 |                         } | 
| 134 |                 }; | 
| 135 |         } | 
| 136 |   | 
| 137 |         @Action(enabledProperty = DRAW_BOARD_SELECTED, block = Task.BlockingScope.WINDOW) | 
| 138 |         public Task        loadComments() | 
| 139 |         { | 
| 140 |                 // Display dialog to enter from..until dates | 
| 141 |                 LoadCommentsParamsPanel paramsPanel = getParamsPanel(); | 
| 142 |                 if (!_application.showDialog(paramsPanel)) | 
| 143 |                 { | 
| 144 |                         return null; | 
| 145 |                 } | 
| 146 |                 // Get all required comments | 
| 147 |                 final Date from = paramsPanel.getFromDate(); | 
| 148 |                 final Date until = paramsPanel.getUntilDate(); | 
| 149 |   | 
| 150 |                 final DataListModel<Event> model = getCommentsModel(); | 
| 151 |                 final Event[] currentEvents = model.getRows(); | 
| 152 |                 return new AbstractTask<List<Event>, Void>(_application) | 
| 153 |                 { | 
| 154 |                         @Override protected List<Event> doInBackground() | 
| 155 |                                 throws Exception | 
| 156 |                         { | 
| 157 |                                 // Change list model of comments | 
| 158 |                                 List<Event> comments = new ArrayList<Event>(); | 
| 159 |                                 Collections.addAll(comments, _service.getBoardComments( | 
| 160 |                                                         _guiContext.getDrawBoard().getId(), from, until)); | 
| 161 |                                 List<Event> events = new ArrayList<Event>(); | 
| 162 |                                 Collections.addAll(events, currentEvents); | 
| 163 |                                 events.removeAll(comments); | 
| 164 |                                 events.addAll(comments); | 
| 165 |                                 return events; | 
| 166 |                         } | 
| 167 |   | 
| 168 |                         @Override protected void succeeded(List<Event> events) | 
| 169 |                         { | 
| 170 |                                 // Set the new rows for the list model | 
| 171 |                                 model.setRows(events.toArray(new Event[events.size()])); | 
| 172 |                         } | 
| 173 |                 }; | 
| 174 |         } | 
| 175 |          | 
| 176 |         @Action(enabledProperty = DRAW_BOARD_SELECTED, block = Task.BlockingScope.WINDOW) | 
| 177 |         public Task        leaveBoard() | 
| 178 |         { | 
| 179 |                 Board board = _guiContext.getDrawBoard(); | 
| 180 |                 // Check if can give token & there are still present participants | 
| 181 |                 if (        TokenSetterHelper.shouldGiveToken(board, _guiContext.getIdentity().getId()) | 
| 182 |                         &&        !showDialog("give-token-panel", board)) | 
| 183 |                 { | 
| 184 |                         // If dialog was cancelled, do nothing at all | 
| 185 |                         return null; | 
| 186 |                 } | 
| 187 |                 return createLeaveBoardTask(board.getId()); | 
| 188 |         } | 
| 189 |   | 
| 190 |         // This action is used when user closes the board area view (not the menu) | 
| 191 |         // Note: for an obscure reason, BlockingScope.WINDOW cannot be used here | 
| 192 |         // because it may lead to hangs (hourglass cursor) after closing board | 
| 193 |         @Action(block = Task.BlockingScope.APPLICATION) | 
| 194 |         public Task        confirmLeaveBoard() | 
| 195 |         { | 
| 196 |                 Board board = _guiContext.getDrawBoard(); | 
| 197 |   | 
| 198 |                 // Check if can give token & there are still present participants | 
| 199 |                 if (TokenSetterHelper.shouldGiveToken(board, _guiContext.getIdentity().getId())) | 
| 200 |                 { | 
| 201 |                         if (!showDialog("give-token-panel", board)) | 
| 202 |                         { | 
| 203 |                                 // If dialog was cancelled, do nothing at all | 
| 204 |                                 return null; | 
| 205 |                         } | 
| 206 |                 } | 
| 207 |                 else if (_application.showMessage( | 
| 208 |                                  "confirm-leave-board", board.getName()) == UserChoice.CANCEL) | 
| 209 |                 { | 
| 210 |                         // User has cancelled leaving the board, do nothing | 
| 211 |                         return null; | 
| 212 |                 } | 
| 213 |                 // Leaving board is confirmed | 
| 214 |                 return createLeaveBoardTask(board.getId()); | 
| 215 |         } | 
| 216 |          | 
| 217 |         protected Task        createLeaveBoardTask(final int board) | 
| 218 |         { | 
| 219 |                 return new AbstractTask<Void, Void>(_application) | 
| 220 |                 { | 
| 221 |                         @Override protected Void doInBackground() throws Exception | 
| 222 |                         { | 
| 223 |                                 _service.leave(board); | 
| 224 |                                 return null; | 
| 225 |                         } | 
| 226 |                 }; | 
| 227 |         } | 
| 228 |   | 
| 229 |         protected DataListModel<Event>        getCommentsModel() | 
| 230 |         { | 
| 231 |                 if (_commentsPanel == null) | 
| 232 |                 { | 
| 233 |                         _commentsPanel = (CommentListPanel) _builder.create(Views.COMMENTS_LIST.id()); | 
| 234 |                 } | 
| 235 |                 return _commentsPanel.getModel(); | 
| 236 |         } | 
| 237 |   | 
| 238 |         protected LoadCommentsParamsPanel        getParamsPanel() | 
| 239 |         { | 
| 240 |                 return (LoadCommentsParamsPanel) _builder.create( | 
| 241 |                                         "load-comments-params-panel", _guiContext.getDrawBoard()); | 
| 242 |         } | 
| 243 |          | 
| 244 |         protected CommentListPanel                        _commentsPanel; | 
| 245 | } |