home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 10.1 KB | 336 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.
-
-
-
- @(#)PaperSection.java 0.00 20-Dec-95
-
-
- A group of stories in a Personal Journal paper all sharing the
- the same section and subsection attribute. Sections are observable,
- and in turn observe other sections.
-
-
- Authors:
-
- rphall Rick Hall
- jlee James Lee
-
-
- Version Ident:
-
- $Header: /PjJavaClient/src/pj/io/PaperSection.java 2 1/21/96 5:27p Rphall $
-
-
- History:
-
- 20-Dec-95 rphall Initial Creation
- 27-Dec-95 rphall Made observerable
- 12-Mar-96 jlee Modified so that multiple stories can be shown as one story
- 21-Mar-96 jlee Added DownloadProgressNotification processing
-
- ---------------------------------------------------------------------------*/
-
- package pj.io;
-
- import pj.io.DownloadProgressNotification;
- import pj.io.PaperStory;
- import pj.io.StoryContainer;
- import pj.io.StoryContainerNotification;
-
- import collections.Assertable;
- import collections.ImplementationCheckable;
- import collections.ImplementationError;
- import java.util.NoSuchElementException;
- import java.util.Observable;
- import java.util.Observer;
- import java.util.Vector;
-
- /**
- * A group of stories in a Personal Journal paper all sharing the
- * the same section and subsection attribute.
- * @version 0.00, 20-Dec-95
- * @author Rick Hall
- */
- public class PaperSection extends Observable
- implements Assertable, ImplementationCheckable, StatefulStoryContainer,
- Observer
- {
-
- // --- Class variables
-
- public static final String typeArticle = "900";
- public static final String typeChart = "901";
- public static final String typeDjia = "902";
- public static final String typeAd = "904";
-
- // --- Instance variables
- private DownloadProgressNotification dpnProgress;
-
- private final int[] statesAllowed =
- {
- stateQuiescent, stateAdding, stateAdded, stateClearing, stateCleared
- };
-
- private String strSection; // Section id of stories in this container
- private String strSubsect; // Subsection id of stories in this container
- private String strType; // Type of stories in this container
- private Vector vStory; // Implementation of this container
- private int idxCurrent; // A cursor into stories
- private int state; // See StatefulStoryContainer for values
-
- // --- Public constructors
-
- PaperSection (
- String section, // Section id of this container
- String subsect, // Subsection id of this container
- String type) // Type of stories this container will hold
- throws ImplementationError
- {
- assert( section != null );
- assert( subsect != null );
- assert( type != null );
-
- strSection = section;
- strSubsect = subsect;
- strType = type;
- dpnProgress = new DownloadProgressNotification(this, strSubsect, stateAdding);
-
- vStory = new Vector();
- idxCurrent = 0;
- state = stateQuiescent;
- clearChanged();
- }
-
- // --- Public operations
- public void assert(boolean predicate)
- throws ImplementationError
- {
- ImplementationError.assert(this, predicate);
- }
-
- public void checkImplementation()
- throws ImplementationError
- {
- assert( idxCurrent >= 0 );
- if (vStory.size() > 0)
- assert( idxCurrent < vStory.size() );
- for (int i=0; i<vStory.size(); i++)
- assert( vStory.elementAt(i) instanceof PaperStory );
- }
-
- public String getId()
- {
- return strSubsect;
- }
-
- public final int numStories()
- {
- return vStory.size();
- }
-
- public void addStory( PaperStory ps)
- throws MalformedPaperStoryException, IncompatiblePaperStoryException,
- IllegalArgumentException
- {
- if (ps == null)
- throw new IllegalArgumentException();
-
- if ( !strSection.equals(ps.getSection()) )
- throw new IncompatiblePaperStoryException();
-
- if ( !strSubsect.equals(ps.getSubsect()) )
- throw new IncompatiblePaperStoryException();
-
- // Add story ...
- vStory.addElement(ps);
-
- // ... then notify observers
- try {
- System.out.println("Debug-PaperSection-addStory: strSubsect=" + strSubsect);
- setState(stateAdding);
-
- // Send download progress observer notification.
- setChanged();
- notifyObservers( dpnProgress );
- }
- catch (IllegalArgumentException e)
- {
- }
-
- } // addStory
-
- public final synchronized void removeStories()
- {
- try {
- setState(stateClearing);
- }
- catch (IllegalArgumentException e)
- {
- }
-
- vStory.removeAllElements();
- }
-
- public synchronized void update(Observable o, Object arg)
- {
- if ( !(arg instanceof StoryContainerNotification) )
- return;
-
- StoryContainerNotification scnTemp = (StoryContainerNotification) arg;
-
- if ( scnTemp.source == this )
- return;
-
- if ( scnTemp.newState == stateAdding && this.state == stateAdding )
- {
- try {
- setState(stateAdded);
- }
- catch(IllegalArgumentException e)
- {
- }
- }
-
- } // update
-
- public synchronized final int getState()
- {
- return state;
- }
-
-
- public final synchronized Vector getAllStories()
- {
- return vStory;
- }
-
- public final boolean doesSectionDisplaysMultipleStories()
- {
- if ( strSubsect.equals(Paper.idFPWorld) || strSubsect.equals(Paper.idFPBusFin) ||
- strSubsect.equals(Paper.idSpHilite) || strSubsect.equals(Paper.idTJIWorld) ||
- strSubsect.equals(Paper.idTJIBusFin)|| strSubsect.equals(Paper.idSpScores) )
- return true;
- else
- return false;
- }
-
- public final synchronized PaperStory firstStory()
- throws NoSuchElementException
- {
- System.out.println("Debug-PaperSection-firstStory:");
- return getStoryAndChangeCurrent(0);
- }
-
- public final synchronized PaperStory nextStory()
- throws NoSuchElementException
- {
- System.out.println("Debug-PaperSection-nextStory:");
- return getStoryAndChangeCurrent( ( (idxCurrent+1) >= vStory.size() ? idxCurrent: (idxCurrent+1) ) );
- }//jlee
-
- public final synchronized PaperStory lastStory()
- throws NoSuchElementException
- {
- System.out.println("Debug-PaperSection-lastStory:");
- return getStoryAndChangeCurrent(vStory.size()-1);
- }
-
- public final synchronized PaperStory previousStory()
- throws NoSuchElementException
- {
- System.out.println("Debug-PaperSection-previousStory:");
- return getStoryAndChangeCurrent( ( (idxCurrent-1) < 0 ? 0: (idxCurrent-1) ) );
- }//jlee
-
- public final synchronized PaperStory currentStory()
- throws NoSuchElementException
- {
- System.out.println("Debug-PaperSection-currentStory:");
- return getStoryAndChangeCurrent(idxCurrent);
- }
-
- /**
- * Return the story with the given index and set the current
- * story to be this story.
- * @param idx An index value in the range 0 - (numStories() - 1)
- * @return A story matching the index value
- * @exception NoSuchElementException thrown if idx is out of range
- */
- public final synchronized PaperStory story(int idx)
- throws NoSuchElementException
- {
- return getStoryAndChangeCurrent(idx);
- }
-
- public String toString()
- {
- checkImplementation();
- String temp;
- if (vStory.size() > 0)
- {
- temp = vStory.elementAt(0).toString();
- for (int i=1; i<vStory.size(); i++)
- temp += vStory.elementAt(i).toString();
- }
- else
- temp = "";
-
- return temp;
- }
-
- // ---Private operations
-
- private synchronized void setState( int s ) throws IllegalArgumentException
- {
- boolean legal = false;
- for ( int i=0; i < statesAllowed.length; i++ )
- if ( s == statesAllowed[i] )
- {
- legal = true; break;
- }
-
- if ( !legal )
- throw new IllegalArgumentException();
-
- // Send notification of story loading status change
- if (state != s)
- {
- state = s;
- StoryContainerNotification scn = new StoryContainerNotification(this,this.strSubsect,state);
- setChanged();
- notifyObservers(scn);
- }
-
- if (state == stateAdded || state == stateCleared)
- state = stateQuiescent;
-
- } // setState
-
-
-
- private final synchronized PaperStory getStoryAndChangeCurrent(int idx)
- throws NoSuchElementException
- {
- System.out.println("Debug-PaperSection-getStoryAndChangeCurrent: strSubsect=" + strSubsect);
- PaperStory temp = (PaperStory) vStory.elementAt(idx);
-
- if (temp == null)
- throw new NoSuchElementException();
-
- idxCurrent = idx;
- return temp;
- }
-
-
- } // PaperSection
-