/*
 * MigrationWindow.java
 *
 * Created on 2008. február 26., 21:52
 */

package hu.nir.nmail.migration.gui.swing;

import hu.nir.nmail.migration.DatabaseManager;
import hu.nir.nmail.migration.MigrationManager;
import java.awt.Color;

/**
 *
 * @author  attila
 */
public class MigrationWindow extends javax.swing.JFrame {
    private DatabaseManager oldDB;
    private DatabaseManager newDB;
    /** Creates new form MigrationWindow */
    public MigrationWindow() {
        initComponents();
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        title_label = new javax.swing.JLabel();
        subtitle_label = new javax.swing.JLabel();
        olddb_panel = new javax.swing.JPanel();
        oldhost_label = new javax.swing.JLabel();
        old_host = new javax.swing.JTextField();
        oldport_label = new javax.swing.JLabel();
        old_port = new javax.swing.JTextField();
        olduser_label = new javax.swing.JLabel();
        password_label = new javax.swing.JLabel();
        old_account = new javax.swing.JTextField();
        old_password = new javax.swing.JPasswordField();
        old_test_btn = new javax.swing.JButton();
        olddbname_label = new javax.swing.JLabel();
        olddb_name = new javax.swing.JTextField();
        newdb_panel = new javax.swing.JPanel();
        newhost_label = new javax.swing.JLabel();
        new_host = new javax.swing.JTextField();
        newport_label = new javax.swing.JLabel();
        new_port = new javax.swing.JTextField();
        newuser_label = new javax.swing.JLabel();
        newpassword_label = new javax.swing.JLabel();
        new_account = new javax.swing.JTextField();
        new_password = new javax.swing.JPasswordField();
        new_test_btn = new javax.swing.JButton();
        newdbname_label = new javax.swing.JLabel();
        newdb_name = new javax.swing.JTextField();
        bottom_panel = new javax.swing.JPanel();
        main_pb = new javax.swing.JProgressBar();
        migrate_btn = new javax.swing.JButton();
        address_pb = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        title_label.setFont(new java.awt.Font("Century Schoolbook L", 3, 16));
        title_label.setText("Nmail Migration Tool");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        getContentPane().add(title_label, gridBagConstraints);

        subtitle_label.setFont(new java.awt.Font("Century Schoolbook L", 3, 12));
        subtitle_label.setText("from 1.0 to 2.0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 20, 0);
        getContentPane().add(subtitle_label, gridBagConstraints);

        olddb_panel.setBorder(javax.swing.BorderFactory.createTitledBorder("Old database connection settings"));
        olddb_panel.setLayout(new java.awt.GridBagLayout());

        oldhost_label.setText("Host:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(oldhost_label, gridBagConstraints);

        old_host.setText("localhost");
        old_host.setMinimumSize(new java.awt.Dimension(100, 18));
        old_host.setPreferredSize(new java.awt.Dimension(100, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(old_host, gridBagConstraints);

        oldport_label.setText("Port:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(oldport_label, gridBagConstraints);

        old_port.setText("5432");
        old_port.setMinimumSize(new java.awt.Dimension(32, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(old_port, gridBagConstraints);

        olduser_label.setText("Account:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(olduser_label, gridBagConstraints);

        password_label.setText("Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(password_label, gridBagConstraints);

        old_account.setMinimumSize(new java.awt.Dimension(100, 18));
        old_account.setPreferredSize(new java.awt.Dimension(100, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(old_account, gridBagConstraints);

        old_password.setMinimumSize(new java.awt.Dimension(100, 18));
        old_password.setPreferredSize(new java.awt.Dimension(100, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(old_password, gridBagConstraints);

        old_test_btn.setText("Test");
        old_test_btn.setBorderPainted(false);
        old_test_btn.setMaximumSize(new java.awt.Dimension(60, 30));
        old_test_btn.setMinimumSize(new java.awt.Dimension(60, 30));
        old_test_btn.setPreferredSize(new java.awt.Dimension(70, 30));
        old_test_btn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                old_test_btnMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                old_test_btnMouseEntered(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        olddb_panel.add(old_test_btn, gridBagConstraints);

        olddbname_label.setText("Database:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(olddbname_label, gridBagConstraints);

        olddb_name.setText("mailds");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        olddb_panel.add(olddb_name, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(olddb_panel, gridBagConstraints);

        newdb_panel.setBorder(javax.swing.BorderFactory.createTitledBorder("New database connection settings"));
        newdb_panel.setLayout(new java.awt.GridBagLayout());

        newhost_label.setText("Host:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(newhost_label, gridBagConstraints);

        new_host.setText("localhost");
        new_host.setMinimumSize(new java.awt.Dimension(100, 18));
        new_host.setPreferredSize(new java.awt.Dimension(100, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(new_host, gridBagConstraints);

        newport_label.setText("Port:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(newport_label, gridBagConstraints);

        new_port.setText("5432");
        new_port.setMinimumSize(new java.awt.Dimension(32, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(new_port, gridBagConstraints);

        newuser_label.setText("Account:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(newuser_label, gridBagConstraints);

        newpassword_label.setText("Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(newpassword_label, gridBagConstraints);

        new_account.setMinimumSize(new java.awt.Dimension(100, 18));
        new_account.setPreferredSize(new java.awt.Dimension(100, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(new_account, gridBagConstraints);

        new_password.setMinimumSize(new java.awt.Dimension(100, 18));
        new_password.setPreferredSize(new java.awt.Dimension(100, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(new_password, gridBagConstraints);

        new_test_btn.setText("Test");
        new_test_btn.setBorderPainted(false);
        new_test_btn.setMaximumSize(new java.awt.Dimension(60, 30));
        new_test_btn.setMinimumSize(new java.awt.Dimension(60, 30));
        new_test_btn.setPreferredSize(new java.awt.Dimension(70, 30));
        new_test_btn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                new_test_btnMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                new_test_btnMouseEntered(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
        newdb_panel.add(new_test_btn, gridBagConstraints);

        newdbname_label.setText("Database:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(newdbname_label, gridBagConstraints);

        newdb_name.setText("nmail");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        newdb_panel.add(newdb_name, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(newdb_panel, gridBagConstraints);

        bottom_panel.setLayout(new java.awt.GridBagLayout());

        main_pb.setMinimumSize(new java.awt.Dimension(300, 14));
        main_pb.setPreferredSize(new java.awt.Dimension(300, 14));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        bottom_panel.add(main_pb, gridBagConstraints);

        migrate_btn.setText("Migrate");
        migrate_btn.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                migrate_btnMouseClicked(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        bottom_panel.add(migrate_btn, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        bottom_panel.add(address_pb, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(bottom_panel, gridBagConstraints);

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

    private void old_test_btnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_old_test_btnMouseClicked
        try {
            oldDB = new DatabaseManager(old_host.getText(), 
                                           Integer.valueOf(old_port.getText()), 
                                           olddb_name.getText(), 
                                           old_account.getText(), 
                                           new String(old_password.getPassword()));
            if (oldDB.testConnection()) { 
                old_test_btn.setText("Ok");
            }
            else {
                old_test_btn.setText("Wrong");
            }
            old_port.setBackground(Color.WHITE);
        } catch (NumberFormatException ex) {
            old_port.setBackground(Color.PINK);
        }
    }//GEN-LAST:event_old_test_btnMouseClicked

    private void old_test_btnMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_old_test_btnMouseEntered
        old_test_btn.setText("Test");
    }//GEN-LAST:event_old_test_btnMouseEntered

    private void new_test_btnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_new_test_btnMouseClicked
        try {
            newDB = new DatabaseManager(new_host.getText(), 
                                           Integer.valueOf(new_port.getText()), 
                                           newdb_name.getText(), 
                                           new_account.getText(), 
                                           new String(new_password.getPassword()));
            if (newDB.testConnection()) { 
                new_test_btn.setText("Ok");
            }
            else {
                new_test_btn.setText("Wrong");
            }
            new_port.setBackground(Color.WHITE);
        } catch (NumberFormatException ex) {
            new_port.setBackground(Color.PINK);
        }
    }//GEN-LAST:event_new_test_btnMouseClicked

    private void new_test_btnMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_new_test_btnMouseEntered
        new_test_btn.setText("Test");
    }//GEN-LAST:event_new_test_btnMouseEntered

    private void migrate_btnMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_migrate_btnMouseClicked
        migrate_btn.setEnabled(false);
        MigrationManager migrationManager = new MigrationManager(oldDB, newDB);
        migrationManager.migrate(main_pb, address_pb);
    }//GEN-LAST:event_migrate_btnMouseClicked

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar address_pb;
    private javax.swing.JPanel bottom_panel;
    private javax.swing.JProgressBar main_pb;
    private javax.swing.JButton migrate_btn;
    private javax.swing.JTextField new_account;
    private javax.swing.JTextField new_host;
    private javax.swing.JPasswordField new_password;
    private javax.swing.JTextField new_port;
    private javax.swing.JButton new_test_btn;
    private javax.swing.JTextField newdb_name;
    private javax.swing.JPanel newdb_panel;
    private javax.swing.JLabel newdbname_label;
    private javax.swing.JLabel newhost_label;
    private javax.swing.JLabel newpassword_label;
    private javax.swing.JLabel newport_label;
    private javax.swing.JLabel newuser_label;
    private javax.swing.JTextField old_account;
    private javax.swing.JTextField old_host;
    private javax.swing.JPasswordField old_password;
    private javax.swing.JTextField old_port;
    private javax.swing.JButton old_test_btn;
    private javax.swing.JTextField olddb_name;
    private javax.swing.JPanel olddb_panel;
    private javax.swing.JLabel olddbname_label;
    private javax.swing.JLabel oldhost_label;
    private javax.swing.JLabel oldport_label;
    private javax.swing.JLabel olduser_label;
    private javax.swing.JLabel password_label;
    private javax.swing.JLabel subtitle_label;
    private javax.swing.JLabel title_label;
    // End of variables declaration//GEN-END:variables
    
}
