home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / allaire / cfide / CFDirectoryTree.class (.txt) next >
Encoding:
Java Class File  |  1999-04-12  |  8.3 KB  |  258 lines

  1. package allaire.cfide;
  2.  
  3. import allaire.controls.CFTree;
  4. import allaire.controls.CFTreeItem;
  5. import java.net.URL;
  6. import java.util.Hashtable;
  7. import java.util.StringTokenizer;
  8. import netscape.application.AWTCompatibility;
  9. import netscape.application.Alert;
  10. import netscape.application.Application;
  11. import netscape.application.Target;
  12. import netscape.application.View;
  13. import netscape.util.Enumeration;
  14. import netscape.util.Vector;
  15.  
  16. public class CFDirectoryTree extends CFTree implements Target {
  17.    public boolean m_isUNIX;
  18.    private boolean m_showFiles;
  19.    private CFNetRouter m_cfRouter;
  20.    private Hashtable m_hashExtensions;
  21.    private String m_initialRoot = "";
  22.    private String m_initialMask = "";
  23.    private String m_mask = "*.*";
  24.    private String m_separator = "\\";
  25.  
  26.    public void doubleClicked(CFTreeItem currentItem) {
  27.    }
  28.  
  29.    public CFDirectoryTree(CFNetRouter cfRouter, Application application, int x, int y, int width, int height, View parentView, boolean mainTree) {
  30.       super(x, y, width, height, parentView);
  31.       Application currentApplication = Application.application();
  32.       CFNavigationApplet currentApplet = (CFNavigationApplet)AWTCompatibility.awtApplet();
  33.       currentApplet.m_treeControl = this;
  34.       String strUNIX = currentApplication.parameterNamed("OS");
  35.       if (strUNIX != null && strUNIX.equalsIgnoreCase("UNIX")) {
  36.          this.m_isUNIX = true;
  37.          this.m_mask = "*";
  38.          this.m_separator = "/";
  39.          this.m_initialRoot = "/";
  40.          this.m_initialMask = "*";
  41.       }
  42.  
  43.       String strVScroll = currentApplication.parameterNamed("VSCROLL");
  44.       boolean wantVScroll = true;
  45.       if (strVScroll != null && strVScroll.equalsIgnoreCase("no")) {
  46.          wantVScroll = false;
  47.       }
  48.  
  49.       ((CFTree)this).setVScroll(true);
  50.       boolean wantHScroll = true;
  51.       String strHScroll = currentApplication.parameterNamed("HSCROLL");
  52.       if (strHScroll != null && strHScroll.equalsIgnoreCase("no")) {
  53.          wantHScroll = false;
  54.       }
  55.  
  56.       ((CFTree)this).setHScroll(wantHScroll);
  57.       boolean wantBorder = true;
  58.       String strBorder = currentApplication.parameterNamed("BORDER");
  59.       if (strBorder != null && strBorder.equalsIgnoreCase("no")) {
  60.          wantBorder = false;
  61.       }
  62.  
  63.       ((CFTree)this).setBorder(wantBorder);
  64.       this.m_cfRouter = cfRouter;
  65.       this.init();
  66.    }
  67.  
  68.    public void performCommand(String command, Object anObject) {
  69.       if (command.equals("INITIAL_LOAD")) {
  70.          this.initialLoad();
  71.          ((View)this).draw();
  72.       }
  73.  
  74.    }
  75.  
  76.    public void init() {
  77.       Application currentApp = Application.application();
  78.       currentApp.performCommandLater(this, "INITIAL_LOAD", this);
  79.    }
  80.  
  81.    private void initialLoad() {
  82.       String strCaption = null;
  83.       ((View)this).rootView().setOverrideCursor(3);
  84.       int rootIndex = ((CFTree)this).addItem("Retrieving initial directories . . .", -1, 2);
  85.       Application currentApplication = Application.application();
  86.       ((View)this).draw();
  87.  
  88.       try {
  89.          ((View)this).disableDrawing();
  90.          Vector vectResults = this.m_cfRouter.callRPC("BrowseDir", this.m_initialRoot, this.m_initialMask);
  91.          this.m_showFiles = true;
  92.          String strShowFiles = currentApplication.parameterNamed("ShowFiles");
  93.          if (strShowFiles != null && strShowFiles.equalsIgnoreCase("no")) {
  94.             this.m_showFiles = false;
  95.          }
  96.  
  97.          if (this.m_showFiles) {
  98.             this.m_hashExtensions = new Hashtable();
  99.             String strExtensions = currentApplication.parameterNamed("Extensions");
  100.             if (strExtensions != null) {
  101.                StringTokenizer tokenizer = new StringTokenizer(strExtensions, " ,");
  102.  
  103.                while(tokenizer.hasMoreTokens()) {
  104.                   String nextToken = tokenizer.nextToken();
  105.                   this.m_hashExtensions.put(nextToken, nextToken);
  106.                }
  107.             } else {
  108.                this.m_hashExtensions.put(".htm", ".htm");
  109.                this.m_hashExtensions.put(".html", ".html");
  110.                this.m_hashExtensions.put(".cfm", ".cfm");
  111.                this.m_hashExtensions.put(".dbm", ".dbm");
  112.             }
  113.          }
  114.  
  115.          strCaption = currentApplication.parameterNamed("ServerCaption");
  116.          if (strCaption == null) {
  117.             URL urlOriginal = currentApplication.codeBase();
  118.             String hostName = urlOriginal.getHost();
  119.             strCaption = "http://" + hostName;
  120.          }
  121.  
  122.          CFTreeItem rootItem = ((CFTree)this).itemAt(rootIndex);
  123.          rootItem.m_caption = strCaption;
  124.          if (this.m_isUNIX) {
  125.             this.populateChildrenDisplay(vectResults, rootIndex, false);
  126.          } else {
  127.             for(Enumeration enumResults = vectResults.elements(); enumResults.hasMoreElements(); enumResults.nextElement()) {
  128.                String strDriveType = (String)enumResults.nextElement();
  129.                boolean bAddDrive = true;
  130.                Integer nDriveType = new Integer(strDriveType);
  131.                int nImageType = 0;
  132.                switch (nDriveType) {
  133.                   case 2:
  134.                      nImageType = 4;
  135.                      break;
  136.                   case 3:
  137.                      nImageType = 6;
  138.                      break;
  139.                   case 4:
  140.                      nImageType = 8;
  141.                      bAddDrive = false;
  142.                      break;
  143.                   case 5:
  144.                      nImageType = 10;
  145.                }
  146.  
  147.                String strDrive = (String)enumResults.nextElement();
  148.                if (bAddDrive) {
  149.                   String strDriveLetter = strDrive;
  150.                   if (strDrive.length() > 2) {
  151.                      strDriveLetter = strDrive.substring(0, 2);
  152.                   }
  153.  
  154.                   int nIndex = ((CFTree)this).addItem(strDrive, strDriveLetter, rootIndex, nImageType);
  155.                   CFTreeItem currentItem = ((CFTree)this).itemAt(nIndex);
  156.                   currentItem.setHasChildren();
  157.                }
  158.             }
  159.          }
  160.  
  161.          String strDefaultPath = currentApplication.parameterNamed("DefaultPath");
  162.          if (strDefaultPath != null) {
  163.             if (this.m_isUNIX && strDefaultPath.startsWith("/")) {
  164.                strDefaultPath = strDefaultPath.substring(1);
  165.             }
  166.  
  167.             rootItem.setDefaultPath(strDefaultPath, this.m_separator);
  168.          }
  169.       } catch (CFRPCParameterException var17) {
  170.          Alert.runAlertExternally(Alert.notificationImage(), "Logic Error", "RPC Parameter error in CFDirectoryTree", "OK", (String)null, (String)null);
  171.       } catch (CFRPCServerException var18) {
  172.          String errorMessage = var18.errorMessage();
  173.          Alert.runAlertExternally(Alert.notificationImage(), "Server Error", errorMessage, "OK", (String)null, (String)null);
  174.       }
  175.  
  176.       ((View)this).reenableDrawing();
  177.       ((View)this).rootView().removeOverrideCursor();
  178.       ((View)this).rootView().updateCursor();
  179.       AWTCompatibility.awtApplet().showStatus("");
  180.    }
  181.  
  182.    public void populateChildren(CFTreeItem currentItem) {
  183.       try {
  184.          int currentIndex = super.m_allItems.indexOf(currentItem);
  185.          ((View)this).rootView().setOverrideCursor(3);
  186.          String currentPath = currentItem.getPath(this.m_separator);
  187.          if (this.m_isUNIX) {
  188.             currentPath = "/" + currentPath;
  189.          }
  190.  
  191.          Vector vectResults = this.m_cfRouter.callRPC("BrowseDir", currentPath, this.m_mask);
  192.          this.populateChildrenDisplay(vectResults, currentIndex, true);
  193.       } catch (CFRPCParameterException var6) {
  194.          Alert.runAlertExternally(Alert.notificationImage(), "Logic Error", "RPC Parameter error in CFDirectoryTree", "OK", (String)null, (String)null);
  195.       } catch (CFRPCServerException var7) {
  196.          String errorMessage = var7.errorMessage();
  197.          errorMessage = new String(errorMessage.substring(0, Math.max(0, errorMessage.length() - 2)));
  198.          Alert.runAlertExternally(Alert.notificationImage(), "Server Access Error", errorMessage, "OK", (String)null, (String)null);
  199.       }
  200.  
  201.    }
  202.  
  203.    public void populateChildrenDisplay(Vector vectResults, int currentIndex, boolean updateCursor) {
  204.       Vector vectDirectories = new Vector();
  205.       Vector vectFiles = new Vector();
  206.       Enumeration enumResults = vectResults.elements();
  207.  
  208.       while(enumResults.hasMoreElements()) {
  209.          String strItemType = (String)enumResults.nextElement();
  210.          String strItem = (String)enumResults.nextElement();
  211.          enumResults.nextElement();
  212.          enumResults.nextElement();
  213.          enumResults.nextElement();
  214.          if (strItemType.compareTo("D:") == 0) {
  215.             vectDirectories.addElement(strItem);
  216.          } else if (this.m_showFiles) {
  217.             String testItem = strItem.toLowerCase();
  218.             int iLastIndex = testItem.lastIndexOf(".");
  219.             if (iLastIndex >= 0 && this.m_hashExtensions.containsKey(testItem.substring(iLastIndex))) {
  220.                vectFiles.addElement(strItem);
  221.             }
  222.          }
  223.       }
  224.  
  225.       if (this.m_isUNIX) {
  226.          if (!vectDirectories.isEmpty()) {
  227.             vectDirectories.sortStrings(true, false);
  228.          }
  229.  
  230.          if (!vectFiles.isEmpty()) {
  231.             vectFiles.sortStrings(true, false);
  232.          }
  233.       }
  234.  
  235.       enumResults = vectDirectories.elements();
  236.  
  237.       while(enumResults.hasMoreElements()) {
  238.          String strItem = (String)enumResults.nextElement();
  239.          int nNewIndex = ((CFTree)this).addItem(strItem, currentIndex);
  240.          CFTreeItem newItem = ((CFTree)this).itemAt(nNewIndex);
  241.          newItem.setHasChildren();
  242.       }
  243.  
  244.       enumResults = vectFiles.elements();
  245.  
  246.       while(enumResults.hasMoreElements()) {
  247.          String strItem = (String)enumResults.nextElement();
  248.          ((CFTree)this).addItem(strItem, currentIndex, 12);
  249.       }
  250.  
  251.       if (updateCursor) {
  252.          ((View)this).rootView().removeOverrideCursor();
  253.          ((View)this).rootView().updateCursor();
  254.       }
  255.  
  256.    }
  257. }
  258.