Posted By

d3developer on 08/06/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

terrerouge


@fractastical Paginator Controller


 / Published in: Other
 

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

Report this snippet  

You need to login to post a comment.