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

  1. // ManifPaper.java
  2. // 01.03.96
  3. //
  4. // the paper on the manifestions screen
  5.  
  6. package cybcerone.manif;
  7.  
  8. import java.util.Enumeration;
  9.  
  10. import cybcerone.utils.Appletlike;
  11. import cybcerone.utils.ScrollingPaper;
  12. import cybcerone.utils.Manif;
  13. import cybcerone.utils.ManifVector;
  14.  
  15.  
  16. /**
  17.  * The list of events.
  18.  */
  19. class ManifPaper extends ScrollingPaper implements Runnable {
  20.   private static final String id = "manifPaper";
  21.   private static final String statusText = "Here are the events";
  22.  
  23.   private Thread updater;
  24.   private Interval currentInterval = new Interval (Interval.ALL);
  25.   private Fac currentFac = new Fac (Fac.ALL);
  26.  
  27.   ManifPaper (Appletlike app) {
  28.     super (id, statusText, app);
  29.   }
  30.  
  31.   void update (Interval theInterval) {
  32.     currentInterval = theInterval;
  33.     update ();
  34.   }
  35.  
  36.   void update (Fac theFac) {
  37.     currentFac = theFac;
  38.     update ();
  39.   }
  40.  
  41.   void update () {
  42.     if (updater != null)
  43.       updater.stop ();
  44.  
  45.     updater = new Thread (this);
  46.     updater.start ();
  47.   }
  48.  
  49.   public void stop () {
  50.     if (updater != null)
  51.       updater.stop ();
  52.     updater = null;
  53.   }
  54.  
  55.   public void run () {
  56.     int first, last;
  57.     Manif oneManif;
  58.  
  59.     while (theData == null) {
  60.       try {
  61.     updater.sleep (1000);
  62.       } catch (InterruptedException e) {
  63.       }
  64.     }
  65.     
  66.     ManifVector theManifs = new ManifVector (theData.toVector ());
  67.     
  68.     if (currentInterval.getInterval () == Interval.ALL) {
  69.       first = 0;
  70.       last = theManifs.size () - 1;
  71.     } else {
  72.       first = searchFirst (currentInterval, theManifs);
  73.       last = searchLast (currentInterval, theManifs, first);
  74.     }
  75.     
  76.     if (currentFac.getFac () == Fac.ALL) {
  77.       setDataRange (theData, first, last);
  78.     } else {
  79.       clearPages ();
  80.       for (int i = first; i <= last; i++) {
  81.     oneManif = theManifs.elementAt (i);
  82.     
  83.     if (currentFac.includes (oneManif)) {
  84.       addLine (oneManif);
  85.     }
  86.       }
  87.     }
  88.     
  89.     repaint ();
  90.   }
  91.  
  92.   /** 
  93.    * Find the first event in this interval.
  94.    */
  95.   private int searchFirst (Interval theInterval, ManifVector events) {
  96.     int first = 0;
  97.     int last = events.size () - 1;
  98.     int current = 0;
  99.     Manif oneManif = null;
  100.  
  101.     while (last > first) {
  102.       current = (first + last) / 2;
  103.       oneManif = events.elementAt (current);
  104.       if (theInterval.includes (oneManif)) {
  105.     last = current;
  106.       } else {
  107.     if (theInterval.after (oneManif))
  108.       first = current + 1;
  109.     else
  110.       last = current - 1;
  111.       }
  112.     }
  113.     
  114.     if (last == first) {
  115.       current = first;
  116.       oneManif = events.elementAt (current);
  117.     }
  118.     
  119.     if (theInterval.includes (oneManif)) {
  120.       return current;
  121.     } else {
  122.       return -1;
  123.     }
  124.   }
  125.  
  126.  
  127.   /** 
  128.    * Find the last event in this interval.
  129.    */
  130.   private int searchLast (Interval theInterval, ManifVector events, int first) {
  131.     int last = events.size () - 1;
  132.     Manif oneManif;
  133.     int current = 0;
  134.  
  135.     if (first >= 0) {
  136.     
  137.       while (last > first) {
  138.         current = (first + last + 1) / 2;
  139.     oneManif = events.elementAt (current);
  140.         if (theInterval.includes (oneManif)) {
  141.           first = current;
  142.         } else {
  143.           if (theInterval.after (oneManif)) {
  144.             first = current + 1;
  145.           } else {
  146.             last = current - 1;
  147.       }
  148.         }
  149.       }
  150.       if (first == last) current = first;
  151.               
  152.       return current;
  153.       
  154.     } else {
  155.       
  156.       return (first - 1);
  157.       
  158.     }
  159.   }
  160. }
  161.  
  162.