home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / arrayformatter.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  8.5 KB  |  292 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.     @(#)ArrayFormatter.java  0.00 05-Feb-96
  15.  
  16.         A StoryFormatter that creates a 4-column layout from a comma-delimited
  17.         array.
  18.  
  19.     Authors:
  20.  
  21.         rphall   Rick Hall
  22.  
  23.     Version Ident:
  24.  
  25.         $Header: /PjJavaClient/src/pj/awt/ArrayFormatter.java 3     2/13/96 9:56p Rphall $
  26.  
  27.     History:
  28.  
  29.         0.00 05-Feb-96  rphall   Initial Creation
  30.  
  31. ---------------------------------------------------------------------------*/
  32.  
  33. package pj.awt;
  34.  
  35. import pj.awt.StoryFormatter;
  36.  
  37. import collections.Assertable;
  38. import collections.ImplementationError;
  39. import java.util.StringTokenizer;
  40. import pj.io.Paper;
  41. import pj.io.PaperStory;
  42.  
  43. /**
  44.  * A special-purpose StoryFormatter that creates a 4-column layout from
  45.  * a sequence of entries.  The entries are grouped four to a line, and
  46.  * typically separated by commas. When formatted, the left-most column
  47.  * is left-justified, and remaining columns are right justified.
  48.  * ArrayFormatter is designed for formatting stock indexes, exchange rates,
  49.  * treasurys, and quotes, but a subclass of ArrayFormatter is also used to
  50.  * format Weather pages.
  51.  * <P>
  52.  * Column positions are specified in units of a space-width.  That is, if
  53.  * the space character in a particular font has a screen width of 5 pixels,
  54.  * then a column position of "7" would correspond to 35 pixels from the left.
  55.  * <pre>
  56.  * |<--Position_0-->
  57.  * |<-----------------------------Position_1-->|
  58.  * |<--------------------------------------------Position_2-->|
  59.  * |<-----------------------------------------------------------Position_3-->|
  60.  *
  61.  *                  Column 0            Column 1       Column 2       Column 3
  62.  *                  Entry                  Entry          Entry          Entry
  63.  *                  A long entry    A long entry   A long entry   A long entry
  64.  *                  Another entry  Another entry  Another entry  Another entry
  65.  *                  Entry                  Entry          Entry          Entry
  66.  *
  67.  * </pre>
  68.  * NOTE: THE CURRENT IMPLEMENTATION ASSUMES THE CURRENT FONT IS A FIXED-PITCH
  69.  * FONT (e.g. Courier)
  70.  *
  71.  * @see        pj.io.StoryFormatter
  72.  * @version    0.00 05-Feb-96
  73.  * @author     rphall
  74. */
  75. public class ArrayFormatter implements StoryFormatter, Assertable
  76.     {
  77.  
  78.     
  79.  
  80.     // --- Class variables
  81.  
  82.     private static final int[] aiStkIdx = {0, 25, 35, 45};
  83.     private static final int[] aiExch   = {0, 25, 35, 45};
  84.     private static final int[] aiTreas  = {0, 25, 35, 45};
  85.     private static final int[] aiPort   = {0, 15, 25, 35};
  86.  
  87.     private static final String strEOL  = "\r\n";
  88.  
  89.     private static final String[] hdrStkIdx = 
  90.             {
  91.             " ,Previous,Last,Change",
  92.             " ,   Close,    ,      ",
  93.             " ,        ,    ,      "
  94.             };
  95.  
  96.     private static final String[] hdrExch =
  97.             {
  98.             " ,Previous,Last,Change",
  99.             " ,   Close,    ,      ",
  100.             " ,        ,    ,      "
  101.             };
  102.  
  103.     private static final String[] hdrTreas  =
  104.             {
  105.             " ,Ask,Change,Yield",
  106.             " ,   ,      ,     "
  107.             };
  108.  
  109.     private static final String[] hdrPort =
  110.             {
  111.             " ,Previous,Last,Change",
  112.             " ,   Close,    ,      ",
  113.             " ,        ,    ,      "
  114.             };
  115.  
  116.  
  117.     // --- Instance variables
  118.  
  119.     private int[] pos;
  120.     private String[] header;
  121.     private String strEntryDelim;
  122.  
  123.  
  124.     // --- Public constructors
  125.  
  126.     /**
  127.      * General purpose constructor.
  128.      *
  129.      * @param p0 Position of left-edge of column 0
  130.      * @param p1 Position of right-edge of column 1
  131.      * @param p2 Position of right-edge of column 2
  132.      * @param p3 Position of right-edge of column 3
  133.      * @param delim Delimiters used to separate entries in a line.
  134.     */
  135.     public ArrayFormatter(int p0, int p1, int p2, int p3, String delim)
  136.         {
  137.         pos = new int[4];
  138.         pos[0] = p0;
  139.         pos[1] = p1;
  140.         pos[2] = p2;
  141.         pos[3] = p3;
  142.         strEntryDelim = delim;
  143.         }
  144.  
  145.     /**
  146.      * A special-purpose constructor that sets up column
  147.      * positions for Stock Indexes, Exchange Rates, Treasurys
  148.      * and Quotes.
  149.      *
  150.      * @param strId An string equal to Paper.idMrkStkIdx, Paper.idMrkExch,
  151.      * Paper.idMrkTreas or Paper.idPortQuote
  152.     */
  153.     public ArrayFormatter(String strId)
  154.         {
  155.         pos = new int[4];
  156.         strEntryDelim = ",";
  157.  
  158.         if (strId.equals(Paper.idMrkStkIdx) )
  159.             {
  160.             assignPositions( aiStkIdx );
  161.             header = hdrStkIdx;
  162.             }
  163.  
  164.         else if (strId.equals(Paper.idMrkExch) )
  165.             {
  166.             assignPositions( aiExch );
  167.             header = hdrExch;
  168.             }
  169.  
  170.         else if (strId.equals(Paper.idMrkTreas) )
  171.             {
  172.             assignPositions( aiTreas );
  173.             header = hdrTreas;
  174.             }
  175.  
  176.         else if (strId.equals(Paper.idPortQuote) )
  177.             {
  178.             assignPositions( aiPort );
  179.             header = hdrPort;
  180.             }
  181.  
  182.         else
  183.             assert(false);
  184.  
  185.         } // special-purpose constructor
  186.  
  187.     // --- Public operations
  188.  
  189.     /**
  190.      * @param story The PaperStory to format.
  191.      * @return A String representation of the story.
  192.     */
  193.     public String formatToString(PaperStory story)
  194.         {
  195.         String strReturn = "";
  196.  
  197.         // Create column headers
  198.         if (header != null)
  199.             for (int i=0; i<header.length; i++)
  200.                 {
  201.                 strReturn += formatToString( header[i] );
  202.                 strReturn += strEOL;
  203.                 }
  204.  
  205.         // Loop over lines in the story body
  206.         StringTokenizer stLines = new StringTokenizer
  207.                 ( story.getBody(), "\n\r", /*returnTokens*/false );
  208.  
  209.         while (stLines.hasMoreTokens())
  210.             {
  211.             strReturn += formatToString( stLines.nextToken() );
  212.             strReturn += strEOL;
  213.             }
  214.  
  215.         return strReturn;
  216.         } // public formatToString(PaperStory)
  217.  
  218.     /**
  219.      * Raise an exception if predicate is false.
  220.      * @see collections.Assertable
  221.     */
  222.     public void assert(boolean predicate) throws ImplementationError
  223.         { 
  224.         ImplementationError.assert(this, predicate); 
  225.         }
  226.  
  227.     // --- Private operations
  228.     
  229.     /**
  230.      * A helper utility
  231.     */
  232.     private void assignPositions(int[] ap)
  233.         { 
  234.         for (int i=0; i<4; i++) pos[i] = ap[i]; 
  235.         }
  236.  
  237.     /**
  238.      * @param line A line to format.  (The line should NOT be
  239.      * terminated with CRLF.)
  240.      * @return A formatted version of the line.
  241.     */
  242.     private String formatToString(String line)
  243.         {
  244.         String strReturn = "";
  245.         int i;
  246.         String[] entries = { "", "", "", "" };
  247.  
  248.         // Treat line as entries delimited by commas
  249.         //System.out.println("Debug line == " + line);
  250.         StringTokenizer stEntries = new StringTokenizer
  251.                 ( line, strEntryDelim, /*returnTokens*/false );
  252.         i = 0;
  253.         while (stEntries.hasMoreTokens())
  254.             {
  255.             String entry = stEntries.nextToken();
  256.             //System.out.println("Debug entry == " + entry);
  257.             entries[i] = entry;
  258.             i++;
  259.             }
  260.  
  261.         // Pad first entry
  262.         int len = 0;
  263.         while (len < pos[0])
  264.             {
  265.             strReturn += " ";
  266.             len++;
  267.             }
  268.  
  269.         strReturn += entries[0];
  270.         len += entries[0].length();
  271.  
  272.         // Loop over remaining entries 1 - 3
  273.         for (i=1; i<4; i++)
  274.             {
  275.             int start = pos[i] - entries[i].length();
  276.             while (len < start)
  277.                 {
  278.                 strReturn += " ";
  279.                 len++;
  280.                 }
  281.             strReturn += entries[i];
  282.             len += entries[i].length();
  283.  
  284.             } // for entries
  285.  
  286.         return strReturn;
  287.         } // private formatToString(String)
  288.  
  289.     
  290.  
  291.     }; // ArrayFormatter
  292.