Posted By

d3developer on 07/24/10


Tagged


Versions (?)

@fractastical Paginator Controller


 / Published in: Other
 

  1. /* Joel Dietz
  2. [email protected]
  3. 03/26/2010
  4. */
  5.  
  6. public class TaskController {
  7.  
  8. public TaskPage activePage { get; set; }
  9. public List<TaskPage> pages { get; set; }
  10. public List<TaskPage> activePages { get; set; }
  11. public Integer activePagesStartPNumber { get; set; }
  12. public Integer activePagesEndPNumber { get; set; }
  13. public Boolean hasMorePagesAfter { get; set; }
  14. public Boolean hasMorePagesBefore { get; set; }
  15. public Integer pnum { get; set; }
  16. public Integer tasksPerPage { get; set; }
  17.  
  18. private static Integer pagesPerSetofActivePages = 10;
  19.  
  20. public TaskController() {
  21.  
  22. tasksPerPage = 10;
  23.  
  24. }
  25.  
  26. public void loadAssignedTasks() {
  27.  
  28. assignedTasks = null;
  29. updateAssignedTasks();
  30.  
  31. if (assignedTasks.size() > 0)
  32. {
  33. pages = populatePages(assignedTasks, tasksPerPage);
  34. pnum = 1;
  35. activePage = pages[0];
  36. pages[0].isActive = true;
  37. updateActivePages();
  38. }
  39. else
  40. {
  41. activePage = new taskPage(new List<ToDoItem__c>(),1);
  42. activePage.first = true;
  43. activePage.last = true;
  44. }
  45. }
  46.  
  47.  
  48. public List<TaskPage> populatePages(List<ToDoItem__c> tasks, Integer ItemsPerPage)
  49. {
  50. List<TaskPage> newPages = new List<TaskPage>();
  51. Integer tasksAdded = 0;
  52. Integer pagenumber = 0;
  53.  
  54. // Adds tasks to TaskPages until either all tasks are added or the page limit is reached
  55. while(tasksAdded < tasks.size())
  56. {
  57. pagenumber++;
  58. List<ToDoItem__c> tasksToAdd = new List<ToDoItem__c>();
  59.  
  60. for(Integer i=0; i < ItemsPerPage; i++) {
  61.  
  62. if(tasksAdded == tasks.size())
  63. break;
  64.  
  65. tasksToAdd.add(tasks[tasksAdded]);
  66. tasksAdded++;
  67.  
  68. }
  69.  
  70. newPages.add(new TaskPage(tasksToAdd,pagenumber));
  71. if(pagenumber == 1) {
  72. newPages[0].first = true;
  73. }
  74.  
  75. //DEPRECATED now that we have ActivePages
  76. //if(pagenumber==pagesPerSetofActivePages)
  77. // break;
  78.  
  79. }
  80.  
  81. newPages[pagenumber-1].last=true;
  82.  
  83. return newPages;
  84. }
  85.  
  86.  
  87. public void updateActivePages()
  88. {
  89. activePages = new List<TaskPage>();
  90. activePagesStartPNumber = pnum;
  91. Integer startIndex = pnum - 1;
  92. Integer endIndex = startIndex + pagesPerSetofActivePages;
  93.  
  94. for (Integer i= startIndex; i < pages.size() && i < endIndex; i++)
  95. activePages.add(pages[i]);
  96.  
  97. activePagesEndPNumber = activePages[activePages.size() - 1].pageNumber;
  98.  
  99. if (activePagesEndPNumber == pages.size())
  100. hasMorePagesAfter = false;
  101. else
  102. hasMorePagesAfter = true;
  103.  
  104. if (activePagesStartPNumber == 1)
  105. hasMorePagesBefore = false;
  106. else
  107. hasMorePagesBefore = true;
  108.  
  109. setActivePage();
  110.  
  111. }
  112.  
  113. public void updateActivePagesBackwards()
  114. {
  115. List<TaskPage> tempActivePages = new List<TaskPage>();
  116. activePages = new List<TaskPage>();
  117.  
  118. activePagesEndPNumber = pnum;
  119. Integer endIndex = pnum - 1;
  120. Integer startIndex = endIndex - pagesPerSetofActivePages - 1;
  121.  
  122. for (Integer i= endIndex; (i >= 0) && (i > startIndex); i--)
  123. tempActivePages.add(pages[i]);
  124.  
  125. for(Integer i = (tempActivePages.size() - 1); i >= 0; i--) {
  126. //System.debug('i:' + i);
  127. activePages.add(tempActivePages[i]);
  128. }
  129.  
  130. activePagesStartPNumber = activePages[0].pageNumber;
  131.  
  132. if (activePagesEndPNumber == pages.size())
  133. hasMorePagesAfter = false;
  134. else
  135. hasMorePagesAfter = true;
  136.  
  137. if (activePagesStartPNumber == 1)
  138. hasMorePagesBefore = false;
  139. else
  140. hasMorePagesBefore = true;
  141.  
  142. setActivePage();
  143.  
  144. }
  145.  
  146. public PageReference updatetasksPerPage()
  147. {
  148. pnum = 1;
  149. pages = populatePages(assignedTasks, tasksPerPage);
  150. activePage = pages[0];
  151. pages[0].isActive = true;
  152. updateActivePages();
  153.  
  154. return null;
  155. }
  156.  
  157. public void setActivePage()
  158. {
  159. Integer oldPN = activePage.pageNumber;
  160. activePage = pages[pnum - 1];
  161. pages[oldPN - 1].isActive = false;
  162. pages[pnum - 1].isActive = true;
  163. }
  164.  
  165.  
  166. public void updateActivePage()
  167. {
  168. setActivePage();
  169. if (pnum > activePagesEndPNumber)
  170. updateActivePages();
  171. if (pnum < activePagesStartPNumber)
  172. updateActivePagesBackwards();
  173. }
  174.  
  175. public void goToLast()
  176. {
  177. if (pages != null && pages.size() > 0)
  178. pnum = pages.size()-1;
  179. updateActivePage();
  180.  
  181. }
  182.  
  183. public List<SelectOption> gettasksPerPageSO()
  184. {
  185. List<SelectOption> bbpso = new List<SelectOption>();
  186. bbpso.add(new SelectOption('1','1'));
  187. bbpso.add(new SelectOption('2','2'));
  188. bbpso.add(new SelectOption('5','5'));
  189. bbpso.add(new SelectOption('10','10'));
  190. bbpso.add(new SelectOption('20','20'));
  191. bbpso.add(new SelectOption('50','50'));
  192. return bbpso;
  193. }
  194.  
  195. public void nextPage()
  196. {
  197. if (!activePage.last)
  198. pnum = activePage.pageNumber + 1;
  199. updateActivePage();
  200. }
  201.  
  202. public void prevPage()
  203. {
  204. if (!activePage.first)
  205. pnum = activePage.pageNumber - 1;
  206. updateActivePage();
  207. }
  208.  
  209.  
  210. class TaskPage
  211. {
  212.  
  213. public Boolean first {get; set;}
  214. public Boolean last {get; set;}
  215. public List <ToDoItem__c> pageTasks {get; set;}
  216. public Integer pageNumber {get; set;}
  217. public Boolean isActive {get; set;}
  218.  
  219. public TaskPage(List <ToDoItem__c> mi, Integer pn)
  220. {
  221. pageTasks = mi;
  222. pageNumber = pn;
  223. isActive = false;
  224. first = false;
  225. last = false;
  226. }
  227.  
  228. }
  229.  
  230. static testMethod void testTaskPages()
  231. {
  232. User tu = TestUtility.createTestUser();
  233. System.runAs(tu)
  234. {
  235. TaskController tc = new TaskController();
  236. List<ToDoItem__c> testTasks = new List<ToDoItem__c>();
  237. ToDoItem__c sampleTask;
  238. String descr = 'D' + math.rint(math.random() * 10000);
  239. for(Integer i=0; i < 41; i++)
  240. {
  241. sampleTask = new ToDoItem__c();
  242. sampleTask.name = 'Test Task ' + i;
  243. sampleTask.description__c = descr;
  244. sampleTask.assigned_to__c = tu.id;
  245. sampleTask.managed_by__c = tu.id;
  246. testTasks.add(sampleTask);
  247. }
  248. insert testTasks;
  249. tc.queryItemLimit = 41;
  250. tc.loadAssignedTasks();
  251. System.assertEquals(41, tc.getAssignedTasks().size());
  252. System.assertEquals(5, tc.ActivePages.size());
  253. System.assert(tc.ActivePage.first);
  254. System.assertEquals(false, tc.ActivePage.last);
  255. System.assertEquals(false, tc.hasMorePagesAfter);
  256. System.assertEquals(false, tc.hasMorePagesBefore);
  257. tc.tasksPerPage = 2;
  258. tc.updatetasksPerPage();
  259. System.assertEquals(21, tc.pages.size());
  260. tc.pnum = 15;
  261. tc.updateActivePage();
  262. System.assert(tc.hasMorePagesBefore);
  263. System.assertEquals(false,tc.hasMorePagesAfter);
  264. System.assertEquals(false, tc.ActivePage.last);
  265. System.assertEquals(false, tc.ActivePage.first);
  266. tc.pnum = 21;
  267. tc.updateActivePage();
  268. System.assert(tc.hasMorePagesBefore);
  269. System.assert(tc.ActivePage.last);
  270. System.assertEquals(false,tc.hasMorePagesAfter);
  271. System.assertEquals(false, tc.ActivePage.first);
  272. }
  273. }
  274.  
  275.  
  276. }

Report this snippet  

You need to login to post a comment.