← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3264: Simplified class Paging

 

Merge authors:
  Lars Helge Øverland (larshelge)
------------------------------------------------------------
revno: 3264 [merge]
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2011-04-02 14:11:25 +0200
message:
  Simplified class Paging
modified:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/paging/Paging.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/paging/Paging.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/paging/Paging.java	2011-03-22 12:25:49 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/paging/Paging.java	2011-04-02 12:10:56 +0000
@@ -32,7 +32,9 @@
  */
 public class Paging
 {
-    static final int MAX_ALLOWED_PAGE_SIZE = 50;
+    private static final int DEFAULT_PAGE_SIZE = 50;
+    private static final int PAGE_OFFSET = 2; // Each side of current page
+    private static final int PAGE_TOTAL_OFFSET = PAGE_OFFSET * 2; // Both sides of current page
 
     private int currentPage;
 
@@ -64,34 +66,44 @@
         return  total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
     }
 
+    /**
+     * Returns first page in paging range.
+     */
     public int getStartPage()
     {
         int startPage = 1;
         
-        if ( currentPage > 2 )
-        {
-            startPage = currentPage - 2;
-            
-            if ( getNumberOfPages() - startPage < 4 )
-            {
-                startPage = getNumberOfPages() - 4;
-                
-                if ( startPage <= 0 )
-                {
-                    startPage = 1;
-                }
-            }
-        }
+        if ( currentPage > PAGE_OFFSET ) // Far enough from start, set start page
+        {
+            startPage = currentPage - PAGE_OFFSET;
+        }
+        
+        if ( ( getNumberOfPages() - startPage ) < PAGE_TOTAL_OFFSET ) // Too close to end, decrease start page to maintain page range length
+        {
+            startPage = getNumberOfPages() - PAGE_TOTAL_OFFSET;            
+        }
+
+        if ( startPage <= 0 ) // Cannnot be 0 or less, set start page to 1
+        {
+            startPage = 1;
+        }
+        
         return startPage;
     }
 
+    /**
+     * Returns first row number in paged table for current page.
+     */
     public int getStartPos()
     {
-        int startPos = currentPage <= 0 ? 0 : (currentPage - 1) * pageSize;
+        int startPos = currentPage <= 0 ? 0 : ( currentPage - 1 ) * pageSize;
         startPos = ( startPos >  total ) ? total : startPos;
         return startPos;
     }
 
+    /**
+     * Returns last row number in paged table for current page.
+     */
     public int getEndPos()
     {
     	int endPos = getStartPos() + pageSize;
@@ -121,7 +133,7 @@
 
     public void setPageSize( int pageSize )
     {
-        this.pageSize = pageSize > 0 ? pageSize : Paging.MAX_ALLOWED_PAGE_SIZE;
+        this.pageSize = pageSize > 0 ? pageSize : Paging.DEFAULT_PAGE_SIZE;
     }
 
     public int getTotal()