home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / marketsection.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  18.0 KB  |  662 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.     @(#)MarketSection.java  0.00 22-Jan-96
  15.  
  16.         A SectionView for the Market pages.
  17.  
  18.     Authors:
  19.  
  20.         rphall   Rick Hall
  21.         jlee     James Lee
  22.  
  23.     Version Ident:
  24.  
  25.         $Header: /PjJavaClient/src/pj/awt/MarketSection.java 14    3/22/96 11:53p Jlee $
  26.  
  27.     History:
  28.  
  29.         0.00 22-Jan-96  rphall      Initial Creation
  30.         0.01 08-Feb-96  jlee        Adopted custom layout managers: PjListLayout and PjButtonLayout.
  31.         0.02 23-Feb-96  Ted S.      Adjustments for PaperView changes. Also changes for icons.
  32.         0.03 22-Mar-96  jlee        Fixed market News's column's font using PjFinals' font variable.
  33.         0.04 27-Mar-96  Ted S.      Made use of BullOrBear.
  34.  
  35. ---------------------------------------------------------------------------*/
  36.  
  37. package pj.awt;
  38.  
  39. import pj.awt.ArrayFormatter;
  40. import pj.awt.Chart;
  41. import pj.awt.Column;
  42. import pj.awt.DJChart;
  43. import pj.awt.PageView;
  44. import pj.awt.PjImages;
  45. import pj.awt.PjButtonLayout;
  46. import pj.awt.PjListLayout;
  47. import pj.awt.PjPageSpec;
  48. import pj.awt.PjFinals;
  49. import pj.awt.PjStr;
  50. import pj.awt.SectionView;
  51. import pj.awt.BullOrBear;
  52. import pj.io.Paper;
  53.  
  54.  
  55. import collections.Assertable;
  56. import collections.ImplementationError;
  57. import java.awt.Button;
  58. import java.awt.Color;
  59. import java.awt.Component;
  60. import java.awt.Container;
  61. import java.awt.Event;
  62. import java.awt.FlowLayout;
  63. import java.awt.Font;
  64. import java.awt.GridBagConstraints;
  65. import java.awt.GridBagLayout;
  66. import java.awt.Insets;
  67. import java.awt.Label;
  68. import java.awt.Panel;
  69. import java.awt.Dimension ;
  70. import java.awt.Rectangle;
  71.  
  72.  
  73.  
  74. /**
  75.  * A SectionView for the Market pages.
  76.  *
  77.  * @see SectionView
  78.  * @version 0.00 22-Jan-96
  79.  * @author  rphall
  80. */
  81. public class MarketSection extends SectionView implements Assertable
  82.     {
  83.  
  84.  
  85.  
  86.  
  87.     // --- Class variables
  88.  
  89.     /**
  90.      * Labels for the Markets buttons
  91.      * and page names for the Market views
  92.     */
  93.     static final String strStkIdx   = "Stock Indexes";
  94.     static final String strMarkets  = "Markets";
  95.     static final String strWrap     = "Market News";
  96.     static final String strExTrs    = "Currencies and Bonds";
  97.     static final String strDjia     = "Dow Jones Industrial Average";
  98.     static final String strExRates  = "Exchange Rates    (Per U.S. Dollar)";
  99.     static final String strUSTrs    = "U.S. Treasurys  (Updated Daily)";
  100.  
  101.  
  102.     // --- Instance variables
  103.  
  104.     private PageView pvStkIdx; // Stock Indexes
  105.     private PageView pvExTrs;  // Currencies & Bonds
  106.     private PageView pvWrap;   // Wrapup
  107.     private Paper    paper;
  108.  
  109.     // --- Public constructors
  110.  
  111.     /**
  112.      * A general-purpose constructor that uses preassembled
  113.      * components.
  114.      * @param stkidx A view of Stocks Indexes.
  115.      * @param extrs  A view of Currencies and Bonds.
  116.      * @param wrap   A view of the Market Wrapup.
  117.      * @param pagespec A specification for the page.
  118.      * @param paper The paper being observed.
  119.     */
  120.     public MarketSection( PageView stkidx, PageView extrs,
  121.             PageView wrap, PjPageSpec pagespec, Paper p)
  122.         {
  123.         super(pagespec.tabspec);
  124.  
  125.         pvStkIdx = stkidx;
  126.         pvExTrs  = extrs;
  127.         pvWrap   = wrap;
  128.         paper = p;
  129.  
  130.         initialLayout();
  131.  
  132.         } // MarketSection constructor
  133.  
  134.     /**
  135.      * A specialized constructor for a MarketSection that uses
  136.      * a page specification.
  137.      * @param pagespec  A page specification, either for the
  138.      * Front Page headlines or the ThisJustIn headlines.
  139.      * @param p The paper being observed.
  140.     */
  141.     public MarketSection(PjPageSpec pagespec, Paper p)
  142.         {
  143.         super(pagespec.tabspec);
  144.         paper = p;
  145.  
  146.         if ( pagespec.strPageName.equals( PjStr.pageMrk) )
  147.             {
  148.             pvStkIdx = new MrkViewStkIdx(paper);
  149.             pvExTrs  = new MrkViewExTrs(paper);
  150.             pvWrap   = new MrkViewWrap(paper);
  151.             }
  152.  
  153.         else
  154.             {
  155.             // Bad page specification
  156.             assert(false);
  157.             }
  158.  
  159.         initialLayout();
  160.         System.out.println("Debug-MarketSection:constructed");
  161.         } // MarketSection constructor
  162.  
  163.  
  164.  
  165.  
  166.     // --- Public operations
  167.  
  168.     /**
  169.      * @return The number (one) of views in a MarketSection.
  170.     */
  171.     public int countViews()
  172.         {
  173.         int num = 0;
  174.         num += pvStkIdx.countViews();
  175.         num += pvExTrs.countViews();
  176.         num += pvWrap.countViews();
  177.         return num;
  178.         }
  179.  
  180.     public boolean action(Event e, Object arg)
  181.         {
  182.         boolean bHandled = false;
  183.  
  184.         // FIXME: a weak check for a hit
  185.         if ( arg instanceof String )
  186.             {
  187.             bHandled = true;
  188.             stack.page((String)arg);
  189.             }
  190.  
  191.         // Let super handle action
  192.         else
  193.             {
  194.             bHandled = super.action(e,arg);
  195.             }
  196.  
  197.         return bHandled;
  198.         } // action
  199.  
  200.     /**
  201.      * Raise an exception if predicate is false.
  202.      * @see collections.Assertable
  203.     */
  204.     public void assert(boolean predicate) throws ImplementationError
  205.         {
  206.         ImplementationError.assert(this, predicate);
  207.         }
  208.  
  209.     // --- Private operations
  210.  
  211.     private void initialLayout()
  212.         {
  213.         assert( pvStkIdx!= null );
  214.         assert( pvExTrs != null );
  215.         assert( pvWrap  != null );
  216.  
  217.         appendPage(pvStkIdx);
  218.         appendPage(pvExTrs);
  219.         appendPage(pvWrap);
  220.         }
  221.  
  222.  
  223.     } // MarketSection
  224.  
  225.  
  226.  
  227. /**
  228.  * A helper class that creates a view of the DJIA and Stock Indexes
  229. */
  230. class MrkViewStkIdx extends PageView
  231.     {
  232.  
  233.     // --- Public constructors
  234.  
  235.     public MrkViewStkIdx(Paper paper)
  236.         {
  237.         super(MarketSection.strStkIdx);
  238.  
  239.         Label lblDjia = new Label(MarketSection.strDjia);
  240.         lblDjia.setFont( PjFinals.fntColumnLabel );
  241.  
  242.         Chart chartDjia = new Chart("DOW", "Dow Jones", paper.idMrkDjia,paper);
  243.         Panel pnlDjia   = new Panel();
  244.  
  245.         pnlDjia.setLayout( new PjListLayout() );
  246.  
  247.         pnlDjia.add( lblDjia );
  248.         pnlDjia.add( chartDjia);
  249.  
  250.         PageView pvStocks   = new Column(Paper.idMrkStkIdx,paper);
  251.         ((Column)pvStocks).setStoryFormatter( new ArrayFormatter(Paper.idMrkStkIdx) );
  252.         ((Column)pvStocks).setFont( PjFinals.fntFormattedColumn );
  253.  
  254.         Button btnWrap      = new Button(MarketSection.strWrap);
  255.         Button btnExTrs     = new Button(MarketSection.strExTrs);
  256.         ViewAndButtons vabs = new ViewAndButtons( pvStocks, btnWrap, btnExTrs, MarketSection.strStkIdx, paper);
  257.  
  258.         GridBagLayout gridbag   = new GridBagLayout();
  259.         GridBagConstraints gbc  = new GridBagConstraints();
  260.         setLayout(gridbag);
  261.  
  262.         // Create a panel to hold the icon and the text for page title.
  263.         Panel pnlHeader = new Panel();
  264.         pnlHeader.setLayout( new FlowLayout( FlowLayout.LEFT ) );
  265.  
  266.  
  267.         // Add the bull or bear icon.
  268.         // fixme: determine bull or bear.
  269.         pnlHeader.add( new BullOrBear( paper ) );
  270.  
  271.         // Add the title
  272.         Label    lblTitle    = new Label(MarketSection.strMarkets);
  273.         lblTitle.setFont( PjFinals.fntSectionBigTitle );
  274.         pnlHeader.add( lblTitle );
  275.  
  276.         // Add the header panel with its contents to the page.
  277.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  278.         gbc.anchor=GridBagConstraints.WEST;
  279.         gridbag.setConstraints(pnlHeader,gbc);
  280.         add( pnlHeader );
  281.  
  282.  
  283.         // Djia
  284.         //   gbc.weightx = 1.0;
  285.         gbc.weighty = 1.0;
  286.  
  287.  
  288.  
  289.         gbc.fill=GridBagConstraints.BOTH;
  290.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  291.         gridbag.setConstraints(pnlDjia,gbc);
  292.         add(pnlDjia);
  293.  
  294.         // Indexes ViewAndButtons
  295.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  296.         gridbag.setConstraints(vabs,gbc);
  297.         add(vabs);
  298.  
  299.         } // constructor
  300.  
  301.     // --- Public operations
  302.  
  303.     public int countViews()
  304.         {
  305.         return 1;
  306.         }
  307.  
  308.     } // MrkViewStkIdx
  309.  
  310.  
  311.  
  312. /**
  313.  * A helper class that creates a view of Currencies and Bonds
  314. */
  315. class MrkViewExTrs extends PageView
  316.     {
  317.  
  318.     // --- Public constructors
  319.  
  320.     public MrkViewExTrs(Paper paper)
  321.         {
  322.         super(MarketSection.strExTrs);
  323.  
  324.         Label    lblEx = new Label(MarketSection.strExRates);
  325.         lblEx.setFont( PjFinals.fntColumnLabel );
  326.  
  327.         PageView pvEx = new Column(Paper.idMrkExch,paper);
  328.         ((Column)pvEx).setStoryFormatter(
  329.                 new ArrayFormatter(Paper.idMrkExch) );
  330.  
  331.         ((Column)pvEx).setFont( PjFinals.fntFormattedColumn );
  332.         Panel    pnlEx = new Panel();
  333.  
  334.         pnlEx.setLayout( new PjListLayout() );
  335.         pnlEx.add( lblEx );
  336.         pnlEx.add( pvEx );
  337.  
  338.         PageView pvTrs = new Column(Paper.idMrkTreas,paper);
  339.         ((Column)pvTrs).setStoryFormatter(
  340.                 new ArrayFormatter(Paper.idMrkTreas) );
  341.  
  342.         ((Column)pvTrs).setFont( PjFinals.fntFormattedColumn );
  343.  
  344.         Button btnStkIdx = new Button(MarketSection.strStkIdx);
  345.         Button btnWrap = new Button(MarketSection.strWrap);
  346.  
  347.         ViewAndButtons vabs = new ViewAndButtons(pvTrs, btnStkIdx, btnWrap, MarketSection.strUSTrs, paper);
  348.  
  349.         GridBagLayout gridbag = new GridBagLayout();
  350.         GridBagConstraints gbc = new GridBagConstraints();
  351.         setLayout(gridbag);
  352.  
  353.  
  354.          // Create a panel to hold the icon and the text for page title.
  355.         Panel pnlHeader = new Panel();
  356.         pnlHeader.setLayout( new FlowLayout( FlowLayout.LEFT ) );
  357.  
  358.  
  359.         // Add the bull or bear icon.
  360.         pnlHeader.add( new BullOrBear( paper ) );
  361.  
  362.         // Add the title
  363.         Label    lblTitle    = new Label(MarketSection.strExTrs);
  364.         lblTitle.setFont( PjFinals.fntSectionBigTitle );
  365.         pnlHeader.add( lblTitle );
  366.  
  367.         // Add the header panel with its contents to the page.
  368.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  369.         gbc.anchor=GridBagConstraints.WEST;
  370.         gridbag.setConstraints(pnlHeader,gbc);
  371.         add( pnlHeader );
  372.  
  373.         // Exchange rates
  374.         gbc.weightx = 1.0;
  375.         gbc.weighty = 1.0;
  376.  
  377.         gbc.fill=GridBagConstraints.BOTH;
  378.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  379.         gridbag.setConstraints(pnlEx,gbc);
  380.         add(pnlEx);
  381.  
  382.         // Treasuries ViewAndButtons
  383.         gbc.gridwidth=GridBagConstraints.REMAINDER;
  384.         gridbag.setConstraints(vabs,gbc);
  385.         add(vabs);
  386.  
  387.         } // constructor
  388.  
  389.     // --- Public operations
  390.  
  391.     public int countViews()
  392.         {
  393.         return 1;
  394.         }
  395.  
  396.     } // MrkViewExTrs
  397.  
  398.  
  399.  
  400. /**
  401.  * A helper class that creates a view of the DJIA and Stock Indexes
  402. */
  403. class MrkViewWrap extends ViewAndButtons
  404.     {
  405.  
  406.     // --- Public constructors
  407.  
  408.     public MrkViewWrap(Paper paper)
  409.         {
  410.         super
  411.             (
  412.             new Column(Paper.idMrkWrap,paper),      // view
  413.             new Button(MarketSection.strExTrs),     // left button
  414.             new Button(MarketSection.strStkIdx),    // right button
  415.             MarketSection.strWrap,                  // (local) page name
  416.             paper
  417.             );
  418.  
  419.         } // constructor
  420.  
  421.     // --- Public operations
  422.  
  423.     public int countViews()
  424.         {
  425.         return 1;
  426.         }
  427.  
  428.     } // MrkViewWrap
  429.  
  430.  
  431.  
  432. /**
  433.  * A PageView containing three components: a view and two buttons
  434.  * laid out beneath the subpanel.  This is a helper class for Market
  435.  * views.
  436.  * <P>
  437.  * The layout of ViewAndButtons is:
  438.  * <pre>
  439.  *      -------------------
  440.  *      |                 |
  441.  *      |       view      |
  442.  *      |                 |
  443.  *      -------------------
  444.  *      |  left  |  right |
  445.  *      -------------------
  446.  * </pre>
  447.  *
  448.  * @version 0.00 01-Jan-96
  449.  * @author  rphall
  450. */
  451. class ViewAndButtons extends PageView
  452.     {
  453.  
  454.     // --- Instance variables
  455.  
  456.     PageView  pageview;
  457.     Button    btnLeft;
  458.     Button    btnRight;
  459.  
  460.  
  461.     // --- Public constructors
  462.  
  463.     /**
  464.      * Construct a ViewAndButtons as a standalone page.
  465.      * @param pv Some page view, already constructed.
  466.      * @param l Left button.
  467.      * @param r Right button.
  468.      * @param pagename  The name of the standalone page.
  469.     */
  470.     public ViewAndButtons(PageView pv, Button l, Button r, String pagename, Paper paper )
  471.         {
  472.  
  473.         super(pagename);
  474.  
  475.         pageview = pv;
  476.         btnLeft = l;
  477.         btnRight = r;
  478.  
  479.         btnLeft.setFont( PjFinals.fntColumnLabel );
  480.         btnRight.setFont( PjFinals.fntColumnLabel );
  481.  
  482.         Label    lblPageView = new Label(pagename, Label.LEFT);
  483.         lblPageView.setFont( PjFinals.fntColumnLabel );
  484.         Panel    pnlPageView = new Panel();
  485.  
  486.         GridBagLayout       gridbag = new GridBagLayout();
  487.         GridBagConstraints  gbc = new GridBagConstraints();
  488.         Panel               pnlButtons = new Panel();
  489.  
  490.  
  491.         if ( pagename.equals( MarketSection.strWrap ) )
  492.             {
  493.             // Set the font
  494.             pageview.setFont( PjFinals.fntUnfColumnPlain );
  495.  
  496.             //GridBagLayout for column and button panels.
  497.             setLayout(gridbag);
  498.  
  499.             //Button panel. false: un-constrained width.
  500.             pnlButtons.setLayout( new PjButtonLayout(false) );
  501.             pnlButtons.add( btnLeft );
  502.             pnlButtons.add( btnRight );
  503.  
  504.             // Create a panel to hold the icon and the text for page title.
  505.             Panel pnlHeader = new Panel();
  506.             pnlHeader.setLayout( new FlowLayout( FlowLayout.LEFT ) );
  507.  
  508.             // Add the bull or bear icon.
  509.             if ( null != paper )
  510.                 pnlHeader.add( new BullOrBear( paper ) );
  511.  
  512.             // Add the title
  513.             //Culumn title
  514.             lblPageView.setFont( PjFinals.fntSectionBigTitle );
  515.             gbc.gridwidth = GridBagConstraints.REMAINDER;
  516.             gridbag.setConstraints(lblPageView, gbc);
  517.             pnlHeader.add(lblPageView);
  518.  
  519.             // Add the header panel with its contents to the page.
  520.             gbc.gridwidth=GridBagConstraints.REMAINDER;
  521.             gbc.anchor=GridBagConstraints.WEST;
  522.             gridbag.setConstraints(pnlHeader,gbc);
  523.             add( pnlHeader );
  524.  
  525.             //Column panel
  526.             gbc.weightx = 1.0;
  527.             gbc.weighty = 1.0;
  528.  
  529.             gbc.fill = GridBagConstraints.BOTH;
  530.             gbc.gridwidth = GridBagConstraints.REMAINDER;
  531.             gridbag.setConstraints(pageview, gbc);
  532.             add(pageview);
  533.  
  534.             // Button panel
  535.             gbc.weighty = 0;
  536.             gbc.fill = GridBagConstraints.HORIZONTAL;
  537.             gbc.gridwidth=GridBagConstraints.REMAINDER;
  538.             gridbag.setConstraints(pnlButtons,gbc);
  539.             add(pnlButtons);
  540.             }
  541.         else
  542.             {
  543.             //GridBagLayout for column and button panels.
  544.             setLayout(gridbag);
  545.  
  546.             //Column panel
  547.             pnlPageView.setLayout( new PjListLayout() );
  548.             pnlPageView.add( lblPageView );
  549.             pnlPageView.add( pageview );
  550.  
  551.             //Button panel. true: constrained width.
  552.             pnlButtons.setLayout( new PjButtonLayout(true) );
  553.             pnlButtons.add( btnLeft );
  554.             pnlButtons.add( btnRight );
  555.  
  556.             // Column panel
  557.             gbc.weightx = 1.0;
  558.             gbc.weighty = 1.0;
  559.  
  560.             gbc.fill=GridBagConstraints.BOTH;
  561.             gbc.gridwidth=GridBagConstraints.REMAINDER;
  562.             gridbag.setConstraints(pnlPageView,gbc);
  563.             add(pnlPageView);
  564.  
  565.             // Button panel
  566.             gbc.weighty = 0;
  567.             gbc.gridwidth=GridBagConstraints.REMAINDER;
  568.             gridbag.setConstraints(pnlButtons,gbc);
  569.             add(pnlButtons);
  570.             }
  571.  
  572.         } // gen'l constructor
  573.  
  574.     /**
  575.      * Construct a ViewAndButtons as a page component.
  576.      * @param pv Some page view, already constructed.
  577.      * @param left Left button.
  578.      * @param right Right button.
  579.     */
  580.     public ViewAndButtons(PageView pv, Button left, Button right)
  581.         {
  582.         this(pv,left,right,(String)null, (Paper)null);
  583.         }
  584.  
  585.  
  586.     // --- Public operations
  587.  
  588.     /**
  589.      * @return The number of stories in the observed section.
  590.     */
  591.     public int countViews()
  592.         {
  593.         return pageview.countViews();
  594.         }
  595.  
  596.     /**
  597.      * Make the first story of the observed section the current view.
  598.     */
  599.     public void firstView()
  600.         {
  601.         pageview.firstView();
  602.         }
  603.  
  604.     /**
  605.      * Make the next story (after the current story) the current view.
  606.     */
  607.     public boolean nextView()
  608.         {
  609.         return pageview.nextView();
  610.         }
  611.  
  612.     /**
  613.      * Make the last story of the observed section the current view.
  614.     */
  615.     public void lastView()
  616.         {
  617.         pageview.lastView();
  618.         }
  619.  
  620.     /**
  621.      * Make the previous story (before the current story) the current view.
  622.     */
  623.     public boolean previousView()
  624.         {
  625.         return pageview.previousView();
  626.         }
  627.  
  628.  
  629.  
  630.     /**
  631.      * Make a specific story the current view.
  632.      * @param idx The index of the view to display.
  633.     */
  634.     public void view(int idx)
  635.         {
  636.         pageview.view(idx);
  637.         }
  638.  
  639.     /*
  640.     public Dimension minimumSize()
  641.         {
  642.         return wwh.minimumSize();
  643.         }
  644.  
  645.     public Dimension preferredSize()
  646.         {
  647.         return wwh.preferredSize();
  648.         }
  649.     */
  650.  
  651.     // --- Private operations
  652.  
  653.     private void postEventToParent(Event evt)
  654.         {
  655.         Container c = getParent();
  656.         if ( c != null ) c.postEvent(evt);
  657.         }
  658.  
  659.  
  660.  
  661.     } // ViewAndButtons
  662.