View Javadoc

1   package org.appfuse.webapp.pages.admin;
2   
3   import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
4   import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
5   import org.apache.wicket.model.IModel;
6   import org.apache.wicket.model.LoadableDetachableModel;
7   import org.appfuse.model.User;
8   
9   import java.util.Collections;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  import static org.appfuse.webapp.util.NumberRangeUtil.checkIfLongWithinIntegerRange;
14  
15  public class StaticUserDataProvider extends SortableDataProvider<User, String> {
16  
17      private final List<User> users;
18  
19      public StaticUserDataProvider(List<User> users) {
20          this.users = users;
21          setSort("username", SortOrder.ASCENDING);
22      }
23  
24      @Override
25      public Iterator<? extends User> iterator(long first, long count) {
26          Collections.sort(users, getSortableUserDataProviderComparator());
27  
28          checkIfLongWithinIntegerRange(first, count, first + count);
29          return users.subList((int)first, (int)first + (int)count).iterator();
30      }
31  
32      private SortableDataProviderComparator<User> getSortableUserDataProviderComparator() {
33          return SortableDataProviderComparator.getInstanceForSortParam(getSort());
34      }
35  
36      @Override
37      public long size() {
38          return users.size();
39      }
40  
41      @Override
42      public IModel<User> model(final User user) {
43          //TODO: MZA: Verification needed
44          return new LoadableDetachableModel<User>(user) {
45              @Override
46              protected User load() {
47                  return users.get(users.indexOf(user));
48              }
49          };
50      }
51  }