

import java.awt.*;
import java.applet.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.io.UnsignedInteger32;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.sftp.FileAttributes;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.sftp.SftpFileOutputStream;
import com.sshtools.j2ssh.SftpClient;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;



/** 
 * An applet to provide an editor of text.
 * @author matth3wbishop<at>yahoo!<dot>com
 */

public class EditPanel extends Panel implements ActionListener, Runnable
{

  //--------------------------------------------
  private StringBuffer sbFileContents;
  //--------------------------------------------
  private Button btnSave;
  //--------------------------------------------
  /** a button for loading a file */
  private Button btnLoad;
  //--------------------------------------------
  /** the main area for editing text */
  private TextArea taEditArea;
  //--------------------------------------------
  private TextField tfLoadFile;
  //--------------------------------------------
  private Label passwordLabel;
  //--------------------------------------------
  private TextField tfPassword;
  //--------------------------------------------
  private TextField tfRemoteSavePath;
  //--------------------------------------------
  private Label lblMessages;
  //--------------------------------------------
  private Font displayFont;

  //-------------------------------------------------
  /** a panel to hold some components */
  private Panel pnlTop;
  //-------------------------------------------------
  private Date startTime;
  //-------------------------------------------------
  private Date initializeTime;
  //-------------------------------------------------
  private Date stopTime;
  //-------------------------------------------------
  private Date destroyDate;
  //-------------------------------------------------
  Color backgroundColour;
  //-------------------------------------------------
  private String sServer = "bumble.sf.net";
  //-------------------------------------------------
  private String sUserName = "matth3wbishop";
  //-------------------------------------------------
  private String sServerWebRoot = "/home/groups/b/bu/bumble/htdocs";
  //-------------------------------------------------
  private String sCurrentFileLocalName;
  //-------------------------------------------------
  private String sCurrentFileUrl;
  //-------------------------------------------------
  private Thread thMessageThread;
  //-------------------------------------------------
  private boolean finishMessageThread;
  //-------------------------------------------------



  //-------------------------------------------------
  /**  */
   public EditPanel() 
   {
     //super();
     this.finishMessageThread = false;

     this.sCurrentFileLocalName = "";
     this.sCurrentFileUrl = "";

     sbFileContents = new StringBuffer();

     this.displayFont = new Font("Monospaced", Font.PLAIN, 16);
     this.pnlTop = new Panel();

     this.setBackground(new Color(0, 33, 0));

     this.initializeTime = new Date();
     System.out.println("init method of applet");

     this.btnSave = new Button("save");
     this.btnSave.addActionListener(this);

     this.btnLoad = new Button("load");
     this.btnLoad.addActionListener(this);

     this.tfLoadFile = new TextField("http://bumble.sf.net/info.txt", 40);
     this.tfLoadFile.addActionListener(this);
     this.tfLoadFile.setFont(displayFont);

     this.passwordLabel = new Label("password");
     this.passwordLabel.setForeground(Color.WHITE);
     this.passwordLabel.setFont(displayFont);

     this.tfPassword = new TextField("", 10);
     this.tfPassword.setFont(displayFont);
     this.tfPassword.setEchoChar('*');

     this.tfRemoteSavePath = new TextField(this.sServerWebRoot, 15);
     this.tfRemoteSavePath.setFont(displayFont);

     this.lblMessages = new Label();
     this.lblMessages.setForeground(Color.WHITE);
     this.lblMessages.setFont(displayFont);


     this.taEditArea = new TextArea("");
     this.taEditArea.setEditable(true);
     this.taEditArea.setFont(this.displayFont);
     this.taEditArea.setForeground(Color.WHITE);
     this.taEditArea.setBackground(Color.BLACK);

     //this.setLayout(new java.awt.GridLayout(1,0));
     this.setLayout(new java.awt.BorderLayout());

     this.pnlTop.add(this.tfLoadFile);
     this.pnlTop.add(this.btnLoad);
     this.pnlTop.add(this.btnSave);
     this.pnlTop.add(this.passwordLabel);
     this.pnlTop.add(this.tfPassword);
     this.pnlTop.add(this.tfRemoteSavePath);


     this.add("North", this.pnlTop);
     this.add("South", this.lblMessages);
     this.add("Center", this.taEditArea);


     //add(taEditArea);
     //validate();


   } //-- const:()


  //--------------------------------------------
  public void actionPerformed(ActionEvent e) 
  {
    Object source = e.getSource();

    if (source == this.btnSave)
    {  

     FileWriter fout;
     StringReader srContents;
     try
     {

      srContents = new StringReader(this.taEditArea.getText());
      fout = new FileWriter(this.sCurrentFileLocalName);


      int iByte;
      int iCounter = 0;

      iByte = srContents.read();
      while (iByte != -1)
      {
         fout.write(iByte);
         iByte++;
         iByte = srContents.read();

      } //-- while

      fout.close();
      srContents.close();
     }
     catch (Exception eeee)
     {
       System.out.println("Problem saving file");
       System.out.println(e);
       
     }


     String sPassword = tfPassword.getText();

     SftpUpload secureUpload;
     secureUpload = new SftpUpload(
       this.sCurrentFileLocalName, this.sServer, this.sUserName,
       this.tfRemoteSavePath.getText());
     secureUpload.setPassword(sPassword);
     secureUpload.upload();
     
     taEditArea.setText(secureUpload.printStatistics());
     taEditArea.append(secureUpload.toString());

     this.lblMessages.setText("file saved " + (new Date()));

    } //-- if save button


    if (source == this.tfLoadFile)
    {  
      this.showRunningMessage("loading ");    
    }

    if (source == this.btnLoad)
    {
      StringBuffer sbMessages = new StringBuffer("");
      //taEditArea.appendText(sbMessages.toString());

      this.lblMessages.setText(
        "Trying to load file " + tfLoadFile.getText());

      this.finishMessageThread = false;
      this.showRunningMessage("loading ");    

      WebDownload wdLoad = new WebDownload(tfLoadFile.getText());
      wdLoad.overwrite();
      wdLoad.download();
      this.taEditArea.append(wdLoad.printStatistics());

      BufferedReader brReader;
      FileReader frTextFile;

      try
      {
        frTextFile = new FileReader(wdLoad.getFileName());
      }
      catch (FileNotFoundException ex)
      {
        this.taEditArea.append("a problem loading the local file");
        this.finishMessageThread = true;
        return;
      }

      sbFileContents.setLength(0);
      this.sCurrentFileLocalName =
        new File(wdLoad.getFileName()).getAbsolutePath();
      this.sCurrentFileUrl = wdLoad.getUrl();

      brReader = new BufferedReader(frTextFile);

      int ii;

      try
      {
        ii = brReader.read();
        while (ii != -1)
        {
         this.sbFileContents.append((char)ii);
         ii = brReader.read();
        }

      
        brReader.close();
        frTextFile.close();
        this.finishMessageThread = true;
        this.lblMessages.setText("File loaded");
      }
      catch (IOException ex)
      {
        this.finishMessageThread = true;
        
      }

      if (!sbFileContents.toString().equals(""))
      {
        this.taEditArea.setText(sbFileContents.toString());
      }
      this.finishMessageThread = true;

    } //-- if loadButton clicked

  } //-- method: actionPerformed

  //--------------------------------------------
  public void showRunningMessage(String sMessage)
  {
    this.thMessageThread = new Thread(this);
    this.thMessageThread.start();

  }

  //--------------------------------------------
  public void run()
  {
    while (true)
    {
      try
      {
      Thread.sleep(100);
      lblMessages.setText("/");
      Thread.sleep(100);
      lblMessages.setText("-");
      Thread.sleep(100);
      lblMessages.setText("\\");
      Thread.sleep(100);
      lblMessages.setText("|");
      Thread.sleep(100);
      lblMessages.setText("/");
      Thread.sleep(100);
      lblMessages.setText("-");
      Thread.sleep(100);
      lblMessages.setText("\\");
      Thread.sleep(100);
      lblMessages.setText("|");
      }
      catch (InterruptedException e)
      {
        return;
      }
      if (this.finishMessageThread)
      {
        return;
      }
    }
  }

  //--------------------------------------------
  /** A main method to test */
  public static void main(String[] args) throws Exception
  {
    StringBuffer sbUsageMessage = new StringBuffer("");
    sbUsageMessage.append("usage: java EditPanel .");
    //sbUsageMessage.append(NEWLINE); 

    /* -----
    if (args.length == 0)
    {	    
      System.out.println(sbUsageMessage);
      System.exit(-1);
    }
    */

    Frame frame = new Frame("Testing EditPanel");
    EditPanel epTest = new EditPanel();
    frame.add("Center", epTest);
    frame.pack();
    //-- exit when the window is closed
    //--
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent ee)
      {
        System.exit(0);
      }
    });

    frame.show();
      
  } //-- main()    
    
} //-- class: EditPanel


