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

  1. // Page.java
  2. // 27.02.96
  3. //
  4. // This is my second try to do it right
  5. // a logical page, not a graphic object
  6.  
  7. package cybcerone.utils;
  8.  
  9. import java.util.Vector;
  10.  
  11. /**
  12.  * This class is used to store the elements displayed on a Paper object.
  13.  * All the elements on a Page ought to be Paintable.
  14.  */
  15. class Page {
  16.   /* it can store references to the objects itself */
  17.   private boolean growable = false;
  18.   private int maxLines;
  19.   
  20.   /* or grab them out of a larger array */
  21.   private int first;
  22.   private int last;
  23.   private PaintableVector theItems;
  24.   
  25.   /** 
  26.    * Creates a new Page capable of holding this 
  27.    * many lines (Paintable objects).
  28.    */
  29.   Page (int maxLines) {
  30.     theItems = new PaintableVector (maxLines);
  31.     this.first = 0;
  32.     this.last = -1;
  33.     this.maxLines = maxLines;
  34.     growable = true;  /* signals other methods that the 
  35.              entire vector can be used */
  36.   }
  37.  
  38.   /**
  39.    * Creates a new Page using the elements of this PaintableVector
  40.    * between first and last (inclusive).  Assumes first and last are
  41.    * valid indices.
  42.    */
  43.   Page (PaintableVector theItems, int first, int last) {
  44.     this.theItems = theItems;
  45.     this.first = first;
  46.     this.last = last;
  47.     this.maxLines = (last - first + 1);
  48.   }
  49.  
  50.   /**
  51.    * Same as above, but uses the entire PaintableVector.
  52.    */
  53.   Page (PaintableVector theItems) {
  54.     this (theItems, 0, theItems.size () - 1);
  55.   }
  56.  
  57.   /** 
  58.    * Adds a new element to this page.
  59.    * This only makes sense if the Page(int) constructor was used.
  60.    * Returns true if it succeeded.
  61.    */
  62.   boolean addLine (Paintable newLine) {
  63.     if (theItems.size() < maxLines) {
  64.       theItems.addElement (newLine);
  65.       last++;
  66.       return true;
  67.     } else {
  68.       return false;
  69.     }
  70.   }
  71.  
  72.   /** 
  73.    * Returns the Paintable object on the specified line.  Note: Line 
  74.    * counting begins with 0.
  75.    */
  76.   Paintable getLine (int index) {
  77.     int location = first + index;
  78.     
  79.     if (location > last) {
  80.       return null;
  81.     } else {
  82.       return theItems.elementAt (location);
  83.     }
  84.   }
  85.   
  86.   /**
  87.    * Returns a PaintableVector of all the lines on this page.
  88.    */
  89.   PaintableVector getLines () {
  90.     if (growable) {
  91.       return theItems;
  92.     } else {
  93.       PaintableVector returnVector = new PaintableVector (maxLines);
  94.       for (int i = first; i <= last; i++) {
  95.     returnVector.addElement (theItems.elementAt (i));
  96.       }
  97.       return returnVector;
  98.     }
  99.   }
  100.   
  101. }
  102.