Package com.jprotectfile.view

Source Code of com.jprotectfile.view.MainView

package com.jprotectfile.view;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

import com.jprotectfile.model.FileEncryptor;
import com.jprotectfile.model.NullLayout;
import com.jprotectfile.model.Utilities;

public class MainView extends JFrame {
  private static final long serialVersionUID = 1L;
  private JTextField jtfInputFile;
  private JTextField jtfOutputFile;
  private JPasswordField jtfPassword;
  private JButton jbSelectInputFile;
  private JButton jbSelectOutputFile;
  private JProgressBar bar;
  private JButton jbEncrypt;
  private JButton jbDecrypt;
 
  public MainView(){
    super("JProtectFile");
    this.setSize(640, 220);
    this.setPreferredSize(this.getSize());
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new BorderLayout());
    Utilities.centralize(this);
   
    this.add(this.getNorthPane(),BorderLayout.NORTH);
    this.add(this.getCenterPane(),BorderLayout.CENTER);
    this.add(this.getSouthPane(),BorderLayout.SOUTH);
  }
 
  public void validateForm() throws IllegalArgumentException {
    String inputFileString = this.jtfInputFile.getText();
    String outputFileString = this.jtfOutputFile.getText();
    String password = new String(this.jtfPassword.getPassword());
    File inputFile = new File(inputFileString);
   
    if(inputFileString == null || inputFileString.trim().isEmpty()){
      throw new IllegalArgumentException("Arquivo de entrada vazio");
    }
    if(outputFileString == null || outputFileString.trim().isEmpty()){
      throw new IllegalArgumentException("Arquivo de sa�da vazio");
    }
    if(!inputFile.isFile()){
      throw new IllegalArgumentException("Arquivo de entrada n�o � um arquivo!");
    }
    if(!inputFile.exists()){
      throw new IllegalArgumentException("Arquivo de entrada n�o existe!");
    }
    if(password == null || password.isEmpty()){
      throw new IllegalArgumentException("Senha vazia!");
    }
  }
 
  public void criptografar(){
    try {
      this.validateForm();
    }catch(IllegalArgumentException e){
      JOptionPane.showMessageDialog(this, e.getMessage());
      return;
    }
   
    if(!this.jbEncrypt.isEnabled() || !this.jbDecrypt.isEnabled()){
      JOptionPane.showMessageDialog(this, "Uma solicita��o de criptografia / descriptografia ainda esta em execu��o!\n" +
          "Aguarde e tente novamente mais tarde.");
      return;
    }
     
    this.jbEncrypt.setEnabled(false);
    this.jbDecrypt.setEnabled(false);
    this.bar.setIndeterminate(true);
   
    Thread runner = new Thread(){
      public void run(){
        FileEncryptor enc = new FileEncryptor(new String(MainView.this.jtfPassword.getPassword()));
        File inputFile = new File(MainView.this.jtfInputFile.getText());
        File outputFile = new File(MainView.this.jtfOutputFile.getText());
        try {
          FileInputStream file1 = new FileInputStream(inputFile);
          FileOutputStream file2 = new FileOutputStream(outputFile);
          enc.encrypt(file1, file2);
        } catch (Exception e) {
          JOptionPane.showMessageDialog(MainView.this, "Erro em criptografar: "+e.getMessage());
        }
        MainView.this.jbEncrypt.setEnabled(true);
        MainView.this.jbDecrypt.setEnabled(true);
        MainView.this.bar.setIndeterminate(false);
      }
    };
    runner.start();
  }
 
  public void descriptografar(){
    try {
      this.validateForm();
    }catch(IllegalArgumentException e){
      JOptionPane.showMessageDialog(this, e.getMessage());
      return;
    }
   
    if(!this.jbEncrypt.isEnabled() || !this.jbDecrypt.isEnabled()){
      JOptionPane.showMessageDialog(this, "Uma solicita��o de criptografia / descriptografia ainda esta em execu��o!\n" +
          "Aguarde e tente novamente mais tarde.");
      return;
    }
     
    this.jbEncrypt.setEnabled(false);
    this.jbDecrypt.setEnabled(false);
    this.bar.setIndeterminate(true);
   
    Thread runner = new Thread(){
      public void run(){
        FileEncryptor enc = new FileEncryptor(new String(MainView.this.jtfPassword.getPassword()));
        File inputFile = new File(MainView.this.jtfInputFile.getText());
        File outputFile = new File(MainView.this.jtfOutputFile.getText());
        try {
          FileInputStream file1 = new FileInputStream(inputFile);
          FileOutputStream file2 = new FileOutputStream(outputFile);
          enc.decrypt(file1, file2);
        } catch (Exception e) {
          JOptionPane.showMessageDialog(MainView.this, "Erro em descriptografar: "+e.getMessage());
        }
        MainView.this.jbEncrypt.setEnabled(true);
        MainView.this.jbDecrypt.setEnabled(true);
        MainView.this.bar.setIndeterminate(false);
      }
    };
    runner.start();
  }
 
  private JPanel getNorthPane(){
    JPanel pane = new JPanel();
    pane.setLayout(new FlowLayout(FlowLayout.LEFT));   
   
    JLabel jlTitle = new JLabel("JProtectFile");
    pane.add(jlTitle);
   
    return pane;
  }
 
  private JPanel getCenterPane(){
    JPanel pane = new JPanel();
    NullLayout layout = new NullLayout(pane);
   
    // arquivo de entrada
    JLabel jlFileInput = new JLabel("Arquivo de Entrada: ");
    this.jtfInputFile = new JTextField(12);
    this.jbSelectInputFile = new JButton("Selecionar");
    this.jbSelectInputFile.setCursor(new Cursor(Cursor.HAND_CURSOR));   
    this.jbSelectInputFile.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int op = chooser.showOpenDialog(MainView.this);
        if(op == JFileChooser.APPROVE_OPTION){
          MainView.this.jtfInputFile.setText(chooser.getSelectedFile().getAbsolutePath());
        }
      }
    });
   
    // arquivo de sa�da
    JLabel jlFileOutput = new JLabel("Arquivo de Sa�da: ");
    this.jtfOutputFile = new JTextField(12);
    this.jbSelectOutputFile = new JButton("Selecionar");
    this.jbSelectOutputFile.setCursor(new Cursor(Cursor.HAND_CURSOR));   
    this.jbSelectOutputFile.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int op = chooser.showOpenDialog(MainView.this);
        if(op == JFileChooser.APPROVE_OPTION){
          MainView.this.jtfOutputFile.setText(chooser.getSelectedFile().getAbsolutePath());
        }
      }
    });
   
    // senha
    JLabel jlPassword = new JLabel("Senha: ");
    this.jtfPassword = new JPasswordField(10);
   
    // barra de progresso
    this.bar = new JProgressBar();
   
    // adicionando no layout
    int labelWidth = 120;
    int textWidth = 390;
    int defaultHeight = 24;
   
    layout.add(jlFileInput, labelWidth, defaultHeight);
    layout.add(jtfInputFile, textWidth, defaultHeight);
    layout.add(jbSelectInputFile, 100, defaultHeight);
    layout.newLine();
    layout.add(jlFileOutput, labelWidth, defaultHeight);
    layout.add(jtfOutputFile, textWidth, defaultHeight);
    layout.add(jbSelectOutputFile, 100, defaultHeight);
    layout.newLine();
    layout.add(jlPassword,labelWidth,defaultHeight);
    layout.add(this.jtfPassword,textWidth,defaultHeight);
    layout.newLine();
    layout.add(this.bar,this.getSize().width-23,defaultHeight);
   
    return pane;
  }
 
  private JPanel getSouthPane(){
    JPanel pane = new JPanel();
    pane.setLayout(new FlowLayout(FlowLayout.RIGHT));   
   
    this.jbEncrypt = new JButton("Criptografar");
    this.jbEncrypt.setCursor(new Cursor(Cursor.HAND_CURSOR));
    this.jbEncrypt.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        criptografar();
      }
    });
    pane.add(this.jbEncrypt);
   
    this.jbDecrypt = new JButton("Descriptografar");
    this.jbDecrypt.setCursor(new Cursor(Cursor.HAND_CURSOR));
    this.jbDecrypt.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        descriptografar();
      }
    });
    pane.add(this.jbDecrypt);
   
    return pane;
  }
 
  public static void main(String[] args) {
    MainView view = new MainView();
    view.setVisible(true);
  }
}
TOP

Related Classes of com.jprotectfile.view.MainView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.