| 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.Account; | 
| 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.model.AccountRepository; | 
| 27 | import net.sourceforge.hiveevents.Channel; | 
| 28 | import net.sourceforge.hiveevents.Filter; | 
| 29 | import net.sourceforge.hivegui.table.DefaultDataListModel; | 
| 30 |   | 
| 31 | public class AccountListModel extends DefaultDataListModel<Account> | 
| 32 | { | 
| 33 |         public AccountListModel(AccountRepository repository, Channel<Event> eventChannel) | 
| 34 |         { | 
| 35 |                 super(Account.class, new AccountComparator()); | 
| 36 |                 setRows(repository.getAccounts()); | 
| 37 |   | 
| 38 |                 // Add events listeners | 
| 39 |                 int priority = EventsPriorities.ACCOUNT_LIST_UPDATE; | 
| 40 |                 String constraint; | 
| 41 |                 constraint = Constraints.eventType(EventType.EVT_CREATE_ACCOUNT); | 
| 42 |                 Filter<Event> filter = new ConstraintEventFilter(constraint); | 
| 43 |                 PersistentDeferredConsumer consumer = new PersistentDeferredConsumer() | 
| 44 |                 { | 
| 45 |                         @Override protected void pushEvent(Event event) | 
| 46 |                         { | 
| 47 |                                 addRow(event.getAccount()); | 
| 48 |                         } | 
| 49 |                 }; | 
| 50 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 51 |   | 
| 52 |                 constraint = Constraints.eventType(EventType.EVT_MODIFY_ACCOUNT); | 
| 53 |                 filter = new ConstraintEventFilter(constraint); | 
| 54 |                 consumer = new PersistentDeferredConsumer() | 
| 55 |                 { | 
| 56 |                         @Override protected void pushEvent(Event event) | 
| 57 |                         { | 
| 58 |                                 setRow(findAccount(event.getWho()), event.getAccount()); | 
| 59 |                         } | 
| 60 |                 }; | 
| 61 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 62 |   | 
| 63 |                 constraint = Constraints.eventType(EventType.EVT_LOGIN, EventType.EVT_LOGOUT); | 
| 64 |                 filter = new ConstraintEventFilter(constraint); | 
| 65 |                 consumer = new PersistentDeferredConsumer() | 
| 66 |                 { | 
| 67 |                         @Override protected void pushEvent(Event event) | 
| 68 |                         { | 
| 69 |                                 int index = findAccount(event.getWho()); | 
| 70 |                                 fireRowsModified(index, index); | 
| 71 |                         } | 
| 72 |                 }; | 
| 73 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 74 |   | 
| 75 |                 constraint = Constraints.eventType(EventType.EVT_DESTROY_ACCOUNT); | 
| 76 |                 filter = new ConstraintEventFilter(constraint); | 
| 77 |                 consumer = new PersistentDeferredConsumer() | 
| 78 |                 { | 
| 79 |                         @Override protected void pushEvent(Event event) | 
| 80 |                         { | 
| 81 |                                 removeRow(findAccount(event.getWho())); | 
| 82 |                         } | 
| 83 |                 }; | 
| 84 |                 eventChannel.registerPushConsumer(priority, filter, consumer); | 
| 85 |         } | 
| 86 |          | 
| 87 |         private int        findAccount(int id) | 
| 88 |         { | 
| 89 |                 int index = 0; | 
| 90 |                 for (Account account: _items) | 
| 91 |                 { | 
| 92 |                         if (account.getId() == id) | 
| 93 |                         { | 
| 94 |                                 return index; | 
| 95 |                         } | 
| 96 |                         index++; | 
| 97 |                 } | 
| 98 |                 return -1; | 
| 99 |         } | 
| 100 |          | 
| 101 |         static private class AccountComparator implements Comparator<Account> | 
| 102 |         { | 
| 103 |                 public int        compare(Account a1, Account a2) | 
| 104 |                 { | 
| 105 |                         int diff = a1.getVisa().compareTo(a2.getVisa()); | 
| 106 |                         if (diff != 0) | 
| 107 |                         { | 
| 108 |                                 return diff; | 
| 109 |                         } | 
| 110 |                         return a1.getId() - a2.getId(); | 
| 111 |                 } | 
| 112 |         } | 
| 113 | } |