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

  1. // FileBrowserComponent
  2. // A JFS application for copying, deleting and moving files on a JFS filesystem.
  3. // Also has the ability to launch other JFS programs for viewing and editing
  4. // files.
  5. import java.awt.*;
  6. import java.io.IOException;
  7. import java.util.Vector;
  8. import java.util.Date;
  9.  
  10. public class FileBrowserComponent extends JFScomponent
  11. {
  12.     java.applet.Applet ap;        // applet we are in
  13.     DoubleListPanel files;        // file list box
  14.     DoubleListPanel versions;    // version list box
  15.     DoubleListPanel users;        // users list box
  16.     Button pathb;            // refresh button
  17.     TextField dir;            // current directory
  18.     Button parb;            // parent button
  19.     TextField file;            // current file
  20.     Button vdelb;            // delete version button
  21.     Button vpurb;            // renumber version button
  22.     Button vinfob;            // version info button
  23.     Button uaddb;            // add user button
  24.     Button udelb;            // delete user button
  25.     Button uinfob;            // user info button
  26.     Button frunb;            // program run button
  27.     Button fopenb;            // file open button
  28.     Button fdelb;            // file delete button
  29.     Button fcopyb;            // file copy button
  30.     FilenameReq copyreq;        // copy file requestor
  31.     Button frenb;            // file rename button
  32.     FilenameReq renreq;        // rename file requestor
  33.     Button fmkdirb;            // make directory button
  34.     FilenameReq mkdirreq;        // make directory requestor
  35.     Button fprintb;            // file print button
  36.     Button fchtypeb;        // change type button
  37.     FilenameReq chtypereq;        // new MIME type requestor
  38.  
  39.     JFSdirectory current;        // current dir
  40.     Vector filesinfo = new Vector();// info about files in list
  41.     Vector versionsinfo = new Vector();
  42.     Vector usersinfo = new Vector();
  43.  
  44.     void init(java.applet.Applet a)
  45.     {
  46.     ap = a;
  47.     Color hi = new Color(220, 220, 220);
  48.     Color lo = new Color(50, 50, 50);
  49.     setLayout(new BorderLayout());
  50.  
  51.     // create bottom buttons
  52.     Panel bot = new SpacedPanel(0,3);
  53.     bot.setLayout(new FlowLayout(FlowLayout.LEFT));
  54.     bot.add(frunb = new Button("Run"));
  55.     bot.add(fopenb = new Button("Open"));
  56.     bot.add(fdelb = new Button("Delete"));
  57.     bot.add(fcopyb = new Button("Copy"));
  58.     bot.add(frenb = new Button("Rename"));
  59.     bot.add(fmkdirb = new Button("Make dir"));
  60.     bot.add(fprintb = new Button("Print"));
  61.     bot.add(fchtypeb = new Button("MIME"));
  62.     add("South",bot);
  63.  
  64.     // create file list
  65.     Panel top = new Panel();
  66.     top.setLayout(new GridLayout(1,2));
  67.     Panel left = new SpacedPanel(3,0);
  68.     left.setLayout(new BorderLayout());
  69.     Panel dirpanel = new SpacedPanel(0,3);
  70.     dirpanel.setLayout(new BorderLayout());
  71.     dirpanel.add("West",pathb = new Button("Path :"));
  72.     dirpanel.add("Center",dir = new TextField("/"));
  73.     dirpanel.add("East",parb = new Button("Parent"));
  74.     dir.setEditable(false);
  75.     left.add("North",dirpanel);
  76.     Panel filepanel = new SpacedPanel(0,3);
  77.     filepanel.setLayout(new BorderLayout());
  78.     filepanel.add("West",new Label("File :"));
  79.     filepanel.add("Center",file = new TextField());
  80.     file.setEditable(false);
  81.     left.add("South",filepanel);
  82.     left.add("Center",files = new DoubleListPanel("Name","Type"));
  83.     top.add(left);
  84.  
  85.     // create version list
  86.     Panel right = new Panel();
  87.     right.setLayout(new GridLayout(2,1));
  88.     Panel verpanel = new SpacedPanel(0,3);
  89.     verpanel.setLayout(new BorderLayout());
  90.     Panel vpbot = new Panel();
  91.     vpbot.setLayout(new GridLayout(1,3));
  92.     vpbot.add(vdelb = new Button("Delete"));
  93.     vpbot.add(vpurb = new Button("Purge"));
  94.     vpbot.add(vinfob = new Button("Info"));
  95.     verpanel.add("South",vpbot);
  96.     verpanel.add("Center",versions = new DoubleListPanel("Version","Size"));
  97.     verpanel.add("North",new Label("Versions"));
  98.     right.add(verpanel);
  99.  
  100.     // create users list
  101.     Panel usrpanel = new SpacedPanel(0,3);
  102.     usrpanel.setLayout(new BorderLayout());
  103.     Panel upbot = new Panel();
  104.     upbot.setLayout(new GridLayout(1,3));
  105.     upbot.add(uaddb = new Button("Add"));
  106.     upbot.add(udelb = new Button("Delete"));
  107.     upbot.add(uinfob = new Button("Info"));
  108.     usrpanel.add("South",upbot);
  109.     usrpanel.add("Center",users = new DoubleListPanel("Name","Access"));
  110.     usrpanel.add("North",new Label("Permissions"));
  111.     right.add(usrpanel);
  112.     top.add(right);
  113.     add("Center",top);
  114.     }
  115.  
  116.     // fillfiles
  117.     // Fill the files window with files from the current directory
  118.     void fillfiles()
  119.     {
  120.     try current = client.getdir(dir.getText());
  121.     catch(RequestException e) {
  122.         new ErrorWindow("Couldn't access "+dir.getText()+" : "+
  123.                 e.getMessage());
  124.         dir.setText(client.currentdir);
  125.         return;
  126.         }
  127.     client.currentdir = dir.getText();
  128.     files.clear();
  129.     filesinfo.removeAllElements();
  130.     versions.clear();
  131.     versionsinfo.removeAllElements();
  132.     users.clear();
  133.     usersinfo.removeAllElements();
  134.     file.setText("");
  135.     for(int i=0; i<current.size(); i++) {
  136.         JFSfile f = (JFSfile)current.files.elementAt(i);
  137.         filesinfo.addElement(f);
  138.         files.addItem(f.name, f.type);
  139.         }
  140.     }
  141.  
  142.     // fillversion
  143.     // Fill the versions list with all the versions of the current file
  144.     void fillversions(JFSfile fl)
  145.     {
  146.     versions.clear();
  147.     versionsinfo.removeAllElements();
  148.     if (fl.multiversion) {
  149.         for(int i=0; i<fl.versions.size(); i++) {
  150.             JFSversion vr = (JFSversion)fl.versions.elementAt(i);
  151.             versions.addItem(String.valueOf(vr.number),
  152.                      String.valueOf(vr.size));
  153.             versionsinfo.addElement(vr);
  154.             }
  155.         vpurb.enable();
  156.         }
  157.     else {
  158.         JFSversion vr = (JFSversion)fl.versions.elementAt(0);
  159.         versions.addItem("Single",String.valueOf(vr.size));
  160.         versionsinfo.addElement(vr);
  161.         vpurb.disable();
  162.         }
  163.     versions.select(versionsinfo.size()-1);
  164.     }
  165.  
  166.     // fillusers
  167.     // Fill the users list with all the users able to access the current
  168.     // file.
  169.     void fillusers(JFSfile fl)
  170.     {
  171.     users.clear();
  172.     usersinfo.removeAllElements();
  173.     for(int i=0; i<fl.users.size(); i++) {
  174.         JFSfileuser us = (JFSfileuser)fl.users.elementAt(i);
  175.         String acc = "";
  176.         if (us.rwp.indexOf('r') != -1) acc += "Read ";
  177.         if (us.rwp.indexOf('w') != -1) acc += "Write ";
  178.         if (us.rwp.indexOf('p') != -1) acc += "Chmod ";
  179.         users.addItem(us.name, acc);
  180.         usersinfo.addElement(us);
  181.         }
  182.     }
  183.  
  184.     // connect
  185.     // Connect to the given JFS server
  186.     void connect(JFSclient c)
  187.     {
  188.     super.connect(c);
  189.     dir.setText(client.currentdir);
  190.     fillfiles();
  191.     try if (!client.canaccess("/dev/Printer", 'w')) fprintb.disable();
  192.     catch(RequestException e);
  193.     }
  194.  
  195.     // wantedsize
  196.     Dimension wantedsize()
  197.     {
  198.     return new Dimension(600, 400);
  199.     }
  200.  
  201.     // action
  202.     // Handle input from the user
  203.     public boolean action(Event evt, Object obj)
  204.     {
  205.     String arg = (String)evt.arg;
  206.     if (evt.target == files && arg.equals("Single")) {
  207.         // single click on filename
  208.         int sel = files.selected();
  209.         JFSfile selfl = (JFSfile)filesinfo.elementAt(sel);
  210.         file.setText(selfl.name);
  211.         fillversions(selfl);
  212.         fillusers(selfl);
  213.         }
  214.     else if (evt.target == pathb) {
  215.         // Refresh directory button clicked
  216.         fillfiles();
  217.         }
  218.     else if (evt.target == parb) {
  219.         // Parent button clicked
  220.         String d = dir.getText();
  221.         if (!d.equals("/")) {
  222.             d = d.substring(0, d.lastIndexOf('/'));
  223.             dir.setText(d.length() == 0 ? "/" : d);
  224.             fillfiles();
  225.             }
  226.         }
  227.     else if (evt.target == fopenb ||
  228.          evt.target == files && arg.equals("Double") ||
  229.          evt.target == versions && arg.equals("Double")) {
  230.         // File chosen by double-click, or click on Open
  231.         int s = files.selected();
  232.         if (s < 0) return true;    // nothing chosen
  233.         JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  234.         if (selfl.type.equals("jfs/directory")) {
  235.             // enter a new dir
  236.             String cd = dir.getText();
  237.             dir.setText(cd + (cd.equals("/") ? "" : "/") +
  238.                     selfl.name);
  239.             fillfiles();
  240.             }
  241.         else {
  242.             // find and start handler program
  243.             String handler = null;
  244.             try handler = client.gethandler(selfl.type);
  245.             catch(RequestException e) {
  246.                 new ErrorWindow("Error getting handler for "+
  247.                         selfl.name + " : " +
  248.                         e.getMessage());
  249.                 return true;
  250.                 }
  251.             if (handler != null) {
  252.                 String full = dir.getText()+"/"+file.getText();
  253.                 int v = ((JFSversion)versionsinfo.elementAt(
  254.                      versions.selected())).number;
  255.                 new ProgramWindow(handler, client, ap, full, v);
  256.                 }
  257.             }
  258.         }
  259.     else if (evt.target == frunb) {
  260.         // Run program button clicked
  261.         new ProgramChooser(client, ap);
  262.         }
  263.     else if (evt.target == fdelb) {
  264.         // Delete file button clicked
  265.         int s = files.selected();
  266.         if (s < 0) return true;    // nothing chosen
  267.         JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  268.         try client.delete(dir.getText()+"/"+selfl.name, 0);
  269.         catch(RequestException e)
  270.             new ErrorWindow("Couldn't delete "+selfl.name+" : "+
  271.                     e.getMessage());
  272.         fillfiles();
  273.         }
  274.     else if (evt.target == fcopyb && copyreq == null) {
  275.         // Copy button clicked
  276.         copyreq = new FilenameReq(this,"Destination",dir.getText()+"/");
  277.         }
  278.     else if (evt.target == copyreq) {
  279.         // Destination file for copy chosen
  280.         if (arg != null) {
  281.             int s = files.selected();
  282.             if (s < 0) return true; // nothing chosen
  283.             JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  284.             try client.copy(dir.getText()+"/"+selfl.name,
  285.                     copyreq.getFile());
  286.             catch(RequestException e)
  287.                 new ErrorWindow("Couldn't copy "+selfl.name+
  288.                         " : "+e.getMessage());
  289.             fillfiles();
  290.             }
  291.         copyreq = null;
  292.         }
  293.     else if (evt.target == frenb && renreq == null) {
  294.         // Rename button clicked
  295.         renreq = new FilenameReq(this, "New name", dir.getText()+"/");
  296.         }
  297.     else if (evt.target == renreq) {
  298.         // Destination file for rename chosen
  299.         if (arg != null) {
  300.             int s = files.selected();
  301.             if (s < 0) return true; // nothing chosen
  302.             JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  303.             try client.rename(dir.getText()+"/"+selfl.name,
  304.                       renreq.getFile());
  305.             catch(RequestException e)
  306.                 new ErrorWindow("Couldn't copy "+selfl.name+
  307.                         " : "+e.getMessage());
  308.             fillfiles();
  309.             }
  310.         renreq = null;
  311.         }
  312.     else if (evt.target == fmkdirb && mkdirreq == null) {
  313.         // Make dir button clicked
  314.         mkdirreq = new FilenameReq(this,"Directory to create","");
  315.         }
  316.     else if (evt.target == mkdirreq) {
  317.         // Directory to make chosen
  318.         if (arg != null) {
  319.             try client.mkdir(dir.getText()+"/"+arg);
  320.             catch(RequestException e) {
  321.                 new ErrorWindow("Couldn't make directory "+
  322.                         arg+" : "+e.getMessage());
  323.                 return true;
  324.                 }
  325.             fillfiles();
  326.             }
  327.         mkdirreq = null;
  328.         }
  329.     else if (evt.target == fprintb) {
  330.         // Print button clicked. Find and load a handler for the
  331.         // the chosen file that knows how to print it.
  332.         int s = files.selected();
  333.         if (s < 0) return true;    // nothing chosen
  334.         JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  335.         String handler = null;
  336.         try handler = client.gethandler(selfl.type);
  337.         catch(RequestException e) {
  338.             new ErrorWindow("Error getting handler to print "+
  339.                     selfl.name + " : " + e.getMessage());
  340.             return true;
  341.             }
  342.         if (handler != null) {
  343.             String full = dir.getText()+"/"+file.getText();
  344.             int v = ((JFSversion)versionsinfo.elementAt(
  345.                  versions.selected())).number;
  346.             JFScomponent cmp = null;
  347.             try {
  348.                 Class hc = Class.forName(handler);
  349.                 cmp = (JFScomponent)hc.newInstance();
  350.                 }
  351.             catch(Exception e) {
  352.                 new ErrorWindow(handler+" couldn't load "+
  353.                         selfl.name+" for printing : "+
  354.                         e.getMessage());
  355.                 return true;
  356.                 }
  357.             cmp.connect(client);
  358.             try cmp.load(full, v);
  359.             catch(RequestException e) {
  360.                 new ErrorWindow("Error loading "+selfl.name+
  361.                         " for printing : "+
  362.                         e.getMessage());
  363.                 return true;
  364.                 }
  365.             new PrintRequestor(cmp, client);
  366.             }
  367.         }
  368.     else if (evt.target == fchtypeb && chtypereq == null) {
  369.         // Change MIME type button clicked. Open type requestor
  370.         int s = files.selected();
  371.         if (s < 0) return true;    // nothing chosen
  372.         JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  373.         chtypereq = new FilenameReq(this, "New MIME type", selfl.type);
  374.         }
  375.     else if (evt.target == chtypereq) {
  376.         // MIME type chosen
  377.         if (arg != null) {
  378.             int s = files.selected();
  379.             if (s < 0) return true; // nothing chosen
  380.             JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  381.             try client.chtype(dir.getText()+"/"+selfl.name,
  382.                       chtypereq.getFile());
  383.             catch(RequestException e)
  384.                 new ErrorWindow("Could change the type of "+
  385.                         selfl.name+" : "+
  386.                         e.getMessage());
  387.             fillfiles();
  388.             }
  389.         chtypereq = null;
  390.         }
  391.     else if (evt.target == uinfob ||
  392.          evt.target == users && arg.equals("Double")) {
  393.         // User chosen by double-click, or click on Open
  394.         int sel = users.selected();
  395.         if (sel < 0) return true;    // no user chosen
  396.         JFSfileuser fu = (JFSfileuser)usersinfo.elementAt(sel);
  397.         try {
  398.             JFSuser u = client.uinfo(fu.name);
  399.             new UserWindow(u);
  400.             return true;
  401.             }
  402.         catch(RequestException e);
  403.         // this must be a group then..
  404.         Vector uv = null;
  405.         try uv = client.ginfo(fu.name);
  406.         catch(RequestException e) {
  407.             new ErrorWindow(e.getMessage());
  408.             return true;
  409.             }
  410.         new GroupWindow(fu.name, uv);
  411.         }
  412.     else if (evt.target == uaddb && !file.getText().equals("")) {
  413.         // File user add button chosen
  414.         new UserAddReq(this, dir.getText()+"/"+file.getText(), client);
  415.         }
  416.     else if (evt.target == udelb) {
  417.         // Delete user button clicked
  418.         int sel = users.selected();
  419.         if (sel < 0) return true;    // no user chosen
  420.         try client.chmod(dir.getText()+"/"+file.getText(),
  421.                  ((JFSfileuser)usersinfo.elementAt(sel)).name,
  422.                  null);
  423.         catch(RequestException e)
  424.             new ErrorWindow("Couldn't delete user : "+
  425.                     e.getMessage());
  426.         fillfiles();
  427.         }
  428.     else if (evt.target == vdelb) {
  429.         // Version delete button clicked
  430.         int sel = versions.selected();
  431.         if (sel < 0) return true;    // no version chosen
  432.         JFSfile selfl = (JFSfile)filesinfo.elementAt(files.selected());
  433.         JFSversion selvr = (JFSversion)versionsinfo.elementAt(sel);
  434.         int ver = selfl.multiversion ? selvr.number : 0;
  435.         try client.delete(dir.getText()+"/"+selfl.name, ver);
  436.         catch(RequestException e)
  437.             new ErrorWindow("Couldn't delete version : "+
  438.                     e.getMessage());
  439.         fillfiles();
  440.         }
  441.     else if (evt.target == vpurb) {
  442.         // Version purge button clicked
  443.         int s = files.selected();
  444.         if (s < 0) return true;    // nothing chosen
  445.         JFSfile selfl = (JFSfile)filesinfo.elementAt(s);
  446.         try client.purge(dir.getText()+"/"+selfl.name);
  447.         catch(RequestException e)
  448.             new ErrorWindow("Couldn't purge "+selfl.name+" : "+
  449.                     e.getMessage());
  450.         fillfiles();
  451.         }
  452.     else if (evt.target == vinfob) {
  453.         // Version info button clicked
  454.         int sel = versions.selected();
  455.         if (sel < 0) return true;    // no version chosen
  456.         JFSfile selfl = (JFSfile)filesinfo.elementAt(files.selected());
  457.         JFSversion selvr = (JFSversion)versionsinfo.elementAt(sel);
  458.         new VersionInfoWindow(selfl, selvr);
  459.         }
  460.     return true;
  461.     }
  462. }
  463.  
  464. // ProgramWindow
  465. // Given a class name, loads that class (which must be a subclass of
  466. // JFScomponent) and intantiates it in a top level window of it's own.
  467. // The connect method of the loaded class is called with the given JFSclient,
  468. // to connect it to a JFS server.
  469. // If a filename and version are given as well, calls the load method of the
  470. // component with the given file.
  471. // If anything goes wrong, an error message window is displayed.
  472. class ProgramWindow extends FixedFrame
  473. {
  474.     JFScomponent cmp;
  475.     JFSclient programclient;
  476.  
  477.     ProgramWindow(String pclass, JFSclient client, java.applet.Applet a)
  478.     {
  479.     init(pclass, client, a);
  480.     setsize(cmp.wantedsize());
  481.     setTitle(cmp.getClass().getName());
  482.     pack();
  483.     show();
  484.     }
  485.  
  486.     ProgramWindow(String pclass, JFSclient client, java.applet.Applet a,
  487.               String f, int v)
  488.     {
  489.     if (!init(pclass, client, a))
  490.         return;
  491.     try cmp.load(f, v);
  492.     catch(RequestException e) {
  493.         new ErrorWindow(pclass+" couldn't load "+
  494.                 f+" : "+e.getMessage());
  495.         return;
  496.         }
  497.     setsize(cmp.wantedsize());
  498.     setTitle(cmp.getClass().getName() + " - " + 
  499.          f.substring(f.lastIndexOf('/')+1));
  500.     pack();
  501.     show();
  502.     }
  503.  
  504.     // init
  505.     // Load and instantiate the given class in this window.
  506.     // Returns true if successful, false otherwise
  507.     boolean init(String pclass, JFSclient client, java.applet.Applet a)
  508.     {
  509.     try {
  510.         Class hc = Class.forName(pclass);
  511.         cmp = (JFScomponent)hc.newInstance();
  512.         }
  513.     catch(ClassNotFoundException e) {
  514.         new ErrorWindow("Program class not found");
  515.         return false;
  516.         }
  517.     catch(InstantiationException e) {
  518.         new ErrorWindow("Could not instantiate "+
  519.                 "program class");
  520.         return false;
  521.         }
  522.     catch(IllegalAccessException e) {
  523.         new ErrorWindow("Illegal access to program "+
  524.                 "class");
  525.         return false;
  526.         }
  527.     catch(ClassCastException e) {
  528.         new ErrorWindow("Not a valid program class");
  529.         return false;
  530.         }
  531.     cmp.init(a);
  532.     try cmp.connect(programclient = client.newclient());
  533.     catch(RequestException e) {
  534.         new ErrorWindow("Failed to make new connection to server");
  535.         return false;
  536.         }
  537.     setLayout(new BorderLayout());
  538.     add("Center",cmp);
  539.     return true;
  540.     }
  541.  
  542.     public boolean handleEvent(Event evt)
  543.     {
  544.     if (evt.id == Event.WINDOW_DESTROY) {
  545.         programclient.close();
  546.         dispose();
  547.         }
  548.     return super.handleEvent(evt);
  549.     }
  550. }
  551.  
  552. // FilenameReq
  553. // A request for entering a single filename, for the stuff like copying,
  554. // renaming and so on.
  555. class FilenameReq extends FixedFrame
  556. {
  557.     Component parent;
  558.     TextField name;
  559.     Button ok, can;
  560.  
  561.     FilenameReq(Component p, String l, String n)
  562.     {
  563.     super(new Dimension(250, -1));
  564.     parent = p;
  565.     setLayout(new BorderLayout());
  566.     Panel top = new Panel();
  567.     top.setLayout(new BorderLayout());
  568.     top.add("West",new Label(l));
  569.     top.add("Center",name = new TextField(n));
  570.     add("North",top);
  571.     Panel bot = new Panel();
  572.     bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
  573.     bot.add(ok = new Button("Ok"));
  574.     bot.add(can = new Button("Cancel"));
  575.     add("South",bot);
  576.     setTitle(l);
  577.     pack();
  578.     show();
  579.     }
  580.  
  581.     // getFile
  582.     // Returns the name of the file chosen by the user
  583.     String getFile()
  584.     {
  585.     return name.getText();
  586.     }
  587.  
  588.     public boolean handleEvent(Event evt)
  589.     {
  590.     if (evt.id == Event.WINDOW_DESTROY ||
  591.         evt.id == Event.ACTION_EVENT && evt.target == can) {
  592.         dispose();
  593.         parent.postEvent(new Event(this, Event.ACTION_EVENT, null));
  594.         }
  595.     else if (evt.id == Event.ACTION_EVENT &&
  596.          (evt.target == ok || evt.target == name)) {
  597.         dispose();
  598.         parent.postEvent(new Event(this, Event.ACTION_EVENT,
  599.                        name.getText()));
  600.         }
  601.     return super.handleEvent(evt);
  602.     }
  603. }
  604.  
  605.  
  606. // ProgramChooser
  607. // A window containing a list of mime types and handler programs. When
  608. // one is chosen by the user, the given handler program is run in a window
  609. // of it's own.
  610. class ProgramChooser extends FixedFrame
  611. {
  612.     JFSclient client;
  613.     DoubleListPanel list;
  614.     Button close;
  615.     Vector progs = new Vector();
  616.     java.applet.Applet ap;
  617.  
  618.     ProgramChooser(JFSclient c, java.applet.Applet a)
  619.     {
  620.     super(new Dimension(300,300));
  621.     // create interface
  622.     ap = a;
  623.     client = c;
  624.     String str = "Select program to run";
  625.     setLayout(new BorderLayout());
  626.     add("North",new Label(str));
  627.     add("Center",list = new DoubleListPanel("Program","Mime type"));
  628.     Panel bot = new Panel();
  629.     bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
  630.     bot.add(close = new Button("Close"));
  631.     add("South",bot);
  632.     setTitle(str);
  633.  
  634.     // Fill list with known programs
  635.     Vector ty = new Vector();
  636.     try client.getprograms(progs, ty);
  637.     catch(RequestException e) {
  638.         new ErrorWindow("Couldn't read handlers file : "+
  639.                 e.getMessage());
  640.         return;
  641.         }
  642.     for(int i=0; i<progs.size(); i++) {
  643.         String hprog = (String)progs.elementAt(i);
  644.         boolean already = false;
  645.         for(int j=0; j<i; j++)
  646.             if (((String)progs.elementAt(i)).equals(
  647.                  (String)progs.elementAt(j)))
  648.                 already = true;
  649.         if (!already) {
  650.             MimeType htype = (MimeType)ty.elementAt(i);
  651.             list.addItem(hprog, htype == null ? "" :
  652.                                 htype.toString());
  653.             }
  654.         else {
  655.             progs.removeElementAt(i);
  656.             ty.removeElementAt(i);
  657.             i--;
  658.             }
  659.         }
  660.     pack();
  661.     show();
  662.     }
  663.  
  664.     public boolean handleEvent(Event evt)
  665.     {
  666.     if (evt.id == Event.WINDOW_DESTROY ||
  667.         evt.id == Event.ACTION_EVENT && evt.target == close)
  668.         dispose();
  669.     else if (evt.id == Event.ACTION_EVENT && evt.target == list &&
  670.          ((String)evt.arg).equals("Double")) {
  671.         // run the chosen program
  672.         new ProgramWindow((String)progs.elementAt(list.selected()),
  673.                   client, ap);
  674.         dispose();
  675.         }
  676.     return super.handleEvent(evt);
  677.     }
  678. }
  679.  
  680.  
  681. // UserWindow
  682. // Displays infomation about a given user.
  683. class UserWindow extends FixedFrame
  684. {
  685.     Button close;
  686.  
  687.     UserWindow(JFSuser u)
  688.     {
  689.     super(new Dimension(300, -1));
  690.     GridBagConstraints leftc = new GridBagConstraints(),
  691.                rightc = new GridBagConstraints();
  692.     leftc.fill = GridBagConstraints.BOTH;
  693.     rightc.gridwidth = GridBagConstraints.REMAINDER;
  694.     rightc.weightx = 1;
  695.     rightc.fill = GridBagConstraints.BOTH;
  696.  
  697.     // create user details panel
  698.     GridBagLayout topl = new GridBagLayout();
  699.     setLayout(new BorderLayout());
  700.     Panel top = new Panel();
  701.     top.setLayout(topl);
  702.     Label l1, l2, l3;
  703.     StaticTextField s1, s2, s3;
  704.     top.add(l1 = new Label("Username"));
  705.     topl.setConstraints(l1, leftc);
  706.     top.add(s1 = new StaticTextField(u.name));
  707.     topl.setConstraints(s1, rightc);
  708.     top.add(l2 = new Label("Real name"));
  709.     topl.setConstraints(l2, leftc);
  710.     top.add(s2 = new StaticTextField(u.realname));
  711.     topl.setConstraints(s2, rightc);
  712.     top.add(l3 = new Label("Home dir"));
  713.     topl.setConstraints(l3, leftc);
  714.     top.add(s3 = new StaticTextField(u.home));
  715.     topl.setConstraints(s3, rightc);
  716.  
  717.     Panel left = new Panel();
  718.     left.setLayout(new BorderLayout());
  719.     left.add("North",new Label("Groups"));
  720.     top.add(left);
  721.     topl.setConstraints(left, leftc);
  722.  
  723.     List glist = new List();
  724.     glist.addItem("all");
  725.     for(int i=0; i<u.groups.size(); i++)
  726.         glist.addItem((String)u.groups.elementAt(i));
  727.     top.add(glist);
  728.     rightc.gridheight = GridBagConstraints.REMAINDER;
  729.     rightc.weighty = 1;
  730.     topl.setConstraints(glist, rightc);
  731.  
  732.     add("Center",top);
  733.  
  734.     // create close button panel
  735.     Panel bot = new Panel();
  736.     bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
  737.     bot.add(close = new Button("Close"));
  738.     add("South",bot);
  739.  
  740.     setTitle(u.name);
  741.     pack();
  742.     show();
  743.     }
  744.  
  745.     public boolean handleEvent(Event evt)
  746.     {
  747.     if (evt.id == Event.WINDOW_DESTROY ||
  748.         evt.id == Event.ACTION_EVENT && evt.target == close)
  749.         dispose();
  750.     return super.handleEvent(evt);
  751.     }
  752. }
  753.  
  754. // GroupWindow
  755. // A window displaying information about a JFS group
  756. class GroupWindow extends FixedFrame
  757. {
  758.     Button close;
  759.  
  760.     GroupWindow(String name, Vector users)
  761.     {
  762.     super(new Dimension(300, -1));
  763.     // create group name panel
  764.     setLayout(new BorderLayout());
  765.     Panel top = new Panel();
  766.     top.setLayout(new GridLayout(1,2));
  767.     top.add(new Label("Groupname", Label.RIGHT));
  768.     top.add(new StaticTextField(name));
  769.     add("North",top);
  770.  
  771.     // create group members panel
  772.     Panel mid = new Panel();
  773.     mid.setLayout(new GridLayout(1,2));
  774.     Panel left = new Panel();
  775.     left.setLayout(new BorderLayout());
  776.     left.add("North",new Label("Members", Label.RIGHT));
  777.     mid.add(left);
  778.     List ulist;
  779.     mid.add(ulist = new List());
  780.     for(int i=0; i<users.size(); i++)
  781.         ulist.addItem((String)users.elementAt(i));
  782.     add("Center",mid);
  783.  
  784.     // create close panel
  785.     Panel bot = new Panel();
  786.     bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
  787.     bot.add(close = new Button("Close"));
  788.     add("South",bot);
  789.  
  790.     setTitle(name);
  791.     pack();
  792.     show();
  793.     }
  794.  
  795.     public boolean handleEvent(Event evt)
  796.     {
  797.     if (evt.id == Event.WINDOW_DESTROY ||
  798.         evt.id == Event.ACTION_EVENT && evt.target == close)
  799.         dispose();
  800.     return super.handleEvent(evt);
  801.     }
  802. }
  803.  
  804. // UserAddReq
  805. // A window for entering details of a user/group to give permissions to a file
  806. class UserAddReq extends FixedFrame
  807. {
  808.     FileBrowserComponent parent;
  809.     String file;
  810.     JFSclient client;
  811.     TextField user;
  812.     Checkbox read, write ,perms;
  813.     Button ok, can;
  814.  
  815.     UserAddReq(FileBrowserComponent p, String f, JFSclient c)
  816.     {
  817.     super(new Dimension(350, -1));
  818.     parent = p;
  819.     file = f;
  820.     client = c;
  821.     setLayout(new BorderLayout());
  822.  
  823.     // Create panel with name and permission setting controls
  824.     Panel top = new Panel();
  825.     GridBagConstraints leftc = new GridBagConstraints(),
  826.                rightc = new GridBagConstraints();
  827.     leftc.fill = GridBagConstraints.BOTH;
  828.     rightc.gridwidth = GridBagConstraints.REMAINDER;
  829.     rightc.weightx = 1;
  830.     rightc.fill = GridBagConstraints.BOTH;
  831.     GridBagLayout topl = new GridBagLayout();
  832.     top.setLayout(topl);
  833.     Label l1, l2, l3;
  834.     StaticTextField s1;
  835.     Panel p1;
  836.     top.add(l1 = new Label("Filename"));
  837.     topl.setConstraints(l1, leftc);
  838.     top.add(s1 = new StaticTextField(f));
  839.     topl.setConstraints(s1, rightc);
  840.     top.add(l2 = new Label("User/group to add"));
  841.     topl.setConstraints(l2, leftc);
  842.     top.add(user = new TextField());
  843.     topl.setConstraints(user, rightc);
  844.     top.add(l3 = new Label("Permissions"));
  845.     topl.setConstraints(l3, leftc);
  846.     Panel pch = new Panel();
  847.     pch.setLayout(new FlowLayout(FlowLayout.LEFT));
  848.     pch.add(read = new Checkbox("Read"));
  849.     pch.add(write = new Checkbox("Write"));
  850.     pch.add(perms = new Checkbox("Chmod"));
  851.     top.add(pch);
  852.     rightc.gridheight = GridBagConstraints.REMAINDER;
  853.     rightc.weighty = 1;
  854.     topl.setConstraints(pch, rightc);
  855.     add("North",top);
  856.  
  857.     // Create panel with ok and cancel buttons
  858.     Panel bot = new Panel();
  859.     bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
  860.     bot.add(ok = new Button("OK"));
  861.     bot.add(can = new Button("Cancel"));
  862.     add("South",bot);
  863.  
  864.     setTitle("File permissions");
  865.     pack();
  866.     show();
  867.     }
  868.  
  869.     public boolean handleEvent(Event e)
  870.     {
  871.     if (e.id == Event.WINDOW_DESTROY ||
  872.         e.id == Event.ACTION_EVENT && e.target == can)
  873.         dispose();
  874.     else if (e.id == Event.ACTION_EVENT && e.target == ok) {
  875.         // do the permission change
  876.         try client.chmod(file, user.getText(),
  877.                  (read.getState() ? "r" : "")+
  878.                  (write.getState() ? "w" : "")+
  879.                  (perms.getState() ? "p" : ""));
  880.         catch(RequestException ex)
  881.             new ErrorWindow("Couldn't set permissions : "+
  882.                     ex.getMessage());
  883.         dispose();
  884.         parent.fillfiles();
  885.         }
  886.     return super.handleEvent(e);
  887.     }
  888. }
  889.  
  890.  
  891. // StaticTextField
  892. // A text field that is set to be non-editable by default
  893. class StaticTextField extends TextField
  894. {
  895.     StaticTextField()
  896.     {
  897.     super();
  898.     setEditable(false);
  899.     }
  900.  
  901.     StaticTextField(String s)
  902.     {
  903.     super(s);
  904.     setEditable(false);
  905.     }
  906.  
  907.     StaticTextField(String s, int i)
  908.     {
  909.     super(s,i);
  910.     setEditable(false);
  911.     }
  912. }
  913.  
  914. // VersionInfoWindow
  915. // A pop-up window displaying information about a version of a file
  916. class VersionInfoWindow extends FixedFrame
  917. {
  918.     Button close;
  919.  
  920.     VersionInfoWindow(JFSfile f, JFSversion v)
  921.     {
  922.     super(300, -1);
  923.     // create UI
  924.     setLayout(new BorderLayout());
  925.     Panel left = new Panel();
  926.     left.setLayout(new GridLayout(5,1));
  927.     left.add(new Label("Filename"));
  928.     left.add(new Label("Version"));
  929.     left.add(new Label("Size"));
  930.     left.add(new Label("Creator"));
  931.     left.add(new Label("Time"));
  932.     add("West", left);
  933.     Panel right = new Panel();
  934.     right.setLayout(new GridLayout(5,1));
  935.     right.add(new StaticTextField(f.name));
  936.     right.add(new StaticTextField(String.valueOf(v.number)));
  937.     right.add(new StaticTextField(String.valueOf(v.size)));
  938.     right.add(new StaticTextField(v.creator));
  939.     right.add(new StaticTextField(new Date(v.time).toString()));
  940.     add("Center", right);
  941.     Panel bot = new Panel();
  942.     bot.setLayout(new FlowLayout(FlowLayout.RIGHT));
  943.     bot.add(close = new Button("Close"));
  944.     add("South", bot);
  945.  
  946.     setTitle("Version info");
  947.     pack();    
  948.     show();
  949.     }
  950.  
  951.     public boolean action(Event evt, Object obj)
  952.     {
  953.     if (evt.target == close)
  954.         dispose();
  955.     return true;
  956.     }
  957. }
  958.  
  959.