| 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.dialog; |
| 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.main.IdentityHolder; |
| 29 | import net.sourceforge.hiveboard.model.AccountRepository; |
| 30 | import net.sourceforge.hiveboard.util.AbstractParticipantListModel; |
| 31 | import net.sourceforge.hiveboard.util.Participant; |
| 32 | import net.sourceforge.hiveevents.Channel; |
| 33 | |
| 34 | public class PresentListModel extends AbstractParticipantListModel |
| 35 | { |
| 36 | static private final Log _logger = LogFactory.getLog(PresentListModel.class); |
| 37 | |
| 38 | public PresentListModel(Board board, |
| 39 | AccountRepository repository, |
| 40 | Channel<Event> eventChannel, |
| 41 | IdentityHolder identity) |
| 42 | { |
| 43 | super(board, repository); |
| 44 | if (_logger.isDebugEnabled()) |
| 45 | { |
| 46 | _logger.debug("<init> board=" + board.getId() + ", writer=" + board.getWriter()); |
| 47 | } |
| 48 | |
| 49 | refresh(identity.getId()); |
| 50 | installConsumers(eventChannel); |
| 51 | |
| 52 | // Add events listeners |
| 53 | String constraint; |
| 54 | |
| 55 | // present persons |
| 56 | _consumerJoin = new DeferredConsumer() |
| 57 | { |
| 58 | @Override protected void pushEvent(Event event) |
| 59 | { |
| 60 | addRow(createParticipant(event.getWho())); |
| 61 | } |
| 62 | }; |
| 63 | constraint = Constraints.eventTypeWhere(_board.getId(), EventType.EVT_JOIN_BOARD); |
| 64 | registerConsumer(eventChannel, constraint, _consumerJoin); |
| 65 | |
| 66 | _consumerLeave = new DeferredConsumer() |
| 67 | { |
| 68 | @Override protected void pushEvent(Event event) |
| 69 | { |
| 70 | removeRow(findParticipant(event.getWho())); |
| 71 | } |
| 72 | }; |
| 73 | constraint = Constraints.eventTypeWhere(_board.getId(), EventType.EVT_LEAVE_BOARD); |
| 74 | registerConsumer(eventChannel, constraint, _consumerLeave); |
| 75 | } |
| 76 | |
| 77 | private void refresh(int self) |
| 78 | { |
| 79 | _logger.debug("refresh() board=" + _board.getId()); |
| 80 | _writer = _board.getWriter(); |
| 81 | List<Participant> presents = |
| 82 | new ArrayList<Participant>(_board.getPresents().size()); |
| 83 | for (int id: _board.getPresents()) |
| 84 | { |
| 85 | if (id != self) |
| 86 | { |
| 87 | Participant participant = createParticipant(id); |
| 88 | if (participant != null) |
| 89 | { |
| 90 | presents.add(participant); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | setRows(presents.toArray(new Participant[presents.size()])); |
| 96 | } |
| 97 | |
| 98 | private final DeferredConsumer _consumerJoin; |
| 99 | private final DeferredConsumer _consumerLeave; |
| 100 | } |