// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import javax.swing.text.*;

import org.apache.commons.net.ftp.*;

public class FtpUploadApplet extends JApplet implements ActionListener, MouseListener {
    private static final String FTP_SERVER = "arne";
    private static final String FTP_USER = "arne";
    private static final String FTP_PASSWORD = "hemmeligt";
    private static final String FTP_SERVERDIR = "FTPTEST";
    private JButton select;
    private JTextArea files;
    private JButton upload;
    private String dirname;
    private String[] filenames;
    public boolean[] selected;
    public void init() {
        getContentPane().setLayout(new BorderLayout());
        select = new JButton("Select dir");
        select.addActionListener(this);
        getContentPane().add(select, BorderLayout.NORTH);
        files = new JTextArea();
        files.addMouseListener(this);
        files.setFont(new Font("Courier", Font.PLAIN, files.getFont().getSize()));
        getContentPane().add(new JScrollPane(files), BorderLayout.CENTER);
        upload = new JButton("Upload");
        upload.addActionListener(this);
        getContentPane().add(upload, BorderLayout.SOUTH);
    }
    private void update() {
        StringBuffer sb = new StringBuffer("");
        for(int i = 0; i < filenames.length; i++) {
            sb.append(selected[i] ? "*" : " ");
            sb.append(filenames[i]);
            sb.append("\r\n");
        }
        files.setText(sb.toString());
    }
    public void actionPerformed(ActionEvent ev) {
        if(ev.getSource() == select) {
            JFileChooser chooser = new JFileChooser(System.getProperty("user.home"));
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                File dir = chooser.getSelectedFile();
                dirname = dir.getAbsolutePath();
                filenames = dir.list();
                selected = new boolean[filenames.length];
                update();
            }
        }
        if(ev.getSource() == upload) {
            for(int i = 0; i < filenames.length; i++) {
                if(selected[i]) {
                    upload(dirname, filenames[i]);
                }
            }
        }
    }
    public void mouseClicked(MouseEvent ev) {
        try {
            int ix = files.getLineOfOffset(files.getCaretPosition());
            selected[ix] = !selected[ix];
            update();
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    private void upload(String dirname, String filename) {
        try {
            FTPClient client = new FTPClient();
            client.connect(FTP_SERVER);
            client.login(FTP_USER, FTP_PASSWORD);
            client.changeWorkingDirectory(FTP_SERVERDIR);
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            client.storeFile(filename, new FileInputStream(dirname + File.separator + filename));
            client.disconnect();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, filename + " not uploaded" + ": " + e);
        }
    }
}
