View.EmbedFile.java Source code

Java tutorial

Introduction

Here is the source code for View.EmbedFile.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 View;

import General.Configuration;
import General.Nimbus;
import db.Dbcon;
import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author Jithinpv
 */
public class EmbedFile extends javax.swing.JFrame {

    public static int process_id = 0;
    String masterFileName;

    /**
     * Creates new form EmbedFile
     */
    public EmbedFile() {
        Nimbus.loadLoogAndFeel();
        initComponents();
        this.setLocationRelativeTo(null);
        loadIcons();
        analyze_complete_label.setVisible(false);
        sucess_label.setVisible(false);
        proceed_button.setEnabled(false);
        analyze_master_file_button.setEnabled(false);
    }

    private void loadIcons() {
        Configuration.setIconOnLabel("file.png", jLabel2);
        Configuration.setIconOnLabel("sucess_icon.png", sucess_label);
        Configuration.setIconOnLabel("blue_back_ground.jpg", main_label);
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        analyze_master_file_button = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        proceed_button = new javax.swing.JButton();
        sucess_label = new javax.swing.JLabel();
        analyze_complete_label = new javax.swing.JLabel();
        progress_bar = new javax.swing.JProgressBar();
        main_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Select Master File");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(34, 41, 154, -1));

        jButton1.setText("Browse");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(247, 40, -1, -1));

        jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(143, 81, 148, 100));

        analyze_master_file_button.setText("Analyse Master File");
        analyze_master_file_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                analyze_master_file_buttonActionPerformed(evt);
            }
        });
        getContentPane().add(analyze_master_file_button,
                new org.netbeans.lib.awtextra.AbsoluteConstraints(78, 199, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Embed File");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(142, 11, 88, -1));

        jButton3.setText("BACK");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(111, 280, -1, -1));
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(297, 103, 103, -1));

        proceed_button.setText("PROCEED");
        proceed_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                proceed_buttonActionPerformed(evt);
            }
        });
        getContentPane().add(proceed_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 280, -1, -1));

        sucess_label.setFont(new java.awt.Font("Tahoma", 1, 14));
        sucess_label.setForeground(new java.awt.Color(255, 255, 255));
        getContentPane().add(sucess_label, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 190, 54, 49));

        analyze_complete_label.setFont(new java.awt.Font("Tahoma", 1, 14));
        analyze_complete_label.setForeground(new java.awt.Color(255, 255, 255));
        analyze_complete_label.setText("Analyse Complete");
        getContentPane().add(analyze_complete_label,
                new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 200, -1, -1));
        getContentPane().add(progress_bar, new org.netbeans.lib.awtextra.AbsoluteConstraints(78, 253, 322, 21));

        main_label.setText("jLabel3");
        getContentPane().add(main_label, new org.netbeans.lib.awtextra.AbsoluteConstraints(-6, -6, 420, 320));

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

    class progressThread extends Thread {

        public void run() {
            try {
                int value = 0;
                while (value <= 100) {
                    progress_bar.setValue(value);
                    value += 1;
                    Thread.sleep(10);
                }
                store_analyse_masterfile();
                proceed_button.setEnabled(true);
                analyze_complete_label.setVisible(true);
                sucess_label.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void analyze_master_file_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_analyze_master_file_buttonActionPerformed
        // TODO add your handling code here:
        new progressThread().start();

    }//GEN-LAST:event_analyze_master_file_buttonActionPerformed

    public void store_analyse_masterfile() {
        Dbcon dbcon = new Dbcon();
        String sql = "update tbl_encryption_log set analyze_started_time='" + System.currentTimeMillis()
                + "' where process_id='" + EmbedFile.process_id + "'";
        System.out.println(sql);
        dbcon.update(sql);

    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        this.dispose();
        Home home = new Home();
        home.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");

        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String path = chooser.getSelectedFile().getPath();
            jLabel2.setText(chooser.getSelectedFile().getName());
            String masterKeyBackUpName = System.currentTimeMillis() + "."
                    + FilenameUtils.getExtension(chooser.getSelectedFile().getPath());
            String masterKeyBackUpLocation = Configuration.masterPoolLocation + masterKeyBackUpName;
            masterFileName = masterKeyBackUpName;
            try {
                FileUtils.copyFile(chooser.getSelectedFile(), new File(masterKeyBackUpLocation));
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("You chose to open this file: " + path);
            BufferedImage img = null;
            try {
                img = ImageIO.read(new File(path));
                Image scaledInstance = img.getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(),
                        Image.SCALE_SMOOTH);
                ImageIcon imageIcon = new ImageIcon(scaledInstance);
                jLabel2.setIcon(imageIcon);
                analyze_master_file_button.setEnabled(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
            long size = (chooser.getSelectedFile().length()) / 1024;

            Dbcon dbcon = new Dbcon();
            int ins = dbcon.insert(
                    "insert into tbl_encryption_log(user_id,master_file,master_file_size,encryption_type)values('"
                            + Login.logged_in_user_id + "','" + path + "','" + size + "',1)");
            if (ins > 0) {
                ResultSet rs = dbcon.select("select max(process_id)  from tbl_encryption_log");
                try {
                    if (rs.next()) {
                        System.out.println(rs.getString(1));
                        EmbedFile.process_id = Integer.parseInt(rs.getString(1));
                        System.out.println(EmbedFile.process_id);

                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void proceed_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proceed_buttonActionPerformed
        // TODO add your handling code here:
        this.dispose();
        FileEncryption fileEncryption = new FileEncryption(masterFileName);
        fileEncryption.setVisible(true);
    }//GEN-LAST:event_proceed_buttonActionPerformed

    /**
     * @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(EmbedFile.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EmbedFile.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EmbedFile.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EmbedFile.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new EmbedFile().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel analyze_complete_label;
    private javax.swing.JButton analyze_master_file_button;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel main_label;
    private javax.swing.JButton proceed_button;
    private javax.swing.JProgressBar progress_bar;
    private javax.swing.JLabel sucess_label;
    // End of variables declaration//GEN-END:variables
}