Package view

Source Code of view.Window

package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import application.RenameType;
import application.Renamer;

public class Window extends JFrame implements ActionListener{
 
  public Window() {
    initComponents();
   
    debug = true;
  }

  /**
   * M�todo para a inicializa��o dos componentes da janela
   */
  private void initComponents() {
    getContentPane().setLayout(null);
   
    JLabel labelTipo = new JLabel("Tipo de Renomea\u00E7�o");
    labelTipo.setBounds(10, 20, 109, 14);
    getContentPane().add(labelTipo);
   
    comboBoxType = new JComboBox<RenameType>(RenameType.values());
    comboBoxType.setBounds(154, 17, 280, 20);
    getContentPane().add(comboBoxType);
   
    JLabel labelSource = new JLabel("Pasta Origem");
    labelSource.setBounds(10, 51, 109, 14);
    getContentPane().add(labelSource);
   
    JLabel labelDestination = new JLabel("Pasta Destino");
    labelDestination.setBounds(10, 82, 109, 14);
    getContentPane().add(labelDestination);
   
    sourceTextField = new JTextField();
    sourceTextField.setBounds(154, 48, 190, 20);
    sourceTextField.setEnabled(false);
    getContentPane().add(sourceTextField);
    sourceTextField.setColumns(10);
   
    destinationTextField = new JTextField();
    destinationTextField.setBounds(154, 79, 190, 20);
    destinationTextField.setEnabled(false);
    getContentPane().add(destinationTextField);
    destinationTextField.setColumns(10);
   
    JButton startButton = new JButton("Iniciar");
    startButton.setBounds(10, 108, 89, 23);
    startButton.addActionListener(this);
    startButton.setActionCommand("start");
    getContentPane().add(startButton);
   
    JButton cleanButton = new JButton("Limpar");
    cleanButton.setBounds(109, 108, 89, 23);
    cleanButton.addActionListener(this);
    cleanButton.setActionCommand("clean");
    getContentPane().add(cleanButton);
   
    JButton closeButton = new JButton("Fechar");
    closeButton.setBounds(206, 108, 89, 23);
    closeButton.addActionListener(this);
    closeButton.setActionCommand("close");
    getContentPane().add(closeButton);
   
    JButton openSourceButton = new JButton("Abrir");
    openSourceButton.setBounds(345, 47, 89, 23);
    openSourceButton.addActionListener(this);
    openSourceButton.setActionCommand("source");
    getContentPane().add(openSourceButton);
   
    JButton openDestinationButton = new JButton("Abrir");
    openDestinationButton.setBounds(345, 78, 89, 23);
    openDestinationButton.addActionListener(this);
    openDestinationButton.setActionCommand("destination");
    getContentPane().add(openDestinationButton);
   
    setSize(450,170);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setLocationRelativeTo(null);
  }

  private static final long serialVersionUID = -3739008754324139579L;
  private JComboBox<RenameType> comboBoxType;
  private JTextField sourceTextField;
  private JTextField destinationTextField;
  private File sourceFile;
  private File destinationFile;
 
  private boolean debug;

  @Override
  public void actionPerformed(ActionEvent action) {
    if(action.getActionCommand().equals("source")){
      JFileChooser fileChooser = new JFileChooser(new File(""));
      fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
      fileChooser.showOpenDialog(this);
      sourceFile = fileChooser.getSelectedFile();
      if(sourceFile != null){
        sourceTextField.setText(sourceFile.getAbsolutePath());
       
        if(debug){
          System.out.println(sourceFile.getAbsolutePath());
        }
      }
    }else if(action.getActionCommand().equals("destination")){
      JFileChooser fileChooser = new JFileChooser(new File(""));
      fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
      fileChooser.showOpenDialog(this);
      destinationFile = fileChooser.getSelectedFile();
      if(destinationFile != null){
        destinationTextField.setText(destinationFile.getAbsolutePath());
       
        if(debug){
          System.out.println(destinationFile.getAbsolutePath());
        }
      }
    }else if(action.getActionCommand().equals("start")){
      String source = sourceTextField.getText();
      String destination = destinationTextField.getText();
     
      boolean sourceEqualsDestination = source.equals(destination);
      if(debug){
        System.out.println("Verificando se source != destination: \n" +
                  source+" == "+destination+"? -> "+sourceEqualsDestination);
      }
      if(sourceEqualsDestination){
        JOptionPane.showMessageDialog(null, "O diret�rio de origem deve ser diferente do diret�rio de destino.");
      }else{
        new Thread(){
          @Override
          public void run(){
            Renamer renamer = new Renamer(sourceFile, destinationFile, (RenameType)comboBoxType.getSelectedItem(), true, debug);
            renamer.batch();
          }
        }.start();
      }
    }else if(action.getActionCommand().equals("clean")){
      sourceTextField.setText("");
      destinationTextField.setText("");
    }else if(action.getActionCommand().equals("close")){
      System.exit(0);
    }
  }
}
TOP

Related Classes of view.Window

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.