1 33 34 package com.icesoft.icefaces.samples.showcase.components.table; 35 36 import com.icesoft.icefaces.samples.showcase.common.Person; 37 38 import java.util.Arrays ; 39 import java.util.Comparator ; 40 41 48 public class DataTablePaginatorBean extends SortableList { 49 50 52 53 public static Person[] buildPersonList() { 54 Person[] personsList = new Person[]{ 55 new Person("Mary", "Smith", "555-2629", 56 "mary.smith@icesoft.com"), 57 new Person("James", "Johnson", "555-3318", 58 "james.johnson@icesoft.com"), 59 new Person("Patricia", "Williams", "555-3702", 60 "patricia.williams@icesoft.com"), 61 new Person("John", "Jones", "555-6589", 62 "john.jones@icesoft.com"), 63 new Person("Linda", "Brown", "555-4736", 64 "linda.brown@icesoft.com"), 65 new Person("Robert", "Davis", "555-9732", 66 "robert.davis@icesoft.com"), 67 new Person("Barbara", "Miller", "555-4660", 68 "barbara.miller@icesoft.com"), 69 new Person("Michael", "Wilson", "555-1236", 70 "michael.wilson@icesoft.com"), 71 new Person("Elizabeth", "Moore", "555-6653", 72 "elizabeth.moore@icesoft.com"), 73 new Person("William", "Taylor", "555-1481", 74 "william.taylor@icesoft.com"), 75 new Person("David", "Garcia", "555-1717", 76 "david.garcia@icesoft.com"), 77 new Person("Maria", "Jackson", "555-8414", 78 "maria.jackson@icesoft.com"), 79 new Person("Richard", "White", "555-1887", 80 "richard.white@icesoft.com"), 81 new Person("Susan", "Harris", "555-9209", 82 "susan.harris@icesoft.com"), 83 new Person("Charles", "Thompson", "555-2040", 84 "charles.thompson@icesoft.com"), 85 new Person("Margaret", "Martinez", "555-9976", 86 "margaret.martinez@icesoft.com"), 87 new Person("Edward", "Phillips", "555-1325", 88 "edward.phillips@icesoft.com"), 89 }; 90 return personsList; 91 } 92 93 private Person[] persons = buildPersonList(); 94 95 private String paginatorLayout = "hor"; 96 97 100 public DataTablePaginatorBean() { 101 super("lastName"); 102 } 103 104 109 public String getPaginatorLayout() { 110 return paginatorLayout; 111 } 112 113 118 public void setPaginatorLayout(String paginatorLayout) { 119 this.paginatorLayout = paginatorLayout; 120 } 121 122 127 public boolean isVertical() { 128 return (paginatorLayout.equalsIgnoreCase("ver")); 129 } 130 131 136 public Person[] getPersons() { 137 return persons; 138 } 139 140 145 public Person[] getSortedPersons() { 146 sort(getSort(), isAscending()); 147 return persons; 148 } 149 150 156 protected boolean isDefaultAscending(String sortColumn) { 157 return true; 158 } 159 160 163 protected void sort(final String column, final boolean ascending) { 164 Comparator comparator = new Comparator () { 165 public int compare(Object o1, Object o2) { 166 Person c1 = (Person) o1; 167 Person c2 = (Person) o2; 168 if (column == null) { 169 return 0; 170 } 171 if (column.equals("firstName")) { 172 return ascending ? 173 c1.getFirstName().compareTo(c2.getFirstName()) : 174 c2.getFirstName().compareTo(c1.getFirstName()); 175 } else if (column.equals("lastName")) { 176 return ascending ? 177 c1.getLastName().compareTo(c2.getLastName()) : 178 c2.getLastName().compareTo(c1.getLastName()); 179 } else if (column.equals("phoneNo")) { 180 return ascending ? 181 c1.getPhoneNo().compareTo(c2.getPhoneNo()) : 182 c2.getPhoneNo().compareTo(c1.getPhoneNo()); 183 } else if (column.equals("email")) { 184 return ascending ? c1.getEmail().compareTo(c2.getEmail()) : 185 c2.getEmail().compareTo(c1.getEmail()); 186 } else return 0; 187 } 188 }; 189 Arrays.sort(persons, comparator); 190 } 191 192 195 public void addContent() { 196 if (persons.length < 300) { 197 int dataLength = persons.length; 198 Person[] newData = new Person[persons.length + 5]; 199 for (int i = 0, j = 0; i < newData.length; i++, j++) { 200 if (j >= dataLength) { 201 j = 0; 202 } 203 newData[i] = persons[j]; 204 } 205 persons = newData; 206 } 207 } 208 209 212 public void removeContent() { 213 214 if (persons.length > 5) { 215 int dataLength = persons.length; 216 217 Person[] newData = new Person[persons.length - 5]; 218 System.arraycopy(persons, 0, newData, 0, dataLength - 5); 220 persons = newData; 221 } 222 } 223 224 225 } | Popular Tags |