1 17 package org.jahia.utils.pagination; 18 19 20 import java.sql.SQLException ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 26 28 33 abstract public class PaginatableList extends ArrayList 34 { 35 protected int itemsPerPage; 36 37 41 public List getPage(Integer page) throws SQLException 42 { 43 int start = firstItem(page); 44 int finish = lastItem(page); 45 return subList(start,finish); 46 } 47 48 51 public String navigationBar(String linkText, Integer page) 52 throws SQLException 53 { 54 boolean prependDots = false; 55 boolean appendDots = false; 56 int pageNo = page.intValue(); 57 int pages = numberOfPages(); 58 int startPage = pageNo - 5; 59 if (startPage <= 0) startPage = 1; 60 if (startPage > 1) prependDots = true; 61 int endPage = pageNo + 4; 62 if (endPage < 10) endPage = 10; 63 if (endPage > pages) endPage = pages; 64 if (endPage < (pages-1)) appendDots = true; 65 66 String extra = appendQueryString(); 67 if (!extra.equals("")) { 68 if (linkText.lastIndexOf("?") == -1) { 69 linkText += "?"; 70 } else { 71 linkText += "&"; 72 } 73 linkText += extra; 74 } 75 76 StringBuffer bar = new StringBuffer (); 77 if (startPage != endPage) { 78 if (pageNo != 1) { 79 bar.append(makeLink(linkText, pageNo-1, "Prev")); 80 } else { 81 bar.append("Prev"); 82 } 83 bar.append(" | "); 84 85 if (prependDots) bar.append(" ... | "); 86 87 for (int i = startPage; i <= endPage; i++) { 88 String linkedText = i + " " + makeGuide(new Integer (i)); 89 90 if (i == pageNo) { 91 bar.append(linkedText); 92 } else { 93 bar.append(makeLink(linkText, i, linkedText)); 94 } 95 if (i < endPage) { 96 bar.append (" | "); 97 } 98 } 99 100 bar.append(" | "); 101 102 if (prependDots) bar.append(" ... | "); 103 104 if (pageNo != endPage) { 105 bar.append(makeLink(linkText, pageNo+1, "Next")); 106 } else { 107 bar.append("Next"); 108 } 109 } 110 return bar.toString(); 111 } 112 113 116 protected String makeLink(String linkText, int pageNo, String linkedText) 117 { 118 StringBuffer link = new StringBuffer (); 119 link.append("<a HREF=\"").append(linkText); 120 if (linkText.lastIndexOf("?") == -1) { 121 link.append("?"); 122 } else { 123 link.append("&"); 124 } 125 link.append("page=").append(pageNo).append("\">").append(linkedText); 126 link.append("</a>"); 127 return link.toString(); 128 } 129 130 136 public String makeGuide(Integer page) throws SQLException 137 { 138 return ""; 139 } 140 141 147 public String appendQueryString() 148 { 149 return ""; 150 } 151 152 155 public int numberOfItems() throws SQLException 156 { 157 return size(); 158 } 159 160 163 public int numberOfPages() throws SQLException 164 { 165 int entries = numberOfItems(); 166 int pages = entries / itemsPerPage; 167 int entriesOnExtraPage = entries % itemsPerPage; 168 if (entriesOnExtraPage != 0) pages++; 169 return pages; 170 } 171 172 177 public int itemsOnPage() throws SQLException 178 { 179 int itemsOnPage = itemsPerPage; 180 if (itemsOnPage == 0) { 181 itemsOnPage = numberOfItems(); 182 } 183 return itemsOnPage; 184 } 185 186 189 public int firstItem(Integer pageNo) throws SQLException 190 { 191 int itemsOnThisPage = itemsOnPage(); 192 193 int start = ((pageNo.intValue()-1) * itemsOnThisPage); 194 if (start >= numberOfItems()) { 195 start = 0; 196 } 197 return start; 198 } 199 200 203 public int lastItem(Integer pageNo) throws SQLException 204 { 205 int finish = firstItem(pageNo) + itemsOnPage(); 206 if (finish >= numberOfItems()) { 207 finish = numberOfItems(); 208 } 209 return finish; 210 } 211 212 } 213 | Popular Tags |