home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / headlinesection.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  15.2 KB  |  515 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.     @(#)HeadlineSection.java  0.00 22-Jan-96
  15.  
  16.         A SectionView for the Front Page and ThisJustIn headlines.
  17.  
  18.     Authors:
  19.  
  20.         rphall   Rick Hall
  21.         jlee     James Lee
  22.         Ted S.   Ted Skolnick
  23.         lning    Lindee Ning
  24.  
  25.     Version Ident:
  26.  
  27.         $Header: /PjJavaClient/src/pj/awt/HeadlineSection.java 13    3/22/96 11:53p Jlee $
  28.  
  29.     History:
  30.  
  31.         0.00 22-Jan-96  rphall      Initial Creation
  32.              21-Feb-96  jlee        Used Courier font for the each column's font
  33.         0.01 1-Mar-96   Ted S.      Added icons to the page,
  34.                                     to make it look a little more PJish.
  35.              12-Mar-96  jlee        Used PjFinals for font info used in text.
  36.              14-Mar-96  Ted S.      Removed CountViews(), since super handles it.
  37.              25-Mar-96  lning       Added two buttons for viewing previous and next
  38.                                                                         story.
  39.              28-Mar-96  Ted S. & L. Ning
  40.                                     Reworked last revision.
  41.                                     Added Prev and next buttons to the the Personal
  42.                                     News column by deriving off of column, and
  43.                                     making use of the View() interface.
  44. ---------------------------------------------------------------------------*/
  45.  
  46. package pj.awt;
  47.  
  48. import pj.awt.Column;
  49. import pj.awt.HeadlineView;
  50. import pj.awt.Page;
  51. import pj.awt.PjPageSpec;
  52. import pj.awt.PjImages;
  53. import pj.awt.PjFinals;
  54. import pj.awt.PjStr;
  55. import pj.awt.SectionView;
  56.  
  57. import collections.Assertable;
  58. import collections.ImplementationError;
  59. import java.awt.Button;
  60. import java.awt.Component;
  61. import java.awt.Dimension;
  62. import java.awt.Event;
  63. import java.awt.Font;
  64. import java.awt.FlowLayout;
  65. import java.awt.GridBagLayout;
  66. import java.awt.GridBagConstraints;
  67. import java.awt.Label;
  68. import java.awt.Panel;
  69. import pj.io.Paper;
  70.  
  71. /**
  72.  * A SectionView for Front Page and ThisJustIn headlines.
  73.  *
  74.  * @see DividerView
  75.  * @version 0.00 22-Jan-96
  76.  * @author  rphall
  77. */
  78. public class HeadlineSection extends SectionView implements Assertable
  79.     {
  80.  
  81.     // --- Public constructors
  82.  
  83.     /**
  84.      * A general-purpose constructor that uses preassembled
  85.      * components.
  86.      * @param hv A HeadlineView.
  87.      * @param pvBF A PageView for Business and Finance stories.
  88.      * @param pvWW A PageView for World-Wide stories.
  89.      * @param pvPN A PageView for Personal News stories.
  90.      * @param pagespec A specification for the page.
  91.      * @param paper The paper being observed.
  92.     */
  93.     public HeadlineSection( HeadlineView hv, PageView pvBF,
  94.             PageView pvWW, PageView pvPN, PjPageSpec pagespec, Paper p)
  95.         {
  96.         super(pagespec.tabspec);
  97.  
  98.         hvHeadlines = hv;
  99.         pvBusFin = pvBF;
  100.         pvWWide = pvWW;
  101.         pvPNews = pvPN;
  102.         paper = p;
  103.         idxIcon = PjImages.idxFrontPage;
  104.  
  105.         initialLayout();
  106.  
  107.         firstView();
  108.  
  109.         } // HeadlineSection constructor
  110.  
  111.     /**
  112.      * A specialized constructor for a HeadlineSection that uses
  113.      * a page specification.
  114.      * @param pagespec  A page specification, either for the
  115.      * Front Page headlines or the ThisJustIn headlines.
  116.      * @param p The paper being observed.
  117.     */
  118.     public HeadlineSection(PjPageSpec pagespec, Paper p)
  119.         {
  120.         super(pagespec.tabspec);
  121.         paper = p;
  122.  
  123.         if  ( pagespec.strPageName.equals(PjStr.pageFPHdlines) )
  124.             {
  125.             pvBusFin = new Column(pgBusFin,Paper.idFPBusFin,paper);
  126.             pvWWide = new Column(pgWWide,Paper.idFPWorld,paper);
  127.             pvPNews = new ColumnAndButtons(pgPNews,Paper.idFPNews,paper);
  128.             idxIcon = PjImages.idxFrontPage;
  129.             }
  130.  
  131.         else if
  132.             ( pagespec.strPageName.equals( PjStr.pageTJIHdlines) )
  133.             {
  134.             pvBusFin = new ColumnSingleView(pgBusFin,Paper.idTJIBusFin,paper);
  135.             pvWWide = new ColumnSingleView(pgWWide,Paper.idTJIWorld,paper);
  136.             pvPNews = new ColumnAndButtons(pgPNews,Paper.idTJINews,paper);
  137.             idxIcon = PjImages.idxTJI;
  138.             }
  139.         else
  140.             {
  141.             // Bad page specification
  142.             assert(false);
  143.             }
  144.  
  145.         pvBusFin.setFont( PjFinals.fntUnfColumnPlain );
  146.         pvWWide.setFont( PjFinals.fntUnfColumnPlain );
  147.         pvPNews.setFont( PjFinals.fntUnfColumnPlain );
  148.  
  149.         hvHeadlines = new HeadlineView(pagespec.strPageName,paper);
  150.         hvHeadlines.setName(pgHeadlines);
  151.         initialLayout();
  152.  
  153.         System.out.println("Debug-HeadlineSection:constructed");
  154.         } // HeadlineSection constructor
  155.  
  156.  
  157.     public boolean handleEvent(Event evt)
  158.         {
  159.         boolean bHandled = false;
  160.  
  161.         switch (evt.id)
  162.             {
  163.             case Event.LIST_SELECT:
  164.                 int iStory = ((Integer)evt.arg).intValue();
  165.                 pvPNews.view(iStory);
  166.                 stack.page(pgPNews);
  167.                 bHandled = true;
  168.                 break;
  169.             default:
  170.                 bHandled = super.handleEvent(evt);
  171.                 break;
  172.             } // switch
  173.  
  174.         return bHandled;
  175.         } // handleEvent
  176.  
  177.     public boolean action(Event e, Object arg)
  178.         {
  179.         boolean bHandled = false;
  180.  
  181.         if ( HeadlineView.strBusFin.equals(arg) )
  182.             {
  183.             bHandled = true;
  184.             stack.page(pgBusFin);
  185.             }
  186.         else
  187.         if ( HeadlineView.strWWide.equals(arg) )
  188.             {
  189.             bHandled = true;
  190.             stack.page(pgWWide);
  191.             }
  192.         else
  193.         if ( HeadlineView.strPNews.equals(arg) )
  194.             {
  195.             bHandled = true;
  196.             if ( pvPNews.countViews() > 0 )
  197.                 {
  198.                 pvPNews.view( 0 );
  199.                 stack.page(pgPNews);
  200.                 }
  201.             }
  202.         else
  203.         if ( e.target instanceof Button )//user clicked storylist selection button
  204.             {
  205.             int iStory = 0;
  206.             try
  207.                 {
  208.                 iStory = Integer.parseInt( ((Button)e.target).getLabel() ) - 1;
  209.                 }
  210.             catch ( NumberFormatException ex )
  211.                 {
  212.                 return bHandled;
  213.                 }
  214.  
  215.             if ( iStory >= 0)
  216.                 {
  217.                 pvPNews.view(iStory);
  218.                 stack.page(pgPNews);
  219.                 bHandled = true;
  220.                 }
  221.             }
  222.  
  223.         return bHandled;
  224.         } // action
  225.  
  226.     /**
  227.      * Raise an exception if predicate is false.
  228.      * @see collections.Assertable
  229.     */
  230.     public void assert(boolean predicate) throws ImplementationError
  231.         { ImplementationError.assert(this, predicate); }
  232.  
  233.     // --- Private operations
  234.  
  235.     private void initialLayout()
  236.         {
  237.         assert( hvHeadlines != null );
  238.         assert( pvBusFin != null );
  239.         assert( pvWWide != null );
  240.         assert( pvPNews != null );
  241.         assert( paper != null );
  242.  
  243.         appendPage( hvHeadlines );
  244.         appendPage( layoutPage( pvBusFin, pgBusFin ) );
  245.         appendPage( layoutPage( pvWWide,  pgWWide  ) );
  246.         appendPage( layoutPage( pvPNews,  pgPNews  ) );
  247.         }
  248.  
  249.     private Page layoutPage( PageView pv, String name )
  250.          {
  251.          Font lblFont = PjFinals.fntSectionBigTitle;
  252.  
  253.          GridBagLayout gridbag = new GridBagLayout();
  254.          GridBagConstraints gbc = new GridBagConstraints();
  255.  
  256.                 // Use DeferPage as the type of page to create and tell it
  257.                 // to defer View() functions to the Page passed in.
  258.          DeferPage pnlPage = new DeferPage( name, pv );
  259.          Label lblPage = new Label(name);
  260.  
  261.          lblPage.setFont( lblFont );
  262.          pnlPage.setLayout( gridbag );
  263.  
  264.  
  265.         // Create a panel to hold the icon and the text for page title.
  266.         Panel pnlHeader = new Panel();
  267.         pnlHeader.setLayout( new FlowLayout( FlowLayout.LEFT ) );
  268.  
  269.         PjImages Imgs = new PjImages();
  270.         // Add the little icon
  271.         pnlHeader.add( new GifCanvas(Imgs.imagespecs()[ idxIcon ].getImage() ) );
  272.         // Add the title
  273.         pnlHeader.add( lblPage );
  274.  
  275.         // Add the header panel with its contents to the page.
  276.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  277.         gbc.anchor=GridBagConstraints.WEST;
  278.         gridbag.setConstraints(pnlHeader,gbc);
  279.         pnlPage.add( pnlHeader );
  280.  
  281.                 gbc.weightx = 1.0;
  282.                 gbc.weighty = 1.0;
  283.                 gbc.fill = GridBagConstraints.BOTH;
  284.                 gbc.gridwidth = GridBagConstraints.REMAINDER;
  285.                 gridbag.setConstraints(pv, gbc);
  286.                 pnlPage.add( pv );
  287.  
  288.  
  289.                 return pnlPage;
  290.      }
  291.     // --- Private attributes
  292.  
  293.     private HeadlineView hvHeadlines;
  294.     private PageView pvBusFin;
  295.     private PageView pvWWide;
  296.     private PageView pvPNews;
  297.     private Paper paper;
  298.  
  299.     // Which icon to display, PJ front page or TJI.
  300.     private int idxIcon;
  301.  
  302.     private final String pgHeadlines = "Headlines";
  303.     private final String pgBusFin ="Business and Finance";
  304.     private final String pgWWide = "World-Wide";
  305.     private final String pgPNews = "Personal News";
  306.  
  307.     } // HeadlineSection
  308.  
  309.  
  310. /**
  311.  * A Column containing three components: a view and two buttons and a lable
  312.  * laid out beneath the subpanel.  This is a helper class for Headline
  313.  * views.
  314.  * <P>
  315.  * The layout of ColumnAndButtons is:
  316.  * <pre>
  317.  *      -------------------
  318.  *      |                 |
  319.  *      |     column      |
  320.  *      |                 |
  321.  *      -------------------
  322.  *      |prev| lable |next|
  323.  *      -------------------
  324.  * </pre>
  325.  *
  326.  * @version 0.00 28-Mar-96
  327.  * @author  lning & Ted S.
  328. */
  329. class ColumnAndButtons extends Column
  330.     {
  331.         private Button btnPrv = new Button("Previous Story");
  332.         private Button btnNxt = new Button("Next Story");
  333.         private Label  lbStory = new Label("",Label.CENTER);
  334.  
  335.     // --- Public constructors
  336.  
  337.     /**
  338.      * Construct a ColumnAndButtons as a standalone page.
  339.      * @param pagename  The name of the standalone page.
  340.      * @param section   The name of the observed section.
  341.      * @param p         The paper that contains the observed section.
  342.     */
  343.     public ColumnAndButtons(String pagename, String section, Paper p)
  344.         {
  345.         super(pagename,section,p);
  346.         // Start with a clean slate, no components
  347.                 removeAll();
  348.         // Add all the componetns I need
  349.                 iniLayout();
  350.                 updateLabel();
  351.         }
  352.  
  353.     /**
  354.      * Construct a Column as a page component.
  355.      * @param section   The name of the observed section.
  356.      * @param paper     The paper that contains the observed section.
  357.     */
  358.     public ColumnAndButtons(String section, Paper paper)
  359.         {
  360.         this(null,section,paper);
  361.         }
  362.  
  363.     // --- Public operations
  364.  
  365.  
  366.  
  367.     // Handle button clicks
  368.     public boolean action(Event e, Object arg)
  369.         {
  370.         boolean bHandled = false;
  371.  
  372.                 if ( "Previous Story".equals(arg) )
  373.                         {
  374.                         previousView();
  375.             bHandled = true;
  376.                         }
  377.  
  378.                 else
  379.                 if ( "Next Story".equals(arg) )
  380.                         {
  381.                         nextView();
  382.                         bHandled = true;
  383.                         }
  384.  
  385.  
  386.         return bHandled;
  387.         } // action
  388.  
  389.         protected void iniLayout()
  390.         {
  391.         btnPrv.setFont( PjFinals.fntColumnLabel );
  392.         btnNxt.setFont( PjFinals.fntColumnLabel );
  393.                 lbStory.setFont( PjFinals.fntColumnLabel );
  394.  
  395.         Panel    pnlColumn = new Panel();
  396.  
  397.         GridBagLayout       gridbag = new GridBagLayout();
  398.         GridBagConstraints  gbc = new GridBagConstraints();
  399.         Panel               pnlButtons = new Panel();
  400.  
  401.  
  402.         //GridBagLayout for column and button panels.
  403.         setLayout(gridbag);
  404.  
  405.         //Button panel. false: un-constrained width.
  406.         pnlButtons.setLayout( new PjBtnLbLayout(false) );
  407.         pnlButtons.add( btnPrv );
  408.                 pnlButtons.add( lbStory );
  409.         pnlButtons.add( btnNxt );
  410.  
  411.         //Column panel
  412.         gbc.weightx = 1.0;
  413.         gbc.weighty = 1.0;
  414.  
  415.         gbc.fill = GridBagConstraints.BOTH;
  416.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  417.         gridbag.setConstraints(ltb, gbc);
  418.         add(ltb);
  419.  
  420.         // Button panel
  421.         gbc.weighty = 0;
  422.         gbc.fill = GridBagConstraints.HORIZONTAL;
  423.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  424.         gridbag.setConstraints(pnlButtons,gbc);
  425.         add(pnlButtons);
  426.         }
  427.  
  428.  
  429.  
  430.  
  431.     // Make sure the label # of # is correct
  432.         private void updateLabel()
  433.             {
  434.         // Update the label.
  435.         String str = Integer.toString(currView()) + "  of  " + Integer.toString(countViews());
  436.         lbStory.setText(str);
  437.  
  438.         // Disable or enable buttons.
  439.         btnPrv.enable( currView() > 1 );
  440.             btnNxt.enable( currView() < countViews() );
  441.             } //updateLabel
  442.  
  443.  
  444.     /**
  445.      * Make the first story of the observed section the current view.
  446.     */
  447.     public void firstView()
  448.         {
  449.         super.firstView();
  450.                 updateLabel();
  451.         }
  452.  
  453.     /**
  454.      * Make the next story (after the current story) the current view.
  455.     */
  456.     public boolean nextView()
  457.         {
  458.                 boolean b = super.nextView();
  459.                 updateLabel();
  460.                 return b;
  461.         }
  462.  
  463.     /**
  464.      * Make the last story of the observed section the current view.
  465.     */
  466.     public void lastView()
  467.         {
  468.         super.lastView();
  469.                 updateLabel();
  470.         }
  471.  
  472.     /**
  473.      * Make the previous story (before the current story) the current view.
  474.     */
  475.     public boolean previousView()
  476.         {
  477.                 boolean b = super.previousView();
  478.                 updateLabel();
  479.                 return b;
  480.         }
  481.  
  482.     /**
  483.      * Make a specific story the current view.
  484.      * @param idx The index of the view to display.
  485.     */
  486.     public void view(int idx)
  487.         {
  488.         // fixme:  view are 1 based, but the caller of this routine is
  489.         //   0 based.  That caller should be fixed to be one based, and the
  490.         //   + 1 should be removed from here.
  491.         super.view(idx + 1);
  492.         updateLabel();
  493.         }
  494.  
  495.  
  496.     public Dimension minimumSize()
  497.         {
  498.         return ltb.minimumSize();
  499.         }
  500.  
  501.     public Dimension preferredSize()
  502.         {
  503.         return ltb.preferredSize();
  504.         }
  505.  
  506.  
  507.  
  508.  
  509.     } // ColumnAdnButtons
  510.  
  511.  
  512.  
  513.  
  514.  
  515.