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

  1. // UnitePaper.java
  2. // 18.03.96
  3. //
  4. // the paper on the unite screen
  5.  
  6. package cybcerone.unite;
  7.  
  8. import cybcerone.utils.Appletlike;
  9. import cybcerone.utils.ScrollingPaper;
  10. import cybcerone.utils.UniteVector;
  11.  
  12. /**
  13.  * The paper on the unite screen.
  14.  */
  15. class UnitePaper extends ScrollingPaper implements Runnable {
  16.   private static final String id = "UnitePaper";
  17.   private static final String statusText = "Click for a room";
  18.  
  19.   private Thread updater;
  20.   private String updateName;
  21.   
  22.   UnitePaper (Appletlike app) {
  23.     super (id, statusText, app);
  24.   }
  25.  
  26.   /**
  27.    * Search for this name.
  28.    */
  29.   void update (String name) {
  30.     if (updater != null)
  31.       updater.stop ();
  32.  
  33.     updateName = name;
  34.     updater = new Thread (this);
  35.     updater.start ();
  36.   }
  37.     
  38.   public void stop () {
  39.     if (updater != null)
  40.       updater.stop ();
  41.     updater = null;
  42.   }
  43.  
  44.   public void run () {
  45.     int first, last;
  46.  
  47.     while (theData == null) {
  48.       try {
  49.     updater.sleep (1000);
  50.       } catch (InterruptedException e) {
  51.       }
  52.     }
  53.  
  54.     UniteVector thePeople = new UniteVector (theData.toVector ());
  55.     
  56.     if (updateName.equals ("")) {
  57.       first = 0;
  58.       last = thePeople.size () - 1;
  59.     } else {
  60.       first = searchFirst (updateName, thePeople);
  61.       last = searchLast (updateName, thePeople, first);
  62.     }
  63.     setDataRange (theData, first, last);
  64.   }
  65.   
  66.   /** 
  67.    * Find the first occurance of this name.
  68.    */
  69.   private int searchFirst (String name, UniteVector rooms) {
  70.     int first = 0;
  71.     int last = rooms.size () - 1;
  72.     int current = 0;
  73.     String UniteName = null;
  74.  
  75.     while (last > first) {
  76.       current = (first + last) / 2;
  77.       UniteName = rooms.elementAt(current).getName();
  78.       if (UniteName.startsWith (name)) {
  79.         last = current;
  80.       } else {
  81.         if (UniteName.compareTo (name) < 0)
  82.           first = current + 1;
  83.         else
  84.           last = current - 1;
  85.       }
  86.     }
  87.     
  88.     if (last == first) {
  89.       current = first;
  90.       UniteName = rooms.elementAt(current).getName();
  91.     }
  92.     
  93.     if (UniteName.startsWith (name)) {
  94.       return current;
  95.     } else {
  96.       return -1;
  97.     }
  98.   }
  99.  
  100.   /** 
  101.    * Find the last occurance of this name.
  102.    */
  103.   private int searchLast (String name, UniteVector rooms, int first) {
  104.     int last = rooms.size () - 1;
  105.     String UniteName = null;
  106.     int current = 0;
  107.  
  108.     if (first >= 0) {
  109.     
  110.       while (last > first) {
  111.         current = (first + last + 1) / 2;
  112.         UniteName = rooms.elementAt(current).getName();
  113.         if (UniteName.startsWith (name)) {
  114.           first = current;
  115.         } else {
  116.           if (UniteName.compareTo (name) < 0) {
  117.             first = current + 1;
  118.           } else {
  119.             last = current - 1;
  120.       }
  121.         }
  122.       }
  123.       if (first == last) current = first;
  124.       
  125.       return current;
  126.       
  127.     } else {
  128.       
  129.       return (first - 1);
  130.       
  131.     }
  132.   }
  133. }
  134.  
  135.