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

  1. // TsolPanel.java
  2. // 18.03.96
  3. //
  4. // The time of the next Tsol
  5.  
  6. package cybcerone.main;
  7.  
  8. import java.util.Vector;
  9. import java.awt.Image;
  10. import java.awt.Rectangle;
  11. import java.awt.Graphics;
  12.  
  13. import cybcerone.utils.Date;
  14. import cybcerone.utils.SuperPanel;
  15.  
  16. /**
  17.  * Keeps a countdown until the next metro in both directions.  When
  18.  * there's less than a minute left, numbers turn red.
  19.  */
  20. class TsolPanel extends SuperPanel implements Runnable {
  21.   private static final Rectangle placement = 
  22.     new Rectangle (768, 433, 256, 148);
  23.  
  24.   private static final String id = "TsolPanel";
  25.   private static final String statusText = "Time until the next metro";
  26.  
  27.   private static final String imagePath = MainPanel.imagePath;
  28.   private static final String dataPath = "TSOL/";
  29.  
  30.   private static final String bgFile = imagePath + "fond_tsol.gif";
  31.   private static final String tsolExceptionsFile = dataPath + "exceptions";
  32.  
  33.   private static final int SUNDAY = 0;
  34.   private static final int SATURDAY = 6;
  35.  
  36.   private TsolTime redTime;  /* when this time remains, display turns red */
  37.   private TsolTime tooLong;  /* if it's this far away, don't show it */
  38.   
  39.   private TsolNumImages redNums;
  40.   private TsolNumImages whiteNums;
  41.  
  42.   private Thread updater;
  43.   private TsolTime nextLausanne;
  44.   private TsolTime nextRenens;
  45.   
  46.   private TsolExceptions exceptions;
  47.   private TsolTimes lausanneTimes;
  48.   private TsolTimes renensTimes;
  49.  
  50.   /* for double buffering */
  51.   private Image offscreenImg;
  52.   private Graphics offscreenG;
  53.  
  54.   /* and for clipping */
  55.   private boolean smallRepaint;
  56.  
  57.   private final int x = scale (155);
  58.   private final int y1 = scale (59);
  59.   private final int y2 = scale (108);
  60.   private Rectangle clip = new Rectangle (scale (155), scale (59), 
  61.                       scale (90), scale (75));
  62.  
  63.   private boolean reloading;
  64.   
  65.   TsolPanel (SuperPanel app) {
  66.     super (id, statusText, app);
  67.     reshape (placement);
  68.     
  69.     setBackground (bgFile, 0);
  70.     redNums = new TsolNumImages (imagePath, "red", 1, this);
  71.     whiteNums = new TsolNumImages (imagePath, "white", 1, this);
  72.     
  73.     redTime = new TsolTime (0, 1, 0);
  74.     tooLong = new TsolTime (1, 0, 0);
  75.     getData (tsolExceptionsFile, new TsolException (), this);
  76.     reloading = true;
  77.   }
  78.  
  79.   public void start () {
  80.     if (updater == null) {
  81.       updater = new Thread (this);
  82.       updater.setPriority (Thread.MAX_PRIORITY - 1);
  83.       updater.start ();
  84.     }
  85.   }
  86.   
  87.   public void stop () {
  88.     if (updater != null) 
  89.       updater.stop ();
  90.     updater = null;
  91.   }
  92.   
  93.   public void run () {
  94.     long now;
  95.     long updateTime = 0;
  96.     TsolTime time;
  97.     TsolTime renens;
  98.     TsolTime lausanne;
  99.  
  100.     while (updater != null) {
  101.       if ((updateTime += 1000) < (now = System.currentTimeMillis ()))
  102.     updateTime = now;
  103.       
  104.       if (lausanneTimes == null || renensTimes == null ||
  105.       (lausanneTimes.size () == 0 && renensTimes.size () == 0)) {
  106.     hide ();
  107.     
  108.     /* if the last metro has left, wait for an hour and then reload 
  109.      * for tomorrow */
  110.     if (renensTimes != null && renensTimes.size () == 0 && !reloading) {
  111.       reloading = true;
  112.       try {
  113.         updater.sleep (3600000);
  114.       } catch (InterruptedException e) {
  115.       }
  116.       update ();
  117.     }
  118.       } else {
  119.  
  120.     if (nextLausanne != null && nextRenens != null &&
  121.         nextLausanne.after (tooLong) && nextRenens.after (tooLong))
  122.       hide ();
  123.     else
  124.       show ();
  125.  
  126.     time = new TsolTime (new Date ());
  127.     lausanne = lausanneTimes.getNext (time);
  128.     if (lausanne != null) {
  129.       nextLausanne = lausanne.minus (time);
  130.     } else
  131.       nextLausanne = null;
  132.     renens = renensTimes.getNext (time);
  133.     if (renens != null)
  134.       nextRenens = renens.minus (time);
  135.     else
  136.       nextRenens = null;
  137.  
  138.     smallRepaint = true;
  139.     repaint ();
  140.       }
  141.       
  142.       try {
  143.     updater.sleep (updateTime - System.currentTimeMillis ());
  144.       } catch (InterruptedException e) {
  145.       }
  146.     }
  147.   }
  148.  
  149.   public void update (Vector theData) {
  150.     if (theData.firstElement () instanceof TsolException) 
  151.       update (new TsolExceptions (theData));
  152.     else if (theData.firstElement () instanceof TsolTime)
  153.       update (new TsolTimes (theData));
  154.     else 
  155.       System.err.println ("TsolPanel: ERROR--updated with " + theData);
  156.   }
  157.   
  158.   public void update (TsolExceptions theExceptions) {
  159.     exceptions = theExceptions;
  160.     update ();
  161.   }
  162.  
  163.   public void update () {
  164.     lausanneTimes = null;
  165.     renensTimes = null;
  166.     getLausanneTimes ();
  167.   }
  168.  
  169.   public void update (TsolTimes theTimes) {
  170.     if (lausanneTimes == null) {
  171.       lausanneTimes = theTimes;
  172.       getRenensTimes ();
  173.     } else {
  174.       renensTimes = theTimes;
  175.       reloading = false;
  176.     }
  177.   }
  178.  
  179.   private void getLausanneTimes () {
  180.     getData (dataPath + getBaseFile () + "Lausanne", new TsolTime (), this);
  181.   }
  182.   
  183.   private void getRenensTimes () {
  184.     getData (dataPath + getBaseFile () + "Renens", new TsolTime (), this);
  185.   }
  186.  
  187.   private String getBaseFile () {
  188.     Date now = new Date ();
  189.     
  190.     if (exceptions.contains (now) || now.getDay () == SUNDAY)
  191.       return "dimanches";
  192.     else if (now.getDay () == SATURDAY)
  193.       return "samedis";
  194.     else
  195.       return "semaine";
  196.   }
  197.  
  198.   public void paint (Graphics g) {
  199.     super.paint (g);
  200.  
  201.     if (smallRepaint) {
  202.       smallRepaint = false;
  203.       g.clipRect (clip.x, clip.y, clip.width, clip.height);
  204.     }
  205.  
  206.     if (nextLausanne != null && nextLausanne.before (tooLong)) {
  207.       if (nextLausanne.after (redTime))
  208.     nextLausanne.paint (g, x, y1, whiteNums, this);
  209.       else
  210.     nextLausanne.paint (g, x, y1, redNums, this);
  211.     }
  212.     
  213.     if (nextRenens != null && nextRenens.before (tooLong)) {
  214.       if (nextRenens.after (redTime))
  215.     nextRenens.paint (g, x, y2, whiteNums, this);
  216.       else
  217.     nextRenens.paint (g, x, y2, redNums, this);
  218.     }
  219.   }
  220.  
  221.   public void update (Graphics g) {
  222.     // for double buffering;
  223.     if (offscreenG == null) {
  224.       offscreenImg = createImage (size().width, size().height);
  225.       offscreenG = offscreenImg.getGraphics ();
  226.     }
  227.  
  228.     paint (offscreenG);
  229.     g.drawImage (offscreenImg, 0, 0, this);
  230.   }
  231. }
  232.