Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aap.controller;
- import aap.view.PilotWindow;
- import app.dao.*;
- import app.model.*;
- import java.awt.event.*;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Vector;
- import javax.swing.*;
- public class PilotController implements ActionListener, MouseListener {
- private Pilot pilot;
- private PilotCategory pilotCategory;
- private PilotDataAccess pilotDAO;
- private PilotCategoryDataAccess pilotCategoryDAO;
- private PilotWindow window;
- private int EXEC_TYPE = 0; // responsável por cuidar de qual ação será tomada
- private int ID, ID_CATEGORY, BEFORE_UPDATE;
- private String FIRST_NAME, LAST_NAME, CATEGORY;
- private Date ENTRY;
- public PilotController(PilotWindow window) {
- this.window = window;
- window.getBtInsert().addActionListener(this);
- window.getBtDelete().addActionListener(this);
- window.getBtEdit().addActionListener(this);
- window.getTable().addMouseListener(this);
- updateTable();
- updateComboBox();
- window.disableComponents(2);
- window.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- JButton b = (JButton) e.getSource();
- switch (b.getText().toLowerCase()) {
- case "inserir":
- case "atualizar":
- exec();
- window.disableComponents(2);
- break;
- case "cancelar":
- window.disableComponents(2);
- window.isEditableMode(false);
- break;
- case "novo":
- window.disableComponents(1);
- window.isNewMode(true);
- updateComboBox();
- EXEC_TYPE = 1;
- break;
- case "editar":
- window.disableComponents(1);
- window.isEditableMode(true);
- updateComboBox();
- EXEC_TYPE = 2;
- break;
- case "remover":
- EXEC_TYPE = 3;
- exec();
- break;
- }
- }
- private void exec() {
- switch (this.EXEC_TYPE) {
- case 1:
- // <editor-fold defaultstate="collapsed" desc=" Insert - Pilot ">
- setCategoryData();
- if (setPilotData()) {
- pilotCategory = new PilotCategory(
- ID_CATEGORY,
- CATEGORY
- );
- pilot = new Pilot(
- pilotCategory,
- ID,
- FIRST_NAME,
- LAST_NAME,
- ENTRY
- );
- pilotDAO = new PilotDataAccess(pilot);
- if (pilotDAO.insert()) {
- JOptionPane.showMessageDialog(null, "\"" + FIRST_NAME + "\" inserido com sucesso!", "", JOptionPane.PLAIN_MESSAGE);
- }
- }
- break;
- // </editor-fold>
- case 2:
- // <editor-fold defaultstate="collapsed" desc=" Update - Pilot ">
- setCategoryData();
- if (setPilotData()) {
- BEFORE_UPDATE = Integer.parseInt(window.getTable().getModel().getValueAt(window.getTable().getSelectedRow(), 0).toString());
- pilotCategory = new PilotCategory(
- ID_CATEGORY,
- CATEGORY
- );
- pilot = new Pilot(
- pilotCategory,
- ID,
- FIRST_NAME,
- LAST_NAME,
- ENTRY
- );
- pilotDAO = new PilotDataAccess(pilot);
- if (pilotDAO.update(BEFORE_UPDATE)) {
- JOptionPane.showMessageDialog(null, "Dados Atualizados com sucesso!", "", JOptionPane.INFORMATION_MESSAGE);
- }
- }
- break;
- // </editor-fold>
- case 3:
- // <editor-fold defaultstate="collapsed" desc=" Delete - Pilot ">
- auxSetCategoryData();
- if (setPilotData()) {
- pilotCategory = new PilotCategory(
- ID_CATEGORY,
- CATEGORY
- );
- pilot = new Pilot(
- pilotCategory,
- ID,
- FIRST_NAME,
- LAST_NAME,
- ENTRY
- );
- int confirm = JOptionPane.showConfirmDialog(null, "Remover o piloto \"" + FIRST_NAME + "\"?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
- if (confirm == JOptionPane.YES_OPTION) {
- pilotDAO = new PilotDataAccess(pilot);
- if (pilotDAO.delete()) {
- JOptionPane.showMessageDialog(null, "\"" + FIRST_NAME + "\" removido com sucesso!", "", JOptionPane.PLAIN_MESSAGE);
- }
- }
- }
- break;
- // </editor-fold>
- }
- window.isEditableMode(false);
- updateTable();
- updateComboBox();
- }
- private boolean setPilotData() {
- boolean success = false;
- ID = Integer.parseInt(window.getTfID().getText());
- FIRST_NAME = window.getTfFName().getText();
- LAST_NAME = window.getTfLName().getText();
- SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy");
- try {
- ENTRY = formater.parse(window.getTfEntry().getText());
- success = true;
- } catch (Exception e) {
- System.out.println("ERRO DE CONVERSAO");
- }
- return success;
- }
- private void setCategoryData() {
- CATEGORY = window.getCombobox().getSelectedItem().toString();
- pilotCategoryDAO = new PilotCategoryDataAccess();
- ID_CATEGORY = pilotCategoryDAO.selectByGenre(CATEGORY);
- }
- private void auxSetCategoryData() {
- JTable table = window.getTable();
- CATEGORY = table.getModel().getValueAt(table.getSelectedRow(), 4).toString();
- pilotCategoryDAO = new PilotCategoryDataAccess();
- ID_CATEGORY = pilotCategoryDAO.selectByGenre(CATEGORY);
- }
- private void updateTable() {
- pilotDAO = new PilotDataAccess();
- window.popTable(pilotDAO.select());
- IDtoGenre();
- }
- private void updateComboBox() {
- pilotCategoryDAO = new PilotCategoryDataAccess();
- window.popComboBox(pilotCategoryDAO.select());
- }
- private void IDtoGenre() {
- JTable table = window.getTable();
- pilotCategoryDAO = new PilotCategoryDataAccess();
- int rows = table.getRowCount();
- for (int i = 0; i < rows; i++) {
- int id = Integer.parseInt(table.getModel().getValueAt(i, 4).toString());
- table.getModel().setValueAt(pilotCategoryDAO.selectByID(id), i, 4);
- }
- }
- @Override
- public void mouseClicked(MouseEvent e) {
- if (e.getSource() instanceof JTable) {
- JTable table = (JTable) e.getSource();
- window.getTfID().setText(table.getModel().getValueAt(table.getSelectedRow(), 0).toString());
- window.getTfFName().setText(table.getModel().getValueAt(table.getSelectedRow(), 1).toString());
- window.getTfLName().setText(table.getModel().getValueAt(table.getSelectedRow(), 2).toString());
- window.getTfEntry().setText(table.getModel().getValueAt(table.getSelectedRow(), 3).toString());
- }
- }
- private String GENRE;
- @Override public void mousePressed(MouseEvent e) { }
- @Override public void mouseReleased(MouseEvent e) { }
- @Override public void mouseEntered(MouseEvent e) { }
- @Override public void mouseExited(MouseEvent e) { }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement