| 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.view; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.List; |
| 19 | |
| 20 | import org.apache.commons.logging.Log; |
| 21 | import org.apache.commons.logging.LogFactory; |
| 22 | |
| 23 | import net.sourceforge.hiveboard.Board; |
| 24 | import net.sourceforge.hiveboard.Event; |
| 25 | import net.sourceforge.hiveboard.EventType; |
| 26 | import net.sourceforge.hiveboard.event.Constraints; |
| 27 | import net.sourceforge.hiveboard.event.DeferredConsumer; |
| 28 | import net.sourceforge.hiveboard.model.AccountRepository; |
| 29 | import net.sourceforge.hiveboard.util.AbstractParticipantListModel; |
| 30 | import net.sourceforge.hiveboard.util.Participant; |
| 31 | import net.sourceforge.hiveevents.Channel; |
| 32 | |
| 33 | public class ParticipantListModel extends AbstractParticipantListModel |
| 34 | { |
| 35 | static private final Log _logger = LogFactory.getLog(ParticipantListModel.class); |
| 36 | |
| 37 | public ParticipantListModel(Board board, |
| 38 | AccountRepository repository, |
| 39 | Channel<Event> eventChannel) |
| 40 | { |
| 41 | super(board, repository); |
| 42 | if (_logger.isDebugEnabled()) |
| 43 | { |
| 44 | _logger.debug("<init> board=" + board.getId() + ", writer=" + board.getWriter()); |
| 45 | } |
| 46 | |
| 47 | refresh(); |
| 48 | installConsumers(eventChannel); |
| 49 | |
| 50 | // Add events listeners |
| 51 | // participants list |
| 52 | _consumerModifyBoard = new DeferredConsumer() |
| 53 | { |
| 54 | @Override protected void pushEvent(Event event) |
| 55 | { |
| 56 | // Just a refresh is needed, since _board has been updated from |
| 57 | // BoardRepository! |
| 58 | refresh(); |
| 59 | } |
| 60 | }; |
| 61 | String constraint; |
| 62 | constraint = Constraints.eventTypeWhere(_board.getId(), EventType.EVT_MODIFY_BOARD); |
| 63 | registerConsumer(eventChannel, constraint, _consumerModifyBoard); |
| 64 | |
| 65 | _consumerAddParticipant = new DeferredConsumer() |
| 66 | { |
| 67 | @Override protected void pushEvent(Event event) |
| 68 | { |
| 69 | addRow(createParticipant(event.getWho())); |
| 70 | } |
| 71 | }; |
| 72 | constraint = Constraints.eventTypeWhere( |
| 73 | _board.getId(), EventType.EVT_BOARD_ADD_PARTICIPANT); |
| 74 | registerConsumer(eventChannel, constraint, _consumerAddParticipant); |
| 75 | |
| 76 | _consumerDelParticipant = new DeferredConsumer() |
| 77 | { |
| 78 | @Override protected void pushEvent(Event event) |
| 79 | { |
| 80 | removeRow(findParticipant(event.getWho())); |
| 81 | } |
| 82 | }; |
| 83 | constraint = Constraints.eventTypeWhere( |
| 84 | _board.getId(), EventType.EVT_BOARD_DEL_PARTICIPANT) + |
| 85 | " || " + Constraints.eventType(EventType.EVT_DESTROY_ACCOUNT); |
| 86 | registerConsumer(eventChannel, constraint, _consumerDelParticipant); |
| 87 | |
| 88 | // token request |
| 89 | _consumerRequest = new DeferredConsumer() |
| 90 | { |
| 91 | @Override protected void pushEvent(Event event) |
| 92 | { |
| 93 | int index = findParticipant(event.getWho()); |
| 94 | if (index >= 0) |
| 95 | { |
| 96 | Participant person = getRow(index); |
| 97 | person.setRequester(); |
| 98 | fireRowsModified(index, index); |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | constraint = Constraints.eventTypeWhere(_board.getId(), EventType.EVT_REQUEST_TOKEN); |
| 103 | registerConsumer(eventChannel, constraint, _consumerRequest); |
| 104 | |
| 105 | // present persons |
| 106 | _consumerJoinLeave = new DeferredConsumer() |
| 107 | { |
| 108 | @Override protected void pushEvent(Event event) |
| 109 | { |
| 110 | updateParticipant(event.getWho(), false); |
| 111 | } |
| 112 | }; |
| 113 | constraint = Constraints.eventTypeWhere( |
| 114 | _board.getId(), EventType.EVT_JOIN_BOARD, EventType.EVT_LEAVE_BOARD); |
| 115 | registerConsumer(eventChannel, constraint, _consumerJoinLeave); |
| 116 | } |
| 117 | |
| 118 | private void refresh() |
| 119 | { |
| 120 | _logger.debug("refresh() board=" + _board.getId()); |
| 121 | _writer = _board.getWriter(); |
| 122 | List<Participant> participants = |
| 123 | new ArrayList<Participant>(_board.getParticipants().size()); |
| 124 | for (int id: _board.getParticipants()) |
| 125 | { |
| 126 | Participant participant = createParticipant(id); |
| 127 | if (participant != null) |
| 128 | { |
| 129 | participants.add(participant); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | setRows(participants.toArray(new Participant[participants.size()])); |
| 134 | } |
| 135 | |
| 136 | private final DeferredConsumer _consumerModifyBoard; |
| 137 | private final DeferredConsumer _consumerAddParticipant; |
| 138 | private final DeferredConsumer _consumerDelParticipant; |
| 139 | private final DeferredConsumer _consumerRequest; |
| 140 | private final DeferredConsumer _consumerJoinLeave; |
| 141 | } |