home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / column.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  10.2 KB  |  352 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.     @(#)Column.java  0.00 01-Jan-96
  15.  
  16.         A simple PageView that views stories in one PaperSection.
  17.  
  18.     Authors:
  19.  
  20.         rphall   Rick Hall
  21.         Ted      Skolnick
  22.         jlee     James Lee
  23.  
  24.     Version Ident:
  25.  
  26.         $Header: /PjJavaClient/src/pj/awt/Column.java 3     2/11/96 4:39p Rphall $
  27.  
  28.     History:
  29.  
  30.         0.00 01-Jan-96  rphall      Initial Creation
  31.         0.01 23-Feb-96  Ted S.      Changes to PaperView Interface
  32.         0.02 12-Mar-96  jlee        Added story formatting functions.
  33.         0.03 18-Mar-96  Ted S.      Filled in more on Next, Prev View.
  34.         0.04 28-Mar-96  Ted S.      Made ltb protected so derived classes can use it.
  35.  
  36. ---------------------------------------------------------------------------*/
  37.  
  38. package pj.awt;
  39.  
  40. import pj.awt.LineTextBox;
  41. import pj.awt.PageView;
  42. import pj.awt.StoryFormatter;
  43. import pj.io.Paper;
  44. import pj.io.PaperSection;
  45. import pj.io.PaperStory;
  46. import pj.io.StoryContainerNotification;
  47.  
  48. import java.awt.Panel;
  49. import java.awt.Dimension;
  50. import java.awt.Font;
  51. import java.util.Observable;
  52. import java.util.Observer;
  53. import java.util.Vector;
  54.  
  55.  
  56. /**
  57.  * A simple PageView that observes one PaperSection and displays
  58.  * stories from that section.  Columns may be used inside other pages
  59.  * or may be used as standalone pages.  Columns may not be used
  60.  * as dividers.
  61.  *
  62.  * @version 0.00 01-Jan-96
  63.  * @author  rphall
  64. */
  65. public class Column extends PageView implements Observer
  66.     {
  67.  
  68.     // --- Class variables
  69.  
  70.     private static final String  CR   = "\r";
  71.     private static final String  LF   = "\n";
  72.     private static final String  CRLF = CR + LF;
  73.     private static final String  PARAG_DEL = "---";
  74.  
  75.     private static final String  LINE_CENTER = "\\qc";
  76.     private static final String  LINE_BOLD = "\\b";
  77.  
  78.     private static final int     TITLE = 0;
  79.     private static final int     BODY = 1;
  80.  
  81.  
  82.     // --- Instance variables
  83.  
  84.     private Paper                paper;
  85.     private String               strPaperSection;
  86.     private StoryFormatter       sfFormatter;
  87.     protected LineTextBox          ltb;
  88.  
  89.     // --- Public constructors
  90.  
  91.     /**
  92.      * Construct a Column as a standalone page.
  93.      * @param pagename  The name of the standalone page.
  94.      * @param section   The name of the observed section.
  95.      * @param p         The paper that contains the observed section.
  96.     */
  97.     public Column(String pagename, String section, Paper p)
  98.         {
  99.         super(pagename);
  100.         strPaperSection = section;
  101.         ltb = new LineTextBox();
  102.         add("Center", ltb);
  103.         paper = p;
  104.         paper.addObserver(this);
  105.         }
  106.  
  107.     /**
  108.      * Construct a Column as a page component.
  109.      * @param section   The name of the observed section.
  110.      * @param paper     The paper that contains the observed section.
  111.     */
  112.     public Column(String section, Paper paper)
  113.         { 
  114.         this(null,section,paper); 
  115.         }
  116.  
  117.  
  118.     // --- Public operations
  119.  
  120.     /**
  121.      * @param formatter The StoryFormatter this column should use
  122.      * in presenting stories.
  123.     */
  124.     public void setStoryFormatter(StoryFormatter formatter)
  125.         { 
  126.         sfFormatter = formatter; 
  127.         }
  128.  
  129.     /**
  130.      * @param f The Font this column should use in presenting stories.
  131.     */
  132.     public void setFont(Font f)
  133.         { 
  134.         ltb.setFont(f); 
  135.         }
  136.  
  137.     /**
  138.      * @return The number of stories in the observed section.
  139.     */
  140.     public int countViews()
  141.         { 
  142.         return getSection().numStories(); 
  143.         }
  144.  
  145.     /**
  146.      * Make the first story of the observed section the current view.
  147.     */
  148.     public void firstView()
  149.         { 
  150.         super.firstView();
  151.         view( getSection().firstStory() ); 
  152.         }
  153.  
  154.     /**
  155.      * Make the next story (after the current story) the current view.
  156.     */
  157.     public boolean nextView()
  158.         {
  159.         boolean bRet;
  160.  
  161.         // Make sure we can do another next, 
  162.         //  if not return false.
  163.         if ( super.nextView() )
  164.             {            
  165.             view( getSection().nextStory() );            
  166.             bRet = true;
  167.             }        
  168.         else
  169.             {
  170.             bRet = false;            
  171.             }
  172.        
  173.         return bRet;
  174.         }
  175.  
  176.     /**
  177.      * Make the last story of the observed section the current view.
  178.     */
  179.     public void lastView()
  180.         { 
  181.         super.lastView();
  182.         view( getSection().lastStory() ); 
  183.         }
  184.  
  185.     /**
  186.      * Make the previous story (before the current story) the current view.
  187.     */
  188.     public boolean previousView()
  189.         {
  190.         boolean bRet;
  191.  
  192.         // Make sure we can do another prev, 
  193.         //  if not return false.
  194.         if ( super.previousView() )
  195.             {
  196.             view( getSection().previousStory() );
  197.             bRet = true;
  198.             }        
  199.         else
  200.             {
  201.             bRet = false;            
  202.             }       
  203.         
  204.         return bRet;    
  205.         }
  206.  
  207.     
  208.  
  209.     /**
  210.      * Make a specific story the current view.  Views are 1 based.
  211.      * @param idx The index of the view to display.
  212.     */
  213.     public void view(int idx)
  214.         { 
  215.         super.view(idx);
  216.         if ( idx > 0 )
  217.             view( getSection().story(idx - 1) );
  218.         }
  219.  
  220.     /**
  221.      * Update view upon notification from a PaperSection. The column
  222.      * resets the current view to the first story of the updated section
  223.      * when that section finishes adding stories.
  224.      * @param src The object that sent or forwarded the notification.
  225.      * @param arg The notification.  An instance of StoryContainerNotification
  226.      * is expected.
  227.     */
  228.     public synchronized void update(Observable src, Object arg)
  229.         {
  230.         if ( !(arg instanceof StoryContainerNotification) )
  231.             return;
  232.  
  233.         StoryContainerNotification scn = (StoryContainerNotification) arg;
  234.  
  235.         if ( !(strPaperSection.equals(scn.strSubsect)) )
  236.             return;
  237.  
  238.         if (scn.newState != PaperSection.stateAdded)
  239.             return;
  240.  
  241.         firstView();
  242.  
  243.         } // update
  244.  
  245.     public Dimension minimumSize()
  246.         { 
  247.         return ltb.minimumSize(); 
  248.         }
  249.  
  250.     public Dimension preferredSize()
  251.         { 
  252.         return ltb.preferredSize(); 
  253.         }
  254.  
  255.     // --- Protected operations
  256.  
  257.     protected void view(PaperStory story)
  258.         {
  259.         if ( getSection().doesSectionDisplaysMultipleStories() )
  260.             {
  261.             Vector vAll = getSection().getAllStories();
  262.             String  strStory = new String();
  263.  
  264.             for ( int i = 0; i < vAll.size(); i++ )
  265.                 if ( ((PaperStory)vAll.elementAt(i)).getBody() != null )
  266.                         strStory += "\\b" + formatText( ((PaperStory)vAll.elementAt(i)).getBody(), BODY ) + "\n";
  267.  
  268.             ltb.setText( strStory );
  269.             }
  270.         else
  271.             ltb.setText( formatToString(story) );
  272.             
  273.         ltb.repaint();    
  274.         
  275.         }
  276.  
  277.     protected String formatToString(PaperStory story)
  278.         {
  279.         if (sfFormatter != null)
  280.             return sfFormatter.formatToString(story);
  281.         else
  282.             {
  283.             return "\\b" + formatText( story.getTitle(), TITLE ) + "\n" + story.getBody() + "\n";
  284.             }
  285.         }
  286.  
  287.     protected String formatText( String text, int type )
  288.         {
  289.         int nextCRLF = 0, idx = 0;
  290.  
  291.         switch ( type )
  292.             {
  293.             case TITLE:
  294.                 nextCRLF = text.indexOf( CRLF );
  295.                 while ( nextCRLF != -1 )
  296.                     {
  297.                     if ( text.length() > nextCRLF + CRLF.length() )
  298.                         text = text.substring( 0, nextCRLF + CRLF.length() ) +
  299.                                LINE_BOLD + text.substring(nextCRLF + CRLF.length());
  300.                     else
  301.                         break;
  302.  
  303.                     nextCRLF  = text.indexOf( CRLF, nextCRLF + CRLF.length() + LINE_BOLD.length() );
  304.                     } // while CRLF
  305.                 break;
  306.  
  307.             case BODY:
  308.                 int nextPARAG_DEL   = text.indexOf( PARAG_DEL );
  309.                 while ( nextPARAG_DEL != -1 )
  310.                     {
  311.                     if ( text.length() > nextPARAG_DEL )
  312.                         {
  313.                         int lastLFIdx = text.substring( nextPARAG_DEL - 10, nextPARAG_DEL ).lastIndexOf( LF );
  314.                         if ( lastLFIdx != -1 )
  315.                             {
  316.                             idx = nextPARAG_DEL - 10 + lastLFIdx + LF.length();
  317.  
  318.                             text = text.substring( 0, nextPARAG_DEL - 10 + lastLFIdx + LF.length() ) + LINE_CENTER +
  319.                                    ( ( ((nextCRLF = text.substring( idx ).indexOf( CRLF )) != -1) &&
  320.                                      (text.length() > nextCRLF + CRLF.length()) ) ?
  321.  
  322.                                             text.substring( idx, idx + nextCRLF + CRLF.length() ) + LINE_BOLD +
  323.                                                             text.substring( idx + nextCRLF + CRLF.length()) :
  324.  
  325.                                             text.substring( idx ) );
  326.                             } // if ( lastLFIdx != -1 )
  327.  
  328.                         } //if ( text.length() > nextPARAG_DEL )
  329.                     else
  330.                         break;
  331.  
  332.                     nextPARAG_DEL  = text.indexOf( PARAG_DEL, nextPARAG_DEL + PARAG_DEL.length() + LINE_CENTER.length() );
  333.  
  334.                     } // while PARAG_DEL
  335.                 break;
  336.  
  337.             default:
  338.                 break;
  339.             }
  340.  
  341.         return text;
  342.         }
  343.  
  344.     protected PaperSection getSection()
  345.         { 
  346.         return paper.section(strPaperSection); 
  347.         }
  348.  
  349.     
  350.  
  351.     } // Column
  352.