home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / WebComponent.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  3.6 KB  |  138 lines

  1. // WebComponent
  2. // A JFS component for loading data from URLs, via /dev/Web
  3. import java.awt.*;
  4. import java.net.*;
  5.  
  6. public class WebComponent extends JFScomponent
  7. {
  8.     java.applet.Applet ap;            // root applet
  9.     TextField url;                // URL string to get
  10.     Button get;
  11.     StaticTextField type, size;
  12.     Button view, save;
  13.     FileReq savereq;
  14.  
  15.     byte urldata[] = null;            // data read from URL
  16.     String handler = null;
  17.  
  18.     void init(java.applet.Applet a)
  19.     {
  20.     ap = a;
  21.     // create user interface
  22.     setLayout(new BorderLayout());
  23.     Panel top = new BorderPanel(2);
  24.     top.setLayout(new BorderLayout());
  25.     top.add("West",new Label("URL"));
  26.     top.add("Center",url = new TextField("http://"));
  27.     Panel getp = new Panel();
  28.     getp.setLayout(new FlowLayout(FlowLayout.RIGHT));
  29.     getp.add(get = new Button("Get"));
  30.     top.add("South",getp);
  31.     add("North",top);
  32.  
  33.     Panel bot = new BorderPanel(2);
  34.     bot.setLayout(new BorderLayout());
  35.     Panel vsp = new Panel();
  36.     vsp.setLayout(new BorderLayout());
  37.     Panel vspl = new Panel();
  38.     vspl.setLayout(new GridLayout(2,1));
  39.     vspl.add(new Label("Content type"));
  40.     vspl.add(new Label("Size"));
  41.     vsp.add("West",vspl);
  42.     Panel vspr = new Panel();
  43.     vspr.setLayout(new GridLayout(2,1));
  44.     vspr.add(type = new StaticTextField());
  45.     vspr.add(size = new StaticTextField());
  46.     vsp.add("Center",vspr);
  47.     bot.add("North",vsp);
  48.     Panel butp = new Panel();
  49.     butp.setLayout(new FlowLayout(FlowLayout.RIGHT));
  50.     butp.add(view = new Button("View"));
  51.     butp.add(save = new Button("Save"));
  52.     view.disable();
  53.     save.disable();
  54.     bot.add("South",butp);
  55.     add("Center",bot);
  56.     }
  57.  
  58.     public boolean action(Event evt, Object arg)
  59.     {
  60.     if (evt.target == get) {
  61.         // Get button clicked.
  62.         // Read from /dev/Web to get the data from this URL
  63.         URL userurl = null;
  64.         try userurl = new URL(url.getText());
  65.         catch(MalformedURLException e) {
  66.             new ErrorWindow("Invalid URL format");
  67.             return true;
  68.             }
  69.         Message msg = new Message();
  70.         msg.add("File","/dev/Web");
  71.         msg.add("URL",userurl.toString());
  72.         Message r = null;
  73.         try r = client.send("Get", msg);
  74.         catch(RequestException e) {
  75.             new ErrorWindow("Get URL failed : "+e.getMessage());
  76.             return true;
  77.             }
  78.  
  79.         // Update URL information fields
  80.         type.setText(r.find("Content-type"));
  81.         client.currenttype = type.getText();
  82.         size.setText(r.find("Content-length"));
  83.         urldata = r.getdata();
  84.         save.enable();
  85.  
  86.         // Try to find a handler class for the data
  87.         view.disable();
  88.         try handler =  client.gethandler(type.getText());
  89.         catch(RequestException e)
  90.             handler = null;
  91.         if (handler != null)
  92.             view.enable();
  93.         }
  94.  
  95.     else if (evt.target == save && savereq == null) {
  96.         // Save URL data button clicked
  97.         String dummy[] = {};
  98.         savereq = new FileReq(client, this, "Save", "*/*", true, dummy);
  99.         }
  100.     else if (evt.target == savereq) {
  101.         // File to save chosen
  102.         if (((String)evt.arg).equals("Save")) {
  103.             // save the current file
  104.             try save(urldata, savereq.getfile(), savereq.gettype(),
  105.                  savereq.getversion(), savereq.multiversion());
  106.             catch(RequestException e)
  107.                 new ErrorWindow("Could not save "+
  108.                         savereq.getfile()+" : "+
  109.                         e.getMessage());
  110.             }
  111.         savereq = null;
  112.         }
  113.  
  114.     else if (evt.target == view) {
  115.         // View URL data button clicked
  116.         // Save the data to a temp file, and invoke a handler
  117.         // program to view it.
  118.         String tmp = "/tmp/"+System.currentTimeMillis();
  119.         try save(urldata, tmp, type.getText(), 0, false);
  120.         catch(RequestException e) {
  121.             new ErrorWindow("Could not write to temp file : "+
  122.                     e.getMessage());
  123.             return true;
  124.             }
  125.         new ProgramWindow(handler, client, ap, tmp, 0);
  126.         try client.delete(tmp,0);
  127.         catch(RequestException e);
  128.         }
  129.     return true;
  130.     }
  131.  
  132.     Dimension wantedsize()
  133.     {
  134.     return new Dimension(400,220);
  135.     }
  136. }
  137.  
  138.