Guest User

viewDekripsiFile

a guest
Apr 23rd, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.18 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package view;
  7.  
  8. import java.awt.Color;
  9. import java.io.File;
  10. import javax.swing.JFileChooser;
  11. import javax.swing.filechooser.FileFilter;
  12. import java.awt.Dimension;
  13. import java.awt.Toolkit;
  14. import javax.swing.JOptionPane;
  15. import enkripsi.Enkrip;
  16. import java.io.FileOutputStream;
  17. import javax.swing.JFrame;
  18. import controller.controllerTeruskan;
  19.  
  20. public class viewEnkripsiFile extends javax.swing.JDialog {
  21. private final Enkrip enkripsi = new Enkrip();
  22. private final controllerTeruskan terus = new controllerTeruskan();
  23. private JFileChooser jfc;
  24. private static File file;
  25. private static String ekstensi;
  26.    
  27.     public viewEnkripsiFile(java.awt.Frame parent, boolean modal) {
  28.         super(parent, modal);
  29.         initComponents();
  30.         jLabel2.setVisible(false);
  31.         jLabel3.setVisible(false);
  32.         passwordField.setVisible(false);
  33.         confirmPasswordField.setVisible(false);
  34.         aturFrame(0.85,true);
  35.         file=null;
  36.         passwordField.setText(terus.getPassword());
  37.         confirmPasswordField.setText(terus.getPassword());
  38.     }
  39.  
  40.            private void aturFrame(double skala, boolean tengah){
  41.         Dimension dimensi = Toolkit.getDefaultToolkit().getScreenSize();
  42.  
  43.    
  44.     if (tengah){
  45.     setLocation ((int) ((dimensi.getWidth()-getWidth())/2),
  46.     (int)((dimensi.getHeight()-getHeight())/2));
  47.     }
  48. }
  49.  
  50.     /**
  51.      * This method is called from within the constructor to initialize the form.
  52.      * WARNING: Do NOT modify this code. The content of this method is always
  53.      * regenerated by the Form Editor.
  54.      */
  55.     @SuppressWarnings("unchecked")
  56.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  57.     private void initComponents() {
  58.  
  59.         panel1 = new editSwing.Panel();
  60.         okButton = new javax.swing.JButton();
  61.         fileLabel = new javax.swing.JLabel();
  62.         jLabel2 = new javax.swing.JLabel();
  63.         passwordField = new javax.swing.JPasswordField();
  64.         jButton4 = new javax.swing.JButton();
  65.         confirmPasswordField = new javax.swing.JPasswordField();
  66.         jLabel3 = new javax.swing.JLabel();
  67.         miripLabel = new javax.swing.JLabel();
  68.         jSeparator1 = new javax.swing.JSeparator();
  69.         jLabel4 = new javax.swing.JLabel();
  70.  
  71.         setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  72.         setTitle("Enkripsi");
  73.         setResizable(false);
  74.  
  75.         okButton.setText("Ok");
  76.         okButton.addActionListener(new java.awt.event.ActionListener() {
  77.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  78.                 okButtonActionPerformed(evt);
  79.             }
  80.         });
  81.  
  82.         fileLabel.setBackground(new java.awt.Color(204, 204, 204));
  83.         fileLabel.setText("No File");
  84.         fileLabel.setOpaque(true);
  85.  
  86.         jLabel2.setText("Password :");
  87.  
  88.         passwordField.setEnabled(false);
  89.  
  90.         jButton4.setText("Choose File");
  91.         jButton4.addActionListener(new java.awt.event.ActionListener() {
  92.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  93.                 jButton4ActionPerformed(evt);
  94.             }
  95.         });
  96.  
  97.         confirmPasswordField.setEnabled(false);
  98.         confirmPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
  99.             public void keyReleased(java.awt.event.KeyEvent evt) {
  100.                 confirmPasswordFieldKeyReleased(evt);
  101.             }
  102.         });
  103.  
  104.         jLabel3.setText("Confrim Password :");
  105.  
  106.         miripLabel.setText("Password file sama dengan password yang di masukan sebelumnya");
  107.  
  108.         jLabel4.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
  109.         jLabel4.setForeground(new java.awt.Color(255, 255, 255));
  110.         jLabel4.setText("Enkripsi File");
  111.  
  112.         javax.swing.GroupLayout panel1Layout = new javax.swing.GroupLayout(panel1);
  113.         panel1.setLayout(panel1Layout);
  114.         panel1Layout.setHorizontalGroup(
  115.             panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  116.             .addComponent(jSeparator1)
  117.             .addGroup(panel1Layout.createSequentialGroup()
  118.                 .addContainerGap()
  119.                 .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  120.                     .addComponent(jLabel4)
  121.                     .addGroup(panel1Layout.createSequentialGroup()
  122.                         .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  123.                             .addComponent(jLabel3)
  124.                             .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
  125.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  126.                         .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  127.                             .addComponent(miripLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  128.                             .addComponent(confirmPasswordField)
  129.                             .addComponent(passwordField)))
  130.                     .addGroup(panel1Layout.createSequentialGroup()
  131.                         .addComponent(jButton4)
  132.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  133.                         .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  134.                             .addComponent(okButton)
  135.                             .addComponent(fileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE))))
  136.                 .addContainerGap(48, Short.MAX_VALUE))
  137.         );
  138.         panel1Layout.setVerticalGroup(
  139.             panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  140.             .addGroup(panel1Layout.createSequentialGroup()
  141.                 .addGap(21, 21, 21)
  142.                 .addComponent(jLabel4)
  143.                 .addGap(17, 17, 17)
  144.                 .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
  145.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  146.                 .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  147.                     .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  148.                     .addComponent(jLabel2))
  149.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  150.                 .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  151.                     .addComponent(confirmPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  152.                     .addComponent(jLabel3))
  153.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  154.                 .addComponent(miripLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
  155.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  156.                 .addGroup(panel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  157.                     .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  158.                     .addComponent(fileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  159.                 .addGap(18, 18, 18)
  160.                 .addComponent(okButton)
  161.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  162.         );
  163.  
  164.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  165.         getContentPane().setLayout(layout);
  166.         layout.setHorizontalGroup(
  167.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  168.             .addGroup(layout.createSequentialGroup()
  169.                 .addComponent(panel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  170.                 .addGap(0, 0, Short.MAX_VALUE))
  171.         );
  172.         layout.setVerticalGroup(
  173.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  174.             .addComponent(panel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  175.         );
  176.  
  177.         pack();
  178.     }// </editor-fold>                        
  179.  
  180.     private void confirmPasswordFieldKeyReleased(java.awt.event.KeyEvent evt) {                                                
  181.         // TODO add your handling code here:
  182.        
  183.         if(!passwordField.getText().equals(confirmPasswordField.getText())){
  184.         miripLabel.setText("Tidak Valid");
  185.         miripLabel.setForeground(Color.red);
  186.         }else{
  187.          miripLabel.setForeground(Color.black);
  188.          miripLabel.setText("Valid");
  189.         }
  190.     }                                                
  191.  
  192.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
  193.         // TODO add your handling code here:
  194.             jfc=new JFileChooser();
  195.            // FileFilter filter1 = new ExtensionFileFilter("Doukumen", new String[] {"DOC", "DOCX","PDF","TXT"});
  196.           //  jfc.setFileFilter(filter1);
  197.             jfc.setAcceptAllFileFilterUsed(false);
  198.             if(JFileChooser.APPROVE_OPTION==jfc.showOpenDialog(fileLabel)){
  199.             fileLabel.setText(jfc.getSelectedFile().getPath());
  200.             file=new File(jfc.getSelectedFile().getPath());
  201.                
  202.                 String namaFile = jfc.getSelectedFile().getPath();
  203.                 int a = namaFile.lastIndexOf('.');
  204.                 int p = Math.max(namaFile.lastIndexOf('/'), namaFile.lastIndexOf('\\'));
  205.             if (a > p) {
  206.                 ekstensi =namaFile.substring(a+1);
  207.             }
  208.               }
  209.     }                                        
  210. /**/
  211.  
  212.     private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                        
  213.         Thread treet = new Thread(){
  214.         public void run(){    
  215.         byte [] outputByte = null;
  216.         String password = terus.getPassword();
  217.         outputByte = enkripsi.enkripFile(file, password);
  218.         if(outputByte!=null){
  219.         jfc = new javax.swing.JFileChooser();
  220.         JFrame parentFrame = new JFrame();
  221.         int userSelection = jfc.showSaveDialog(parentFrame);                      
  222.         if(userSelection == JFileChooser.APPROVE_OPTION){
  223.                 String extension = "";
  224.                 String namaFile = "";
  225.                 int a = namaFile.lastIndexOf('.');
  226.                 int p = Math.max(namaFile.lastIndexOf('/'), namaFile.lastIndexOf('\\'));
  227.             if (a > p) {
  228.                 extension =namaFile.substring(a+1);
  229.             }
  230.             int lengtExtensionFile =extension.length();
  231.             String pathUser="";  
  232.             int lastSelected =jfc.getSelectedFile().getAbsolutePath().length()-lengtExtensionFile;
  233.             String selectExtension =jfc.getSelectedFile().getAbsolutePath().substring(lastSelected);
  234.             String ext ="";
  235.             if(!extension.equals(selectExtension)){
  236.             }
  237.             switch(ekstensi)
  238.             {
  239.                 case "pdf":ext="."+"pdf";break;
  240.                 case "doc":ext="."+"doc";break;
  241.                 case "docx":ext="."+"docx";break;
  242.                 case "xls":ext="."+"xls";break;
  243.                 case "xlsx":ext="."+"xlsx";break;
  244.                 case "pptx":ext="."+"pptx";break;
  245.                 default :ext="."+"etc";break;
  246.             }
  247.             pathUser=jfc.getSelectedFile().getAbsolutePath()+ext;
  248.             try{
  249.                 File fileSave = new File(pathUser);    
  250.                 FileOutputStream outputStream = new FileOutputStream(fileSave);
  251.                 outputStream.write(outputByte);
  252.                 outputStream.close();
  253.                 file =fileSave;
  254.                 JOptionPane.showMessageDialog(null,"File Berhasil Di Enkripsi","Informasi",JOptionPane.INFORMATION_MESSAGE);
  255.                
  256.                 //  START: Fungsi asli tombol ok
  257.                 if(file!=null){
  258.                 path=file.getAbsolutePath();
  259.                 }
  260.                 if(file==null){
  261.                 JOptionPane.showMessageDialog(null,"Belum Memilih File","Informasi",JOptionPane.INFORMATION_MESSAGE);
  262.                 return;
  263.                 }      
  264.                 dispose();
  265.                 //  END: Fungsi asli tombol ok
  266.                
  267.             }catch(Exception e){
  268.                 JOptionPane.showMessageDialog(null, e.getMessage(),pathUser,JOptionPane.INFORMATION_MESSAGE);
  269.             }
  270.         }
  271.         }else{
  272.         JOptionPane.showMessageDialog(null,enkripsi.getPesanKesalahan(),"Informasi",JOptionPane.INFORMATION_MESSAGE);
  273.         }
  274.        }
  275.     };treet.start();
  276.     }                                        
  277. /**/
  278.     private String path="";
  279.  
  280.     public String getPath() {
  281.         return path;
  282.     }
  283.  
  284.  
  285.    
  286.     /**
  287.      * @param args the command line arguments
  288.      */
  289.     public static void main(String args[]) {
  290.         /* Set the Nimbus look and feel */
  291.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  292.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  293.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  294.          */
  295.         try {
  296.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  297.                 if ("Nimbus".equals(info.getName())) {
  298.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  299.                     break;
  300.                 }
  301.             }
  302.         } catch (ClassNotFoundException ex) {
  303.             java.util.logging.Logger.getLogger(viewEnkripsiFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  304.         } catch (InstantiationException ex) {
  305.             java.util.logging.Logger.getLogger(viewEnkripsiFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  306.         } catch (IllegalAccessException ex) {
  307.             java.util.logging.Logger.getLogger(viewEnkripsiFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  308.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  309.             java.util.logging.Logger.getLogger(viewEnkripsiFile.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  310.         }
  311.         //</editor-fold>
  312.         //</editor-fold>
  313.  
  314.         /* Create and display the dialog */
  315.         java.awt.EventQueue.invokeLater(new Runnable() {
  316.             public void run() {
  317.                 viewEnkripsiFile dialog = new viewEnkripsiFile(new javax.swing.JFrame(), true);
  318.                 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
  319.                     @Override
  320.                     public void windowClosing(java.awt.event.WindowEvent e) {
  321.                         System.exit(0);
  322.                     }
  323.                 });
  324.                 dialog.setVisible(true);
  325.             }
  326.         });
  327.     }
  328.  
  329.     // Variables declaration - do not modify                    
  330.     private javax.swing.JPasswordField confirmPasswordField;
  331.     private javax.swing.JLabel fileLabel;
  332.     private javax.swing.JButton jButton4;
  333.     private javax.swing.JLabel jLabel2;
  334.     private javax.swing.JLabel jLabel3;
  335.     private javax.swing.JLabel jLabel4;
  336.     private javax.swing.JSeparator jSeparator1;
  337.     private javax.swing.JLabel miripLabel;
  338.     private javax.swing.JButton okButton;
  339.     private editSwing.Panel panel1;
  340.     private javax.swing.JPasswordField passwordField;
  341.     // End of variables declaration                  
  342. }
  343. class ExtensionFileFilter extends FileFilter {
  344.   String description;
  345.  
  346.   String extensions[];
  347.  
  348.   public ExtensionFileFilter(String description, String extension) {
  349.     this(description, new String[] { extension });
  350.   }
  351.  
  352.   public ExtensionFileFilter(String description, String extensions[]) {
  353.     if (description == null) {
  354.       this.description = extensions[0];
  355.     } else {
  356.       this.description = description;
  357.     }
  358.     this.extensions = (String[]) extensions.clone();
  359.     toLower(this.extensions);
  360.   }
  361.  
  362.   private void toLower(String array[]) {
  363.     for (int i = 0, n = array.length; i < n; i++) {
  364.       array[i] = array[i].toLowerCase();
  365.     }
  366.   }
  367.  
  368.   public String getDescription() {
  369.     return description;
  370.   }
  371.  
  372.   public boolean accept(File file) {
  373.     if (file.isDirectory()) {
  374.       return true;
  375.     } else {
  376.       String path = file.getAbsolutePath().toLowerCase();
  377.       for (int i = 0, n = extensions.length; i < n; i++) {
  378.         String extension = extensions[i];
  379.         if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
  380.           return true;
  381.         }
  382.       }
  383.     }
  384.     return false;
  385.   }
  386. }
Add Comment
Please, Sign In to add comment