home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / applet / pjjava.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  16.4 KB  |  512 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.     PjJava.java   0.00 09-Jan-95
  15.  
  16.         The Personal Journal Java applet.
  17.  
  18.     Authors:
  19.  
  20.         rphall      Rick Hall
  21.         jlee        James Lee
  22.         lning       Lindee Ning
  23.  
  24.     Version Ident:
  25.  
  26.         $Header: /PjJavaClient/src/pj/applet/PjJava.java 7     2/07/96 4:17p Rphall $
  27.  
  28.     History:
  29.  
  30.         09-Jan-96   rphall      Initial Creation
  31.         23-Jan-96   rphall      Major rewrite, focusing on resource locations
  32.         04-Mar-96   jlee        Added statement that make first tab selected.
  33.         09-Mar-96   lning       Added action() inorder to pass the menu action to the notebook.
  34.         20-Mar-96   jlee        Set the priority of DownloadThread to MIN_PRIORITY to start with,
  35.                                 later, it'll be changed to MAX_PRIORITY
  36.         26-Mar-96   jlee        Added getPjFrame().
  37.         28-Mar-96   jlee        Added a line passing AboutBox image to PjFrame
  38.         29-Mar-96   jlee        Modified so that PjImages object created in init() can be passed
  39.                                 to PjFrame constructor.
  40. ---------------------------------------------------------------------------*/
  41.  
  42. package pj.applet;
  43.  
  44. import pj.applet.DownloadThread;
  45. import pj.awt.PjAds;
  46. import pj.awt.PjFrame;
  47. import pj.awt.PjImages;
  48. import pj.awt.PjFinals;
  49. import pj.awt.PjTabNotebook;
  50. import pj.io.Paper;
  51.  
  52. import collections.Assertable;
  53. import collections.ImplementationError;
  54. import java.applet.Applet;
  55. import java.awt.BorderLayout;
  56. import java.awt.Dimension;
  57. import java.awt.Event;
  58. import java.awt.Frame;
  59. import java.lang.Runnable;
  60. import java.lang.SecurityException;
  61. import java.lang.Thread;
  62. import java.net.MalformedURLException;
  63. import java.net.URL;
  64.  
  65. /**
  66.  * The Personal Journal Java applet.
  67.  * <P>
  68.  * The applet delegates most of its functionality.  The user interface
  69.  * is handled by PjTabNotebook.  Personal Journal content is handled
  70.  * by Paper. Downloading is handled by DownloadThread.
  71.  * <P>
  72.  * The applet itself is primarily responsible for locating resources:
  73.  * urls for content, advertisements, and images.
  74.  *
  75.  * @version 0.00 21-Jan-96
  76.  * @author Rick Hall
  77. */
  78.  
  79. public class PjJava extends Applet implements Assertable
  80.     {
  81.  
  82.     // --- Class variables
  83.  
  84.     /** A command line flag for content URL */
  85.     public static final String flagContentUrl = "--content";
  86.  
  87.     /** A command line flag for ads URL */
  88.     public static final String flagAdsUrl = "--ads";
  89.  
  90.     /** A command line flag for images URL */
  91.     public static final String flagImagesUrl = "--images";
  92.  
  93.     /** A relative base url for content sources, currently "data/" */
  94.     public static final String strContentBaseUrl = "data/";
  95.  
  96.     /** A content source, currently "frames.txt" */
  97.     public static final String strContentSourceUrl = "frames.txt";
  98.  
  99.     /** Parameters sent to the content source during a "GET" */
  100.     public static final String strGetParams = "";
  101.  
  102.     /** A relative base url for advertisements, currently "ads/" */
  103.     public static final String strAdsBaseUrl = "ads/";
  104.  
  105.     /** A relative base url for images, currently "images/" */
  106.     public static final String strImagesBaseUrl = "images/";
  107.  
  108.     /** Width for screen presentation */
  109.     public static final int WIDTH = PjFinals.nPjDefaultAppletWidth;
  110.  
  111.     /** Height for screen presentation */
  112.     public static final int HEIGHT = PjFinals.nPjDefaultAppletHeight;
  113.  
  114.     /** Frame object used in PjJava */
  115.     private static Frame frmPjJava = null;
  116.  
  117.     /** Advertisement  */
  118.     private static PjAds    pjads;
  119.  
  120.     /** Images */
  121.     private static PjImages pjimages;
  122.  
  123.     // --- Instance variables
  124.  
  125.     /** A url for content "GET" transactions */
  126.     private URL urlContent;
  127.  
  128.     /** A base url for advertisements */
  129.     private URL urlAds;
  130.  
  131.     /** A base url for images */
  132.     private URL urlImages;
  133.  
  134.     /** The data object for this applet */
  135.     private Paper paper;
  136.  
  137.     /** The thread that downloads content into the paper */
  138.     private DownloadThread dlthread;
  139.  
  140.     /** The user interface to the paper */
  141.     private PjTabNotebook notebook;
  142.  
  143.     // --- Public constructors
  144.  
  145.     /**
  146.      * A general constructor for PjJava.
  147.      *
  148.      * @param content An absolute url with HTTP GET parameters that returns
  149.      * Personal Journal content.
  150.      * @param ads An absolute base url from which PjJava ads are located
  151.      * @param images An absolute base url from which PjJava images are
  152.      * located
  153.     */
  154.     public PjJava(URL content, URL ads, URL images)
  155.         {
  156.         urlContent = content;
  157.         urlAds = ads;
  158.         urlImages = images;
  159.         System.out.println("Debug-PjJava:constructed");
  160.         } // General constructor
  161.  
  162.     /**
  163.      * Construct PjJava with default urls for content, ads, and images.
  164.      * See the public variables of PjJava for relative content, ad,
  165.      * and image default urls.  Absolute urls are calculated relative
  166.      * to the base url passed as a parameter to this constructor.
  167.      *
  168.      * @param base The base url from which content, ad and image defaults
  169.      * are located.
  170.     */
  171.     public PjJava(URL base)
  172.         {
  173.         urlContent = getURL( base, strContentBaseUrl
  174.                    + strContentSourceUrl + strGetParams );
  175.         urlAds = getURL( base, strAdsBaseUrl );
  176.         urlImages = getURL( base, strImagesBaseUrl );
  177.         } // Constructor with default urls
  178.  
  179.     /**
  180.     * Construct PjJava as an embedded applet with default urls
  181.     * for content, ads, and images.
  182.     */
  183.     public PjJava()
  184.         {
  185.         /*
  186.         * This constructor can be called from
  187.         * either standalone or embedded applets.
  188.         */
  189.  
  190.         // Try to get URL context.
  191.         URL context = null;
  192.         try {
  193.             // Try document base (embedded applets)
  194.             context = getDocumentBase();
  195.             }
  196.         catch (NullPointerException e1)
  197.             {
  198.             try {
  199.                 // Try current directory (standalone applets)
  200.                 context = new URL("file:///"+System.getProperty("user.dir")+"/");
  201.                 }
  202.             catch(MalformedURLException e2)
  203.                 {
  204.                 // Should never happen
  205.                 assert(false);
  206.                 }
  207.             catch(SecurityException e3)
  208.                 {
  209.                 // Can happen if NullPointerException in an embedded app
  210.                 // and remote client tries to access file system.
  211.                 // Just set context to null.
  212.                 context = null;
  213.                 }
  214.             }
  215.  
  216.         // Get resource locations
  217.         urlContent = getURL( context, strContentBaseUrl
  218.                    + strContentSourceUrl + strGetParams );
  219.         urlAds = getURL( context, strAdsBaseUrl );
  220.         urlImages = getURL( context, strImagesBaseUrl );
  221.  
  222.         } // Default constructor
  223.  
  224.     // --- Public operations
  225.  
  226.     /**
  227.      * The entry point for PjJava as a standalone applet.
  228.      * Several command line options may be specified:
  229.      * <pre>
  230.      * --content <a url for content>
  231.      * --ads <a base url for advertisements>
  232.      * --images <a base url for images>
  233.      * </pre>
  234.      * If these arguments are not specified, but PjJava is invoked from
  235.      * the Java interpretter, PjJava attempts to construct defaults relative
  236.      * to the current user directory. If PjJava is invoked from a
  237.      * browser without these arguments, PjJava will throw an
  238.      * ImplementationError at runtime.
  239.      * <P>
  240.      * In general, construction of default urls can fail, and PjJava will
  241.      * throw an ImplementationError at runtime.
  242.     */
  243.     public static void main(String args[])
  244.         {
  245.         URL base;
  246.         URL content = null;
  247.         URL images = null;
  248.         URL ads = null;
  249.  
  250.         try {
  251.             base = new URL("file:///"+System.getProperty("user.dir")+"/");
  252.             }
  253.         catch(MalformedURLException e)
  254.             {
  255.             throw new ImplementationError();
  256.             }
  257.         catch(SecurityException e3)
  258.             {
  259.             // Can happen if main is invoked by a remote client.
  260.             // Just set base to null.
  261.             base = null;
  262.             }
  263.  
  264.         for (int i=0; i<args.length; i++)
  265.             if ( args[i].equals(flagContentUrl) && i<args.length-1 )
  266.                 {
  267.                 //System.out.println("Debug content arg == " + args[i+1] );
  268.                 content = getURL(args[i+1]);
  269.                 //if (content != null) System.out.println("Debug content URL from arg == " + content.toExternalForm() );
  270.                 //else System.out.println("Debug content URL from arg == null" );
  271.                 }
  272.  
  273.         if (content == null)
  274.             {
  275.             content = getURL( base, strContentBaseUrl
  276.                     + strContentSourceUrl + strGetParams );
  277.             if (content != null) System.out.println("Debug content URL from instance data == " + content.toExternalForm() );
  278.             //else System.out.println("Debug content URL from instance data == null" );
  279.             }
  280.  
  281.         for (int i=0; i<args.length; i++)
  282.             if ( args[i].equals(flagAdsUrl) && i<args.length-1 )
  283.                 {
  284.                 //System.out.println("Debug ads arg == " + args[i+1] );
  285.                 ads = getURL(args[i+1]);
  286.                 //if (ads != null) System.out.println("Debug ads URL from arg == " + ads.toExternalForm() );
  287.                 //else System.out.println("Debug ads URL from arg == null" );
  288.                 }
  289.  
  290.         if (ads == null)
  291.             {
  292.             ads = getURL( base, strAdsBaseUrl );
  293.             //if (ads != null) System.out.println("Debug ads URL from instance data == " + ads.toExternalForm() );
  294.             //else System.out.println("Debug ads URL from instance data == null" );
  295.             }
  296.  
  297.         for (int i=0; i<args.length; i++)
  298.             if ( args[i].equals(flagImagesUrl) && i<args.length-1 )
  299.                 {
  300.                 //System.out.println("Debug images arg == " + args[i+1] );
  301.                 images = getURL(args[i+1]);
  302.                 //if (images != null) System.out.println("Debug images URL from arg == " + images.toExternalForm() );
  303.                 //else System.out.println("Debug images URL from arg == null" );
  304.                 }
  305.  
  306.         if (images == null)
  307.             {
  308.             images = getURL( base, strImagesBaseUrl );
  309.             //if (images != null) System.out.println("Debug images URL from instance data == " + images.toExternalForm() );
  310.             //else System.out.println("Debug images URL from instance data == null" );
  311.             }
  312.  
  313.         PjJava pjApplet = new PjJava( content, ads, images );
  314.  
  315.         pjApplet.init();
  316.         pjApplet.start();
  317.  
  318.         frmPjJava = new PjFrame( getPjImages() );
  319.         frmPjJava.add("Center", pjApplet);
  320.         frmPjJava.pack();
  321.  
  322.         frmPjJava.resize( PjJava.WIDTH, PjJava.HEIGHT );
  323.  
  324.         frmPjJava.show();
  325.         //frmPjJava.list();
  326.  
  327.         } // main
  328.  
  329.     /**
  330.      * Initialize the applet.  If the applet is invoked in the context
  331.      * of a browser, check whether the applet tag specifies a content,
  332.      * ad or image urls and try to use the specified urls.  Otherwise,
  333.      * just use the urls specified during construction.
  334.      * <P>
  335.      * The relevant applet tags are:
  336.      * <pre>
  337.      * "ContentUrl"
  338.      * "AdBaseUrl"
  339.      * "ImageBaseUrl"
  340.      * </pre>
  341.     */
  342.     public void init()
  343.         {
  344.         setLayout( new BorderLayout() );
  345.  
  346.         // Check applet tag for url parameters
  347.         try {
  348.             urlContent = getUrlFromParameter(urlContent,"ContentUrl");
  349.             urlAds     = getUrlFromParameter(urlAds,"AdsUrl");
  350.             urlImages  = getUrlFromParameter(urlImages,"ImagesUrl");
  351.             }
  352.         catch(NullPointerException e)
  353.             { /*applet not invoked in browser context*/ }
  354.  
  355.         // Create data object
  356.         paper = new Paper();
  357.  
  358.         // Create user interface
  359.         assert(urlAds != null);
  360.         pjads = new PjAds(urlAds);
  361.  
  362.         assert(urlImages != null);
  363.         pjimages = new PjImages(urlImages);
  364.  
  365.         notebook = new PjTabNotebook(paper,pjads,pjimages);
  366.         notebook.firstPage();
  367.  
  368.         add("Center", notebook);
  369.         resize(minimumSize());
  370.         } // init
  371.  
  372.     /**
  373.      * Returns PjImage object created in init()
  374.      */
  375.     public static PjImages getPjImages()
  376.         {
  377.         return pjimages;
  378.         }
  379.  
  380.     /**
  381.      * Start a download thread using the server url specified during
  382.      * construction or initialization. If the server url is null,
  383.      * this operation aborts without starting a download.
  384.     */
  385.     public void start()
  386.         {
  387.         assert(urlContent != null);
  388.         //System.out.println("Debug downloading from " + urlContent.toString());
  389.         dlthread = new DownloadThread(urlContent,paper);
  390.         dlthread.setPriority( Thread.MIN_PRIORITY );
  391.         dlthread.start();
  392.         } // start
  393.  
  394.     /**
  395.      * Stop the applet and any download thread that is running.
  396.     */
  397.     public void stop()
  398.         {
  399.         if ( dlthread != null && dlthread.isAlive() )
  400.             dlthread.stop();
  401.         }
  402.  
  403.     public void destroy()
  404.         {
  405.         if ( dlthread != null && dlthread.isAlive() )
  406.             {
  407.             dlthread.stop();
  408.             dlthread = null;
  409.             System.out.println("Debug-PjJava-destroy:passed1");
  410.             }
  411.         System.out.println("Debug-PjJava-destroy:passed2");
  412.         }
  413.  
  414.     public static Frame getPjFrame()
  415.         {
  416.         return frmPjJava;
  417.         }
  418.  
  419.     public Dimension minimumSize()
  420.         {
  421.         return new Dimension( PjJava.WIDTH, PjJava.HEIGHT );
  422.         }
  423.  
  424.     /**
  425.      * Raise an exception if predicate is false.
  426.      * @see collections.Assertable
  427.      * @exception ImplementationError thrown if predicate is false.
  428.     */
  429.     public void assert(boolean predicate) throws ImplementationError
  430.         {
  431.         ImplementationError.assert(this, predicate);
  432.         }
  433.  
  434.     /**
  435.      * Let notebook handle the action from the menu selection.
  436.     */
  437.     public boolean action(Event evt, Object arg)
  438.         {
  439.         notebook.handleEvent(evt);
  440.         return true;
  441.  
  442.         }
  443.  
  444.  
  445.     // --- Package operations
  446.  
  447.     /**
  448.      * Convert string to an absolute url.
  449.      * @param urlParam full specification for absolute url.
  450.      * @return A valid url, or null if urlParam is null or malformed.
  451.     */
  452.     static URL getURL(String urlParam)
  453.         {
  454.         return getURL(null,urlParam);
  455.         }
  456.  
  457.     /**
  458.      * Convert string to a relative or absolute url
  459.      * @param docBase base for relative url, null for absolute url
  460.      * @param urlParam offset for relative url, full specification for
  461.      * absolute url.
  462.      * @return A valid url, or null if urlParam is null or parameters
  463.      * are malformed.
  464.     */
  465.     static URL getURL(URL docBase, String urlParam)
  466.         {
  467.         URL url = null;
  468.  
  469.         if (urlParam == null)
  470.             return url;
  471.  
  472.         try {
  473.             if (docBase != null)
  474.                 url = new URL( docBase, urlParam);
  475.             else
  476.                 url = new URL( urlParam );
  477.             }
  478.         catch (MalformedURLException e)
  479.             {
  480.             url = null;
  481.             }
  482.  
  483.         return url;
  484.         } // getURL
  485.  
  486.     // --- Private operations
  487.  
  488.     /**
  489.      * Get a url from an applet tag parameter
  490.     */
  491.     private URL getUrlFromParameter(URL urlDefault, String param)
  492.         {
  493.         URL url;
  494.         String str = getParameter(param);
  495.  
  496.         // Try interpretting str as absolute URL
  497.         url = getURL(str);
  498.         if (url != null) return url;
  499.  
  500.         // Try interpretting as relative
  501.         url = getURL(getDocumentBase(), str);
  502.         if (url != null) return url;
  503.  
  504.         // If neither absolute and nor relative, ignore it
  505.         return urlDefault;
  506.  
  507.         } // getUrlFromParameter
  508.  
  509.  
  510.  
  511.     } // PjJava
  512.