home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 5.8 KB | 232 lines |
- // TsolPanel.java
- // 18.03.96
- //
- // The time of the next Tsol
-
- package cybcerone.main;
-
- import java.util.Vector;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.Graphics;
-
- import cybcerone.utils.Date;
- import cybcerone.utils.SuperPanel;
-
- /**
- * Keeps a countdown until the next metro in both directions. When
- * there's less than a minute left, numbers turn red.
- */
- class TsolPanel extends SuperPanel implements Runnable {
- private static final Rectangle placement =
- new Rectangle (768, 433, 256, 148);
-
- private static final String id = "TsolPanel";
- private static final String statusText = "Time until the next metro";
-
- private static final String imagePath = MainPanel.imagePath;
- private static final String dataPath = "TSOL/";
-
- private static final String bgFile = imagePath + "fond_tsol.gif";
- private static final String tsolExceptionsFile = dataPath + "exceptions";
-
- private static final int SUNDAY = 0;
- private static final int SATURDAY = 6;
-
- private TsolTime redTime; /* when this time remains, display turns red */
- private TsolTime tooLong; /* if it's this far away, don't show it */
-
- private TsolNumImages redNums;
- private TsolNumImages whiteNums;
-
- private Thread updater;
- private TsolTime nextLausanne;
- private TsolTime nextRenens;
-
- private TsolExceptions exceptions;
- private TsolTimes lausanneTimes;
- private TsolTimes renensTimes;
-
- /* for double buffering */
- private Image offscreenImg;
- private Graphics offscreenG;
-
- /* and for clipping */
- private boolean smallRepaint;
-
- private final int x = scale (155);
- private final int y1 = scale (59);
- private final int y2 = scale (108);
- private Rectangle clip = new Rectangle (scale (155), scale (59),
- scale (90), scale (75));
-
- private boolean reloading;
-
- TsolPanel (SuperPanel app) {
- super (id, statusText, app);
- reshape (placement);
-
- setBackground (bgFile, 0);
- redNums = new TsolNumImages (imagePath, "red", 1, this);
- whiteNums = new TsolNumImages (imagePath, "white", 1, this);
-
- redTime = new TsolTime (0, 1, 0);
- tooLong = new TsolTime (1, 0, 0);
- getData (tsolExceptionsFile, new TsolException (), this);
- reloading = true;
- }
-
- public void start () {
- if (updater == null) {
- updater = new Thread (this);
- updater.setPriority (Thread.MAX_PRIORITY - 1);
- updater.start ();
- }
- }
-
- public void stop () {
- if (updater != null)
- updater.stop ();
- updater = null;
- }
-
- public void run () {
- long now;
- long updateTime = 0;
- TsolTime time;
- TsolTime renens;
- TsolTime lausanne;
-
- while (updater != null) {
- if ((updateTime += 1000) < (now = System.currentTimeMillis ()))
- updateTime = now;
-
- if (lausanneTimes == null || renensTimes == null ||
- (lausanneTimes.size () == 0 && renensTimes.size () == 0)) {
- hide ();
-
- /* if the last metro has left, wait for an hour and then reload
- * for tomorrow */
- if (renensTimes != null && renensTimes.size () == 0 && !reloading) {
- reloading = true;
- try {
- updater.sleep (3600000);
- } catch (InterruptedException e) {
- }
- update ();
- }
- } else {
-
- if (nextLausanne != null && nextRenens != null &&
- nextLausanne.after (tooLong) && nextRenens.after (tooLong))
- hide ();
- else
- show ();
-
- time = new TsolTime (new Date ());
- lausanne = lausanneTimes.getNext (time);
- if (lausanne != null) {
- nextLausanne = lausanne.minus (time);
- } else
- nextLausanne = null;
- renens = renensTimes.getNext (time);
- if (renens != null)
- nextRenens = renens.minus (time);
- else
- nextRenens = null;
-
- smallRepaint = true;
- repaint ();
- }
-
- try {
- updater.sleep (updateTime - System.currentTimeMillis ());
- } catch (InterruptedException e) {
- }
- }
- }
-
- public void update (Vector theData) {
- if (theData.firstElement () instanceof TsolException)
- update (new TsolExceptions (theData));
- else if (theData.firstElement () instanceof TsolTime)
- update (new TsolTimes (theData));
- else
- System.err.println ("TsolPanel: ERROR--updated with " + theData);
- }
-
- public void update (TsolExceptions theExceptions) {
- exceptions = theExceptions;
- update ();
- }
-
- public void update () {
- lausanneTimes = null;
- renensTimes = null;
- getLausanneTimes ();
- }
-
- public void update (TsolTimes theTimes) {
- if (lausanneTimes == null) {
- lausanneTimes = theTimes;
- getRenensTimes ();
- } else {
- renensTimes = theTimes;
- reloading = false;
- }
- }
-
- private void getLausanneTimes () {
- getData (dataPath + getBaseFile () + "Lausanne", new TsolTime (), this);
- }
-
- private void getRenensTimes () {
- getData (dataPath + getBaseFile () + "Renens", new TsolTime (), this);
- }
-
- private String getBaseFile () {
- Date now = new Date ();
-
- if (exceptions.contains (now) || now.getDay () == SUNDAY)
- return "dimanches";
- else if (now.getDay () == SATURDAY)
- return "samedis";
- else
- return "semaine";
- }
-
- public void paint (Graphics g) {
- super.paint (g);
-
- if (smallRepaint) {
- smallRepaint = false;
- g.clipRect (clip.x, clip.y, clip.width, clip.height);
- }
-
- if (nextLausanne != null && nextLausanne.before (tooLong)) {
- if (nextLausanne.after (redTime))
- nextLausanne.paint (g, x, y1, whiteNums, this);
- else
- nextLausanne.paint (g, x, y1, redNums, this);
- }
-
- if (nextRenens != null && nextRenens.before (tooLong)) {
- if (nextRenens.after (redTime))
- nextRenens.paint (g, x, y2, whiteNums, this);
- else
- nextRenens.paint (g, x, y2, redNums, this);
- }
- }
-
- public void update (Graphics g) {
- // for double buffering;
- if (offscreenG == null) {
- offscreenImg = createImage (size().width, size().height);
- offscreenG = offscreenImg.getGraphics ();
- }
-
- paint (offscreenG);
- g.drawImage (offscreenImg, 0, 0, this);
- }
- }
-