home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 16.4 KB | 512 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.
-
-
- PjJava.java 0.00 09-Jan-95
-
- The Personal Journal Java applet.
-
- Authors:
-
- rphall Rick Hall
- jlee James Lee
- lning Lindee Ning
-
- Version Ident:
-
- $Header: /PjJavaClient/src/pj/applet/PjJava.java 7 2/07/96 4:17p Rphall $
-
- History:
-
- 09-Jan-96 rphall Initial Creation
- 23-Jan-96 rphall Major rewrite, focusing on resource locations
- 04-Mar-96 jlee Added statement that make first tab selected.
- 09-Mar-96 lning Added action() inorder to pass the menu action to the notebook.
- 20-Mar-96 jlee Set the priority of DownloadThread to MIN_PRIORITY to start with,
- later, it'll be changed to MAX_PRIORITY
- 26-Mar-96 jlee Added getPjFrame().
- 28-Mar-96 jlee Added a line passing AboutBox image to PjFrame
- 29-Mar-96 jlee Modified so that PjImages object created in init() can be passed
- to PjFrame constructor.
- ---------------------------------------------------------------------------*/
-
- package pj.applet;
-
- import pj.applet.DownloadThread;
- import pj.awt.PjAds;
- import pj.awt.PjFrame;
- import pj.awt.PjImages;
- import pj.awt.PjFinals;
- import pj.awt.PjTabNotebook;
- import pj.io.Paper;
-
- import collections.Assertable;
- import collections.ImplementationError;
- import java.applet.Applet;
- import java.awt.BorderLayout;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Frame;
- import java.lang.Runnable;
- import java.lang.SecurityException;
- import java.lang.Thread;
- import java.net.MalformedURLException;
- import java.net.URL;
-
- /**
- * The Personal Journal Java applet.
- * <P>
- * The applet delegates most of its functionality. The user interface
- * is handled by PjTabNotebook. Personal Journal content is handled
- * by Paper. Downloading is handled by DownloadThread.
- * <P>
- * The applet itself is primarily responsible for locating resources:
- * urls for content, advertisements, and images.
- *
- * @version 0.00 21-Jan-96
- * @author Rick Hall
- */
-
- public class PjJava extends Applet implements Assertable
- {
-
- // --- Class variables
-
- /** A command line flag for content URL */
- public static final String flagContentUrl = "--content";
-
- /** A command line flag for ads URL */
- public static final String flagAdsUrl = "--ads";
-
- /** A command line flag for images URL */
- public static final String flagImagesUrl = "--images";
-
- /** A relative base url for content sources, currently "data/" */
- public static final String strContentBaseUrl = "data/";
-
- /** A content source, currently "frames.txt" */
- public static final String strContentSourceUrl = "frames.txt";
-
- /** Parameters sent to the content source during a "GET" */
- public static final String strGetParams = "";
-
- /** A relative base url for advertisements, currently "ads/" */
- public static final String strAdsBaseUrl = "ads/";
-
- /** A relative base url for images, currently "images/" */
- public static final String strImagesBaseUrl = "images/";
-
- /** Width for screen presentation */
- public static final int WIDTH = PjFinals.nPjDefaultAppletWidth;
-
- /** Height for screen presentation */
- public static final int HEIGHT = PjFinals.nPjDefaultAppletHeight;
-
- /** Frame object used in PjJava */
- private static Frame frmPjJava = null;
-
- /** Advertisement */
- private static PjAds pjads;
-
- /** Images */
- private static PjImages pjimages;
-
- // --- Instance variables
-
- /** A url for content "GET" transactions */
- private URL urlContent;
-
- /** A base url for advertisements */
- private URL urlAds;
-
- /** A base url for images */
- private URL urlImages;
-
- /** The data object for this applet */
- private Paper paper;
-
- /** The thread that downloads content into the paper */
- private DownloadThread dlthread;
-
- /** The user interface to the paper */
- private PjTabNotebook notebook;
-
- // --- Public constructors
-
- /**
- * A general constructor for PjJava.
- *
- * @param content An absolute url with HTTP GET parameters that returns
- * Personal Journal content.
- * @param ads An absolute base url from which PjJava ads are located
- * @param images An absolute base url from which PjJava images are
- * located
- */
- public PjJava(URL content, URL ads, URL images)
- {
- urlContent = content;
- urlAds = ads;
- urlImages = images;
- System.out.println("Debug-PjJava:constructed");
- } // General constructor
-
- /**
- * Construct PjJava with default urls for content, ads, and images.
- * See the public variables of PjJava for relative content, ad,
- * and image default urls. Absolute urls are calculated relative
- * to the base url passed as a parameter to this constructor.
- *
- * @param base The base url from which content, ad and image defaults
- * are located.
- */
- public PjJava(URL base)
- {
- urlContent = getURL( base, strContentBaseUrl
- + strContentSourceUrl + strGetParams );
- urlAds = getURL( base, strAdsBaseUrl );
- urlImages = getURL( base, strImagesBaseUrl );
- } // Constructor with default urls
-
- /**
- * Construct PjJava as an embedded applet with default urls
- * for content, ads, and images.
- */
- public PjJava()
- {
- /*
- * This constructor can be called from
- * either standalone or embedded applets.
- */
-
- // Try to get URL context.
- URL context = null;
- try {
- // Try document base (embedded applets)
- context = getDocumentBase();
- }
- catch (NullPointerException e1)
- {
- try {
- // Try current directory (standalone applets)
- context = new URL("file:///"+System.getProperty("user.dir")+"/");
- }
- catch(MalformedURLException e2)
- {
- // Should never happen
- assert(false);
- }
- catch(SecurityException e3)
- {
- // Can happen if NullPointerException in an embedded app
- // and remote client tries to access file system.
- // Just set context to null.
- context = null;
- }
- }
-
- // Get resource locations
- urlContent = getURL( context, strContentBaseUrl
- + strContentSourceUrl + strGetParams );
- urlAds = getURL( context, strAdsBaseUrl );
- urlImages = getURL( context, strImagesBaseUrl );
-
- } // Default constructor
-
- // --- Public operations
-
- /**
- * The entry point for PjJava as a standalone applet.
- * Several command line options may be specified:
- * <pre>
- * --content <a url for content>
- * --ads <a base url for advertisements>
- * --images <a base url for images>
- * </pre>
- * If these arguments are not specified, but PjJava is invoked from
- * the Java interpretter, PjJava attempts to construct defaults relative
- * to the current user directory. If PjJava is invoked from a
- * browser without these arguments, PjJava will throw an
- * ImplementationError at runtime.
- * <P>
- * In general, construction of default urls can fail, and PjJava will
- * throw an ImplementationError at runtime.
- */
- public static void main(String args[])
- {
- URL base;
- URL content = null;
- URL images = null;
- URL ads = null;
-
- try {
- base = new URL("file:///"+System.getProperty("user.dir")+"/");
- }
- catch(MalformedURLException e)
- {
- throw new ImplementationError();
- }
- catch(SecurityException e3)
- {
- // Can happen if main is invoked by a remote client.
- // Just set base to null.
- base = null;
- }
-
- for (int i=0; i<args.length; i++)
- if ( args[i].equals(flagContentUrl) && i<args.length-1 )
- {
- //System.out.println("Debug content arg == " + args[i+1] );
- content = getURL(args[i+1]);
- //if (content != null) System.out.println("Debug content URL from arg == " + content.toExternalForm() );
- //else System.out.println("Debug content URL from arg == null" );
- }
-
- if (content == null)
- {
- content = getURL( base, strContentBaseUrl
- + strContentSourceUrl + strGetParams );
- if (content != null) System.out.println("Debug content URL from instance data == " + content.toExternalForm() );
- //else System.out.println("Debug content URL from instance data == null" );
- }
-
- for (int i=0; i<args.length; i++)
- if ( args[i].equals(flagAdsUrl) && i<args.length-1 )
- {
- //System.out.println("Debug ads arg == " + args[i+1] );
- ads = getURL(args[i+1]);
- //if (ads != null) System.out.println("Debug ads URL from arg == " + ads.toExternalForm() );
- //else System.out.println("Debug ads URL from arg == null" );
- }
-
- if (ads == null)
- {
- ads = getURL( base, strAdsBaseUrl );
- //if (ads != null) System.out.println("Debug ads URL from instance data == " + ads.toExternalForm() );
- //else System.out.println("Debug ads URL from instance data == null" );
- }
-
- for (int i=0; i<args.length; i++)
- if ( args[i].equals(flagImagesUrl) && i<args.length-1 )
- {
- //System.out.println("Debug images arg == " + args[i+1] );
- images = getURL(args[i+1]);
- //if (images != null) System.out.println("Debug images URL from arg == " + images.toExternalForm() );
- //else System.out.println("Debug images URL from arg == null" );
- }
-
- if (images == null)
- {
- images = getURL( base, strImagesBaseUrl );
- //if (images != null) System.out.println("Debug images URL from instance data == " + images.toExternalForm() );
- //else System.out.println("Debug images URL from instance data == null" );
- }
-
- PjJava pjApplet = new PjJava( content, ads, images );
-
- pjApplet.init();
- pjApplet.start();
-
- frmPjJava = new PjFrame( getPjImages() );
- frmPjJava.add("Center", pjApplet);
- frmPjJava.pack();
-
- frmPjJava.resize( PjJava.WIDTH, PjJava.HEIGHT );
-
- frmPjJava.show();
- //frmPjJava.list();
-
- } // main
-
- /**
- * Initialize the applet. If the applet is invoked in the context
- * of a browser, check whether the applet tag specifies a content,
- * ad or image urls and try to use the specified urls. Otherwise,
- * just use the urls specified during construction.
- * <P>
- * The relevant applet tags are:
- * <pre>
- * "ContentUrl"
- * "AdBaseUrl"
- * "ImageBaseUrl"
- * </pre>
- */
- public void init()
- {
- setLayout( new BorderLayout() );
-
- // Check applet tag for url parameters
- try {
- urlContent = getUrlFromParameter(urlContent,"ContentUrl");
- urlAds = getUrlFromParameter(urlAds,"AdsUrl");
- urlImages = getUrlFromParameter(urlImages,"ImagesUrl");
- }
- catch(NullPointerException e)
- { /*applet not invoked in browser context*/ }
-
- // Create data object
- paper = new Paper();
-
- // Create user interface
- assert(urlAds != null);
- pjads = new PjAds(urlAds);
-
- assert(urlImages != null);
- pjimages = new PjImages(urlImages);
-
- notebook = new PjTabNotebook(paper,pjads,pjimages);
- notebook.firstPage();
-
- add("Center", notebook);
- resize(minimumSize());
- } // init
-
- /**
- * Returns PjImage object created in init()
- */
- public static PjImages getPjImages()
- {
- return pjimages;
- }
-
- /**
- * Start a download thread using the server url specified during
- * construction or initialization. If the server url is null,
- * this operation aborts without starting a download.
- */
- public void start()
- {
- assert(urlContent != null);
- //System.out.println("Debug downloading from " + urlContent.toString());
- dlthread = new DownloadThread(urlContent,paper);
- dlthread.setPriority( Thread.MIN_PRIORITY );
- dlthread.start();
- } // start
-
- /**
- * Stop the applet and any download thread that is running.
- */
- public void stop()
- {
- if ( dlthread != null && dlthread.isAlive() )
- dlthread.stop();
- }
-
- public void destroy()
- {
- if ( dlthread != null && dlthread.isAlive() )
- {
- dlthread.stop();
- dlthread = null;
- System.out.println("Debug-PjJava-destroy:passed1");
- }
- System.out.println("Debug-PjJava-destroy:passed2");
- }
-
- public static Frame getPjFrame()
- {
- return frmPjJava;
- }
-
- public Dimension minimumSize()
- {
- return new Dimension( PjJava.WIDTH, PjJava.HEIGHT );
- }
-
- /**
- * Raise an exception if predicate is false.
- * @see collections.Assertable
- * @exception ImplementationError thrown if predicate is false.
- */
- public void assert(boolean predicate) throws ImplementationError
- {
- ImplementationError.assert(this, predicate);
- }
-
- /**
- * Let notebook handle the action from the menu selection.
- */
- public boolean action(Event evt, Object arg)
- {
- notebook.handleEvent(evt);
- return true;
-
- }
-
-
- // --- Package operations
-
- /**
- * Convert string to an absolute url.
- * @param urlParam full specification for absolute url.
- * @return A valid url, or null if urlParam is null or malformed.
- */
- static URL getURL(String urlParam)
- {
- return getURL(null,urlParam);
- }
-
- /**
- * Convert string to a relative or absolute url
- * @param docBase base for relative url, null for absolute url
- * @param urlParam offset for relative url, full specification for
- * absolute url.
- * @return A valid url, or null if urlParam is null or parameters
- * are malformed.
- */
- static URL getURL(URL docBase, String urlParam)
- {
- URL url = null;
-
- if (urlParam == null)
- return url;
-
- try {
- if (docBase != null)
- url = new URL( docBase, urlParam);
- else
- url = new URL( urlParam );
- }
- catch (MalformedURLException e)
- {
- url = null;
- }
-
- return url;
- } // getURL
-
- // --- Private operations
-
- /**
- * Get a url from an applet tag parameter
- */
- private URL getUrlFromParameter(URL urlDefault, String param)
- {
- URL url;
- String str = getParameter(param);
-
- // Try interpretting str as absolute URL
- url = getURL(str);
- if (url != null) return url;
-
- // Try interpretting as relative
- url = getURL(getDocumentBase(), str);
- if (url != null) return url;
-
- // If neither absolute and nor relative, ignore it
- return urlDefault;
-
- } // getUrlFromParameter
-
-
-
- } // PjJava
-