| 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.Comparator; | 
| 18 |   | 
| 19 | import net.sourceforge.hiveboard.Board; | 
| 20 | import net.sourceforge.hiveboard.Event; | 
| 21 | import net.sourceforge.hiveboard.EventType; | 
| 22 | import net.sourceforge.hiveboard.event.ConstraintEventFilter; | 
| 23 | import net.sourceforge.hiveboard.event.Constraints; | 
| 24 | import net.sourceforge.hiveboard.event.EventsPriorities; | 
| 25 | import net.sourceforge.hiveboard.event.PersistentDeferredConsumer; | 
| 26 | import net.sourceforge.hiveboard.main.IdentityHolder; | 
| 27 | import net.sourceforge.hiveboard.model.BoardRepository; | 
| 28 | import net.sourceforge.hiveevents.Channel; | 
| 29 | import net.sourceforge.hiveevents.Filter; | 
| 30 | import net.sourceforge.hivegui.table.DefaultDataListModel; | 
| 31 |   | 
| 32 | public class BoardListModel extends DefaultDataListModel<Board> | 
| 33 | { | 
| 34 |         public BoardListModel(        BoardRepository                repository, | 
| 35 |                                                         IdentityHolder                holder, | 
| 36 |                                                         Channel<Event>                eventChannel) | 
| 37 |         { | 
| 38 |                 super(Board.class, new BoardComparator()); | 
| 39 |                 setRows(repository.getBoards()); | 
| 40 |                 _identity = holder; | 
| 41 |   | 
| 42 |                 // Add events listeners | 
| 43 |                 int priority = EventsPriorities.BOARD_LIST_UPDATE; | 
| 44 |                 String constraint = Constraints.eventType(        EventType.EVT_CREATE_BOARD, | 
| 45 |                                                                                                         EventType.EVT_BOARD_ADD_PARTICIPANT); | 
| 46 |                 Filter<Event> filter = new ConstraintEventFilter(constraint); | 
| 47 |                 PersistentDeferredConsumer consumer = new PersistentDeferredConsumer() | 
| 48 |                 { | 
| 49 |                         @Override protected void pushEvent(Event event) | 
| 50 |                         { | 
| 51 |                                 if (event.getBoard() != null) | 
| 52 |                                 { | 
| 53 |                                         addRow(event.getBoard()); | 
| 54 |                                 } | 
| 55 |                         } | 
| 56 |                 }; | 
| 57 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 58 |   | 
| 59 |                 constraint = Constraints.eventType(EventType.EVT_MODIFY_BOARD); | 
| 60 |                 filter = new ConstraintEventFilter(constraint); | 
| 61 |                 consumer = new PersistentDeferredConsumer() | 
| 62 |                 { | 
| 63 |                         @Override protected void pushEvent(Event event) | 
| 64 |                         { | 
| 65 |                                 boardModified(event.getBoard()); | 
| 66 |                         } | 
| 67 |                 }; | 
| 68 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 69 |   | 
| 70 |                 constraint = Constraints.eventType(EventType.EVT_DESTROY_BOARD) + | 
| 71 |                                          " || (" + | 
| 72 |                                          Constraints.eventTypeWho( | 
| 73 |                                                          _identity.getId(), EventType.EVT_BOARD_DEL_PARTICIPANT) + | 
| 74 |                                          ")"; | 
| 75 |                 filter = new ConstraintEventFilter(constraint); | 
| 76 |                 consumer = new PersistentDeferredConsumer() | 
| 77 |                 { | 
| 78 |                         @Override protected void pushEvent(Event event) | 
| 79 |                         { | 
| 80 |                                 removeRow(findBoard(event.getWhere())); | 
| 81 |                         } | 
| 82 |                 }; | 
| 83 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 84 |   | 
| 85 |                 constraint = Constraints.eventType(EventType.EVT_MODIFY_ACCOUNT); | 
| 86 |                 filter = new ConstraintEventFilter(constraint); | 
| 87 |                 consumer = new PersistentDeferredConsumer() | 
| 88 |                 { | 
| 89 |                         @Override protected void pushEvent(Event event) | 
| 90 |                         { | 
| 91 |                                 accountModified(event.getAccount().getId()); | 
| 92 |                         } | 
| 93 |                 }; | 
| 94 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 95 |         } | 
| 96 |          | 
| 97 |         private void        boardModified(Board board) | 
| 98 |         { | 
| 99 |                 if (board.getParticipants().contains(_identity.getId())) | 
| 100 |                 { | 
| 101 |                         int index = findBoard(board.getId()); | 
| 102 |                         if (index >= 0) | 
| 103 |                         { | 
| 104 |                                 // Normal modification on board to which self already participates | 
| 105 |                                 // No need to use event.getBoard() which is a temporary object, | 
| 106 |                                 // the right board is already updated by BoardRepository, so | 
| 107 |                                 // we just need to notify the change to the model | 
| 108 |                                 // NB: setRow(getRow()) is ugly but necessary currently because | 
| 109 |                                 // it is the only way to make sure that the list is always sorted | 
| 110 |                                 setRow(index, getRow(index)); | 
| 111 |                         } | 
| 112 |                         else | 
| 113 |                         { | 
| 114 |                                 // Modification includes adding self to list of participants | 
| 115 |                                 addRow(board); | 
| 116 |                         } | 
| 117 |                 } | 
| 118 |                 else | 
| 119 |                 { | 
| 120 |                         // Modification includes removal of self from participants | 
| 121 |                         removeRow(findBoard(board.getId())); | 
| 122 |                 } | 
| 123 |         } | 
| 124 |   | 
| 125 |         private void        accountModified(int account) | 
| 126 |         { | 
| 127 |                 int index = 0; | 
| 128 |                 for (Board board: _items) | 
| 129 |                 { | 
| 130 |                         if (board.getInitiator() == account) | 
| 131 |                         { | 
| 132 |                                 fireRowsModified(index, index); | 
| 133 |                         } | 
| 134 |                         index++; | 
| 135 |                 } | 
| 136 |         } | 
| 137 |          | 
| 138 |         public int        findBoard(int id) | 
| 139 |         { | 
| 140 |                 int index = 0; | 
| 141 |                 for (Board board: _items) | 
| 142 |                 { | 
| 143 |                         if (board.getId() == id) | 
| 144 |                         { | 
| 145 |                                 return index; | 
| 146 |                         } | 
| 147 |                         index++; | 
| 148 |                 } | 
| 149 |                 return -1; | 
| 150 |         } | 
| 151 |          | 
| 152 |         static private class BoardComparator implements Comparator<Board> | 
| 153 |         { | 
| 154 |                 public int        compare(Board b1, Board b2) | 
| 155 |                 { | 
| 156 |                         int diff = b1.getName().compareTo(b2.getName()); | 
| 157 |                         if (diff != 0) | 
| 158 |                         { | 
| 159 |                                 return diff; | 
| 160 |                         } | 
| 161 |                         return b1.getId() - b2.getId(); | 
| 162 |                 } | 
| 163 |         } | 
| 164 |   | 
| 165 |         final private IdentityHolder        _identity; | 
| 166 | } |