/**  This file contains the implementation of a FTP Explorer 
  *
  *  constructors:
  *       public FTPExplorer(Gui gui)
  *       The method index:
  *       init();
  *
  *  The interface with user is implemented using
  *       a Action Listener.
  *
  */

import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.applet.*;
import java.io.*;
import Gui;
import FtpClient;

class FTPExplorer extends Frame
{

     public Gui gui;
     public List entries;
     public boolean isDir[] = new boolean[1000];
     String currentDirectory;

     String one,two, three;

     /**
      *private Label profileName;
      *private TextField profile;
      **/

     private Label fileNamesLabel;
     private Label statusReporterLabel;
     private Label modeLabel;

     private Label hostAdd;
     public TextField host;

     private Label portNo;
     public TextField port;

     private Label loginName;
     public TextField login;

     private Label passwdNo;
     public TextField passwd;

     private Label pathName;
     public TextField path;

     private Button explore;
     private Button download;
     private Button upload;
     private Button connect;
     private Button changeDirectory;
     private Button exit;
     public CheckboxGroup checkboxGroup;
     public Checkbox ascii,binary;

     public TextArea statusReporter;

     public FtpClient fp;
     	
     public FTPExplorer(Gui gui)
     {
        this.gui = gui;

        entries = new List(1000);

        currentDirectory = System.getProperty("user.dir");

        fileNamesLabel= new Label("Files and Directories",Label.LEFT);
        statusReporterLabel= new Label("Status",Label.LEFT);
        modeLabel = new Label("Mode",Label.LEFT);

        hostAdd = new Label("Host Address:",Label.LEFT);
        host = new TextField(15);

        portNo = new Label("Port Number:",Label.LEFT);
        port = new TextField(4);

        loginName = new Label("Login Name:",Label.LEFT);
        login = new TextField(15);

        passwdNo = new Label("Password:",Label.LEFT);
        passwd = new TextField(15);


        explore = new Button("Explore");
        download = new Button("Download");
        upload = new Button("Upload");

        connect = new Button("Connect");
        changeDirectory = new Button("Change Directory");
        exit = new Button("Exit");
        checkboxGroup = new CheckboxGroup();
        
        statusReporter = new TextArea();
        
        fp = new FtpClient(this);
        
     }

     public void init()
     {

        setTitle("FTP Explorer");

        setLayout(null);
        setResizable(false);
        setSize(600,500);
        

        entries.setBounds(10,50,350,350);
        add(entries);

        fileNamesLabel.setBounds(10,30,350,15);
        statusReporterLabel.setBounds(10,400,80,15);
        modeLabel.setBounds(410,400,80,15);

        add(fileNamesLabel);
        add(statusReporterLabel);
        add(modeLabel);

        hostAdd.setBounds(370,85,80,15);
        add(hostAdd);
        host.setBounds(450,80,100,20);
        add(host);

        portNo.setBounds(370,115,80,15);
        add(portNo);
        port.setBounds(450,110,100,20);
        add(port);

        loginName.setBounds(370,145,80,15);
        add(loginName);
        login.setBounds(450,140,100,20);
        add(login);

        passwdNo.setBounds(370,175,80,15);
        add(passwdNo);
        passwd.setBounds(450,170,100,20);
        passwd.setEchoChar('*'); 
        add(passwd);

        FtpManager ftpManager = new FtpManager(this);

        add(connect);
        connect.setBounds(370,205,100,25);
        connect.addActionListener(ftpManager);
        
        add(explore);
        explore.setBounds(370,235,100,25);
        explore.addActionListener(ftpManager);
        
        add(download);
        download.setBounds(370,265,100,25);
        download.addActionListener(ftpManager);

        add(upload);
        upload.setBounds(370,295,100,25);
        upload.addActionListener(ftpManager);

        add(changeDirectory);
        changeDirectory.setBounds(370,325,100,25);
        changeDirectory.addActionListener(ftpManager);

        add(exit);
        exit.setBounds(370,355,100,25);
        exit.addActionListener(ftpManager);

        add(statusReporter);
        statusReporter.setEditable(false);
        statusReporter.setBounds(10,420,370,75);

        add(ascii = new Checkbox("Ascii",checkboxGroup,false));
        add(binary = new Checkbox("Binary",checkboxGroup,true));
        ascii.setBounds(415,420,100,25);
        binary.setBounds(415,450,100,25);


     }

     void reportProgress(String message)
     {
        one = two;
        two = three;
        three = message;
        statusReporter.setText(one+"\n"+two+"\n"+three);
     }

}

/**
  *  The action listener listenning to the buttons.
  */

class FtpManager implements ActionListener
{
    FTPExplorer fp;
    public FtpManager(FTPExplorer fp)
    {
      this.fp = fp;
    }

    public void actionPerformed(ActionEvent ae) 
    {
        System.out.println(ae.getActionCommand());

        if(ae.getActionCommand().equals("Connect"))
        {
           fp.fp.makeConnection(fp.host.getText()); 
        }
        else
        if(ae.getActionCommand().equals("Explore"))
        {
           if (fp.entries.getSelectedItem() != null )
           if (fp.isDir[fp.entries.getSelectedIndex()])
            fp.fp.explore(fp.entries.getSelectedItem());
           else
            fp.reportProgress("Not a directory");
           else
            fp.reportProgress("Please select an item first"); 
        }
        else
        if(ae.getActionCommand().equals("Download"))
        {
            if (fp.isDir[fp.entries.getSelectedIndex()])
             fp.fp.getDirectory(fp.currentDirectory,fp.entries.getSelectedItem());
            else
             fp.fp.getFile(fp.entries.getSelectedItem(),fp.currentDirectory);
            fp.reportProgress("DownLoad Complete....");
        }
        else
        if(ae.getActionCommand().equals("Upload"))
        {
            FileDialog fd = new FileDialog(fp,"File to be uploaded");
            fd.setVisible(true);
            if (fd.getFile() != null)
                fp.fp.putFile(fd.getFile(),fd.getDirectory());
            fp.reportProgress("UpLoad Complete....");
        }
        else
        if(ae.getActionCommand().equals("Change Directory"))
        {
            fp.reportProgress("Changes Directory");
            {
            FileDialog fd = new FileDialog(fp,"Save in",FileDialog.SAVE);
            fd.setFile("anything");
            fd.setVisible(true);
            if (fd.getFile() !=null)
                fp.currentDirectory = fd.getDirectory();
            }
            fp.gui.setEnabled(false);
        }
        else
        if(ae.getActionCommand().equals("Exit"))
        {
            fp.gui.setEnabled(true);
            fp.setEnabled(false);
            fp.setVisible(false);
        }
    }

}


