home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / pagestack.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  5.4 KB  |  220 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)PageStack.java
  15.  
  16.         A container of Pages in which the pages are stacked on top of each
  17.         other.
  18.  
  19.  
  20.     Authors:
  21.  
  22.         rphall          Rick Hall
  23.  
  24.  
  25.     Version Ident:
  26.  
  27.         $Header$
  28.  
  29.  
  30.     History:
  31.  
  32.         04-Jan-1995 rphall  Initial Creation
  33.         27-Mar-1996    Ted S.    Added conversion from name to index.
  34.         28-Mar-1996    Ted S.    Added getPage().
  35.  
  36. ---------------------------------------------------------------------------*/
  37.  
  38. package pj.awt;
  39.  
  40. import pj.awt.Page;
  41.  
  42. import collections.Assertable;
  43. import collections.ImplementationCheckable;
  44. import collections.ImplementationError;
  45. import java.awt.CardLayout;
  46. import java.awt.Component;
  47. import java.awt.Panel;
  48.  
  49. /**
  50.  * A container of Pages in which the pages are stacked on top of each
  51.  * other.
  52.  * <P>
  53.  * A PageStack maintains Pages in a sequence.  The page sequence is set
  54.  * by the order in which Pages are appended to a stack: the first page
  55.  * appended to the stack is the first page in the sequence; the last page
  56.  * appended to the stack is the last page in the sequence.
  57.  * <P>
  58.  * A PageStack has a cursor into the sequence of pages.  The cursor is
  59.  * called the current page.  The page to which the cursor points is the page
  60.  * that appears topmost to a user.  Moving the current page cursor changes
  61.  * the page that a user sees.  The current page cursor may be moved
  62.  * sequentially through a page stack by first, next, last, and previous
  63.  * operations. The current page cursor may be moved to specific page by
  64.  * the setCurrentPage operation.
  65.  *
  66.  * @see     pj.awt.Page
  67.  * @see     pj.awt.Notebook
  68.  * @version 0.00 04-Jan-96
  69.  * @author  rphall
  70. */
  71. public class PageStack extends Panel implements Assertable
  72.     {
  73.  
  74.     // --- Instance variables
  75.  
  76.     protected CardLayout clLayout;
  77.         
  78.  
  79.     // --- Public constructors
  80.  
  81.     /**
  82.      * Construct a PageStack
  83.     */
  84.     public PageStack()
  85.         {
  86.         clLayout = new CardLayout();
  87.         setLayout( clLayout );
  88.         }
  89.  
  90.     // --- Public operations
  91.  
  92.     public void appendPage(Page page)
  93.         { 
  94.         add(page.getName(),page); 
  95.         }
  96.  
  97.     public int countPages()
  98.         { 
  99.         return countComponents(); 
  100.         }
  101.  
  102.     public Page page()
  103.         {
  104.         // Current page is the only visible component
  105.         Component c = null;
  106.         for (int i=0; i<countComponents(); i++)
  107.             {
  108.             c = getComponent(i);
  109.             if ( c.isVisible() )
  110.                 {
  111.                 assert(c instanceof Page);
  112.                 break;
  113.                 } // if visible
  114.  
  115.             } // for
  116.  
  117.         return (Page)c;
  118.         } // page()
  119.  
  120.     public Page page(String name)
  121.         {
  122.         clLayout.show(this,name);
  123.         return page();
  124.         }
  125.  
  126.     public Page firstPage()
  127.         {
  128.         clLayout.first(this);
  129.         return page();
  130.         }
  131.  
  132.     public Page nextPage()
  133.         {
  134.         clLayout.next(this);
  135.         return page();
  136.         }
  137.  
  138.     public Page previousPage()
  139.         {
  140.         clLayout.previous(this);
  141.         return page();
  142.         }
  143.  
  144.     public Page lastPage()
  145.         {
  146.         clLayout.last(this);
  147.         return page();
  148.         }
  149.  
  150.  
  151.     // Return a reference to the n'th page.
  152.     // This index is 1 based.
  153.     public Page getPage( int Index )
  154.         {
  155.         Component[] PageList = getComponents();
  156.  
  157.         if ( ( Index >= 1 ) && ( Index <= PageList.length ) )
  158.             return ((Page)PageList[Index - 1]);
  159.         else
  160.             return null;
  161.         }
  162.  
  163.     // Return the index (  1 based ) of the current page.    
  164.     public int currPage()
  165.         {
  166.         return PageNameToIndex( page().getName() );
  167.         }
  168.  
  169.     // Find the index of the page with the given name.
  170.     // Index is 1 based.
  171.     public int PageNameToIndex( String name )
  172.         {
  173.         int nRet = 0;
  174.         Component[] PageList;
  175.  
  176.         // Step through all pages, until
  177.         // I find the one with matching name.
  178.         // Then I return it's index.
  179.         PageList = getComponents();
  180.  
  181.         while ( ( name != ((Page)PageList[nRet]).getName() ) && ( nRet < ( PageList.length - 1 ) ) )
  182.             {
  183.             nRet ++;
  184.             }
  185.             
  186.         // Index is 1 based.        
  187.         return nRet + 1;
  188.         }
  189.  
  190.  
  191.     /**
  192.      * Raise an exception if predicate is false.
  193.      * @see collections.Assertable
  194.     */
  195.     public void assert(boolean predicate) throws ImplementationError
  196.         { 
  197.         ImplementationError.assert(this, predicate); 
  198.         }
  199.  
  200.     /**
  201.      * Checks if all components of a PageStack are Pages.
  202.      * @see collections.ImplementationCheckable
  203.      * @exception ImplementationError Thrown if any component is
  204.      * not a Page.
  205.     */
  206.     public void checkImplementation() throws ImplementationError
  207.         {
  208.         Component c = null;
  209.         for (int i=0; i<countComponents(); i++)
  210.             {
  211.             c = getComponent(i);
  212.             assert(c instanceof Page);
  213.             } // for   
  214.  
  215.         } // checkImplementation
  216.  
  217.     
  218.  
  219.     } // PageStack
  220.