home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 8.5 KB | 292 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.
-
-
- @(#)ArrayFormatter.java 0.00 05-Feb-96
-
- A StoryFormatter that creates a 4-column layout from a comma-delimited
- array.
-
- Authors:
-
- rphall Rick Hall
-
- Version Ident:
-
- $Header: /PjJavaClient/src/pj/awt/ArrayFormatter.java 3 2/13/96 9:56p Rphall $
-
- History:
-
- 0.00 05-Feb-96 rphall Initial Creation
-
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.StoryFormatter;
-
- import collections.Assertable;
- import collections.ImplementationError;
- import java.util.StringTokenizer;
- import pj.io.Paper;
- import pj.io.PaperStory;
-
- /**
- * A special-purpose StoryFormatter that creates a 4-column layout from
- * a sequence of entries. The entries are grouped four to a line, and
- * typically separated by commas. When formatted, the left-most column
- * is left-justified, and remaining columns are right justified.
- * ArrayFormatter is designed for formatting stock indexes, exchange rates,
- * treasurys, and quotes, but a subclass of ArrayFormatter is also used to
- * format Weather pages.
- * <P>
- * Column positions are specified in units of a space-width. That is, if
- * the space character in a particular font has a screen width of 5 pixels,
- * then a column position of "7" would correspond to 35 pixels from the left.
- * <pre>
- * |<--Position_0-->
- * |<-----------------------------Position_1-->|
- * |<--------------------------------------------Position_2-->|
- * |<-----------------------------------------------------------Position_3-->|
- *
- * Column 0 Column 1 Column 2 Column 3
- * Entry Entry Entry Entry
- * A long entry A long entry A long entry A long entry
- * Another entry Another entry Another entry Another entry
- * Entry Entry Entry Entry
- *
- * </pre>
- * NOTE: THE CURRENT IMPLEMENTATION ASSUMES THE CURRENT FONT IS A FIXED-PITCH
- * FONT (e.g. Courier)
- *
- * @see pj.io.StoryFormatter
- * @version 0.00 05-Feb-96
- * @author rphall
- */
- public class ArrayFormatter implements StoryFormatter, Assertable
- {
-
-
-
- // --- Class variables
-
- private static final int[] aiStkIdx = {0, 25, 35, 45};
- private static final int[] aiExch = {0, 25, 35, 45};
- private static final int[] aiTreas = {0, 25, 35, 45};
- private static final int[] aiPort = {0, 15, 25, 35};
-
- private static final String strEOL = "\r\n";
-
- private static final String[] hdrStkIdx =
- {
- " ,Previous,Last,Change",
- " , Close, , ",
- " , , , "
- };
-
- private static final String[] hdrExch =
- {
- " ,Previous,Last,Change",
- " , Close, , ",
- " , , , "
- };
-
- private static final String[] hdrTreas =
- {
- " ,Ask,Change,Yield",
- " , , , "
- };
-
- private static final String[] hdrPort =
- {
- " ,Previous,Last,Change",
- " , Close, , ",
- " , , , "
- };
-
-
- // --- Instance variables
-
- private int[] pos;
- private String[] header;
- private String strEntryDelim;
-
-
- // --- Public constructors
-
- /**
- * General purpose constructor.
- *
- * @param p0 Position of left-edge of column 0
- * @param p1 Position of right-edge of column 1
- * @param p2 Position of right-edge of column 2
- * @param p3 Position of right-edge of column 3
- * @param delim Delimiters used to separate entries in a line.
- */
- public ArrayFormatter(int p0, int p1, int p2, int p3, String delim)
- {
- pos = new int[4];
- pos[0] = p0;
- pos[1] = p1;
- pos[2] = p2;
- pos[3] = p3;
- strEntryDelim = delim;
- }
-
- /**
- * A special-purpose constructor that sets up column
- * positions for Stock Indexes, Exchange Rates, Treasurys
- * and Quotes.
- *
- * @param strId An string equal to Paper.idMrkStkIdx, Paper.idMrkExch,
- * Paper.idMrkTreas or Paper.idPortQuote
- */
- public ArrayFormatter(String strId)
- {
- pos = new int[4];
- strEntryDelim = ",";
-
- if (strId.equals(Paper.idMrkStkIdx) )
- {
- assignPositions( aiStkIdx );
- header = hdrStkIdx;
- }
-
- else if (strId.equals(Paper.idMrkExch) )
- {
- assignPositions( aiExch );
- header = hdrExch;
- }
-
- else if (strId.equals(Paper.idMrkTreas) )
- {
- assignPositions( aiTreas );
- header = hdrTreas;
- }
-
- else if (strId.equals(Paper.idPortQuote) )
- {
- assignPositions( aiPort );
- header = hdrPort;
- }
-
- else
- assert(false);
-
- } // special-purpose constructor
-
- // --- Public operations
-
- /**
- * @param story The PaperStory to format.
- * @return A String representation of the story.
- */
- public String formatToString(PaperStory story)
- {
- String strReturn = "";
-
- // Create column headers
- if (header != null)
- for (int i=0; i<header.length; i++)
- {
- strReturn += formatToString( header[i] );
- strReturn += strEOL;
- }
-
- // Loop over lines in the story body
- StringTokenizer stLines = new StringTokenizer
- ( story.getBody(), "\n\r", /*returnTokens*/false );
-
- while (stLines.hasMoreTokens())
- {
- strReturn += formatToString( stLines.nextToken() );
- strReturn += strEOL;
- }
-
- return strReturn;
- } // public formatToString(PaperStory)
-
- /**
- * Raise an exception if predicate is false.
- * @see collections.Assertable
- */
- public void assert(boolean predicate) throws ImplementationError
- {
- ImplementationError.assert(this, predicate);
- }
-
- // --- Private operations
-
- /**
- * A helper utility
- */
- private void assignPositions(int[] ap)
- {
- for (int i=0; i<4; i++) pos[i] = ap[i];
- }
-
- /**
- * @param line A line to format. (The line should NOT be
- * terminated with CRLF.)
- * @return A formatted version of the line.
- */
- private String formatToString(String line)
- {
- String strReturn = "";
- int i;
- String[] entries = { "", "", "", "" };
-
- // Treat line as entries delimited by commas
- //System.out.println("Debug line == " + line);
- StringTokenizer stEntries = new StringTokenizer
- ( line, strEntryDelim, /*returnTokens*/false );
- i = 0;
- while (stEntries.hasMoreTokens())
- {
- String entry = stEntries.nextToken();
- //System.out.println("Debug entry == " + entry);
- entries[i] = entry;
- i++;
- }
-
- // Pad first entry
- int len = 0;
- while (len < pos[0])
- {
- strReturn += " ";
- len++;
- }
-
- strReturn += entries[0];
- len += entries[0].length();
-
- // Loop over remaining entries 1 - 3
- for (i=1; i<4; i++)
- {
- int start = pos[i] - entries[i].length();
- while (len < start)
- {
- strReturn += " ";
- len++;
- }
- strReturn += entries[i];
- len += entries[i].length();
-
- } // for entries
-
- return strReturn;
- } // private formatToString(String)
-
-
-
- }; // ArrayFormatter
-