home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / storylist.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.3 KB  |  269 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.     @(#)StoryList.java  0.00 01-Jan-96
  15.  
  16.         A PageView that lists the titles of stories in one PaperSection.
  17.  
  18.     Authors:
  19.  
  20.         rphall   Rick Hall
  21.         jlee     James Lee
  22.  
  23.     Version Ident:
  24.  
  25.         $Header: /PjJavaClient/src/pj/awt/StoryList.java 2     1/23/96 2:56p Rphall $
  26.  
  27.     History:
  28.  
  29.         0.00 01-Jan-96  rphall      Initial Creation
  30.         0.01  1-Mar-96  Ted S.      Conform to new PaperView interface.
  31.         0.02  1-Mar-96  jlee        Added a private function CleanTitle to replace CRLF with null char.
  32.              12-Mar-96  jlee        Used a custom made TitleList in place of List.
  33.  
  34. ---------------------------------------------------------------------------*/
  35.  
  36. package pj.awt;
  37.  
  38. import pj.awt.PageView;
  39. import pj.awt.TitleList;
  40. import pj.io.Paper;
  41. import pj.io.PaperSection;
  42. import pj.io.PaperStory;
  43. import pj.io.StoryContainerNotification;
  44.  
  45. import java.awt.Color;
  46. import java.awt.Event;
  47. import java.awt.GridBagConstraints;
  48. import java.awt.GridBagLayout;
  49. import java.lang.String;
  50. import java.util.Observable;
  51. import java.util.Observer;
  52.  
  53.  
  54. /**
  55.  * A PageView that lists the titles of stories in one PaperSection.
  56.  * StoryLists may be used inside other pages or as standalone pages.
  57.  * StoryLists may not be used as dividers.
  58.  *
  59.  * @version 0.00 01-Jan-96
  60.  * @author  rphall
  61. */
  62. public class StoryList extends PageView implements Observer
  63.     {
  64.  
  65.     // --- Class variables
  66.  
  67.     static int debugCount = 0;
  68.  
  69.     // --- Instance variables
  70.  
  71.     private final String  CR   = "\r";
  72.     private final String  LF   = "\n";
  73.     private final String  CRLF = CR + LF;
  74.  
  75.     private boolean   bSelectionEnabled;
  76.     private Paper     paper;
  77.     private String    strPaperSection;
  78.     private TitleList list;
  79.  
  80.     // --- Public constructors
  81.  
  82.     /**
  83.      * Construct a StoryList as a standalone page.
  84.      * @param pagename  The name of the standalone page.
  85.      * @param section   The name of the observed section.
  86.      * @param paper     The paper that contains the observed section.
  87.     */
  88.     public StoryList(String pagename, String section, Paper p)
  89.         {
  90.         super(pagename);
  91.         bSelectionEnabled = false;
  92.         strPaperSection = section;
  93.  
  94.         list = new TitleList();
  95.  
  96.         GridBagLayout gridbag = new GridBagLayout();
  97.         setLayout(gridbag);
  98.         GridBagConstraints gbc = new GridBagConstraints();
  99.  
  100.         gbc.weightx = 1.0;
  101.         gbc.weighty = 1.0;
  102.  
  103.         gbc.fill = GridBagConstraints.BOTH;
  104.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  105.         gbc.gridheight= GridBagConstraints.REMAINDER;
  106.  
  107.         gridbag.setConstraints(list, gbc);
  108.         add(list);
  109.  
  110.         paper = p;
  111.         paper.addObserver(this);
  112.         }
  113.  
  114.     /**
  115.      * Construct a StoryList as a page component.
  116.      * @param section   The name of the observed section.
  117.      * @param paper     The paper that contains the observed section.
  118.     */
  119.     public StoryList(String section, Paper paper)
  120.         { 
  121.         this(null,section,paper); 
  122.         }
  123.  
  124.     // --- Public operations
  125.  
  126.     /**
  127.      * @return A story list displays one view.
  128.     */
  129.     public int countViews()
  130.         { 
  131.         return 1; 
  132.         }
  133.  
  134.     /**
  135.      * Make the first story of the observed section the current view.
  136.     */
  137.     public void firstView() 
  138.         {
  139.         }
  140.  
  141.     /**
  142.      * Make the next story (after the current story) the current view.
  143.     */
  144.     public boolean nextView()
  145.         {
  146.         return false;
  147.         }
  148.  
  149.     /**
  150.      * Make the last story of the observed section the current view.
  151.     */
  152.     public void lastView() 
  153.         {
  154.         }
  155.  
  156.     /**
  157.      * Make the previous story (before the current story) the current view.
  158.     */
  159.     public boolean previousView()
  160.         {
  161.         return false;
  162.         }
  163.  
  164.     
  165.  
  166.     /**
  167.      * Make a specific story the current view.
  168.      * @param idx The index of the view to display.
  169.     */
  170.     public void view(int idx) 
  171.         {
  172.         }
  173.  
  174.     /**
  175.      * Update view upon notification from a PaperSection. When a section
  176.      * notifies of a transisition to the Added state, a story list clears
  177.      * its list and then iterates over the stories in the section, adding
  178.      * each story title to its list.
  179.      * @param src The object that sent or forwarded the notification.
  180.      * @param arg The notification.  An instance of StoryContainerNotification
  181.      * is expected.
  182.     */
  183.     public void update(Observable src, Object arg)
  184.         {
  185.         if ( !(arg instanceof StoryContainerNotification) )
  186.             return;
  187.  
  188.         StoryContainerNotification scn = (StoryContainerNotification) arg;
  189.  
  190.         if ( !(strPaperSection.equals(scn.strSubsect)) )
  191.             return;
  192.  
  193.         if (scn.newState != PaperSection.stateAdded)
  194.             return;
  195.  
  196.         view();
  197.  
  198.         } // update
  199.  
  200.     /**
  201.      * Handle event:
  202.      * If the event is LIST_SELECT, but list selection is disenabled,
  203.      * immediately deselect this selection, otherwise let super handle it.
  204.      * In either case, return false so that LIST_SELECT events propagate
  205.      * up the window chain.
  206.     */
  207.     public boolean handleEvent(Event evt)
  208.         {
  209.         switch (evt.id)
  210.             {
  211.             case Event.MOUSE_DOWN:
  212.                 if ( evt.clickCount > 0 )
  213.                     super.handleEvent(evt);
  214.                 return false;
  215.  
  216.             default:
  217.                 return super.handleEvent(evt);
  218.                 } // switch
  219.         } // handleEvent
  220.  
  221.  
  222.     // --- Protected operations
  223.    
  224.  
  225.     protected void view()
  226.         {
  227.         list.clear();
  228.         PaperSection sec = getSection();
  229.  
  230.         for (int i=0; i < sec.numStories(); i++)
  231.             list.addItem( cleanTitleString( sec.story(i).getTitle() ), false );
  232.  
  233.         if ( sec.numStories() > 0 )
  234.             list.addItem( "", true );//neccessary to mark the end of adding
  235.  
  236.         } // view
  237.  
  238.     protected PaperSection getSection()
  239.         { 
  240.         return paper.section(strPaperSection); 
  241.         }
  242.  
  243.     // Private operations
  244.     private String cleanTitleString( String strTitle )
  245.         {
  246.         // Replace CRLF with null
  247.         String  text        = strTitle;
  248.         int     nextCRLF    = text.indexOf(CRLF);
  249.         int     nextSubstr  = 0;
  250.  
  251.         while ( nextCRLF != -1 )
  252.             {
  253.             nextSubstr = text.length() - CRLF.length();
  254.  
  255.             if ( nextSubstr == 0 )
  256.                 text = text.substring(0, nextCRLF);
  257.             else
  258.                 text = text.substring(0, nextCRLF) + text.substring(nextCRLF + CRLF.length());
  259.  
  260.             nextCRLF  = text.indexOf(CRLF);
  261.             } // while CRLF
  262.  
  263.         return text;
  264.         }
  265.  
  266.     
  267.  
  268.     } // StoryList
  269.