home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 10.2 KB | 352 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.
-
-
- @(#)Column.java 0.00 01-Jan-96
-
- A simple PageView that views stories in one PaperSection.
-
- Authors:
-
- rphall Rick Hall
- Ted Skolnick
- jlee James Lee
-
- Version Ident:
-
- $Header: /PjJavaClient/src/pj/awt/Column.java 3 2/11/96 4:39p Rphall $
-
- History:
-
- 0.00 01-Jan-96 rphall Initial Creation
- 0.01 23-Feb-96 Ted S. Changes to PaperView Interface
- 0.02 12-Mar-96 jlee Added story formatting functions.
- 0.03 18-Mar-96 Ted S. Filled in more on Next, Prev View.
- 0.04 28-Mar-96 Ted S. Made ltb protected so derived classes can use it.
-
- ---------------------------------------------------------------------------*/
-
- package pj.awt;
-
- import pj.awt.LineTextBox;
- import pj.awt.PageView;
- import pj.awt.StoryFormatter;
- import pj.io.Paper;
- import pj.io.PaperSection;
- import pj.io.PaperStory;
- import pj.io.StoryContainerNotification;
-
- import java.awt.Panel;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.util.Observable;
- import java.util.Observer;
- import java.util.Vector;
-
-
- /**
- * A simple PageView that observes one PaperSection and displays
- * stories from that section. Columns may be used inside other pages
- * or may be used as standalone pages. Columns may not be used
- * as dividers.
- *
- * @version 0.00 01-Jan-96
- * @author rphall
- */
- public class Column extends PageView implements Observer
- {
-
- // --- Class variables
-
- private static final String CR = "\r";
- private static final String LF = "\n";
- private static final String CRLF = CR + LF;
- private static final String PARAG_DEL = "---";
-
- private static final String LINE_CENTER = "\\qc";
- private static final String LINE_BOLD = "\\b";
-
- private static final int TITLE = 0;
- private static final int BODY = 1;
-
-
- // --- Instance variables
-
- private Paper paper;
- private String strPaperSection;
- private StoryFormatter sfFormatter;
- protected LineTextBox ltb;
-
- // --- Public constructors
-
- /**
- * Construct a Column 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 Column(String pagename, String section, Paper p)
- {
- super(pagename);
- strPaperSection = section;
- ltb = new LineTextBox();
- add("Center", ltb);
- paper = p;
- paper.addObserver(this);
- }
-
- /**
- * Construct a Column as a page component.
- * @param section The name of the observed section.
- * @param paper The paper that contains the observed section.
- */
- public Column(String section, Paper paper)
- {
- this(null,section,paper);
- }
-
-
- // --- Public operations
-
- /**
- * @param formatter The StoryFormatter this column should use
- * in presenting stories.
- */
- public void setStoryFormatter(StoryFormatter formatter)
- {
- sfFormatter = formatter;
- }
-
- /**
- * @param f The Font this column should use in presenting stories.
- */
- public void setFont(Font f)
- {
- ltb.setFont(f);
- }
-
- /**
- * @return The number of stories in the observed section.
- */
- public int countViews()
- {
- return getSection().numStories();
- }
-
- /**
- * Make the first story of the observed section the current view.
- */
- public void firstView()
- {
- super.firstView();
- view( getSection().firstStory() );
- }
-
- /**
- * Make the next story (after the current story) the current view.
- */
- public boolean nextView()
- {
- boolean bRet;
-
- // Make sure we can do another next,
- // if not return false.
- if ( super.nextView() )
- {
- view( getSection().nextStory() );
- bRet = true;
- }
- else
- {
- bRet = false;
- }
-
- return bRet;
- }
-
- /**
- * Make the last story of the observed section the current view.
- */
- public void lastView()
- {
- super.lastView();
- view( getSection().lastStory() );
- }
-
- /**
- * Make the previous story (before the current story) the current view.
- */
- public boolean previousView()
- {
- boolean bRet;
-
- // Make sure we can do another prev,
- // if not return false.
- if ( super.previousView() )
- {
- view( getSection().previousStory() );
- bRet = true;
- }
- else
- {
- bRet = false;
- }
-
- return bRet;
- }
-
-
-
- /**
- * Make a specific story the current view. Views are 1 based.
- * @param idx The index of the view to display.
- */
- public void view(int idx)
- {
- super.view(idx);
- if ( idx > 0 )
- view( getSection().story(idx - 1) );
- }
-
- /**
- * Update view upon notification from a PaperSection. The column
- * resets the current view to the first story of the updated section
- * when that section finishes adding stories.
- * @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;
-
- firstView();
-
- } // update
-
- public Dimension minimumSize()
- {
- return ltb.minimumSize();
- }
-
- public Dimension preferredSize()
- {
- return ltb.preferredSize();
- }
-
- // --- Protected operations
-
- protected void view(PaperStory story)
- {
- if ( getSection().doesSectionDisplaysMultipleStories() )
- {
- Vector vAll = getSection().getAllStories();
- String strStory = new String();
-
- for ( int i = 0; i < vAll.size(); i++ )
- if ( ((PaperStory)vAll.elementAt(i)).getBody() != null )
- strStory += "\\b" + formatText( ((PaperStory)vAll.elementAt(i)).getBody(), BODY ) + "\n";
-
- ltb.setText( strStory );
- }
- else
- ltb.setText( formatToString(story) );
-
- ltb.repaint();
-
- }
-
- protected String formatToString(PaperStory story)
- {
- if (sfFormatter != null)
- return sfFormatter.formatToString(story);
- else
- {
- return "\\b" + formatText( story.getTitle(), TITLE ) + "\n" + story.getBody() + "\n";
- }
- }
-
- protected String formatText( String text, int type )
- {
- int nextCRLF = 0, idx = 0;
-
- switch ( type )
- {
- case TITLE:
- nextCRLF = text.indexOf( CRLF );
- while ( nextCRLF != -1 )
- {
- if ( text.length() > nextCRLF + CRLF.length() )
- text = text.substring( 0, nextCRLF + CRLF.length() ) +
- LINE_BOLD + text.substring(nextCRLF + CRLF.length());
- else
- break;
-
- nextCRLF = text.indexOf( CRLF, nextCRLF + CRLF.length() + LINE_BOLD.length() );
- } // while CRLF
- break;
-
- case BODY:
- int nextPARAG_DEL = text.indexOf( PARAG_DEL );
- while ( nextPARAG_DEL != -1 )
- {
- if ( text.length() > nextPARAG_DEL )
- {
- int lastLFIdx = text.substring( nextPARAG_DEL - 10, nextPARAG_DEL ).lastIndexOf( LF );
- if ( lastLFIdx != -1 )
- {
- idx = nextPARAG_DEL - 10 + lastLFIdx + LF.length();
-
- text = text.substring( 0, nextPARAG_DEL - 10 + lastLFIdx + LF.length() ) + LINE_CENTER +
- ( ( ((nextCRLF = text.substring( idx ).indexOf( CRLF )) != -1) &&
- (text.length() > nextCRLF + CRLF.length()) ) ?
-
- text.substring( idx, idx + nextCRLF + CRLF.length() ) + LINE_BOLD +
- text.substring( idx + nextCRLF + CRLF.length()) :
-
- text.substring( idx ) );
- } // if ( lastLFIdx != -1 )
-
- } //if ( text.length() > nextPARAG_DEL )
- else
- break;
-
- nextPARAG_DEL = text.indexOf( PARAG_DEL, nextPARAG_DEL + PARAG_DEL.length() + LINE_CENTER.length() );
-
- } // while PARAG_DEL
- break;
-
- default:
- break;
- }
-
- return text;
- }
-
- protected PaperSection getSection()
- {
- return paper.section(strPaperSection);
- }
-
-
-
- } // Column
-