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.util; |
16 | |
17 | import net.sourceforge.hiveboard.Account; |
18 | import net.sourceforge.hiveboard.Board; |
19 | import net.sourceforge.hiveboard.Event; |
20 | import net.sourceforge.hiveboard.EventType; |
21 | import net.sourceforge.hiveboard.event.ConstraintEventFilter; |
22 | import net.sourceforge.hiveboard.event.Constraints; |
23 | import net.sourceforge.hiveboard.event.DeferredConsumer; |
24 | import net.sourceforge.hiveboard.event.EventsPriorities; |
25 | import net.sourceforge.hiveboard.model.AccountRepository; |
26 | import net.sourceforge.hiveevents.Channel; |
27 | import net.sourceforge.hiveevents.Filter; |
28 | import net.sourceforge.hivegui.table.DefaultDataListModel; |
29 | |
30 | public abstract class AbstractParticipantListModel extends DefaultDataListModel<Participant> |
31 | { |
32 | protected AbstractParticipantListModel(Board board, AccountRepository repository) |
33 | { |
34 | super(Participant.class, new ParticipantComparator()); |
35 | _repository = repository; |
36 | _board = board; |
37 | } |
38 | |
39 | protected void installConsumers(Channel<Event> eventChannel) |
40 | { |
41 | String constraint; |
42 | |
43 | // account modifications |
44 | _consumerModifyAccount = new DeferredConsumer() |
45 | { |
46 | @Override protected void pushEvent(Event event) |
47 | { |
48 | updateParticipant(event.getWho(), true); |
49 | } |
50 | }; |
51 | constraint = Constraints.eventType(EventType.EVT_MODIFY_ACCOUNT); |
52 | registerConsumer(eventChannel, constraint, _consumerModifyAccount); |
53 | |
54 | // writer |
55 | _consumerGive = new DeferredConsumer() |
56 | { |
57 | @Override protected void pushEvent(Event event) |
58 | { |
59 | // First update the previous writer |
60 | updateParticipant(_writer, false); |
61 | // Then set new writer |
62 | _writer = event.getWho(); |
63 | updateParticipant(_writer, false); |
64 | } |
65 | }; |
66 | constraint = Constraints.eventTypeWhere(_board.getId(), EventType.EVT_GIVE_TOKEN); |
67 | registerConsumer(eventChannel, constraint, _consumerGive); |
68 | } |
69 | |
70 | static protected void registerConsumer( Channel<Event> channel, |
71 | String constraint, |
72 | DeferredConsumer consumer) |
73 | { |
74 | Filter<Event> filter = new ConstraintEventFilter(constraint); |
75 | channel.registerPushConsumer( EventsPriorities.PARTICIPANTS_LIST_UPDATE, |
76 | filter, |
77 | consumer); |
78 | } |
79 | |
80 | protected void updateParticipant(int who, boolean refresh) |
81 | { |
82 | int index = findParticipant(who); |
83 | if (index >= 0) |
84 | { |
85 | if (refresh) |
86 | { |
87 | setRow(index, createParticipant(who)); |
88 | } |
89 | else |
90 | { |
91 | fireRowsModified(index, index); |
92 | } |
93 | } |
94 | } |
95 | |
96 | protected Participant createParticipant(int id) |
97 | { |
98 | Account account = _repository.getAccount(id); |
99 | if (account == null) |
100 | { |
101 | return null; |
102 | } |
103 | else |
104 | { |
105 | return new Participant(account, _board); |
106 | } |
107 | } |
108 | |
109 | protected int findParticipant(int id) |
110 | { |
111 | int index = 0; |
112 | for (Participant participant: _items) |
113 | { |
114 | if (participant.getAccount().getId() == id) |
115 | { |
116 | return index; |
117 | } |
118 | index++; |
119 | } |
120 | return -1; |
121 | } |
122 | |
123 | protected final AccountRepository _repository; |
124 | protected final Board _board; |
125 | protected int _writer; |
126 | protected DeferredConsumer _consumerModifyAccount; |
127 | protected DeferredConsumer _consumerGive; |
128 | } |