1 19 20 package com.sslexplorer.table; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.regex.Matcher ; 28 import java.util.regex.Pattern ; 29 30 import com.sslexplorer.boot.Util; 31 32 42 public class Pager { 43 44 private TableItemModel model; 45 private int pageSize; 46 private int startRow; 47 private String sortName; 48 private boolean sortReverse; 49 private List filteredList; 50 private boolean sorts = true; 51 52 57 public Pager(TableItemModel model) { 58 super(); 59 this.model = model; 60 pageSize = 10; 61 startRow = 0; 62 filteredList = null; 63 } 64 65 70 public boolean isSorts() { 71 return sorts; 72 } 73 74 79 public void setSorts(boolean sorts) { 80 this.sorts = sorts; 81 } 82 83 88 public int getFilteredRowCount() { 89 return filteredList == null ? 0 : filteredList.size(); 90 } 91 92 98 public TableItem getFilteredItem(int row) { 99 return (TableItem) filteredList.get(row); 100 } 101 102 107 public boolean getEmpty() { 108 return getFilteredRowCount() == 0; 109 } 110 111 116 public void setPageSize(int pageSize) { 117 this.pageSize = pageSize; 118 } 119 120 125 public int getPageSize() { 126 return pageSize; 127 } 128 129 134 public Iterator getPageItems() { 135 return new ItemIterator(startRow); 136 } 137 138 143 public boolean getHasNextPage() { 144 boolean hasNextPage = (startRow + calcPageSize()) < getFilteredRowCount(); 145 return hasNextPage; 146 } 147 148 153 public boolean getHasPreviousPage() { 154 boolean hasNextPage = (startRow - calcPageSize()) >= 0; 155 return hasNextPage; 156 } 157 158 163 public int getStartRow() { 164 return startRow; 165 } 166 167 172 public void setStartRow(int startRow) { 173 this.startRow = startRow; 174 } 175 176 179 public void nextPage() { 180 if (!getHasNextPage()) { 181 throw new IllegalArgumentException ("No more pages."); 182 } 183 setStartRow(startRow + calcPageSize()); 184 } 185 186 189 public void previousPage() { 190 if (!getHasPreviousPage()) { 191 throw new IllegalArgumentException ("No more pages."); 192 } 193 setStartRow(startRow - calcPageSize()); 194 } 195 196 201 public TableItemModel getModel() { 202 return model; 203 } 204 205 210 public String getSortName() { 211 return sortName; 212 } 213 214 219 public boolean getSortReverse() { 220 return sortReverse; 221 } 222 223 229 public void setSortName(String sortName) { 230 this.sortName = sortName; 231 } 232 233 239 public void setSortReverse(boolean sortReverse) { 240 this.sortReverse = sortReverse; 241 } 242 243 249 public void rebuild(String filterText) { 250 if (filterText == null || filterText.equals("") || filterText.equals("*")) { 251 filteredList = new ArrayList (model.getItems()); 252 } else { 253 filteredList = new ArrayList (); 254 for (Iterator i = model.getItems().iterator(); i.hasNext();) { 255 TableItem ti = (TableItem) i.next(); 256 if (matchedFilter(ti, filterText)) { 257 filteredList.add(ti); 258 } 259 } 260 } 261 if (isSorts()) { 262 if (sortName == null || sortName.equals("")) { 263 sortName = model.getId() + "." + model.getColumnName(0); 264 } 265 if (sortName != null && !sortName.equals("")) { 266 for (int i = model.getColumnCount() - 1; i >= 0; i--) { 267 if (model.getColumnName(i).equals(sortName)) { 268 Collections.sort(filteredList, new TableItemComparator(i)); 269 return; 270 } 271 } 272 } 273 } 274 } 275 276 279 public void firstPage() { 280 setStartRow(0); 281 } 282 283 286 public void lastPage() { 287 int lastPageRow = getFilteredRowCount() - calcPageSize(); 288 setStartRow(lastPageRow < 0 ? 0 : lastPageRow); 289 } 290 291 297 public int calcPageSize() { 298 return (pageSize == 0 ? getFilteredRowCount() : pageSize); 299 } 300 301 boolean matchedFilter(TableItem item, String filterText) { 302 if (filterText == null || filterText.equals("")) { 303 return true; 304 } else { 305 if (filterText.startsWith("!")) { 306 filterText = filterText.substring(1); 307 Pattern p = Pattern.compile(Util.parseSimplePatternToRegExp(filterText), Pattern.CASE_INSENSITIVE); 308 for (int i = 0; i < model.getColumnCount(); i++) { 309 Object cv = item.getColumnValue(i); 310 String val = cv == null ? "" : cv.toString(); 311 Matcher matcher = p.matcher(val); 312 if (matcher.matches()) { 313 return false; 314 } 315 } 316 } else { 317 Pattern p = Pattern.compile(Util.parseSimplePatternToRegExp(filterText), Pattern.CASE_INSENSITIVE); 318 for (int i = 0; i < model.getColumnCount(); i++) { 319 Object cv = item.getColumnValue(i); 320 String val = cv == null ? "" : cv.toString(); 321 Matcher matcher = p.matcher(val); 322 if (matcher.matches()) { 323 return true; 324 } 325 } 326 } 327 } 328 return false; 329 } 330 331 class TableItemComparator implements Comparator { 332 333 int col; 334 335 TableItemComparator(int col) { 336 this.col = col; 337 } 338 339 public int compare(Object arg0, Object arg1) { 340 return (sortReverse ? -1 : 1) 341 * ((Comparable ) ((TableItem) arg0).getColumnValue(col)).compareTo((Comparable ) ((TableItem) arg1).getColumnValue(col)); 342 } 343 } 344 345 class ItemIterator implements Iterator { 346 347 int idx; 348 int end; 349 350 ItemIterator(int idx) { 351 this.idx = idx; 352 this.end = idx + calcPageSize(); 353 } 354 355 public boolean hasNext() { 356 return idx < getFilteredRowCount() && idx < end; 357 } 358 359 public Object next() { 360 return filteredList.get(idx++); 361 } 362 363 public void remove() { 364 } 365 366 } 367 } 368 | Popular Tags |