home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 7.6 KB | 294 lines |
- /*---------------------------------------------------------------------------
-
- Written by the Personal Journal developers of Dow Jones & Company, Inc.
-
- Dow Jones makes no representations or warranties about
- the suitability of this software, either express or
- implied, including but not limited to the implied warranties
- of merchantability, fitness for a particular purpose,
- or non-infringement. Dow Jones will not be liable for
- any damages suffered by a user as a result of using,
- modifying or distributing this software or its derivatives.
-
-
- @(#)Chart.java 0.00 01-Jan-96
-
- A simple PageView that views charts in one PaperSection.
-
- Authors:
-
- alkarao Alka Rao
- Nimesh Nimesh Shah
- jlee James Lee
-
- Version Ident:
-
- $Header$
-
- History:
-
- 0.00 05-Feb-96 alkarao Initial Creation
- 0.01 15-Feb-96 Nimesh Revised
- 23-Feb-96 Nimesh & jlee Fixed resizing problem of DJChart based on observer notification
-
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.PageView;
- import pj.awt.DJChart;
- import pj.io.Paper;
- import pj.io.PaperSection;
- import pj.io.PaperStory;
- import pj.io.StoryContainerNotification;
-
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Font;
- import java.awt.Insets;
- import java.awt.Component;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Panel;
- import java.awt.Canvas;
- import java.awt.Container;
- import java.awt.Color;
-
- import java.lang.Integer;
- import java.util.Observable;
- import java.util.Observer;
- import java.util.StringTokenizer;
- import java.util.NoSuchElementException;
-
-
-
- /**
- * A simple PageView that observes one PaperSection and displays
- * data from that section in a chart form.
- * Charts may be used inside other pages or may be used as standalone pages.
- *
- * @version 0.00 05-Feb-96
- * @author alkarao
- */
- public class Chart extends PageView implements Observer
- {
-
- // --- Instance variables
- private Paper paper;
- private String chartTitle;
- private String strPaperSection;
- private StringTokenizer stChartData;
- private int xValues[];
- private int yValues[];
- private int nDataValues;
- private DJChart djChart;
- private int bottom, left;
- private int outerwt, outerht;
-
- // --- Public constructors
-
- /**
- * Construct a Chart as a standalone page.
- * @param pagename The name of the standalone page.
- * @param section The name of the observed section.
- * @param p The paper that contains the observed section.
- */
- public Chart(String pagename, String title, String section, Paper p)
- {
- super(pagename);
- chartTitle = title;
- strPaperSection = section;
-
- paper = p;
-
- xValues = new int[50];
- yValues = new int[50];
-
- paper.addObserver(this);
-
- Initialize(); // get x and y values from the section data
-
- if(nDataValues > 1)
- {
- djChart = new DJChart(xValues, yValues, nDataValues, chartTitle);
- add("Center",djChart);
- }
- System.out.println("Debug-Chart:constructed");
- }
-
- /**
- * Construct a Chart as a page component.
- * @param section The name of the observed section.
- * @param paper The paper that contains the observed section.
- */
- public Chart(String section, Paper paper)
- {
- this(null,null,section,paper);
- }
-
- // --- Public operations
-
- /**
- * @return The number of stories in the observed section.
- */
- public int countViews()
- {
- return getSection().numStories();
- }
-
- /**
- * Update view upon notification from a PaperSection. The x-y vlaue
- * pairs are initialized.
- * @param src The object that sent or forwarded the notification.
- * @param arg The notification. An instance of StoryContainerNotification
- * is expected.
- */
- public synchronized void update(Observable src, Object arg)
- {
- if ( !(arg instanceof StoryContainerNotification) )
- return;
-
- StoryContainerNotification scn = (StoryContainerNotification) arg;
-
- if ( !(strPaperSection.equals(scn.strSubsect)) )
- return;
-
- if (scn.newState != PaperSection.stateAdded)
- return;
-
- Initialize();
-
- if(djChart != null)
- remove(djChart);
-
- djChart = new DJChart(xValues, yValues, nDataValues, chartTitle);
- add("Center",djChart);
-
- layout();
-
- } // update
-
-
- public int getCount()
- {
- return (nDataValues+1);
- }
-
- public int[] getXValues()
- {
- return (xValues);
- }
-
- public int[] getYValues()
- {
- return (yValues);
- }
-
- public String getTitle()
- {
- return chartTitle;
- }
-
- public DJChart getChart()
- {
- return djChart;
- }
-
-
- public DJChart getDJChart()
- {
- return djChart;
- }
-
- public synchronized void reshape(int x, int y, int width, int height)
- {
- bottom = y;
- left = x;
-
- outerwt = width;
- outerht = height;
-
- super.reshape(x, y, width, height);
-
- if ( djChart != null )
- {
- djChart.resizeDjChart( width, height );
- }
- else
- {
- djChart = new DJChart();
- djChart.resizeDjChart( width, height );
- }
- }
- // --- Protected operations
-
- protected String formatToString(PaperStory story)
- {
- return story.getTitle() + "\n" + story.getBody() + "\n";
- }
-
- protected PaperSection getSection()
- {
- return paper.section(strPaperSection);
- }
-
- /**
- * Get x and y values from the specified section
- * Assumptions: values are integer values
- * Maximum of 50 x-y pairs are in any section
- */
- protected void Initialize()
- {
- // convert paper section to string
- if (getSection().numStories() < 1)
- return;
-
- String tempStr;
-
- try
- {
- tempStr = getSection().firstStory().getBody();
- }
- catch(NoSuchElementException e)
- {
- return;
- }
-
- // create the tokenizer
- stChartData = new StringTokenizer(tempStr, ".,\n\r");
-
- TokenLoop:
- for (int i=0; i<50 ; i++)
- {
- // Implementation note: countTokens() does not seem to be
- // working correctly. Hence this loop continues until an
- // exception occurs, rather than politely using countTokens
- // to limit iterations.
- try
- {
- String strX = stChartData.nextToken();
- String strY = stChartData.nextToken();
- xValues[i] = Integer.parseInt(strX);
- yValues[i] = Integer.parseInt(strY);
- stChartData.nextToken();
- }
- catch (NoSuchElementException e)
- {
- nDataValues = i;
- break TokenLoop;
- }
- }//for
-
- nDataValues++;
- }
-
-
-
-
-
-
- } // Chart
-
-
-
-