home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / awt / MediaTracker.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.1 KB  |  224 lines

  1. package java.awt;
  2.  
  3. public class MediaTracker {
  4.    Component target;
  5.    MediaEntry head;
  6.    public static final int LOADING = 1;
  7.    public static final int ABORTED = 2;
  8.    public static final int ERRORED = 4;
  9.    public static final int COMPLETE = 8;
  10.    static final int DONE = 14;
  11.  
  12.    public MediaTracker(Component comp) {
  13.       this.target = comp;
  14.    }
  15.  
  16.    public void addImage(Image image, int id) {
  17.       this.addImage(image, id, -1, -1);
  18.    }
  19.  
  20.    public synchronized void addImage(Image image, int id, int w, int h) {
  21.       this.head = MediaEntry.insert(this.head, new ImageMediaEntry(this, image, id, w, h));
  22.    }
  23.  
  24.    public boolean checkAll() {
  25.       return this.checkAll(false);
  26.    }
  27.  
  28.    public synchronized boolean checkAll(boolean load) {
  29.       MediaEntry cur = this.head;
  30.  
  31.       boolean done;
  32.       for(done = true; cur != null; cur = cur.next) {
  33.          if ((cur.getStatus(load) & 14) == 0) {
  34.             done = false;
  35.          }
  36.       }
  37.  
  38.       return done;
  39.    }
  40.  
  41.    public synchronized boolean isErrorAny() {
  42.       for(MediaEntry cur = this.head; cur != null; cur = cur.next) {
  43.          if ((cur.getStatus(false) & 4) != 0) {
  44.             return true;
  45.          }
  46.       }
  47.  
  48.       return false;
  49.    }
  50.  
  51.    public synchronized Object[] getErrorsAny() {
  52.       MediaEntry cur = this.head;
  53.  
  54.       int numerrors;
  55.       for(numerrors = 0; cur != null; cur = cur.next) {
  56.          if ((cur.getStatus(false) & 4) != 0) {
  57.             ++numerrors;
  58.          }
  59.       }
  60.  
  61.       if (numerrors == 0) {
  62.          return null;
  63.       } else {
  64.          Object[] errors = new Object[numerrors];
  65.          cur = this.head;
  66.  
  67.          for(int var5 = 0; cur != null; cur = cur.next) {
  68.             if ((cur.getStatus(false) & 4) != 0) {
  69.                errors[var5++] = cur.getMedia();
  70.             }
  71.          }
  72.  
  73.          return errors;
  74.       }
  75.    }
  76.  
  77.    public void waitForAll() throws InterruptedException {
  78.       this.waitForAll(0L);
  79.    }
  80.  
  81.    public synchronized boolean waitForAll(long ms) throws InterruptedException {
  82.       long end = System.currentTimeMillis() + ms;
  83.       boolean first = true;
  84.  
  85.       while(true) {
  86.          int status = this.statusAll(first);
  87.          if ((status & 1) == 0) {
  88.             if (status != 8) {
  89.                return false;
  90.             }
  91.  
  92.             return true;
  93.          }
  94.  
  95.          first = false;
  96.          long timeout;
  97.          if (ms == 0L) {
  98.             timeout = 0L;
  99.          } else {
  100.             timeout = end - System.currentTimeMillis();
  101.             if (timeout <= 0L) {
  102.                return false;
  103.             }
  104.          }
  105.  
  106.          this.wait(timeout);
  107.       }
  108.    }
  109.  
  110.    public int statusAll(boolean load) {
  111.       MediaEntry cur = this.head;
  112.  
  113.       int status;
  114.       for(status = 0; cur != null; cur = cur.next) {
  115.          status |= cur.getStatus(load);
  116.       }
  117.  
  118.       return status;
  119.    }
  120.  
  121.    public boolean checkID(int id) {
  122.       return this.checkID(id, false);
  123.    }
  124.  
  125.    public synchronized boolean checkID(int id, boolean load) {
  126.       MediaEntry cur = this.head;
  127.  
  128.       boolean done;
  129.       for(done = true; cur != null; cur = cur.next) {
  130.          if (cur.getID() == id && (cur.getStatus(load) & 14) == 0) {
  131.             done = false;
  132.          }
  133.       }
  134.  
  135.       return done;
  136.    }
  137.  
  138.    public synchronized boolean isErrorID(int id) {
  139.       for(MediaEntry cur = this.head; cur != null; cur = cur.next) {
  140.          if (cur.getID() == id && (cur.getStatus(false) & 4) != 0) {
  141.             return true;
  142.          }
  143.       }
  144.  
  145.       return false;
  146.    }
  147.  
  148.    public synchronized Object[] getErrorsID(int id) {
  149.       MediaEntry cur = this.head;
  150.  
  151.       int numerrors;
  152.       for(numerrors = 0; cur != null; cur = cur.next) {
  153.          if (cur.getID() == id && (cur.getStatus(false) & 4) != 0) {
  154.             ++numerrors;
  155.          }
  156.       }
  157.  
  158.       if (numerrors == 0) {
  159.          return null;
  160.       } else {
  161.          Object[] errors = new Object[numerrors];
  162.          cur = this.head;
  163.  
  164.          for(int var6 = 0; cur != null; cur = cur.next) {
  165.             if (cur.getID() == id && (cur.getStatus(false) & 4) != 0) {
  166.                errors[var6++] = cur.getMedia();
  167.             }
  168.          }
  169.  
  170.          return errors;
  171.       }
  172.    }
  173.  
  174.    public void waitForID(int id) throws InterruptedException {
  175.       this.waitForID(id, 0L);
  176.    }
  177.  
  178.    public synchronized boolean waitForID(int id, long ms) throws InterruptedException {
  179.       long end = System.currentTimeMillis() + ms;
  180.       boolean first = true;
  181.  
  182.       while(true) {
  183.          int status = this.statusID(id, first);
  184.          if ((status & 1) == 0) {
  185.             if (status != 8) {
  186.                return false;
  187.             }
  188.  
  189.             return true;
  190.          }
  191.  
  192.          first = false;
  193.          long timeout;
  194.          if (ms == 0L) {
  195.             timeout = 0L;
  196.          } else {
  197.             timeout = end - System.currentTimeMillis();
  198.             if (timeout <= 0L) {
  199.                return false;
  200.             }
  201.          }
  202.  
  203.          this.wait(timeout);
  204.       }
  205.    }
  206.  
  207.    public int statusID(int id, boolean load) {
  208.       MediaEntry cur = this.head;
  209.  
  210.       int status;
  211.       for(status = 0; cur != null; cur = cur.next) {
  212.          if (cur.getID() == id) {
  213.             status |= cur.getStatus(load);
  214.          }
  215.       }
  216.  
  217.       return status;
  218.    }
  219.  
  220.    synchronized void setDone() {
  221.       this.notifyAll();
  222.    }
  223. }
  224.