home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 2.3 KB | 102 lines |
- // Page.java
- // 27.02.96
- //
- // This is my second try to do it right
- // a logical page, not a graphic object
-
- package cybcerone.utils;
-
- import java.util.Vector;
-
- /**
- * This class is used to store the elements displayed on a Paper object.
- * All the elements on a Page ought to be Paintable.
- */
- class Page {
- /* it can store references to the objects itself */
- private boolean growable = false;
- private int maxLines;
-
- /* or grab them out of a larger array */
- private int first;
- private int last;
- private PaintableVector theItems;
-
- /**
- * Creates a new Page capable of holding this
- * many lines (Paintable objects).
- */
- Page (int maxLines) {
- theItems = new PaintableVector (maxLines);
- this.first = 0;
- this.last = -1;
- this.maxLines = maxLines;
- growable = true; /* signals other methods that the
- entire vector can be used */
- }
-
- /**
- * Creates a new Page using the elements of this PaintableVector
- * between first and last (inclusive). Assumes first and last are
- * valid indices.
- */
- Page (PaintableVector theItems, int first, int last) {
- this.theItems = theItems;
- this.first = first;
- this.last = last;
- this.maxLines = (last - first + 1);
- }
-
- /**
- * Same as above, but uses the entire PaintableVector.
- */
- Page (PaintableVector theItems) {
- this (theItems, 0, theItems.size () - 1);
- }
-
- /**
- * Adds a new element to this page.
- * This only makes sense if the Page(int) constructor was used.
- * Returns true if it succeeded.
- */
- boolean addLine (Paintable newLine) {
- if (theItems.size() < maxLines) {
- theItems.addElement (newLine);
- last++;
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Returns the Paintable object on the specified line. Note: Line
- * counting begins with 0.
- */
- Paintable getLine (int index) {
- int location = first + index;
-
- if (location > last) {
- return null;
- } else {
- return theItems.elementAt (location);
- }
- }
-
- /**
- * Returns a PaintableVector of all the lines on this page.
- */
- PaintableVector getLines () {
- if (growable) {
- return theItems;
- } else {
- PaintableVector returnVector = new PaintableVector (maxLines);
- for (int i = first; i <= last; i++) {
- returnVector.addElement (theItems.elementAt (i));
- }
- return returnVector;
- }
- }
-
- }
-