ui.FtpDialog.java Source code

Java tutorial

Introduction

Here is the source code for ui.FtpDialog.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import connectors.FtpConnection;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import utils.Trace;

/**
 *
 * @author '? ?
 */
public class FtpDialog extends javax.swing.JFrame {

    FtpConnection ftpHandler = new FtpConnection();
    private boolean connected = false;
    public boolean hasUploaded = false;
    private String lastKnownDir = null;
    private String currentDir = null;
    private final JFrame rootFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
    private FileInputStream input = null;
    private File selectedFile = null;

    private ProgressBar pb;

    public FtpDialog() {
        initComponents();
    }

    class ProgressBar extends SwingWorker<Void, Void> {

        @Override
        protected Void doInBackground() throws IOException {
            progressBarDialog.setVisible(true);
            progressBarDialog.setSize(350, 100);
            progressBarDialog.pack();

            uploadProgressBar.setIndeterminate(true);

            fileNameRsLb.setText(selectedFile.getName());
            completRsLb.setText("");
            fileSizeRsLb.setText(FileUtils.byteCountToDisplaySize(selectedFile.length()));

            long startTime = System.currentTimeMillis();
            hasUploaded = ftpHandler.stor(input, selectedFile.getName());
            long endTime = System.currentTimeMillis();

            long completionTime = endTime - startTime;

            Trace.trc("Completion time: " + TimeUnit.MILLISECONDS.toSeconds(completionTime));

            completRsLb.setText(String.format("%02d min, %02d sec", TimeUnit.MILLISECONDS.toMinutes(completionTime),
                    TimeUnit.MILLISECONDS.toSeconds(completionTime)
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(completionTime))));

            if (!hasUploaded) {
                JOptionPane.showMessageDialog(rootFrame, "Error uploading file to the remote server!", "Error",
                        JOptionPane.ERROR_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(rootFrame,
                        "File: " + selectedFile.getName() + " has uploaded successfully.");
                try {
                    String dirList = null;
                    dirList = ftpHandler.list();
                    outputTextArea.setText(dirList);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void done() {
            Trace.trc("Swing worker done");
            progressBarDialog.setVisible(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progressBarDialog = new javax.swing.JDialog();
        progressBarPanel = new javax.swing.JPanel();
        uploadProgressBar = new javax.swing.JProgressBar();
        progressBarLb = new javax.swing.JLabel();
        completLb = new javax.swing.JLabel();
        fileSizeLb = new javax.swing.JLabel();
        fileNameLb = new javax.swing.JLabel();
        completRsLb = new javax.swing.JLabel();
        fileSizeRsLb = new javax.swing.JLabel();
        fileNameRsLb = new javax.swing.JLabel();
        loginDialog = new javax.swing.JPanel();
        hostAddressLabel = new javax.swing.JLabel();
        portNumberLabel = new javax.swing.JLabel();
        userNameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        hostAddressField = new javax.swing.JTextField();
        userNameField = new javax.swing.JTextField();
        portNumberSp = new javax.swing.JSpinner();
        loginBt = new javax.swing.JButton();
        disconnectBt = new javax.swing.JButton();
        passField = new javax.swing.JPasswordField();
        closeBt = new javax.swing.JButton();
        outputPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        outputTextArea = new javax.swing.JTextArea();
        commandPanel = new javax.swing.JPanel();
        fileDlLabel = new javax.swing.JLabel();
        fileDlText = new javax.swing.JTextField();
        fileDlBt = new javax.swing.JButton();
        uploadFileBt = new javax.swing.JButton();
        uploadFileLb = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        createDirText = new javax.swing.JTextField();
        createDirBt = new javax.swing.JButton();
        deleteFileLb = new javax.swing.JLabel();
        deleteFileBt = new javax.swing.JButton();
        deleteFileText = new javax.swing.JTextField();
        deleteDirLb = new javax.swing.JLabel();
        deleteDirText = new javax.swing.JTextField();
        deleteDirBt = new javax.swing.JButton();
        navigationPanel = new javax.swing.JPanel();
        cdDirLabel = new javax.swing.JLabel();
        cdDirText = new javax.swing.JTextField();
        cdDirBt = new javax.swing.JButton();
        backLabel = new javax.swing.JLabel();
        backBt = new javax.swing.JButton();
        forwardLabel = new javax.swing.JLabel();
        forwardBt = new javax.swing.JButton();
        rootLabel = new javax.swing.JLabel();
        rootBt = new javax.swing.JButton();
        refreshLb = new javax.swing.JLabel();
        refreshBt = new javax.swing.JButton();
        remoteSitePanel = new javax.swing.JPanel();
        remoteSiteText = new javax.swing.JTextField();

        progressBarDialog.setTitle("File upload");

        uploadProgressBar.setPreferredSize(new java.awt.Dimension(146, 50));

        progressBarLb.setText("Uploading file:");

        completLb.setText("Completion time:");

        fileSizeLb.setText("File size: ");

        fileNameLb.setText("File name: ");

        javax.swing.GroupLayout progressBarPanelLayout = new javax.swing.GroupLayout(progressBarPanel);
        progressBarPanel.setLayout(progressBarPanelLayout);
        progressBarPanelLayout.setHorizontalGroup(progressBarPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(progressBarPanelLayout.createSequentialGroup().addGroup(progressBarPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(progressBarPanelLayout.createSequentialGroup().addGap(160, 160, 160)
                                .addComponent(progressBarLb).addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(progressBarPanelLayout.createSequentialGroup().addContainerGap()
                                .addGroup(progressBarPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(uploadProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(progressBarPanelLayout.createSequentialGroup()
                                                .addComponent(fileNameLb)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(fileNameRsLb))))
                        .addGroup(progressBarPanelLayout.createSequentialGroup().addContainerGap()
                                .addGroup(progressBarPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(progressBarPanelLayout.createSequentialGroup()
                                                .addComponent(completLb)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGroup(progressBarPanelLayout.createSequentialGroup()
                                                .addComponent(fileSizeLb)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                .addGroup(progressBarPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(completRsLb, javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(fileSizeRsLb, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addContainerGap()));
        progressBarPanelLayout.setVerticalGroup(
                progressBarPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        progressBarPanelLayout.createSequentialGroup().addContainerGap()
                                .addComponent(progressBarLb, javax.swing.GroupLayout.PREFERRED_SIZE, 21,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(uploadProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 50,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(progressBarPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(completLb).addComponent(completRsLb))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(progressBarPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(fileSizeLb)
                                        .addGroup(progressBarPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(fileSizeRsLb)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(progressBarPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(fileNameLb).addComponent(fileNameRsLb))
                                .addContainerGap(31, Short.MAX_VALUE)));

        javax.swing.GroupLayout progressBarDialogLayout = new javax.swing.GroupLayout(
                progressBarDialog.getContentPane());
        progressBarDialog.getContentPane().setLayout(progressBarDialogLayout);
        progressBarDialogLayout.setHorizontalGroup(
                progressBarDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        progressBarPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        progressBarDialogLayout.setVerticalGroup(
                progressBarDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        progressBarPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Java FTP Client ");
        setName("loginFrame"); // NOI18N
        setPreferredSize(new java.awt.Dimension(1100, 1000));
        setSize(new java.awt.Dimension(1100, 1000));

        loginDialog.setBorder(javax.swing.BorderFactory.createTitledBorder("Session"));

        hostAddressLabel.setText("Host address:");

        portNumberLabel.setText("Port number:");

        userNameLabel.setText("Username:");

        passwordLabel.setText("Password:");

        portNumberSp
                .setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(21), null, null, Integer.valueOf(1)));

        loginBt.setText("Login");
        loginBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBtActionPerformed(evt);
            }
        });

        disconnectBt.setMnemonic('1');
        disconnectBt.setText("Disconnect");
        disconnectBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                disconnectBtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout loginDialogLayout = new javax.swing.GroupLayout(loginDialog);
        loginDialog.setLayout(loginDialogLayout);
        loginDialogLayout.setHorizontalGroup(loginDialogLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(loginDialogLayout.createSequentialGroup().addContainerGap().addComponent(hostAddressLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(hostAddressField, javax.swing.GroupLayout.PREFERRED_SIZE, 195,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(portNumberLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(portNumberSp, javax.swing.GroupLayout.PREFERRED_SIZE, 80,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(userNameLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userNameField, javax.swing.GroupLayout.PREFERRED_SIZE, 130,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(passwordLabel).addGap(6, 6, 6)
                        .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE, 120,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(loginBt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(disconnectBt).addContainerGap(95, Short.MAX_VALUE)));
        loginDialogLayout.setVerticalGroup(loginDialogLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(loginDialogLayout.createSequentialGroup().addGroup(loginDialogLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(hostAddressLabel)
                        .addComponent(hostAddressField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(portNumberLabel)
                        .addComponent(portNumberSp, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(userNameLabel)
                        .addComponent(userNameField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(passwordLabel).addComponent(loginBt).addComponent(disconnectBt)
                        .addComponent(passField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        closeBt.setText("Close");
        closeBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeBtActionPerformed(evt);
            }
        });

        outputPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Remote server output"));

        outputTextArea.setColumns(20);
        outputTextArea.setRows(5);
        jScrollPane1.setViewportView(outputTextArea);

        javax.swing.GroupLayout outputPanelLayout = new javax.swing.GroupLayout(outputPanel);
        outputPanel.setLayout(outputPanelLayout);
        outputPanelLayout.setHorizontalGroup(
                outputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING, outputPanelLayout.createSequentialGroup()
                                .addContainerGap().addComponent(jScrollPane1).addContainerGap()));
        outputPanelLayout
                .setVerticalGroup(outputPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(outputPanelLayout.createSequentialGroup().addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 230,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        commandPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Command set"));

        fileDlLabel.setText("Select file to download:");

        fileDlBt.setText("Download");
        fileDlBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileDlBtActionPerformed(evt);
            }
        });

        uploadFileBt.setText("Upload");
        uploadFileBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                uploadFileBtActionPerformed(evt);
            }
        });

        uploadFileLb.setText("Upload file:");

        jLabel1.setText("Create new directory:");

        createDirBt.setText("Create");
        createDirBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createDirBtActionPerformed(evt);
            }
        });

        deleteFileLb.setText("Select file to delete:");

        deleteFileBt.setText("Delete");
        deleteFileBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteFileBtActionPerformed(evt);
            }
        });

        deleteDirLb.setText("Select directory to delete:");

        deleteDirBt.setText("Delete");
        deleteDirBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteDirBtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout commandPanelLayout = new javax.swing.GroupLayout(commandPanel);
        commandPanel.setLayout(commandPanelLayout);
        commandPanelLayout.setHorizontalGroup(commandPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(commandPanelLayout.createSequentialGroup().addContainerGap().addGroup(commandPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, commandPanelLayout
                                .createSequentialGroup()
                                .addGroup(commandPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(fileDlLabel).addComponent(uploadFileLb)
                                        .addComponent(deleteFileLb))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(commandPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(fileDlText).addComponent(deleteFileText,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE)))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, commandPanelLayout
                                .createSequentialGroup()
                                .addGroup(commandPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1).addComponent(deleteDirLb))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12,
                                        Short.MAX_VALUE)
                                .addGroup(commandPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(createDirText).addComponent(deleteDirText,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(commandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(createDirBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(uploadFileBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(fileDlBt, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                                .addComponent(deleteFileBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(deleteDirBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        commandPanelLayout.setVerticalGroup(commandPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(commandPanelLayout.createSequentialGroup().addContainerGap().addGroup(commandPanelLayout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(fileDlLabel)
                        .addComponent(fileDlText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(fileDlBt)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(commandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(uploadFileLb).addComponent(uploadFileBt))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(commandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(deleteFileLb).addComponent(deleteFileBt).addComponent(deleteFileText,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(commandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel1)
                                .addComponent(createDirText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(createDirBt))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(commandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(deleteDirLb)
                                .addComponent(deleteDirText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(deleteDirBt))
                        .addContainerGap(57, Short.MAX_VALUE)));

        navigationPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Navigation Commands"));

        cdDirLabel.setText("Change to another directory:");

        cdDirBt.setText("Change");
        cdDirBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cdDirBtActionPerformed(evt);
            }
        });

        backLabel.setText("Go to back:");

        backBt.setText("Back");
        backBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backBtActionPerformed(evt);
            }
        });

        forwardLabel.setText("Go forward:");

        forwardBt.setText("Forward");
        forwardBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                forwardBtActionPerformed(evt);
            }
        });

        rootLabel.setText("Go to root:");

        rootBt.setText("Root");
        rootBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rootBtActionPerformed(evt);
            }
        });

        refreshLb.setText("Refresh:");

        refreshBt.setText("Refresh");
        refreshBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshBtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout navigationPanelLayout = new javax.swing.GroupLayout(navigationPanel);
        navigationPanel.setLayout(navigationPanelLayout);
        navigationPanelLayout.setHorizontalGroup(navigationPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(navigationPanelLayout.createSequentialGroup().addContainerGap()
                        .addGroup(navigationPanelLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(navigationPanelLayout.createSequentialGroup().addComponent(cdDirLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cdDirText, javax.swing.GroupLayout.PREFERRED_SIZE, 127,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(backLabel).addComponent(forwardLabel).addComponent(rootLabel)
                                .addComponent(refreshLb))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(navigationPanelLayout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cdDirBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(backBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(forwardBt, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                .addComponent(rootBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(refreshBt, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap()));
        navigationPanelLayout.setVerticalGroup(
                navigationPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(navigationPanelLayout.createSequentialGroup().addContainerGap()
                                .addGroup(navigationPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(cdDirLabel)
                                        .addComponent(cdDirText, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(cdDirBt))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(navigationPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(backLabel).addComponent(backBt))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(navigationPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(forwardLabel).addComponent(forwardBt))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(navigationPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(rootLabel).addComponent(rootBt))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(navigationPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(refreshLb).addComponent(refreshBt))
                                .addContainerGap(51, Short.MAX_VALUE)));

        remoteSitePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Remote site"));

        remoteSiteText.setEditable(false);

        javax.swing.GroupLayout remoteSitePanelLayout = new javax.swing.GroupLayout(remoteSitePanel);
        remoteSitePanel.setLayout(remoteSitePanelLayout);
        remoteSitePanelLayout.setHorizontalGroup(remoteSitePanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(remoteSitePanelLayout
                        .createSequentialGroup().addComponent(remoteSiteText).addContainerGap()));
        remoteSitePanelLayout.setVerticalGroup(
                remoteSitePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                        remoteSiteText, javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(outputPanel, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                        .addComponent(loginDialog, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(closeBt))
                        .addComponent(remoteSitePanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(commandPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(navigationPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(loginDialog, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(outputPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(remoteSitePanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(commandPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(navigationPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 196, Short.MAX_VALUE)
                        .addComponent(closeBt).addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeBtActionPerformed
        // TODO add your handling code here:
        if (connected) {
            try {
                ftpHandler.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Trace.trc("Closing connection and exiting program...");
            System.exit(0);
        } else {
            Trace.trc("Exiting program...");
            System.exit(0);
        }

    }//GEN-LAST:event_closeBtActionPerformed

    private void loginBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBtActionPerformed

        String host = hostAddressField.getText();
        int port = (int) portNumberSp.getValue();
        String user = userNameField.getText();
        char[] password = passField.getPassword();
        String pass = new String(password);
        String hostIP = null;

        Trace.connectionFtp = true;

        InetAddress address = null;

        if (!connected) {
            try {
                address = InetAddress.getByName(host);
                ;
                hostIP = StringUtils.substringAfter(address.toString(), "/");
                if (hostIP == null || hostIP.equals("127.0.0.1")) {
                    JOptionPane.showMessageDialog(rootFrame, "Host name not set", "Error",
                            JOptionPane.ERROR_MESSAGE);
                    return;
                }
                if (port == 0) {
                    JOptionPane.showMessageDialog(rootFrame, "Invalid port number", "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
                if (Trace.connectionFtp) {
                    Trace.trc("Host IP address: " + hostIP);
                }
            } catch (UnknownHostException e) {
                JOptionPane.showMessageDialog(rootFrame, "Cannot resolve host address", "Error",
                        JOptionPane.ERROR_MESSAGE);
                e.printStackTrace();
            }

            try {
                if (address != null && port != 0 && user != null && pass != null) {
                    connected = ftpHandler.doConnect(hostIP, port, user, pass);
                    Arrays.fill(password, '0');
                    if (!connected) {
                        JOptionPane.showMessageDialog(rootFrame,
                                "Problem iniating connection to the FTP server, please check your login credentials",
                                "Error", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (connected) {
                try {
                    if (Trace.connectionFtp) {
                        Trace.trc("Attempting to list the remote directory...");
                    }
                    String dirList = ftpHandler.list();
                    lastKnownDir = ftpHandler.pwd();
                    outputTextArea.setText(dirList);
                    remoteSiteText.setText(ftpHandler.pwd());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Already connected to a FTP server, cannot reconnect!",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_loginBtActionPerformed

    private void cdDirBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cdDirBtActionPerformed

        Trace.connectionFtp = true;
        boolean hasChangedDir = false;

        if (connected) {
            if (!cdDirText.getText().equals("")) {
                if (Trace.connectionFtp) {
                    Trace.trc("Changing to new directory: " + cdDirText.getText().trim());
                }
                try {
                    hasChangedDir = ftpHandler.cwd(cdDirText.getText().trim());

                    if (hasChangedDir) {
                        lastKnownDir = ftpHandler.pwd();
                        String dirList = ftpHandler.list();
                        outputTextArea.setText(dirList);
                        cdDirText.setText("");
                        remoteSiteText.setText(ftpHandler.pwd());
                    } else {
                        JOptionPane.showMessageDialog(rootFrame, "Non existing directory, cannot CWD!", "Error",
                                JOptionPane.ERROR_MESSAGE);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                cdDirText.setText("");
                JOptionPane.showMessageDialog(rootFrame, "No directory has been selected, cannot CWD!", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot CWD!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_cdDirBtActionPerformed

    private void fileDlBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileDlBtActionPerformed

        Trace.connectionFtp = true;

        if (connected) {
            boolean hasDownloaded = false;
            if (!fileDlText.getText().equals("")) {
                if (Trace.connectionFtp) {
                    Trace.trc("Download file: " + fileDlText.getText().trim());
                }
                try {
                    hasDownloaded = ftpHandler.retr(fileDlText.getText().trim());
                    if (hasDownloaded) {
                        JOptionPane.showMessageDialog(rootFrame,
                                "File " + fileDlText.getText().trim() + " was downloaded successfully");
                        fileDlText.setText("");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                JOptionPane.showMessageDialog(rootFrame, "No file to download has been selected!", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot disconnect!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_fileDlBtActionPerformed

    private void disconnectBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_disconnectBtActionPerformed

        Trace.connectionFtp = true;

        if (connected) {
            if (Trace.connectionFtp) {
                Trace.trc("Disconnecting from server: " + hostAddressField.getText());
            }
            fileDlText.setText("");
            outputTextArea.setText("");
            hostAddressField.setText("");
            userNameField.setText("");
            passField.setText("");
            remoteSiteText.setText("");

            try {
                ftpHandler.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }

            connected = false;
            JOptionPane.showMessageDialog(rootFrame, "Disconnected from server.");
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot disconnect!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_disconnectBtActionPerformed

    private void uploadFileBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_uploadFileBtActionPerformed

        if (connected) {
            JFileChooser uploadFileChooser = new JFileChooser();
            uploadFileChooser.setPreferredSize(new Dimension(650, 450));

            File rootDirectory = new File("C:\\");

            uploadFileChooser
                    .setCurrentDirectory(uploadFileChooser.getFileSystemView().getParentDirectory(rootDirectory));

            uploadFileChooser.setDialogTitle("File to upload");
            int result = uploadFileChooser.showOpenDialog(this);
            switch (result) {

            case JFileChooser.APPROVE_OPTION:
                selectedFile = uploadFileChooser.getSelectedFile();

                Trace.trc("File to upload: " + selectedFile.getName() + " File size: "
                        + FileUtils.byteCountToDisplaySize(selectedFile.length()));

                if (connected) {
                    if (!selectedFile.equals(null)) {
                        try {
                            input = new FileInputStream(selectedFile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        pb = new ProgressBar();
                        pb.execute();

                        uploadFileChooser.setVisible(false);

                    } else {
                        JOptionPane.showMessageDialog(rootFrame, "No file to upload has been chosen!", "Error",
                                JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot upload file!",
                            "Error", JOptionPane.ERROR_MESSAGE);
                }
                break;

            case JFileChooser.CANCEL_OPTION:
                Trace.trc("Closing file chooser dialog");
                break;

            case JFileChooser.ERROR_OPTION:
                Trace.trc("An error occured");
                break;
            }

        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot upload file!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_uploadFileBtActionPerformed

    private void backBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBtActionPerformed

        Trace.connectionFtp = true;
        String currentDir = null;

        if (connected) {
            if (Trace.connectionFtp) {
                Trace.trc("Going back to previous directory");
            }
            try {
                currentDir = ftpHandler.pwd();
                lastKnownDir = currentDir;

                if (currentDir.equals("/")) {
                    JOptionPane.showMessageDialog(rootFrame, "Cannot go back, you are in the root directory.");
                } else {
                    ftpHandler.cwd("..");
                    ftpHandler.pwd();
                    String dirList = ftpHandler.list();
                    outputTextArea.setText(dirList);
                    remoteSiteText.setText(ftpHandler.pwd());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot change directories!",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_backBtActionPerformed

    private void forwardBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_forwardBtActionPerformed

        String currentDir = null;

        if (connected) {
            if (Trace.connectionFtp) {
                Trace.trc("Going forward to previous directory");
            }
            try {
                currentDir = ftpHandler.pwd();

                if (currentDir.equals("/")) {
                    JOptionPane.showMessageDialog(rootFrame, "Cannot go back, you are in the root directory.");
                } else if (currentDir.equals(lastKnownDir)) {
                    JOptionPane.showMessageDialog(rootFrame,
                            "Cannot go forward, " + "this is the last know directory you visited.");
                } else {
                    ftpHandler.cwd(lastKnownDir);
                    currentDir = ftpHandler.pwd();
                    String dirList = ftpHandler.list();
                    outputTextArea.setText(dirList);
                    remoteSiteText.setText(ftpHandler.pwd());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot change directories!",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_forwardBtActionPerformed

    private void rootBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rootBtActionPerformed

        final char DIR_LIMITER = '/';
        StringBuilder sb = new StringBuilder();

        if (connected) {
            if (Trace.connectionFtp) {
                Trace.trc("Returning to root directory");
            }
            try {
                String currentDir = ftpHandler.pwd();
                if (!currentDir.equals("/")) {
                    int counter = 0;

                    for (int i = 0; i < currentDir.length(); i++) {
                        if (currentDir.charAt(i) == DIR_LIMITER) {
                            counter++;
                        }
                    }
                    for (int i = 0; i < counter; i++) {
                        sb.append("../");
                    }
                    ftpHandler.cwd(sb.toString());
                    currentDir = ftpHandler.pwd();
                    String dirList = ftpHandler.list();
                    outputTextArea.setText(dirList);
                    remoteSiteText.setText(ftpHandler.pwd());
                } else {
                    JOptionPane.showMessageDialog(rootFrame, "Cannot go to root, you already here.");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot change directories!",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_rootBtActionPerformed

    private void createDirBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createDirBtActionPerformed

        String dirName = createDirText.getText().trim();
        boolean hasMadeDir = false;

        if (connected) {
            if (Trace.connectionFtp) {
                Trace.trc("Creating new directory: " + dirName);
            }

            try {
                hasMadeDir = ftpHandler.mkd(dirName);
                if (hasMadeDir) {
                    JOptionPane.showMessageDialog(rootFrame, "Created new directory " + dirName + ".");
                    ftpHandler.pwd();
                    String dirList = ftpHandler.list();
                    outputTextArea.setText(dirList);
                    createDirText.setText("");
                } else {
                    JOptionPane.showMessageDialog(rootFrame, "Could not create new directory!", "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot create a new directory!",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_createDirBtActionPerformed

    private void deleteFileBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteFileBtActionPerformed
        String fileName = deleteFileText.getText().trim();
        boolean hasDeleteFile = false;

        if (connected) {
            if (Trace.connectionFtp) {
                Trace.trc("Deleting file: " + fileName);
            }

            try {
                hasDeleteFile = ftpHandler.dele(fileName);
                if (hasDeleteFile) {
                    JOptionPane.showMessageDialog(rootFrame, "Deleted file " + fileName + ".");
                    ftpHandler.pwd();
                    String dirList = ftpHandler.list();
                    outputTextArea.setText(dirList);
                    deleteFileText.setText("");
                } else {
                    JOptionPane.showMessageDialog(rootFrame, "Could not delete file!", "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot delete file!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_deleteFileBtActionPerformed

    private void deleteDirBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteDirBtActionPerformed
        String dirName = deleteDirText.getText().trim();
        boolean hasDeletedDir = false;

        if (connected) {
            if (Trace.connectionFtp) {
                Trace.trc("Deleting directory: " + dirName);
            }

            try {
                hasDeletedDir = ftpHandler.rmd(dirName);
                if (hasDeletedDir) {
                    JOptionPane.showMessageDialog(rootFrame, "Deleted directory " + dirName + ".");
                    ftpHandler.pwd();
                    String dirList = ftpHandler.list();
                    outputTextArea.setText(dirList);
                    deleteDirText.setText("");
                } else {
                    JOptionPane.showMessageDialog(rootFrame, "Could not delete directory!", "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot delete directory!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_deleteDirBtActionPerformed

    private void refreshBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshBtActionPerformed

        if (connected) {
            try {
                ftpHandler.pwd();
                String dirList = ftpHandler.list();
                outputTextArea.setText(dirList);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            JOptionPane.showMessageDialog(rootFrame, "Not connected to a server, cannot refresh directory!",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_refreshBtActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FtpDialog.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FtpDialog.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FtpDialog.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FtpDialog.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FtpDialog().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backBt;
    private javax.swing.JLabel backLabel;
    private javax.swing.JButton cdDirBt;
    private javax.swing.JLabel cdDirLabel;
    private javax.swing.JTextField cdDirText;
    private javax.swing.JButton closeBt;
    private javax.swing.JPanel commandPanel;
    private javax.swing.JLabel completLb;
    private javax.swing.JLabel completRsLb;
    private javax.swing.JButton createDirBt;
    private javax.swing.JTextField createDirText;
    private javax.swing.JButton deleteDirBt;
    private javax.swing.JLabel deleteDirLb;
    private javax.swing.JTextField deleteDirText;
    private javax.swing.JButton deleteFileBt;
    private javax.swing.JLabel deleteFileLb;
    private javax.swing.JTextField deleteFileText;
    private javax.swing.JButton disconnectBt;
    private javax.swing.JButton fileDlBt;
    private javax.swing.JLabel fileDlLabel;
    private javax.swing.JTextField fileDlText;
    private javax.swing.JLabel fileNameLb;
    private javax.swing.JLabel fileNameRsLb;
    private javax.swing.JLabel fileSizeLb;
    private javax.swing.JLabel fileSizeRsLb;
    private javax.swing.JButton forwardBt;
    private javax.swing.JLabel forwardLabel;
    private javax.swing.JTextField hostAddressField;
    private javax.swing.JLabel hostAddressLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton loginBt;
    private javax.swing.JPanel loginDialog;
    private javax.swing.JPanel navigationPanel;
    private javax.swing.JPanel outputPanel;
    private javax.swing.JTextArea outputTextArea;
    private javax.swing.JPasswordField passField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel portNumberLabel;
    private javax.swing.JSpinner portNumberSp;
    private javax.swing.JDialog progressBarDialog;
    private javax.swing.JLabel progressBarLb;
    private javax.swing.JPanel progressBarPanel;
    private javax.swing.JButton refreshBt;
    private javax.swing.JLabel refreshLb;
    private javax.swing.JPanel remoteSitePanel;
    private javax.swing.JTextField remoteSiteText;
    private javax.swing.JButton rootBt;
    private javax.swing.JLabel rootLabel;
    private javax.swing.JButton uploadFileBt;
    private javax.swing.JLabel uploadFileLb;
    private javax.swing.JProgressBar uploadProgressBar;
    private javax.swing.JTextField userNameField;
    private javax.swing.JLabel userNameLabel;
    // End of variables declaration//GEN-END:variables
}