home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / chart.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  7.6 KB  |  294 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.     @(#)Chart.java  0.00 01-Jan-96
  15.  
  16.         A simple PageView that views charts in one PaperSection.
  17.  
  18.     Authors:
  19.  
  20.        alkarao   Alka Rao
  21.        Nimesh    Nimesh Shah
  22.        jlee      James Lee
  23.  
  24.     Version Ident:
  25.  
  26.         $Header$
  27.  
  28.     History:
  29.  
  30.         0.00 05-Feb-96  alkarao         Initial Creation
  31.         0.01 15-Feb-96  Nimesh          Revised
  32.              23-Feb-96  Nimesh & jlee   Fixed resizing problem of DJChart based on observer notification
  33.  
  34. ---------------------------------------------------------------------------*/
  35.  
  36. package pj.awt;
  37.  
  38. import pj.awt.PageView;
  39. import pj.awt.DJChart;
  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.Dimension;
  46. import java.awt.Graphics;
  47. import java.awt.Font;
  48. import java.awt.Insets;
  49. import java.awt.Component;
  50. import java.awt.GridBagConstraints;
  51. import java.awt.GridBagLayout;
  52. import java.awt.Panel;
  53. import java.awt.Canvas;
  54. import java.awt.Container;
  55. import java.awt.Color;
  56.  
  57. import java.lang.Integer;
  58. import java.util.Observable;
  59. import java.util.Observer;
  60. import java.util.StringTokenizer;
  61. import java.util.NoSuchElementException;
  62.  
  63.  
  64.  
  65. /**
  66.  * A simple PageView that observes one PaperSection and displays
  67.  * data from that section in a chart form.
  68.  * Charts may be used inside other pages or may be used as standalone pages.
  69.  *
  70.  * @version 0.00 05-Feb-96
  71.  * @author  alkarao
  72. */
  73. public class Chart extends PageView implements Observer
  74.     {
  75.  
  76.     // --- Instance variables
  77.     private Paper           paper;
  78.     private String          chartTitle;
  79.     private String          strPaperSection;
  80.     private StringTokenizer stChartData;
  81.     private int             xValues[];
  82.     private int             yValues[];
  83.     private int             nDataValues;
  84.     private DJChart         djChart;
  85.     private int             bottom, left;
  86.     private int             outerwt, outerht;
  87.  
  88.     // --- Public constructors
  89.  
  90.     /**
  91.      * Construct a Chart as a standalone page.
  92.      * @param pagename  The name of the standalone page.
  93.      * @param section   The name of the observed section.
  94.      * @param p         The paper that contains the observed section.
  95.     */
  96.     public Chart(String pagename, String title, String section, Paper p)
  97.         {
  98.         super(pagename);
  99.         chartTitle = title;
  100.         strPaperSection = section;
  101.  
  102.         paper = p;
  103.  
  104.         xValues = new int[50];
  105.         yValues = new int[50];
  106.  
  107.         paper.addObserver(this);
  108.  
  109.         Initialize();   // get x and y values from the section data
  110.  
  111.         if(nDataValues > 1)
  112.             {
  113.             djChart = new DJChart(xValues, yValues, nDataValues, chartTitle);
  114.             add("Center",djChart);
  115.             }
  116.         System.out.println("Debug-Chart:constructed");
  117.         }
  118.  
  119.     /**
  120.      * Construct a Chart as a page component.
  121.      * @param section   The name of the observed section.
  122.      * @param paper     The paper that contains the observed section.
  123.     */
  124.     public Chart(String section, Paper paper)
  125.         { 
  126.         this(null,null,section,paper); 
  127.         }
  128.  
  129.     // --- Public operations
  130.  
  131.     /**
  132.      * @return The number of stories in the observed section.
  133.     */
  134.     public int countViews()
  135.         { 
  136.         return getSection().numStories(); 
  137.         }
  138.  
  139.     /**
  140.      * Update view upon notification from a PaperSection. The x-y vlaue
  141.      * pairs are initialized.
  142.      * @param src The object that sent or forwarded the notification.
  143.      * @param arg The notification.  An instance of StoryContainerNotification
  144.      * is expected.
  145.     */
  146.     public synchronized void update(Observable src, Object arg)
  147.         {
  148.         if ( !(arg instanceof StoryContainerNotification) )
  149.             return;
  150.  
  151.         StoryContainerNotification scn = (StoryContainerNotification) arg;
  152.  
  153.         if ( !(strPaperSection.equals(scn.strSubsect)) )
  154.             return;
  155.  
  156.         if (scn.newState != PaperSection.stateAdded)
  157.             return;
  158.  
  159.         Initialize();
  160.  
  161.         if(djChart != null)
  162.             remove(djChart);
  163.  
  164.         djChart = new DJChart(xValues, yValues, nDataValues, chartTitle);
  165.         add("Center",djChart);
  166.  
  167.         layout();
  168.  
  169.         } // update
  170.  
  171.  
  172.     public int getCount()
  173.         { 
  174.         return (nDataValues+1); 
  175.         }
  176.  
  177.     public int[] getXValues()
  178.         { 
  179.         return (xValues); 
  180.         }
  181.  
  182.     public int[] getYValues()
  183.         { 
  184.         return (yValues); 
  185.         }
  186.  
  187.     public String getTitle()
  188.         {
  189.         return chartTitle;     
  190.         }
  191.  
  192.     public DJChart getChart()
  193.         {
  194.         return djChart;        
  195.         }
  196.  
  197.  
  198.     public DJChart getDJChart()
  199.         { 
  200.         return djChart; 
  201.         }
  202.  
  203.     public synchronized void reshape(int x, int y, int width, int height)
  204.         {
  205.         bottom = y;
  206.         left =   x;
  207.  
  208.         outerwt = width;
  209.         outerht = height;
  210.  
  211.         super.reshape(x, y, width, height);
  212.  
  213.         if ( djChart != null )
  214.             {
  215.             djChart.resizeDjChart( width, height );
  216.             }
  217.         else
  218.             {
  219.             djChart = new DJChart();
  220.             djChart.resizeDjChart( width, height );
  221.             }
  222.         }
  223.     // --- Protected operations
  224.  
  225.     protected String formatToString(PaperStory story)
  226.         { 
  227.         return story.getTitle() + "\n" + story.getBody() + "\n"; 
  228.         }
  229.  
  230.     protected PaperSection getSection()
  231.         { 
  232.         return paper.section(strPaperSection); 
  233.         }
  234.  
  235.      /**
  236.        * Get x and y values from the specified section
  237.        * Assumptions: values are integer values
  238.        * Maximum of 50 x-y pairs are in any section
  239.       */
  240.     protected void Initialize()
  241.         {
  242.         // convert paper section to string
  243.         if (getSection().numStories() < 1)
  244.             return;
  245.  
  246.         String tempStr;
  247.  
  248.         try
  249.             {
  250.             tempStr = getSection().firstStory().getBody();
  251.             }
  252.         catch(NoSuchElementException e)
  253.             {
  254.             return;
  255.             }
  256.  
  257.         // create the tokenizer
  258.         stChartData = new StringTokenizer(tempStr,  ".,\n\r");
  259.  
  260.         TokenLoop:
  261.         for (int i=0; i<50 ; i++)
  262.             {
  263.             // Implementation note: countTokens() does not seem to be
  264.             // working correctly.  Hence this loop continues until an
  265.             // exception occurs, rather than politely using countTokens
  266.             // to limit iterations.
  267.             try
  268.                 {
  269.                 String strX = stChartData.nextToken();
  270.                 String strY = stChartData.nextToken();
  271.                 xValues[i] =  Integer.parseInt(strX);
  272.                 yValues[i] =  Integer.parseInt(strY);
  273.                 stChartData.nextToken();
  274.                 }
  275.             catch (NoSuchElementException e)
  276.                 {
  277.                 nDataValues = i;
  278.                 break TokenLoop;
  279.                 }
  280.             }//for
  281.  
  282.         nDataValues++;
  283.         }
  284.  
  285.     
  286.  
  287.  
  288.     
  289.  
  290.     } // Chart
  291.  
  292.  
  293.  
  294.