view.CreateNewPresentation.java Source code

Java tutorial

Introduction

Here is the source code for view.CreateNewPresentation.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 db.Dbcon;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

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

    public String filePath = "";
    public String fileName = "";
    String newFileName = null;
    public static long size;

    /**
     * Creates new form CreateNewPresentation
     */
    public CreateNewPresentation() {
        initComponents();
        this.setLocationRelativeTo(null);
        ok_button.setEnabled(false);
        Configuration.setIconOnLabel("presentation.jpg", jLabel4);
    }

    /**
     * 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();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        ok_button = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        presenetation_name_text = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = 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, 11)); // NOI18N
        jLabel1.setText("CREATE NEW PRESENTATION");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(88, 22, 213, -1));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Choose File:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(43, 71, 109, 25));

        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(257, 71, -1, 25));

        ok_button.setText("OK");
        ok_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ok_buttonActionPerformed(evt);
            }
        });
        getContentPane().add(ok_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(224, 163, 100, 25));

        jButton3.setText("HOME");
        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(43, 163, 100, 25));
        getContentPane().add(presenetation_name_text,
                new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 114, 154, 25));

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Presentation Name");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(43, 114, 109, 25));

        jLabel4.setText("jLabel4");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(-6, -6, 380, 230));

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

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

    private void ok_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ok_buttonActionPerformed
        // TODO add your handling code here:
        Dbcon dbcon = new Dbcon();
        String presentationName = presenetation_name_text.getText().trim();
        if (presentationName.equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Please enter presentation name");
            return;
        }

        if (newFileName == null) {
            JOptionPane.showMessageDialog(rootPane, "Please choose a video presentation");
            return;
        }

        int ins = dbcon.insert("insert into tbl_create_presentation(" + " name," + " file_name," + " file_size,"
                + " created_at)values('" + presentationName + "', '" + newFileName + "','" + size + "','"
                + System.currentTimeMillis() + "')");
        if (ins > 0) {
            this.dispose();
            AnalyzeFile analyzeFile = new AnalyzeFile(newFileName, size + "", presentationName);
            analyzeFile.setVisible(true);
        }

    }//GEN-LAST:event_ok_buttonActionPerformed

    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("Vedio files", "avi", "mov", "3gp", "mp4",
                "mpg");

        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            filePath = chooser.getSelectedFile().getPath();
            if (FilenameUtils.getExtension(filePath).equals("avi")
                    || FilenameUtils.getExtension(filePath).equals("mov")
                    || FilenameUtils.getExtension(filePath).equals("3gp")
                    || FilenameUtils.getExtension(filePath).equals("mp4")
                    || FilenameUtils.getExtension(filePath).equals("txt")) {
                fileName = chooser.getSelectedFile().getName();
                BufferedImage img = null;
                size = (chooser.getSelectedFile().length()) / 1024;
                ok_button.setEnabled(true);
                newFileName = System.currentTimeMillis() + "." + FilenameUtils.getExtension(fileName);
                File newFile = new File(Configuration.presentationFolder + newFileName);

                try {
                    FileUtils.copyFile(chooser.getSelectedFile(), newFile);

                } catch (Exception e) {
                    System.out.println(e);
                }
            } else {
                JOptionPane.showMessageDialog(rootPane, "Please choose a vedio presentation");
                return;
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

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

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton ok_button;
    private javax.swing.JTextField presenetation_name_text;
    // End of variables declaration//GEN-END:variables
}