skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Tuesday, September 18, 2012

Page Navigator in Java

Posted by Admin at 2:27 PM – 0 comments
 
This component is used for navigating through records in cases where the results set is huge and records are displayed as set of records of some predefined size.

Features
- Easy navigation of records
- User can configure the no of records per page

public class PageNavigator {


    int min =1, max,total , recSize;

    /**
  * This is the constructor
  * @param sRecSize the numer of records to display in a page.
     */

     public PageNavigator(int sRecSize)
      {
     this.recSize = sRecSize;
     this.max= sRecSize;
       }


    /**
     * function to decrement the records set when the 'Next' button is clicked
     */

    public void changeNextValue()
    {

         if(total==0)
         return;
         max=max+recSize;
         min =max-(recSize-1);
         if (max >= total)
             {
                max=total;
                min=(max/recSize)*recSize +1;
             }

         if(min>total)
            {
            min =max-(recSize-1);
            }



       }

    /**
     * function to decrement the records set when the 'Prev' button is clicked
     */

        public void changePrevValue() {
        int test = max;
        if(total==0) return;

        max=max-recSize;
        min=min-recSize;

        if (min< 0)
        {
         min=1;
         }


         if(test%recSize !=0 )
         {
             max=test-(test%recSize);


         }
            if(max >total )
                max=total;
            if( max <=0 )   max=recSize;
            if( total <recSize )
            max=total;


        }

    /**
     * function to get the first set of records or the First page
     */
     public void changeToFirstPage()
     {

         if(total==0) { min=max=0;return; }
         min = 1 ;
         max=recSize;
         if (total<= recSize && total >0)
         {
          max=total;

          }

         }

    /**
     * function to get the last set of records or the Last page
     */
     public void changeToLastPage()
     {

         if(total==0) { min=max=0;return; }

         max=total;
         int pageNo = total/recSize;
         min  =   pageNo*recSize +1 ;
         if (total<= recSize)
         {
          min=1;
         }



      }
    /**
     * Retrieve the total count of records based on the parameters.Function gets the generated query and updates the value of variable 'total' to the number of records.
     *
     * @param  selectStmt List the colums to be retrieved from table.
     * @param  tableName List the table to select the value from.
     * @param  isWhereClausePresent Determin if a Where clause exist for the query.
     * @param  WhereClause   The dynamically constructed query string to append to the where clause
     *
     */



    public void getTotalCount(String selectStmt,String tableName , boolean isWhereClausePresent,String WhereClause)
    {
           if(isWhereClausePresent)
           {
           String query = "select "+selectStmt+ " from "+tableName +"Where " +WhereClause;
           }
           else
           {
               String query = "select "+selectStmt+ " from "+tableName + "";
           }


           //Run the query and get the total count


           if(total<recSize && total>0)
           {
           min = 1;
           max= total;
           }
           if(total==0) min =0;max =0;

        }


   /**
  * Retrieve records based on the parameters . Function executes the generated query and retrieve the results
  *
  * @param  selectStmt List the colums to be retrieved from table.
  * @param  tableName  List the table to select the value from.
  * @param  isWhereClausePresent  Determin if a Where clause exist for the query.
  * @param  WhereClause   The dynamically constructed query string to append to the where clause
  *
  *
  */


       public void getRecordSet(String selectStmt,String tableName , boolean isWhereClausePresent,String WhereClause)
       {
   if(isWhereClausePresent)
           {
         String query = "SELECT * FROM (select " +selectStmt +",ROWNUM ROWIDX from " +tableName+ " WHERE "+WhereClause +"  ) WHERE  ROWIDX >=" +min+" AND ROWIDX <="+max;
         }
         else
         {
     String query = "SELECT * FROM (select " +selectStmt +",ROWNUM ROWIDX from " +tableName+ " ) WHERE  ROWIDX >=" +min+" AND ROWIDX <="+max;
      }

       }



  /**
   * Determine which navation to take from any of these : 'First','Prev','Next','Last'
   *
   * @param  buttonType determine which page to navigate to.
   *
   */

    public void buttonAction(int buttonType)
    {

  switch (buttonType)
  {
    case 1: changeToFirstPage();getRecordSet("*" ,"testTable" , true , "age>30"); break;
    case 2: changePrevValue();getRecordSet("*" ,"testTable" , true , "age>30"); break;
    case 3: changeNextValue();getRecordSet("*" ,"testTable" , true , "age>30"); break;
    case 4: changeToLastPage();getRecordSet("*" ,"testTable" , true , "age>30"); break;

  }

    }

       /*
        * The  number of records to display in a page can be made configurable.
        *
        */

    public static void main(String[] args) {

        int pageSize =100; //used to set the records per page

        PageNavigator pageNavigator = new PageNavigator(pageSize);

        pageNavigator.getTotalCount("*" ,"userdetails" , true , "age>30"); //Specify the fileds to be selected , the table  and the where Clause if any.
        pageNavigator.getRecordSet("*" ,"testTable" , true , "age>30");
            //The navigation  First ,Prev , Next ,Last can be some actions/ button /links in the page
        pageNavigator.buttonAction(3); //Calling Next  to return next set of records

        }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger