home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / ProgressData.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  4.3 KB  |  150 lines

  1. /*
  2.  * @(#)ProgressData.java    1.7 95/08/22 Chris Warth
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.net;
  21.  
  22. import java.util.Observer;
  23. import java.util.Observable;
  24. import java.net.URL;
  25.  
  26. public class ProgressData extends Observable {
  27.     // We create a single instance of this class.
  28.     // the Observer/Observable stuff only works with instances.
  29.     //
  30.     public static ProgressData pdata = new ProgressData();
  31.     public static final int NEW = 0;
  32.     public static final int CONNECTED = 1;
  33.     public static final int UPDATE = 2;
  34.     public static final int DELETE = 3;
  35.  
  36.     public int lastchanged = 0;
  37.     public int what = 0;
  38.     public ProgressEntry streams[] = new ProgressEntry[20];
  39.  
  40.     /*
  41.      * Call this routine to register a new URL for the progress
  42.      * window.  until it is marked as connected this entry will have
  43.      * a busy indicator.
  44.      */
  45.     public synchronized void register(URL m) {
  46.     ProgressEntry te;
  47.     int i;
  48.  
  49.     //System.out.println("-- REGISTER: " + m.toExternalForm());
  50.     for (i = 0; i < streams.length; i++) {
  51.         if (streams[i] == null) {
  52. //        if (m != null) {
  53. //            System.out.println("register "+m.toExternalForm());
  54. //        } else {
  55. //            System.out.println("Trying to register a null url!!!");
  56. //        }
  57. //
  58. /* REMIND: content type is not available! */
  59.         te = new ProgressEntry(m, m.getFile(), null/*m.getContentType()*/) ;
  60.         streams[i] = te;
  61.         lastchanged = i;
  62.         what = NEW;
  63.         setChanged();
  64.         notifyObservers();
  65.         break;
  66.         }
  67.     }
  68.     }
  69.  
  70.     /*
  71.      * Call this routine to register a new URL for the progress
  72.      * window.  until it is marked as connected this entry will have
  73.      * a busy indicator.
  74.      */
  75.     public synchronized void connected(URL m) {
  76.     /* AVH: I made this a noop since it sends a CONNECT
  77.      * message when the first data arrives.
  78.     ProgressEntry te;
  79.     int i;
  80.     System.out.println("-- CONNECTED: " + m.toExternalForm());
  81.  
  82.     for (i = 0; i < streams.length; i++) {
  83.         if (streams[i] != null && streams[i].key == m) {
  84.         te = (ProgressEntry) streams[i];
  85.         if (!te.connected()) {
  86.             te.setType(m.toExternalForm(), m.content_type);
  87.             lastchanged = i;
  88.             what = CONNECTED;
  89.             setChanged();
  90.             notifyObservers();
  91.         }
  92.         break;
  93.         }
  94.     }
  95.     */
  96.     }
  97.  
  98.  
  99.     /*
  100.      * Call this routine to unregister a new URL for the progress
  101.      * window.  This will nuke the indicator from the ProgressWindow.
  102.      */
  103.     public synchronized void unregister(URL m) {
  104.     int i;
  105.  
  106.     //System.out.println("-- UNREGISTER: " + m.toExternalForm());
  107.     what = DELETE;
  108.     for (i = 0; i < streams.length; i++) {
  109.         if (streams[i] != null && streams[i].key == m) {
  110. //        ProgressEntry pe = (ProgressEntry) streams[i];
  111. //        System.out.println("unregister "+pe.label);
  112.         streams[i] = null;
  113.         lastchanged = i;
  114.         setChanged();
  115.               notifyObservers();
  116.         break;
  117.         }
  118.     }
  119.     }
  120.  
  121.     public synchronized void update(URL m, int total_read, int total_need) {
  122. /* REMIND: should get the URLConnection, instead of the URL, in order to
  123.    get the content-type */
  124.     int i;
  125.  
  126.     what = UPDATE;
  127.     for (i = 0; i < streams.length; i++) {
  128.         if (streams[i] != null && streams[i].key == m) {
  129.         ProgressEntry te = streams[i];
  130.             te.update(total_read, total_need);
  131.         if (!te.connected()) {
  132.             te.setType(m.getFile(), null/*m.content_type*/);
  133.             lastchanged = i;
  134.             what = CONNECTED;
  135.             setChanged();
  136.             notifyObservers();
  137.         }
  138.         lastchanged = i;
  139.         setChanged();
  140.         if (te.read >= te.need && te.read != 0) {
  141.             streams[i] = null;
  142.             what = DELETE;
  143.         }
  144.               notifyObservers();
  145.         break;
  146.         }
  147.     }
  148.     }
  149. }
  150.