home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 5.4 KB | 220 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
- @(#)PageStack.java
-
- A container of Pages in which the pages are stacked on top of each
- other.
-
-
- Authors:
-
- rphall Rick Hall
-
-
- Version Ident:
-
- $Header$
-
-
- History:
-
- 04-Jan-1995 rphall Initial Creation
- 27-Mar-1996 Ted S. Added conversion from name to index.
- 28-Mar-1996 Ted S. Added getPage().
-
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.Page;
-
- import collections.Assertable;
- import collections.ImplementationCheckable;
- import collections.ImplementationError;
- import java.awt.CardLayout;
- import java.awt.Component;
- import java.awt.Panel;
-
- /**
- * A container of Pages in which the pages are stacked on top of each
- * other.
- * <P>
- * A PageStack maintains Pages in a sequence. The page sequence is set
- * by the order in which Pages are appended to a stack: the first page
- * appended to the stack is the first page in the sequence; the last page
- * appended to the stack is the last page in the sequence.
- * <P>
- * A PageStack has a cursor into the sequence of pages. The cursor is
- * called the current page. The page to which the cursor points is the page
- * that appears topmost to a user. Moving the current page cursor changes
- * the page that a user sees. The current page cursor may be moved
- * sequentially through a page stack by first, next, last, and previous
- * operations. The current page cursor may be moved to specific page by
- * the setCurrentPage operation.
- *
- * @see pj.awt.Page
- * @see pj.awt.Notebook
- * @version 0.00 04-Jan-96
- * @author rphall
- */
- public class PageStack extends Panel implements Assertable
- {
-
- // --- Instance variables
-
- protected CardLayout clLayout;
-
-
- // --- Public constructors
-
- /**
- * Construct a PageStack
- */
- public PageStack()
- {
- clLayout = new CardLayout();
- setLayout( clLayout );
- }
-
- // --- Public operations
-
- public void appendPage(Page page)
- {
- add(page.getName(),page);
- }
-
- public int countPages()
- {
- return countComponents();
- }
-
- public Page page()
- {
- // Current page is the only visible component
- Component c = null;
- for (int i=0; i<countComponents(); i++)
- {
- c = getComponent(i);
- if ( c.isVisible() )
- {
- assert(c instanceof Page);
- break;
- } // if visible
-
- } // for
-
- return (Page)c;
- } // page()
-
- public Page page(String name)
- {
- clLayout.show(this,name);
- return page();
- }
-
- public Page firstPage()
- {
- clLayout.first(this);
- return page();
- }
-
- public Page nextPage()
- {
- clLayout.next(this);
- return page();
- }
-
- public Page previousPage()
- {
- clLayout.previous(this);
- return page();
- }
-
- public Page lastPage()
- {
- clLayout.last(this);
- return page();
- }
-
-
- // Return a reference to the n'th page.
- // This index is 1 based.
- public Page getPage( int Index )
- {
- Component[] PageList = getComponents();
-
- if ( ( Index >= 1 ) && ( Index <= PageList.length ) )
- return ((Page)PageList[Index - 1]);
- else
- return null;
- }
-
- // Return the index ( 1 based ) of the current page.
- public int currPage()
- {
- return PageNameToIndex( page().getName() );
- }
-
- // Find the index of the page with the given name.
- // Index is 1 based.
- public int PageNameToIndex( String name )
- {
- int nRet = 0;
- Component[] PageList;
-
- // Step through all pages, until
- // I find the one with matching name.
- // Then I return it's index.
- PageList = getComponents();
-
- while ( ( name != ((Page)PageList[nRet]).getName() ) && ( nRet < ( PageList.length - 1 ) ) )
- {
- nRet ++;
- }
-
- // Index is 1 based.
- return nRet + 1;
- }
-
-
- /**
- * Raise an exception if predicate is false.
- * @see collections.Assertable
- */
- public void assert(boolean predicate) throws ImplementationError
- {
- ImplementationError.assert(this, predicate);
- }
-
- /**
- * Checks if all components of a PageStack are Pages.
- * @see collections.ImplementationCheckable
- * @exception ImplementationError Thrown if any component is
- * not a Page.
- */
- public void checkImplementation() throws ImplementationError
- {
- Component c = null;
- for (int i=0; i<countComponents(); i++)
- {
- c = getComponent(i);
- assert(c instanceof Page);
- } // for
-
- } // checkImplementation
-
-
-
- } // PageStack
-