home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / weathersection.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  13.1 KB  |  429 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.     @(#)WeatherSection.java  0.00 26-Jan-96
  15.  
  16.         A SectionView for the Weather.
  17.  
  18.     Authors:
  19.  
  20.         jlee    James Lee
  21.         rphall  Rick Hall
  22.  
  23.     Version Ident:
  24.  
  25.         $Header: /PjJavaClient/src/pj/awt/WeatherSection.java 11    3/22/96 11:13p Jlee $
  26.  
  27.     History:
  28.  
  29.         0.00 26-Jan-96  jlee      Initial Creation
  30.              13-Feb-96  rphall    Defined WeatherColumn
  31.              14-Feb-96  jlee      Added Map button functionality
  32.              21-Feb-96  jlee      Changed(corrected) regional page's previous GridBagLayout to PjListLayout.
  33.              3-Mar-96   Ted S.    Added weather icon.
  34.              3-Mar-96   Ted S.    Removed countViews(), let parent class handle it.
  35.             26-Mar-96   jlee      Changed "&Map" to "Map"
  36. ---------------------------------------------------------------------------*/
  37.  
  38. package pj.awt;
  39.  
  40. import pj.awt.Column;
  41. import pj.awt.Notebook;
  42. import pj.awt.PjImages;
  43. import pj.awt.PjListLayout;
  44. import pj.awt.PjPageSpec;
  45. import pj.awt.PjFinals;
  46. import pj.awt.PjStr;
  47. import pj.awt.SectionView;
  48. import pj.awt.StoryFormatter;
  49. import pj.awt.WeatherView;
  50. import pj.io.MalformedPaperStoryException;
  51. import pj.io.Paper;
  52. import pj.io.PaperStory;
  53. import pj.net.RemoteImage;
  54.  
  55. import collections.Assertable;
  56. import collections.ImplementationError;
  57. import java.awt.Button;
  58. import java.awt.Component;
  59. import java.awt.Event;
  60. import java.awt.FlowLayout;
  61. import java.awt.Font;
  62. import java.awt.GridBagLayout;
  63. import java.awt.GridBagConstraints;
  64. import java.awt.Insets;
  65. import java.awt.Label;
  66. import java.awt.Panel;
  67.  
  68. /**
  69.  * A SectionView for Weather.
  70.  *
  71.  * @see DividerView
  72.  * @version 0.00 26-Jan-96
  73.  * @author  jlee
  74. */
  75. public class WeatherSection extends SectionView implements Assertable
  76.     {
  77.  
  78.     // --- Instance variables
  79.  
  80.     private WeatherView     hvWeather;
  81.     private RemoteImage     riWeather;
  82.     private Paper           paper;
  83.  
  84.     private PageView        pvNortheast;
  85.     private PageView        pvSoutheast;
  86.     private PageView        pvMiddleUS;
  87.     private PageView        pvWest;
  88.     private PageView        pvCanada;
  89.     private PageView        pvSAmCarib;
  90.     private PageView        pvEurope;
  91.     private PageView        pvMideast;
  92.     private PageView        pvAfrica;
  93.     private PageView        pvAsiaAustrl;
  94.  
  95.  
  96.     private final String    pgWeather    = "Weather";
  97.  
  98.     private final String    pgNortheast  = "U.S. Northeast";
  99.     private final String    pgSoutheast  = "U.S. Southeast";
  100.     private final String    pgMiddleUS   = "U.S. Middle";
  101.     private final String    pgWest       = "U.S. West";
  102.     private final String    pgCanada     = "Canada";
  103.     private final String    pgSAmCarib   = "South America/Caribbean";
  104.     private final String    pgEurope     = "Europe";
  105.     private final String    pgMideast    = "Middle East";
  106.     private final String    pgAfrica     = "Africa";
  107.     private final String    pgAsiaAustrl = "Asia/Australia";
  108.  
  109.     private final String    strMap ="Map";
  110.     // --- Public constructors
  111.  
  112.     /**
  113.      * A general-purpose constructor that uses preassembled
  114.      * components.
  115.      * @param hv A WeatherView.
  116.      * @param pagespec A specification for the page.
  117.      * @param paper The paper being observed.
  118.     */
  119.     public WeatherSection( WeatherView hv, PjPageSpec pagespec, Paper p)
  120.         {
  121.         super(pagespec.tabspec);
  122.  
  123.         hvWeather = hv;
  124.         paper = p;
  125.  
  126.         // Column pages creation
  127.         InitColumns();
  128.  
  129.         initialLayout();
  130.  
  131.         } // WeatherSection constructor
  132.  
  133.     /**
  134.      * A specialized constructor for a WeatherSection that uses
  135.      * a page specification.
  136.      * @param pagespec  A page specification, either for the Weather.
  137.      * @param pas A PjAdSpec object.
  138.      * @param p The paper being observed.
  139.      * @param riWeatherMap The Weather Map image.
  140.     */
  141.     public WeatherSection(PjPageSpec pagespec, PjAdSpec pas, Paper p, RemoteImage riWeatherMap)
  142.         {
  143.         super(pagespec.tabspec);
  144.  
  145.         paper = p;
  146.  
  147.         // Column pages creation
  148.         InitColumns();
  149.  
  150.         // Weather viewer ceation
  151.         hvWeather = new WeatherView(pagespec.strPageName, pas, riWeatherMap);
  152.         hvWeather.setName(pgWeather);
  153.  
  154.         initialLayout();
  155.  
  156.         System.out.println("Debug-WeatherSection:constructed");
  157.         } // WeatherSection constructor
  158.  
  159.  
  160.  
  161.  
  162.  
  163.     // --- Public operations
  164.  
  165.     /**
  166.      * Handles events
  167.      *
  168.      * @param evt An Event object
  169.      * @return true if the event's handled, false otherwise.
  170.      */
  171.     public boolean handleEvent(Event evt)
  172.         {
  173.         boolean bHandled = false;
  174.  
  175.         switch (evt.id)
  176.             {
  177.             default:
  178.                 bHandled = super.handleEvent(evt);
  179.                 break;
  180.             } // switch
  181.  
  182.         return bHandled;
  183.         } // handleEvent
  184.  
  185.     /**
  186.      * Handles ACTION_EVENT. Specifically, when you click one of those regions in
  187.      * the weather map, it'll generate this action event,  which will be eventually handled here.
  188.      *
  189.      * @param e An Event object
  190.      * @param arg An String object which is the name of the hot area which user cliked on.
  191.      * @return true if further event handling's neccessary, false otherwise.
  192.      */
  193.     public boolean action(Event e, Object arg)
  194.         {
  195.         boolean bHandled = false;
  196.  
  197.         if ( pgNortheast.equals( arg ) )
  198.             {
  199.             bHandled = true;
  200.             stack.page(pgNortheast);
  201.             }
  202.         else if ( pgSoutheast.equals( arg ) )
  203.             {
  204.             bHandled = true;
  205.             stack.page(pgSoutheast);
  206.             }
  207.         else if ( pgMiddleUS.equals( arg ) )
  208.             {
  209.             bHandled = true;
  210.             stack.page(pgMiddleUS);
  211.             }
  212.         else if ( pgWest.equals( arg ) )
  213.             {
  214.             bHandled = true;
  215.             stack.page(pgWest);
  216.             }
  217.         else if ( pgCanada.equals( arg ) )
  218.             {
  219.             bHandled = true;
  220.             stack.page(pgCanada);
  221.             }
  222.         else if ( pgSAmCarib.equals( arg ) )
  223.             {
  224.             bHandled = true;
  225.             stack.page(pgSAmCarib);
  226.             }
  227.         else if ( pgEurope.equals( arg ) )
  228.             {
  229.             bHandled = true;
  230.             stack.page(pgEurope);
  231.             }
  232.         else if ( pgMideast.equals( arg ) )
  233.             {
  234.             bHandled = true;
  235.             stack.page(pgMideast);
  236.             }
  237.         else if ( pgAfrica.equals( arg ) )
  238.             {
  239.             bHandled = true;
  240.             stack.page(pgAfrica);
  241.             }
  242.         else if ( pgAsiaAustrl.equals( arg ) )
  243.             {
  244.             bHandled = true;
  245.             stack.page(pgAsiaAustrl);
  246.             }
  247.         else if ( strMap.equals( arg ) )
  248.             {
  249.             bHandled = true;
  250.             firstPage();
  251.             }
  252.  
  253.         return bHandled;
  254.         } // action
  255.  
  256.     /**
  257.      * Raise an exception if predicate is false.
  258.      * @see collections.Assertable
  259.     */
  260.     public void assert(boolean predicate) throws ImplementationError
  261.         {
  262.         ImplementationError.assert(this, predicate);
  263.         }
  264.  
  265.  
  266.     // --- Private operations
  267.  
  268.  
  269.     /**
  270.      * Weather column pages init.
  271.      */
  272.     private void InitColumns()
  273.         {
  274.         pvNortheast = new WeatherColumn(pgNortheast, Paper.idWthUSNE,  paper);
  275.         pvSoutheast = new WeatherColumn(pgSoutheast, Paper.idWthUSSE,  paper);
  276.         pvMiddleUS  = new WeatherColumn(pgMiddleUS,  Paper.idWthUSMW,  paper);
  277.         pvWest      = new WeatherColumn(pgWest,      Paper.idWthUSWest,paper);
  278.         pvCanada    = new WeatherColumn(pgCanada,    Paper.idWthCanada,paper);
  279.         pvSAmCarib  = new WeatherColumn(pgSAmCarib,  Paper.idWthSouthA,paper);
  280.         pvEurope    = new WeatherColumn(pgEurope,    Paper.idWthEurope,paper);
  281.         pvMideast   = new WeatherColumn(pgMideast,   Paper.idWthMidE,  paper);
  282.         pvAfrica    = new WeatherColumn(pgAfrica,    Paper.idWthAfrica,paper);
  283.         pvAsiaAustrl= new WeatherColumn(pgAsiaAustrl,Paper.idWthAsia,  paper);
  284.         }
  285.  
  286.     private void initialLayout()
  287.         {
  288.         assert( hvWeather != null );
  289.         assert( pvNortheast != null );
  290.         assert( pvSoutheast != null );
  291.         assert( pvMiddleUS != null );
  292.         assert( pvWest != null );
  293.         assert( pvCanada != null );
  294.         assert( pvSAmCarib != null );
  295.         assert( pvEurope != null );
  296.         assert( pvMideast != null );
  297.         assert( pvAfrica != null );
  298.         assert( pvAsiaAustrl != null );
  299.         assert( paper != null );
  300.  
  301.  
  302.  
  303.         appendPage(hvWeather);
  304.         appendPage(layoutPage( pvNortheast,     pgNortheast  ));
  305.         appendPage(layoutPage( pvSoutheast,     pgSoutheast  ));
  306.         appendPage(layoutPage( pvMiddleUS,      pgMiddleUS   ));
  307.         appendPage(layoutPage( pvWest,          pgWest       ));
  308.         appendPage(layoutPage( pvCanada,        pgCanada     ));
  309.         appendPage(layoutPage( pvSAmCarib,      pgSAmCarib   ));
  310.         appendPage(layoutPage( pvEurope,        pgEurope     ));
  311.         appendPage(layoutPage( pvMideast,       pgMideast    ));
  312.         appendPage(layoutPage( pvAfrica,        pgAfrica     ));
  313.         appendPage(layoutPage( pvAsiaAustrl,    pgAsiaAustrl ));
  314.  
  315.         }
  316.  
  317.  
  318.     private Page layoutPage( PageView pv, String name )
  319.          {
  320.          //Create GridBagLayout
  321.          GridBagLayout gridbag = new GridBagLayout();
  322.          GridBagConstraints gbc = new GridBagConstraints();
  323.  
  324.          //Create panels, label and button, pnlPage will contain pnlColumn
  325.          Page   pnlPage = new Page(name);
  326.          Panel  pnlColumn = new Panel();
  327.          Label  lblPageTitle = new Label(pgWeather);
  328.          Label  lblPageSubTitle = new Label(name);
  329.          Button btnMap = new Button( strMap );
  330.  
  331.          //Set fonts
  332.          lblPageTitle.setFont( PjFinals.fntSectionBigTitle );
  333.          lblPageSubTitle.setFont( PjFinals.fntColumnLabel );
  334.          btnMap.setFont( PjFinals.fntColumnLabel );
  335.  
  336.          //Set up pnlColumn
  337.          pnlColumn.setLayout( new PjListLayout() );
  338.          pnlColumn.add( lblPageSubTitle );
  339.          pnlColumn.add( btnMap );
  340.          pnlColumn.add( pv );
  341.  
  342.          //Layout lblPageTitle and pnlColumn
  343.          pnlPage.setLayout( gridbag );
  344.  
  345.          // Create a panel to hold the icon and the text for page title.
  346.          Panel pnlHeader = new Panel();
  347.          pnlHeader.setLayout( new FlowLayout( FlowLayout.LEFT ) );
  348.  
  349.          // Weather icon
  350.          PjImages Imgs = new PjImages();
  351.          // Add the little weather
  352.          pnlHeader.add( new GifCanvas(Imgs.imagespecs()[ PjImages.idxWeather ].getImage() ) );
  353.          // Add the title
  354.          pnlHeader.add( lblPageTitle );
  355.  
  356.          // Add the header panel with its contents to the page.
  357.          gbc.gridwidth=GridBagConstraints.REMAINDER;
  358.          gbc.anchor=GridBagConstraints.WEST;
  359.          gridbag.setConstraints(pnlHeader,gbc);
  360.          pnlPage.add( pnlHeader );
  361.  
  362.          gbc.weightx = 1.0;
  363.          gbc.weighty = 1.0;
  364.          gbc.fill = GridBagConstraints.BOTH;
  365.          gbc.gridwidth = GridBagConstraints.REMAINDER;
  366.          gridbag.setConstraints(pnlColumn, gbc);
  367.          pnlPage.add( pnlColumn );
  368.  
  369.         return pnlPage;
  370.  
  371.      }
  372.  
  373.  
  374.     } // WeatherSection
  375.  
  376. /**
  377.  * A special-purpose Column for Weather.  A WeatherColumn uses
  378.  * an ArrayFormatter by default, and fiddles with the first line
  379.  * of the PaperStory that it observes.
  380.  *
  381.  * @see Column
  382.  * @version 0.00 13-Feb-96
  383.  * @author  rphall
  384. */
  385. class WeatherColumn extends Column
  386.     {
  387.  
  388.     // --- Public constructors
  389.  
  390.     /**
  391.      * Construct a WeatherColumn as a standalone page.
  392.      * @param pagename  The name of the standalone page.
  393.      * @param section   The name of the observed section.
  394.      * @param p         The paper that contains the observed section.
  395.     */
  396.     public WeatherColumn(String pagename, String section, Paper p)
  397.         {
  398.         super(pagename, section, p);
  399.         setStoryFormatter( new WeatherFormatter() );
  400.         setFont( PjFinals.fntFormattedColumn );
  401.         }
  402.  
  403.     } // WeatherColumn
  404.  
  405. /**
  406.  * A special-purpose StoryFormatter for Weather stories.
  407.  *
  408.  * @see StoryFormatter
  409.  * @version 0.00 13-Feb-96
  410.  * @author rphall
  411. */
  412. class WeatherFormatter implements StoryFormatter
  413.     {
  414.  
  415.     // --- Public constructors
  416.  
  417.     public WeatherFormatter()
  418.         {
  419.         }
  420.  
  421.     // --- Public operations
  422.  
  423.     public String formatToString(PaperStory story)
  424.         {
  425.         return "           " + story.getBody();
  426.         }
  427.  
  428.     } // WeatherFormatter
  429.