home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  10.0 KB  |  340 lines

  1. // Cybcerone.java
  2. // 27.02.96
  3. //
  4. // the mother of all applets
  5.  
  6. import java.applet.Applet;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Frame;
  10. import java.awt.Event;
  11.  
  12. import cybcerone.utils.SuperPanel;
  13. import cybcerone.utils.Scaler;
  14. import cybcerone.utils.SuperPanels;
  15. import cybcerone.utils.Appletlike;
  16. import cybcerone.utils.Literate;
  17. import cybcerone.utils.Mapable;
  18. import cybcerone.utils.DataLoader;
  19. import cybcerone.utils.Date;
  20.  
  21. import cybcerone.init.InitPanel;
  22. import cybcerone.main.MainPanel;
  23. import cybcerone.credit.CreditPanel;
  24. import cybcerone.orient.PreorientPanel;
  25. import cybcerone.orient.OrientPanel;
  26. import cybcerone.person.PersonPanel;
  27. import cybcerone.faculte.FacultePanel;
  28. import cybcerone.manif.ManifPanel;
  29. import cybcerone.unite.UnitePanel;
  30. import cybcerone.control.ControlPanel;
  31. import cybcerone.control.UrgencesPanel;
  32.  
  33. import cybcerone.main.NowPanel;
  34.  
  35. /**
  36.  * This is the class that implements Cybcerone, an interactive information
  37.  * kiosk.  The interface consists of a ControlPanel along the bottom and
  38.  * one SuperPanel showing in the body of the screen.  Each SuperPanel 
  39.  * provides some functionality, and switching between them can be accomplished
  40.  * via the ControlPanel or often by selecting elements on the current
  41.  * SuperPanel itself.  This class merely starts all the SuperPanels and
  42.  * provides a means for them to read images and data, as well as to switch
  43.  * among themselves.
  44.  */
  45. public class Cybcerone extends Applet implements Runnable, Appletlike {
  46.   public static final String id = "cybcerone";
  47.   public static String imagePath = "images/";
  48.   public static final String dataPath = "data/";
  49.   private static final int fullWidth = 1024;
  50.   private static final int fullHeight = 768;
  51.  
  52.   private static final float scale = (float) (1 /Math.sqrt (2.0));
  53.  
  54.   private SuperPanels panels;
  55.   private SuperPanel currentPanel;
  56.  
  57.   private MediaTracker tracker;
  58.   private Thread imageLoader;
  59.   private int maxPriority = 0;
  60.   private boolean panelsLoaded;
  61.  
  62.   private DataLoader dataLoader;
  63.   private OrientPanel theMapPanel;
  64.   private ControlPanel theControlBar;
  65.   private InitPanel theInitPanel;
  66.  
  67.   /**
  68.    * Read available parameters, set up the SuperPanel's 
  69.    * and the control bar.
  70.    */
  71.   public void init () {
  72.     setLayout (null);
  73.     readParameters ();
  74.     resize (Scaler.scale (fullWidth), Scaler.scale (fullHeight));
  75.     
  76.     tracker = new MediaTracker (this);
  77.     dataLoader = new DataLoader (this, this);
  78.     
  79.     add (theControlBar = new ControlPanel (this));
  80.     add (theInitPanel = new InitPanel (this));
  81.     theInitPanel.show ();
  82.     theControlBar.show ();
  83.  
  84.     panels = new SuperPanels ();
  85.     panels.add (new MainPanel (this));
  86.     panels.add (new CreditPanel (this));
  87.     panels.add (new PreorientPanel (this));
  88.     panels.add (new UnitePanel (this));
  89.     panels.add (new PersonPanel (this));
  90.     panels.add (theMapPanel = new OrientPanel (this));
  91.     panels.add (new FacultePanel (this));
  92.     panels.add (new ManifPanel (this));
  93.     panels.add (new UrgencesPanel (this));
  94.     panels.hide ();
  95.     add (panels);
  96.   }
  97.  
  98.   /**
  99.    * Read the parameters from the web page.
  100.    * date      - specifies the current day: yyyy:mm:dd
  101.    * preevent  - how many minutes before an event is happening now
  102.    * postevent - how many minutes after an event has started should it still
  103.    *           - be displayed as happening now
  104.    * scale     - all constants in these files are based on a 1024x768 screen
  105.                - that can be reduced (or enlarged) by setting a scaling factor
  106.    * imagepath - the directory to use for images
  107.    */
  108.   private void readParameters () {
  109.     /* of the form yyyy:mm:dd */
  110.     String currentDate = getParameter ("DATE");
  111.     if (currentDate != null && currentDate.length () > 0)
  112.       Date.setCurrent (Integer.parseInt (currentDate.substring (0, 4)),
  113.                Integer.parseInt (currentDate.substring (5, 7)),
  114.                Integer.parseInt (currentDate.substring (8, 10)));
  115.     
  116.     String preEvent = getParameter ("PREEVENT");
  117.     if (preEvent != null && preEvent.length () > 0)
  118.       NowPanel.setFutureOffset (Integer.parseInt (preEvent));
  119.     
  120.  
  121.     String postEvent = getParameter ("POSTEVENT");
  122.     if (postEvent != null && postEvent.length () > 0)
  123.       NowPanel.setPastOffset (Integer.parseInt (postEvent));
  124.  
  125.     String scale = getParameter ("SCALE");
  126.     if (scale != null && scale.length () > 0)
  127.       Scaler.setScale ((Float.valueOf(scale)).floatValue ());
  128.  
  129.     String path = getParameter ("IMAGEPATH");
  130.     if (path != null && path.length () > 0)
  131.       imagePath = path;
  132.   }
  133.  
  134.   /** Optional parameters. */
  135.   public String[][] getParameterInfo() {
  136.     String[][] info = {
  137.       // Parameter Name     Kind of Value   Description
  138.       {"date",      "string", "Set the current date, yyyy:mm:dd"},
  139.       {"preEvent",  "int",    "Number of minutes ahead for the now panel"},
  140.       {"postEvent", "int",    "Number of minutes behind for the now panel"},
  141.       {"scale",     "float",  "Image (and font) scaling factor"},
  142.       {"imagePath", "string", "Image directory, relative to the html file"},
  143.     };
  144.     return info;
  145.   }   
  146.  
  147.   /** Information about this applet. */
  148.   public String getAppletInfo() {
  149.     return ("Cybcerone, v1.0, 1995-1996, Singularis Communication & " + 
  150.         "The Computer Science Institute of the " + 
  151.         "University of Lausanne, Switzerland");
  152.   }
  153.   
  154.   /**
  155.    * Go to the initialization screen.  Start a thread in the background
  156.    * to load the image and data.
  157.    */
  158.   public void start () {
  159.     theInitPanel.show ();
  160.     theInitPanel.start ();
  161.  
  162.     if (imageLoader == null) {
  163.       imageLoader = new Thread (this);
  164.       imageLoader.start ();
  165.     }
  166.     theControlBar.start ();
  167.   }
  168.  
  169.   /**
  170.    * Stop anything going on in the current screen.  If we're loading images
  171.    * or data, stop those threads too.
  172.    */
  173.   public void stop () {
  174.     if (currentPanel != null)
  175.       currentPanel.stop ();
  176.     if (imageLoader != null && imageLoader.isAlive ())
  177.       imageLoader.stop ();
  178.     imageLoader = null;
  179.  
  180.     theInitPanel.stop ();
  181.     dataLoader.stop ();
  182.     theControlBar.stop ();
  183.   }
  184.  
  185.   /**
  186.    * Load the images for the initialization screen, then load the images
  187.    * for the rest of the screens.  Wait until all the data is done loading
  188.    * before going to the main screen.
  189.    */
  190.   public void run () {
  191.     try {
  192.       initMessage ("Loading image set " + 0 + " of " + maxPriority + "...");
  193.       tracker.waitForID (0);
  194.       dataLoader.start ();
  195.       
  196.       for (int i = 1; i <= maxPriority; i++) {
  197.     initMessage ("Loading image set " + i + " of " + maxPriority + "...");
  198.     tracker.waitForID (i);
  199.       }
  200.     } catch (InterruptedException e) {
  201.     }
  202.     initMessage ("Finished loading images.");
  203.  
  204.     while (!dataLoader.finished ()) {
  205.       try {
  206.     imageLoader.sleep (1000);
  207.       } catch (InterruptedException e) {
  208.       }
  209.     }
  210.     
  211.     theInitPanel.stop ();
  212.     theInitPanel.hide ();
  213.     panels.show ();
  214.     reset ();
  215.   }
  216.  
  217.   /** 
  218.    * Handle this object according to its data type.  This method is
  219.    * implemented here to comply with the Appletlike interface.
  220.    */
  221.   public void update (Object updateVal) {
  222.     if (updateVal instanceof String) {
  223.       update ((String)updateVal);
  224.     } else if (updateVal instanceof Mapable) {
  225.       update ((Mapable)updateVal);
  226.     } else {
  227.       System.err.println ("Cybcerone: ERROR--updated with " + updateVal);
  228.     }
  229.   }
  230.  
  231.   /**
  232.    * Updated with a String, use it as the ID of the panel to switch to.
  233.    */
  234.   private void update (String newPanelId) {
  235.     theControlBar.update (newPanelId);
  236.     switchTo (newPanelId);
  237.   }
  238.  
  239.   /**
  240.    * Updated with a Mapable object.  Switch to the map panel, and display
  241.    * the appropriate information.
  242.    */
  243.   private void update (Mapable selected) {
  244.     theMapPanel.update (selected);
  245.     theControlBar.update (selected);
  246.     switchTo (OrientPanel.id);
  247.   }
  248.  
  249.   /**
  250.    * Switch to the panel with the given ID.
  251.    */
  252.   private void switchTo (String newPanelId) {
  253.     if (currentPanel != null) 
  254.       currentPanel.stop ();
  255.     currentPanel = panels.switchTo (newPanelId);
  256.     currentPanel.start ();
  257.   }    
  258.  
  259.   /** 
  260.    * Load the image with this filename, at priority 0.
  261.    */
  262.   public Image getImage (String filename) {
  263.     return getImage (filename, 0);
  264.   }
  265.  
  266.   /**
  267.    * Load the image with this filename at this priority.
  268.    */
  269.   public Image getImage (String filename, int priority) {
  270.     Image theImage = getImage (getDocumentBase (), imagePath + filename);
  271.  
  272.     tracker.addImage (theImage, priority);
  273.     if (priority > maxPriority) {
  274.       maxPriority = priority;
  275.     }
  276.     return theImage;
  277.   }
  278.  
  279.   /** 
  280.    * Load the specified file, use this Literate object to do the reading
  281.    * and create a Vector of Objects, update the requester with that Vector
  282.    * when you're done.
  283.    */
  284.   public void getData (String filename, Literate reader, 
  285.                Appletlike requester) {
  286.     dataLoader.getData (dataPath + filename, reader, requester);
  287.   }
  288.   
  289.   /**
  290.    * Same as above, but since no reader has been provided, update
  291.    * the requester with a Vector of Strings.
  292.    */
  293.   public void getData (String filename, Appletlike requester) {
  294.     dataLoader.getData (dataPath + filename, requester);
  295.   }
  296.  
  297.   /** 
  298.    * Returns the ID of this panel.
  299.    */
  300.   public String getId () { return id; }
  301.  
  302.   /** 
  303.    * If the browser supports it, change the cursor image.  Note, this
  304.    * does not work in the appletviewer.
  305.    */
  306.   public void setCursor (int cursorType) {
  307.     if (getParent () instanceof Frame)
  308.       ((Frame)getParent()).setCursor (cursorType);
  309.   }
  310.  
  311.   /**
  312.    * Go back to the main screen and reset all of the other screens.
  313.    */
  314.   public void reset () {
  315.     update (MainPanel.id);
  316.     panels.reset ();
  317.   }    
  318.  
  319.   /**
  320.    * Used to send a message to the initialization log on the initialization
  321.    * screen.  Has no effect after initialization is complete.
  322.    */
  323.   public void initMessage (String message) {
  324.     theInitPanel.update (message);
  325.   }
  326.  
  327.   /**
  328.    * The current panel receives mouse events but not keyboard events.  Is this
  329.    * a bug?  In any case, this method ensures that all events are directed
  330.    * to the current panel.
  331.    */
  332.   public boolean handleEvent (Event evt) {
  333.     if (currentPanel != null)
  334.       return currentPanel.handleEvent (evt);
  335.     else
  336.       return true;
  337.   }
  338.  
  339. }
  340.