home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / utils / pages.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.5 KB  |  118 lines

  1. // Pages.java
  2. // 27.02.96
  3. //
  4. // implements multiple Page objects
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.util.Vector;
  9.  
  10. /**
  11.  * This class is used to store multiple Page objects in a single
  12.  * object.  Better than a PageVector because it lets you easily 
  13.  * create and flip through those pages.
  14.  *
  15.  * @see Page
  16.  */
  17. class Pages {
  18.   
  19.   private PageVector pages = new PageVector ();
  20.   private int maxLines;
  21.   private boolean growable = false;
  22.  
  23.   private int currentPage = 0;
  24.  
  25.   /**
  26.    * all new pages created will be of the specified maximum length
  27.    */
  28.   Pages (int maxLines) {
  29.     this.maxLines = maxLines;
  30.     pages.addElement (new Page (maxLines));
  31.     growable = true;
  32.   }
  33.  
  34.   /**
  35.    * Creates as many Page objects as necessary, each of maximum size
  36.    * maxLines, to hold this PaintableVector
  37.    */
  38.   Pages (PaintableVector theItems, int maxLines) {
  39.     this (theItems, 0, theItems.size () - 1, maxLines);
  40.   }
  41.  
  42.   /** 
  43.    * Same as above, but lets you specify first and last elements (inclusive).
  44.    * Assumes first and last are valid indices.
  45.    */
  46.   Pages (PaintableVector theItems, int first, int last, int maxLines) {
  47.     this.maxLines = maxLines;
  48.  
  49.     for (int remaining = last - first + 1;
  50.      remaining > maxLines; 
  51.      remaining -= maxLines) {
  52.       pages.addElement (new Page (theItems, first, first + maxLines - 1));
  53.       first += maxLines;
  54.     }
  55.     
  56.     pages.addElement (new Page (theItems, first, last));
  57.   }
  58.  
  59.   /**
  60.    * Adds a new element.  If a new Page must be created, so be it.
  61.    */
  62.   boolean addLine (Paintable newLine) {
  63.     if (growable == true) {
  64.       if (!pages.lastElement().addLine (newLine)) {
  65.     pages.addElement (new Page (maxLines));
  66.     pages.lastElement().addLine (newLine);
  67.       }
  68.       return true;
  69.     } else {
  70.       return false;
  71.     }
  72.   }
  73.  
  74.   /**
  75.    * Returns the current page, by default the first one.
  76.    */
  77.   Page getCurrentPage () {
  78.     return pages.elementAt (currentPage);
  79.   }
  80.  
  81.   /**
  82.    * Checks to see if there is a previous page.
  83.    */
  84.   boolean hasPreviousPage () {
  85.     return (currentPage != 0);
  86.   }
  87.  
  88.   /**
  89.    * Checks to see if there is a next page.
  90.    */
  91.   boolean hasNextPage () {
  92.     return (currentPage < pages.size () - 1);
  93.   }
  94.  
  95.   /**
  96.    * Flips to the previous page and returns that page.
  97.    */
  98.   Page flipToPrevious () {
  99.     if (hasPreviousPage ())
  100.       currentPage--;
  101.     return getCurrentPage ();
  102.   }
  103.  
  104.   /**
  105.    * Flips to the next page and returns it.
  106.    */
  107.   Page flipToNext () {
  108.     if (hasNextPage ())
  109.       currentPage++;
  110.     return getCurrentPage ();
  111.   }
  112.  
  113. }
  114.  
  115.  
  116.     
  117.  
  118.