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

  1. // NowPanel.java
  2. // 20.03.96
  3. //
  4. // events that are happening now
  5.  
  6. package cybcerone.main;
  7.  
  8. import java.awt.Rectangle;
  9. import java.awt.Image;
  10. import java.util.Enumeration;
  11.  
  12. import cybcerone.utils.Date;
  13. import cybcerone.utils.Appletlike;
  14. import cybcerone.utils.Manif;
  15.  
  16. /**
  17.  * Events that are happening now, up to 3 at a time.  If there's
  18.  * more than that, they scroll.
  19.  */
  20. public class NowPanel extends HappeningPanel implements Runnable {
  21.   private static final Rectangle placement = new Rectangle (0, 433, 768, 150);
  22.   
  23.   private static final String id = "NowPanel";
  24.   private static String statusText = "Events happening now";
  25.   
  26.   private static final String bgFile = imagePath + "nowBg.gif";
  27.   private static final String panelsBgBase = imagePath + "nowBg";
  28.   private static final int totalPanels = 3;
  29.  
  30.   private NowEventPanel panels[];
  31.  
  32.   private Thread scroller;
  33.   private NowBlinkThread blinker;
  34.   private NowUpdateThread updater;
  35.  
  36.   private Enumeration eventList;
  37.   private int activePanels;
  38.  
  39.   private static int pastOffset = 11;
  40.   private static int futureOffset = 21;
  41.  
  42.   NowPanel (Image ledsOn, Image ledsOff, Appletlike app) {
  43.     super (id, statusText, app);
  44.     reshape (placement);
  45.  
  46.     if (!Date.realDate ()) {
  47.       statusText += " (demonstration)";
  48.       setStatusText (statusText);
  49.     }
  50.     
  51.     setBackground (bgFile, 0);
  52.     panels = new NowEventPanel[totalPanels];
  53.     
  54.     for (int i = 0; i < totalPanels; i++) {
  55.       add (panels[i] = new NowEventPanel (id, statusText, ledsOn, 
  56.                       ledsOff, app));
  57.       panels[i].reshape (0, scale (37 + (35 * i)), scale (768), scale (26));
  58.       panels[i].setTitleBg (panelsBgBase + (i + 1) + ".gif");
  59.     }
  60.     
  61.     activePanels = 0;
  62.     hide ();
  63.   }
  64.  
  65.   int getActivePanels () { return activePanels; }
  66.   NowEventPanel[] getPanels () { return panels; }
  67.  
  68.   /** Remove the topmost event */
  69.   void popEvent () {
  70.     int i;
  71.  
  72.     if (activePanels > 0) {
  73.       for (i = 0; i < activePanels - 1; i++)
  74.     panels[i].copyFrom (panels[i+1]);
  75.       panels[i].setManif (null);
  76.       if (--activePanels == 0)
  77.     hide ();
  78.     }
  79.   }
  80.  
  81.   public static void setPastOffset (int offset) {
  82.     pastOffset = offset;
  83.   }
  84.  
  85.   public static void setFutureOffset (int offset) {
  86.     futureOffset = offset;
  87.   }
  88.  
  89.   /** Add an event to the bottom */
  90.   void pushEvent () {
  91.     if (getTheEvents () != null && getTheEvents().size () > 0) {
  92.       if (eventList == null || !eventList.hasMoreElements ())
  93.     eventList = getTheEvents().elements ();
  94.     
  95.       panels[activePanels++].setManif ((Manif)eventList.nextElement ());
  96.       show ();
  97.     }
  98.   }
  99.  
  100.   /** Take an event off the top and add a new one to the bottom */
  101.   void cycle () {
  102.     int i;
  103.  
  104.     if (activePanels > 0) {
  105.       for (i = 0; i < activePanels - 1; i++)
  106.     panels[i].copyFrom (panels[i+1]);
  107.       
  108.       if (!eventList.hasMoreElements ())
  109.     eventList = getTheEvents().elements ();
  110.       
  111.       panels[i].setManif ((Manif) eventList.nextElement ());
  112.     }
  113.   }
  114.  
  115.   protected Date pastDate (Date now) {
  116.     return (new Date (now.getYear (), now.getMonth (), now.getDate (),
  117.               now.getHours (), now.getMinutes () - pastOffset));
  118.   }
  119.   
  120.   protected Date futureDate (Date now) {
  121.     return (new Date (now.getYear (), now.getMonth (), now.getDate (),
  122.               now.getHours (), now.getMinutes () + futureOffset));
  123.   }
  124.  
  125.   public void update () {
  126.     super.update ();
  127.     updatePanels ();
  128.   }
  129.  
  130.   public void updatePanels () {
  131.     int size = getTheEvents().size ();
  132.  
  133.     while (activePanels > size)
  134.       popEvent ();
  135.     while (activePanels < size && activePanels < totalPanels)
  136.       pushEvent ();
  137.   }
  138.   
  139.   public void start () {
  140.     super.start ();
  141.     if (getTheEvents () != null && getTheEvents().size () > 0) {
  142.       if (scroller == null) {
  143.     scroller = new Thread (this);
  144.     scroller.start ();
  145.       }
  146.       if (blinker == null) {
  147.     blinker = new NowBlinkThread (this);
  148.     blinker.start ();
  149.       }
  150.       if (updater == null) {
  151.     updater = new NowUpdateThread (this);
  152.     updater.start ();
  153.       }
  154.     }
  155.   }
  156.   
  157.   public void stop () {
  158.     super.stop ();
  159.     if (scroller != null)
  160.       scroller.stop ();
  161.     scroller = null;
  162.     if (blinker != null)
  163.       blinker.stop ();
  164.     blinker = null;
  165.     if (updater != null)
  166.       updater.stop ();
  167.     updater = null;
  168.   }
  169.  
  170.   public void run () {
  171.     while (scroller != null) {
  172.       repaint ();
  173.     
  174.       try {
  175.     scroller.sleep (25000);
  176.       } catch (InterruptedException e) {
  177.       }
  178.       
  179.       cycle ();
  180.  
  181.     }
  182.   }
  183. }
  184.