package oracle.forms.fd;

import ftp.*;
import java.awt.Graphics;
import java.awt.Point;
import java.io.File;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JProgressBar;
import javax.swing.border.Border;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;
import oracle.forms.ui.CustomEvent;
import oracle.forms.ui.VBean;

/**
 * A javabean to handle FTP communication
 * <br />
 * @author   Francois Degrelle (Bean Wrapper)
 * @author   Calvin Tai        (ftpbean creator)
 *           FtpBean
 *           Copyright 1999 Calvin Tai
 *           E-mail: calvin_tai2000@yahoo.com.hk
 *           URL: http://www.geocities.com/SiliconValley/Code/9129/javabean/ftpbean
 * 
 * creation  October 2008
 * @version  1.1
 * 
 */
 
public class SimpleFTP extends VBean implements FtpObserver
{

  protected static final ID pConnect          = ID.registerProperty("CONNECT");
  protected static final ID pClose            = ID.registerProperty("CLOSE");
  protected static final ID pSetDirectory     = ID.registerProperty("SET_DIRECTORY");
  protected static final ID pGetDirList       = ID.registerProperty("DIRECTORY_LIST");
  protected static final ID pGetDirectory     = ID.registerProperty("GET_DIRECTORY");  
  protected static final ID pGetBinaryFile    = ID.registerProperty("GET_BINARY_FILE");
  protected static final ID pGetAsciiFile     = ID.registerProperty("GET_ACSII_FILE");  
  protected static final ID pPutBinaryFile    = ID.registerProperty("PUT_BINARY_FILE");
  protected static final ID pGetMessage       = ID.registerProperty("GET_MESSAGE");  
  protected static final ID pGetFileList      = ID.registerProperty("GET_FILE_LIST");  
  protected static final ID pGetReply         = ID.registerProperty("GET_REPLY");
  protected static final ID pGetPort          = ID.registerProperty("GET_PORT");  
  protected static final ID pSetPort          = ID.registerProperty("SET_PORT");    
  protected static final ID pGetSocketTimeout = ID.registerProperty("GET_SOCKET_TIMEOUT");    
  protected static final ID pSetSocketTimeout = ID.registerProperty("SET_SOCKET_TIMEOUT");      
  protected static final ID pGetServerName    = ID.registerProperty("GET_SERVER_NAME");   
  protected static final ID pToParentDir      = ID.registerProperty("TO_PARENT_DIR");  
  protected static final ID pMakeDirectory    = ID.registerProperty("MAKE_DIRECTORY");  
  protected static final ID pRemoveDirectory  = ID.registerProperty("REMOVE_DIRECTORY");    
  protected static final ID pExecute          = ID.registerProperty("EXECUTE");    
  protected static final ID pFileDelete       = ID.registerProperty("FILE_DELETE");      
  protected static final ID pFileRename       = ID.registerProperty("FILE_RENAME");  
  protected static final ID pSetModeTransfer  = ID.registerProperty("SET_PASSIVE_MODE_TRANSFER");    
  protected static final ID pSetProgressBar   = ID.registerProperty("SET_PROGRESS_BAR");      
  protected static final ID pSetPBLocation    = ID.registerProperty("SET_PROGRESS_BAR_LOCATION");      
  protected static final ID pSetPBSize        = ID.registerProperty("SET_PROGRESS_BAR_SIZE");        
  protected static final ID pSetPBTitle       = ID.registerProperty("SET_PROGRESS_BAR_TITLE");          
  protected static final ID pSetLog           = ID.registerProperty("SET_LOG");    
  // messaging
  protected static final ID pProgressMessage  = ID.registerProperty("PROGRESS_MESSAGE");    
  protected static final ID pProgressValue    = ID.registerProperty("PROGRESS_VALUE");      
  
  private IHandler mHandler     = null;   // Forms Handler
  private FtpBean  ftp;
  private long     total_bytes  = 0 ;  
  private int      iFileSize    = 0 ;
  private int      iCurPerCent  = 0 ;
  private boolean  bProgressBar = false ;
  private boolean  bRunning     = false ;
  private Point    pBarLocation = null;
  private Point    pBarSize     = new Point(200,16);  
  private String   sPBTitle     = null ;
  private String   sCurDir      = "" ;
  private String   sMessage     = "" ;
  private String   sFileList    = "" ;
  public  String   sRemote      = "" ;
  public  String   sLocal       = "" ;  
  public  JProgressBar pBar     = null ;
  public  FtpObserver obs       = null ;
  public  VBean       bean      = null ;
  private FtpListResult ftplrs  = null ;
  private boolean  bLog         = false ;
  private boolean  bFirst       = true ;
  
  public SimpleFTP()
  {
  }
  
  public final void init(IHandler handler)
  {
    mHandler = handler;
    super.init(handler);
    // Create a new FtpBean object.
    ftp = new FtpBean();
    obs = this ;
    bean = this ;    
  }
  
  public void paint(Graphics g)
  {
    
    if(bFirst)
    {
       // find the center point of the current canvas
       bFirst = false ;
       int iX = 0, iY = 0 ;
       int iWidth  = this.getParent().getSize().width ;
       int iHeight = this.getParent().getSize().height ;
       if(pBarSize != null)
       {
         iX = (int)((iWidth / 2) - (pBarSize.x / 2)) ;
         iY = (int)((iHeight / 2) - (pBarSize.y / 2)) ;
       }
      pBarLocation = new Point(iX,iY);      
    }
  }
  
  /*-----------------------*
   *    properties to set
   *-----------------------*/
  public boolean setProperty(ID id, Object value)
  {
    log(id+":"+value);
    
    /*------------------------
     * Connect the FTP server
     *-----------------------*/
    if (id == pConnect)
    {
      String s = (String)value ;
      String sHost = "" ;
      String sUser = "" ;
      String sPwd  = "" ;
      StringTokenizer st = new StringTokenizer(s,",");
      // host
      if (st.hasMoreTokens()) sHost = st.nextToken() ; else return false ;
      // user
      if (st.hasMoreTokens()) sUser = st.nextToken() ; else return false ;
      // password
      if (st.hasMoreTokens()) sPwd  = st.nextToken() ; 
      log("connect to:"+sHost+"-"+sUser+"@"+sPwd);
      try
      {
            ftp.ftpConnect(sHost, sUser, sPwd);
            sMessage = "OK" ;
            sCurDir = ftp.getDirectory() ;
      } catch(Exception e)
      {
            sMessage = e.toString() ;
            System.out.println(sMessage);
      }      
      printLastReply() ;
      return true ;
    }  
    
    /*------------------------
     * Close connection
     *-----------------------*/
    else if (id == pClose)
    {
      try
      {
          ftp.close();
          sMessage = "OK" ;
      } catch(Exception e)
      {
          sMessage = e.toString() ;
          System.out.println(sMessage);
      }    
      printLastReply() ;
      return true ;
    }
    
    /*------------------------
     *    Set directory
     *-----------------------*/
    else if (id == pSetDirectory)
    {
      String sDir = "" ;
      if( ! value.toString().equalsIgnoreCase("false")) sDir = (String)value ;
      else sDir = sCurDir ;
      try
      {
         ftp.setDirectory(sDir);
         sMessage = "OK" ;
         sCurDir = ftp.getDirectory() ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }

    /*------------------------
     *  Set parent directory
     *-----------------------*/
    else if (id == pToParentDir)
    {
      try
      {
         ftp.toParentDirectory();
         sMessage = "OK" ;
         sCurDir = ftp.getDirectory() ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }
    
    /*------------------------
     *   Get directory list
     *-----------------------*/
    else if (id == pGetDirList)
    {
      String sDir = null ;
      log(value.toString());
      
      if(!value.toString().equalsIgnoreCase("false")) sDir = (String)value ;
      else sDir = sCurDir ;

      try
      {
         if(sDir != null) ftp.setDirectory(sDir);
         // Get directory content.
         ftplrs = ftp.getDirectoryContent();
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }

      // Print out the type and file name of each row.
      StringBuffer buff = new StringBuffer() ;
      sFileList = "" ;
      
      while(ftplrs.next())
      {
         int type = ftplrs.getType();
         if(type == FtpListResult.DIRECTORY)
              buff.append("DIR");
         else if(type == FtpListResult.FILE)
              buff.append("FILE");
         else if(type == FtpListResult.LINK)
              buff.append("LINK");
         else if(type == FtpListResult.OTHERS)
              buff.append("OTHER");
         buff.append((char)9);
         buff.append(ftplrs.getName());
         buff.append((char)9);
         buff.append(ftplrs.getSize());
         buff.append((char)9);
         buff.append(ftplrs.getDate());
         buff.append((char)10);
      }
      sFileList = buff.toString() ;
      log(sFileList);      
      printLastReply() ;
      return true ;
    }    

    /*------------------------
     *    Make directory
     *-----------------------*/
    else if (id == pMakeDirectory)
    {
      String sDir = (String)value ;
      try
      {
         ftp.makeDirectory(sDir);;
         sMessage = "OK" ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }

    /*------------------------
     *   Remove directory
     *-----------------------*/
    else if (id == pRemoveDirectory)
    {
      String sDir = (String)value ;
      try
      {
         ftp.removeDirectory(sDir);
         sMessage = "OK" ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }

    /*------------------------
     *    File delete
     *-----------------------*/
    else if (id == pFileDelete)
    {
      String sFile = (String)value ;
      try
      {
         ftp.fileDelete(sFile);
         sMessage = "OK" ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }

    /*------------------------
     *    File rename
     *-----------------------*/
    else if (id == pFileRename)
    {
      String s = (String)value ;
      String sOld = "" ;
      String sNew = "" ;
      StringTokenizer st = new StringTokenizer(s,",");
      // old name
      if (st.hasMoreTokens()) sOld = st.nextToken() ; else return false ;
      // new name
      if (st.hasMoreTokens()) sNew = st.nextToken() ; else return false ;
      try
      {
         ftp.fileRename(sOld, sNew);
         sMessage = "OK" ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }
    
    /*----------------------------
     *    get a binary file
     *---------------------------*/
    else if (id == pGetBinaryFile)
    {
      while(bRunning)
      {
        System.out.println("waiting...");
        try{
          Thread.sleep(500l);
        }
        catch(Exception e) {;}
      }
      String s = (String)value ;
      String sSize   = "" ;
      iFileSize = 0 ;
      StringTokenizer st = new StringTokenizer(s,",");
      while(true)
      {
        // remote file name
        if (st.hasMoreTokens()) sRemote = st.nextToken() ; else return false ;
        // local file name
        if (st.hasMoreTokens()) sLocal = st.nextToken() ; else return false ;
        // file size
        if (st.hasMoreTokens()) 
        {
          try{
            iFileSize = Integer.parseInt(st.nextToken()) ;
          }
          catch(Exception e) {System.out.println("Invalid file size");}
        } 
        total_bytes = 0l ;
        iCurPerCent = 0 ;
        new ThreadBinaryTransfert(true).start();
        printLastReply() ;
      }
    }

    /*----------------------------
     *    get an ASCII file
     *---------------------------*/
    else if (id == pGetAsciiFile)
    {
      while(bRunning)
      {
        System.out.println("waiting...");
        try{
          Thread.sleep(500l);
        }
        catch(Exception e) {;}
      }
      String s = (String)value ;
      String sSize   = "" ;
      iFileSize = 0 ;
      StringTokenizer st = new StringTokenizer(s,",");
      while(true)
      {      
        // remote file name
        if (st.hasMoreTokens()) sRemote = st.nextToken() ; else return false ;
        // local file name
        if (st.hasMoreTokens()) sLocal = st.nextToken() ; else return false ;
        // file size
        if (st.hasMoreTokens()) 
        {
          try{
            iFileSize = Integer.parseInt(st.nextToken()) ;
          }
          catch(Exception e) {System.out.println("Invalid file size");}
        } 
        total_bytes = 0l ;
        iCurPerCent = 0 ;
        new ThreadAsciiTransfert().start();
        printLastReply() ;
      }
    }

    /*----------------------------
     *    put a binary file
     *---------------------------*/
    else if (id == pPutBinaryFile)
    {
      while(bRunning)
      {
        System.out.println("waiting...");
        try{
          Thread.sleep(500l);
        }
        catch(Exception e) {;}
      }
      String s = (String)value ;
      String sSize   = "" ;
      StringTokenizer st = new StringTokenizer(s,",");
      int iNbrTokens = st.countTokens() ;
      while(true)
      {
        // local file name
        if (st.hasMoreTokens()) sLocal  = st.nextToken() ; else return false ;
        // remote file name
        if (st.hasMoreTokens()) sRemote = st.nextToken() ; else return false ;
        // file size
        try{
           File file = new File(sLocal);
           iFileSize = (int)file.length() ;
           if(iFileSize == 0) {
              sMessage = "file:" + sLocal + " not found" ;
              return false ;            
           }
        }
        catch(Exception e){
           sMessage = "file:" + sLocal + " not found" ;
           return false ;
        }    
        log("put local file:"+sLocal+" to :"+sRemote);
        total_bytes = 0l ;
        iCurPerCent = 0 ;
        new ThreadBinaryTransfert(false).start();
      }
      //return true ;
    }

    /*------------------------
     * execute FTP command
     *-----------------------*/
    else if (id == pExecute)
    {
      String s = (String)value ;
      try
      {
          ftp.execute(s);
          sMessage = "OK" ;
      } catch(Exception e)
      {
          sMessage = e.toString() ;
          System.out.println(sMessage);
      }    
      printLastReply() ;
      return true ;
    }
    
    /*------------------------
     *  Set port (if not 21)
     *-----------------------*/
    else if (id == pSetPort)
    {
      String sPort = (String)value ;
      int iPort    = 0 ;
      try
      {
         iPort = Integer.parseInt(sPort) ;
         ftp.setPort(iPort);
         sMessage = "OK" ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }

    /*------------------------
     *  Set socket timeout
     *-----------------------*/
    else if (id == pSetSocketTimeout)
    {
      String s = (String)value ;
      int i    = 0 ;
      try
      {
         i = Integer.parseInt(s) ;
         ftp.setSocketTimeout(i);
         sMessage = "OK" ;
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }
      printLastReply() ;
      return true ;
    }
    
    /*----------------------------
     * Set passive transfer mode
     *---------------------------*/
    else if (id == pSetModeTransfer)
    {
      String s = (String)value ;
      if(s.equalsIgnoreCase("TRUE")) ftp.setPassiveModeTransfer(true);
      else ftp.setPassiveModeTransfer(false);
      printLastReply() ;
      return true ;
    }

    /*------------------------
     *   set log
     *-----------------------*/
    else if (id == pSetLog)
    {
      String s = (String)value ;
      if(s.equalsIgnoreCase("true")) bLog = true ;
      else bLog = false ;
      return true ;
    }

    /*------------------------
     *   set progress bar
     *-----------------------*/
    else if (id == pSetProgressBar)
    {
      String s = (String)value ;
      if(s.equalsIgnoreCase("true")) bProgressBar = true ;
      else bProgressBar = false ;
      return true ;
    }    
    
    /*---------------------------
     *  set progress bar title
     *--------------------------*/
    else if (id == pSetPBTitle)
    {
      sPBTitle = null ;
      try{
        String s = (String)value ;
        sPBTitle = s ;
        if(pBarSize.y < 40)
        {
          pBarSize.y = 40 ;
        }
      }
      catch(Exception e) {System.out.println("Null title ProgressBar");}
      return true ;
    }    
    
    /*------------------------
     * ProgressBar location
     *-----------------------*/
    else if (id == pSetPBLocation)
    {
      String s = (String)value ;
      String sX = "", sY = "" ;
      int iX = 0 ;
      int iY = 0 ;
      StringTokenizer st = new StringTokenizer(s,",");
      // X
      if (st.hasMoreTokens()) sX = st.nextToken() ; else return false ;
      // Y
      if (st.hasMoreTokens()) sY = st.nextToken() ; else return false ;

      try
      {
         iX = Integer.parseInt(sX) ;
         iY = Integer.parseInt(sY) ;
         // center progress bar ?
         if((iX == -1) || (iY == -1))
         {
           int iWidth  = bean.getParent().getSize().width ;
           int iHeight = bean.getParent().getSize().height ;
           if(pBarSize != null)
           {
             iX = (int)((iWidth / 2) - (pBarSize.x / 2)) ;
             iY = (int)((iHeight / 2) - (pBarSize.y / 2)) ;
           }
         }
         pBarLocation = new Point(iX,iY);
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }      
      return true ;
    }  
    
   /*------------------------
    *   ProgressBar size
    *-----------------------*/
    else if (id == pSetPBSize)
    {
      String s = (String)value ;
      String sX = "", sY = "" ;
      int iX = 0 ;
      int iY = 0 ;
      StringTokenizer st = new StringTokenizer(s,",");
      // X
      if (st.hasMoreTokens()) sX = st.nextToken() ; else return false ;
      // Y
      if (st.hasMoreTokens()) sY = st.nextToken() ; else return false ;

      try
      {
         iX = Integer.parseInt(sX) ;
         iY = Integer.parseInt(sY) ;
         pBarSize = new Point(iX,iY);
      } catch(Exception e)
      {
         sMessage = e.toString() ;
         System.out.println(sMessage);
      }      
      return true ;
    }      
    
    else{
        return super.setProperty(id, value);
    }    
  }
  
  // --------------------------------------------------------------------------
 /*---------------------------------*
  *  Get the properties of the bean
  *---------------------------------*/
  public Object getProperty(ID property)
  {
    // get the last ststus message
    if (property == pGetMessage) {
       return sMessage ;
    }
    
    // get the file list
    else if (property == pGetFileList)
    {
      return sFileList ;
    }
    
    // get current directory
    else if (property == pGetDirectory)
    {
      try{
        sCurDir = ftp.getDirectory() ;
        return sCurDir ;
      }
      catch (Exception e) { return "" ;}
    }    

    // get port
    else if (property == pGetPort)
    {
      try{
        return "" + ftp.getPort() ;
      }
      catch (Exception e) { return "" ;}
    }    

    // get socket timeout
    else if (property == pGetSocketTimeout)
    {
      try{
        return "" + ftp.getSocketTimeout() ;
      }
      catch (Exception e) { return "" ;}
    }    

    // get server name
    else if (property == pGetServerName)
    {
      try{
        return "" + ftp.getServerName() ;
      }
      catch (Exception e) { return "" ;}
    }    
    
    // get last reply
    else if (property == pGetReply)
    {
      try{
        return ftp.getReply() + "-" + ftp.getReplyMessage() ;
      }
      catch (Exception e) { return "" ;}
    }    

    else // default behaviour
    {
      return super.getProperty(property);
    }
  }

  void printLastReply()
  {
    log("Last reply:"+ftp.getReply() + "-" + ftp.getReplyMessage()) ;
  }
  
    // Implemented for FtpObserver interface.
    // To monitor download progress.
    public void byteRead(int bytes)
    {
        total_bytes += bytes;
        int r = 0 ;
        if(iFileSize > 0 )
        {
          int iPerCent = (int)(((double)total_bytes / (double)iFileSize) * 100.0d) ;
          r = (int)(iPerCent % 5) ;
          if(r == 0) 
          {
            if(iPerCent > iCurPerCent)
            {
              if(bProgressBar)
              {
                pBar.setValue(iPerCent);
                pBar.setString(""+iPerCent+"%");
                pBar.repaint();
              }
              iCurPerCent = iPerCent ;
              log( iPerCent+"%");
              sendMessage(""+iPerCent);
              try
              {
                Thread.sleep(20);
              }
              catch(Exception e) {;}
            }  
          }
        }
        else
        {
          r = (int)(total_bytes % 10000) ;
          if(r == 0) log(total_bytes + " of bytes read already.");        
        }
    }

    // Needed to implements by FtpObserver interface.
    public void byteWrite(int bytes)
    {
       total_bytes += bytes;
        int r = 0 ;
        if(iFileSize > 0 )
        {
          int iPerCent = (int)(((double)total_bytes / (double)iFileSize) * 100.0d) ;
          r = (int)(iPerCent % 5) ;
          if(r == 0) 
          {
            if(iPerCent > iCurPerCent)
            {
              if(bProgressBar)
              {
                pBar.setValue(iPerCent);
                pBar.setString(""+iPerCent+"%");
                pBar.repaint();
              }
              iCurPerCent = iPerCent ;
              log( iPerCent+"%");
              sendMessage(""+iPerCent);
              try
              {
                Thread.sleep(20);
              }
              catch(Exception e) {;}
            }  
          }
        }
        else
        {
          r = (int)(total_bytes % 10000) ;
          if(r == 0) log(total_bytes + " of bytes written already.");        
        } 
    }
 
    /*-----------------------------
     * send message back to Forms
     *----------------------------*/
    public void sendMessage(String value)
    { 
     // this is the method that will dispatch the customEvent to forms
     try{
         CustomEvent ce = new CustomEvent(mHandler,pProgressMessage);
         mHandler.setProperty(pProgressValue, value);
         dispatchCustomEvent(ce);
        }
     catch (Exception e) 
           {log("SimpleFTP -> exception while dispatching: " + e);
           }
    }   
 
    /*-----------------------------------
     * class to transfert a binary file
     *----------------------------------*/
    public class ThreadBinaryTransfert extends Thread {
         boolean bRead = true ;
         public ThreadBinaryTransfert( boolean p_read )
         {
           bRead = p_read ;
         }
         public void run() {
            while(bRunning)
            {
              try{
                Thread.sleep(500l);
              }
              catch(Exception e) {;}
            }
            bRunning = true ;
            try
            {
              if(bProgressBar)
              {
                pBar = new JProgressBar();
                pBar.setLocation(pBarLocation.x,pBarLocation.y);
                pBar.setSize(pBarSize.x,pBarSize.y);
                pBar.setValue(0);
                pBar.setStringPainted(true);
                if(sPBTitle != null)
                {
                  Border border = BorderFactory.createTitledBorder(sPBTitle);
                  pBar.setBorder(border);
                }
                pBar.setString("0%");
                if(iFileSize == 0) pBar.setIndeterminate(true);
                bean.getParent().add(pBar,0);
              }
               total_bytes = 0 ;
               iCurPerCent = 0 ;
               if(bRead)
               {
                 log("getBinaryFile:"+sRemote+" -> "+sLocal);
                 ftp.getBinaryFile(sRemote, sLocal, obs);
               }  
               else
               {
                 log("putBinaryFile:"+sLocal+" -> "+sRemote);
                 ftp.putBinaryFile(sLocal, sRemote, obs);
               }  
               if(bProgressBar) bean.getParent().remove(pBar);
               bean.getParent().repaint();
               bRunning = false ;                                
               sMessage = "OK";
            } catch(Exception e)
            {
               sMessage = e.toString() ;
               System.out.println(sMessage);
            }
            finally
            {
              if(bProgressBar) bean.getParent().remove(pBar);
              bRunning = false ;
            }                         
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {}
        }
    }
 
    /*-----------------------------------
     * class to transfert an ASCII file
     *----------------------------------*/
    public class ThreadAsciiTransfert extends Thread {
         public void run() {
            while(bRunning)
            {
              try{
                Thread.sleep(500l);
              }
              catch(Exception e) {;}
            }
            bRunning = true ;
            try
            {
              if(bProgressBar)
              {
                pBar = new JProgressBar();
                pBar.setLocation(pBarLocation.x,pBarLocation.y);
                pBar.setSize(pBarSize.x,pBarSize.y);
                pBar.setValue(0);
                pBar.setStringPainted(true);
                if(sPBTitle != null)
                {
                  Border border = BorderFactory.createTitledBorder(sPBTitle);
                  pBar.setBorder(border);
                }
                pBar.setString("0%");
                if(iFileSize == 0) pBar.setIndeterminate(true);
                bean.getParent().add(pBar,0);
              }
               total_bytes = 0 ;
               iCurPerCent = 0 ;
               ftp.getAsciiFile(sRemote, sLocal, obs);
               if(bProgressBar) bean.getParent().remove(pBar);
               bean.getParent().repaint();
               bRunning = false ;                                               
               sMessage = "OK";
            } catch(Exception e)
            {
               sMessage = e.toString() ;
               System.out.println(sMessage);
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {}
            finally
            {
              if(bProgressBar) bean.getParent().remove(pBar);
              bRunning = false ;
            }             
        }
    }    

    void log(String sMessage)
    {
      if(bLog) System.out.println("[SimpleFTP] "+sMessage);
    }
  
}
