home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / io / papersection.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  10.1 KB  |  336 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.  
  15.     @(#)PaperSection.java   0.00 20-Dec-95
  16.  
  17.  
  18.         A group of stories in a Personal Journal paper all sharing the
  19.         the same section and subsection attribute.  Sections are observable,
  20.         and in turn observe other sections.
  21.  
  22.  
  23.     Authors:
  24.  
  25.         rphall      Rick Hall
  26.         jlee        James Lee
  27.  
  28.  
  29.     Version Ident:
  30.  
  31.         $Header: /PjJavaClient/src/pj/io/PaperSection.java 2     1/21/96 5:27p Rphall $
  32.  
  33.  
  34.     History:
  35.  
  36.         20-Dec-95    rphall      Initial Creation
  37.         27-Dec-95    rphall      Made observerable
  38.         12-Mar-96    jlee        Modified so that multiple stories can be shown as one story
  39.         21-Mar-96    jlee        Added DownloadProgressNotification processing
  40.  
  41. ---------------------------------------------------------------------------*/
  42.  
  43. package pj.io;
  44.  
  45. import pj.io.DownloadProgressNotification;
  46. import pj.io.PaperStory;
  47. import pj.io.StoryContainer;
  48. import pj.io.StoryContainerNotification;
  49.  
  50. import collections.Assertable;
  51. import collections.ImplementationCheckable;
  52. import collections.ImplementationError;
  53. import java.util.NoSuchElementException;
  54. import java.util.Observable;
  55. import java.util.Observer;
  56. import java.util.Vector;
  57.  
  58. /**
  59.  * A group of stories in a Personal Journal paper all sharing the
  60.  * the same section and subsection attribute.
  61.  * @version 0.00, 20-Dec-95
  62.  * @author      Rick Hall
  63.  */
  64. public class PaperSection extends Observable
  65.     implements Assertable, ImplementationCheckable, StatefulStoryContainer,
  66.         Observer
  67.     {
  68.  
  69.     // --- Class variables
  70.  
  71.     public static final String typeArticle = "900";
  72.     public static final String typeChart   = "901";
  73.     public static final String typeDjia    = "902";
  74.     public static final String typeAd      = "904";
  75.  
  76.     // --- Instance variables
  77.     private DownloadProgressNotification dpnProgress;
  78.  
  79.     private final int[] statesAllowed =
  80.         {
  81.         stateQuiescent, stateAdding, stateAdded, stateClearing, stateCleared
  82.         };
  83.  
  84.     private String strSection;    // Section id of stories in this container
  85.     private String strSubsect;    // Subsection id of stories in this container
  86.     private String strType;       // Type of stories in this container
  87.     private Vector vStory;        // Implementation of this container
  88.     private int    idxCurrent;    // A cursor into stories
  89.     private int    state;         // See StatefulStoryContainer for values
  90.  
  91.     // --- Public constructors
  92.  
  93.     PaperSection (
  94.         String section, // Section id of this container
  95.         String subsect, // Subsection id of this container
  96.         String type)    // Type of stories this container will hold
  97.         throws ImplementationError
  98.         {
  99.         assert( section != null );
  100.         assert( subsect != null );
  101.         assert( type != null );
  102.  
  103.         strSection = section;
  104.         strSubsect = subsect;
  105.         strType = type;
  106.         dpnProgress = new DownloadProgressNotification(this, strSubsect, stateAdding);
  107.  
  108.         vStory = new Vector();
  109.         idxCurrent = 0;
  110.         state = stateQuiescent;
  111.         clearChanged();
  112.         }
  113.  
  114.     // --- Public operations
  115.     public void assert(boolean predicate)
  116.         throws ImplementationError
  117.         { 
  118.         ImplementationError.assert(this, predicate); 
  119.         }
  120.  
  121.     public void checkImplementation()
  122.         throws ImplementationError
  123.         {
  124.         assert( idxCurrent >= 0 );
  125.         if (vStory.size() > 0)
  126.             assert( idxCurrent < vStory.size() );
  127.         for (int i=0; i<vStory.size(); i++)
  128.             assert( vStory.elementAt(i) instanceof PaperStory );
  129.         }
  130.  
  131.     public String getId()
  132.         { 
  133.         return strSubsect; 
  134.         }
  135.  
  136.     public final int numStories()
  137.         { 
  138.         return vStory.size(); 
  139.         }
  140.  
  141.     public void addStory( PaperStory ps)
  142.         throws MalformedPaperStoryException, IncompatiblePaperStoryException,
  143.             IllegalArgumentException
  144.         {
  145.         if (ps == null)
  146.             throw new IllegalArgumentException();
  147.  
  148.         if ( !strSection.equals(ps.getSection()) )
  149.             throw new IncompatiblePaperStoryException();
  150.  
  151.         if ( !strSubsect.equals(ps.getSubsect()) )
  152.             throw new IncompatiblePaperStoryException();
  153.  
  154.         // Add story ...
  155.         vStory.addElement(ps);
  156.  
  157.         // ... then notify observers
  158.         try {
  159.             System.out.println("Debug-PaperSection-addStory: strSubsect=" + strSubsect);
  160.             setState(stateAdding);
  161.  
  162.             // Send download progress observer notification.
  163.             setChanged();
  164.             notifyObservers( dpnProgress );
  165.             }
  166.         catch (IllegalArgumentException e)
  167.             {
  168.             }
  169.  
  170.         } // addStory
  171.  
  172.     public final synchronized void removeStories()
  173.         {
  174.         try { 
  175.             setState(stateClearing); 
  176.             } 
  177.         catch (IllegalArgumentException e) 
  178.             {
  179.             }
  180.  
  181.         vStory.removeAllElements();
  182.         }
  183.  
  184.     public synchronized void update(Observable o, Object arg)
  185.         {
  186.         if ( !(arg instanceof StoryContainerNotification) )
  187.             return;
  188.  
  189.         StoryContainerNotification scnTemp = (StoryContainerNotification) arg;
  190.  
  191.         if ( scnTemp.source == this )
  192.             return;
  193.  
  194.         if ( scnTemp.newState == stateAdding && this.state == stateAdding )
  195.             {
  196.             try { 
  197.                 setState(stateAdded); 
  198.                 }
  199.             catch(IllegalArgumentException e)
  200.                 {
  201.                 }
  202.             }
  203.  
  204.         } // update
  205.  
  206.     public synchronized final int getState()
  207.         { 
  208.         return state; 
  209.         }
  210.  
  211.     
  212.     public final synchronized Vector getAllStories()
  213.         {
  214.         return vStory;
  215.         }
  216.  
  217.     public final boolean doesSectionDisplaysMultipleStories()
  218.         {
  219.         if ( strSubsect.equals(Paper.idFPWorld)  || strSubsect.equals(Paper.idFPBusFin) ||
  220.              strSubsect.equals(Paper.idSpHilite) || strSubsect.equals(Paper.idTJIWorld) ||
  221.              strSubsect.equals(Paper.idTJIBusFin)|| strSubsect.equals(Paper.idSpScores) )
  222.             return true;
  223.         else
  224.             return false;
  225.         }
  226.  
  227.     public final synchronized PaperStory firstStory()
  228.         throws NoSuchElementException
  229.         {
  230.         System.out.println("Debug-PaperSection-firstStory:");
  231.         return getStoryAndChangeCurrent(0);
  232.         }
  233.  
  234.     public final synchronized PaperStory nextStory()
  235.         throws NoSuchElementException
  236.         {
  237.         System.out.println("Debug-PaperSection-nextStory:");
  238.         return getStoryAndChangeCurrent( ( (idxCurrent+1) >= vStory.size() ? idxCurrent: (idxCurrent+1) ) ); 
  239.         }//jlee
  240.  
  241.     public final synchronized PaperStory lastStory()
  242.         throws NoSuchElementException
  243.         {
  244.         System.out.println("Debug-PaperSection-lastStory:");
  245.         return getStoryAndChangeCurrent(vStory.size()-1); 
  246.         }
  247.  
  248.     public final synchronized PaperStory previousStory()
  249.         throws NoSuchElementException
  250.         {
  251.         System.out.println("Debug-PaperSection-previousStory:");
  252.         return getStoryAndChangeCurrent( ( (idxCurrent-1) < 0 ? 0: (idxCurrent-1) ) ); 
  253.         }//jlee
  254.  
  255.     public final synchronized PaperStory currentStory()
  256.         throws NoSuchElementException
  257.         {
  258.         System.out.println("Debug-PaperSection-currentStory:");
  259.         return getStoryAndChangeCurrent(idxCurrent); 
  260.         }
  261.  
  262.     /**
  263.      * Return the story with the given index and set the current
  264.      * story to be this story.
  265.      * @param idx An index value in the range 0 - (numStories() - 1)
  266.      * @return A story matching the index value
  267.      * @exception NoSuchElementException thrown if idx is out of range
  268.     */
  269.     public final synchronized PaperStory story(int idx)
  270.         throws NoSuchElementException
  271.         { 
  272.         return getStoryAndChangeCurrent(idx); 
  273.         }
  274.  
  275.     public String toString()
  276.         {
  277.         checkImplementation();
  278.         String temp;
  279.         if (vStory.size() > 0)
  280.             {
  281.             temp = vStory.elementAt(0).toString();
  282.             for (int i=1; i<vStory.size(); i++)
  283.                 temp += vStory.elementAt(i).toString();
  284.             }
  285.         else
  286.             temp = "";
  287.  
  288.         return temp;
  289.         }
  290.  
  291.     // ---Private operations
  292.  
  293.     private synchronized void setState( int s ) throws IllegalArgumentException
  294.         {
  295.         boolean legal = false;
  296.         for ( int i=0; i < statesAllowed.length; i++ )
  297.             if ( s == statesAllowed[i] )
  298.                 { 
  299.                 legal = true; break; 
  300.                 }
  301.  
  302.         if ( !legal )
  303.             throw new IllegalArgumentException();
  304.  
  305.         // Send notification of story loading status change
  306.         if (state != s)
  307.             {
  308.             state = s;
  309.             StoryContainerNotification scn = new StoryContainerNotification(this,this.strSubsect,state);
  310.             setChanged();
  311.             notifyObservers(scn);
  312.             }
  313.  
  314.         if (state == stateAdded || state == stateCleared)
  315.             state = stateQuiescent;
  316.  
  317.         } // setState
  318.  
  319.     
  320.  
  321.     private final synchronized PaperStory getStoryAndChangeCurrent(int idx)
  322.         throws NoSuchElementException
  323.         {
  324.         System.out.println("Debug-PaperSection-getStoryAndChangeCurrent: strSubsect=" + strSubsect);
  325.         PaperStory temp = (PaperStory) vStory.elementAt(idx);
  326.  
  327.         if (temp == null)
  328.             throw new NoSuchElementException();
  329.  
  330.         idxCurrent = idx;
  331.         return temp;
  332.         }
  333.  
  334.  
  335.     } // PaperSection
  336.