| 1 package org.appfuse.webapp.action; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Collections ; 6 import java.util.Comparator ; 7 import java.util.List ; 8 import java.util.Set ; 9 10 import org.appfuse.model.User; 11 import org.appfuse.webapp.listener.UserCounterListener; 12 13 public class ActiveUserList extends BasePage implements Serializable { 14 private static final long serialVersionUID = -2725378005612769815L; 15 private String sort = "username"; 16 private boolean ascending = false; 17 18 public String getSort(){ 19 return sort; 20 } 21 22 public void setSort(String sort){ 23 this.sort = sort; 24 } 25 26 public boolean isAscending(){ 27 return ascending; 28 } 29 30 public void setAscending(boolean ascending){ 31 this.ascending = ascending; 32 } 33 34 public List getUsers() { 35 Set users = (Set ) getServletContext().getAttribute(UserCounterListener.USERS_KEY); 36 if (users != null) { 37 List usersAsList = new ArrayList (users); 38 39 Comparator comparator = new Comparator (){ 40 public int compare(Object o1, Object o2){ 41 User u1 = (User) o1; 42 User u2 = (User) o2; 43 if (sort == null){ 44 return 0; 45 } 46 if (sort.equals("username")){ 47 return !ascending ? u1.getUsername().toLowerCase() 48 .compareTo(u2.getUsername().toLowerCase()) 49 : u2.getUsername().toLowerCase() 50 .compareTo(u1.getUsername().toLowerCase()); 51 } 52 if (sort.equals("fullName")){ 53 return !ascending ? u1.getFullName().toLowerCase() 54 .compareTo(u2.getFullName().toLowerCase()) 55 : u2.getFullName().toLowerCase() 56 .compareTo(u1.getFullName().toLowerCase()); 57 } 58 return 0; 59 } 60 }; 61 Collections.sort(usersAsList, comparator); 62 return usersAsList; 63 } else { 64 return new ArrayList (); 65 } 66 } 67 } 68 | Popular Tags |