Advertisement
xlrnxnlx

PilotController-working

May 21st, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.10 KB | None | 0 0
  1. package aap.controller;
  2.  
  3. import aap.view.PilotWindow;
  4. import app.dao.*;
  5. import app.model.*;
  6. import java.awt.event.*;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.Vector;
  10. import javax.swing.*;
  11.  
  12. public class PilotController implements ActionListener, MouseListener {
  13.  
  14.     private Pilot pilot;
  15.     private PilotCategory pilotCategory;
  16.     private PilotDataAccess pilotDAO;
  17.     private PilotCategoryDataAccess pilotCategoryDAO;
  18.     private PilotWindow window;
  19.     private int EXEC_TYPE = 0; // responsável por cuidar de qual ação será tomada
  20.     private int ID, ID_CATEGORY, BEFORE_UPDATE;
  21.     private String FIRST_NAME, LAST_NAME, CATEGORY;
  22.     private Date ENTRY;
  23.  
  24.     public PilotController(PilotWindow window) {
  25.         this.window = window;
  26.  
  27.         window.getBtInsert().addActionListener(this);
  28.         window.getBtDelete().addActionListener(this);
  29.         window.getBtEdit().addActionListener(this);
  30.         window.getTable().addMouseListener(this);
  31.         updateTable();
  32.         updateComboBox();
  33.         window.disableComponents(2);
  34.         window.setVisible(true);
  35.     }
  36.  
  37.     @Override
  38.     public void actionPerformed(ActionEvent e) {
  39.         JButton b = (JButton) e.getSource();
  40.         switch (b.getText().toLowerCase()) {
  41.             case "inserir":
  42.             case "atualizar":
  43.                 exec();
  44.                 window.disableComponents(2);
  45.                 break;
  46.  
  47.             case "cancelar":
  48.                 window.disableComponents(2);
  49.                 window.isEditableMode(false);
  50.                 break;
  51.  
  52.             case "novo":
  53.                 window.disableComponents(1);
  54.                 window.isNewMode(true);
  55.                 updateComboBox();
  56.                 EXEC_TYPE = 1;
  57.                 break;
  58.  
  59.             case "editar":
  60.                 window.disableComponents(1);
  61.                 window.isEditableMode(true);
  62.                 updateComboBox();
  63.                 EXEC_TYPE = 2;
  64.                 break;
  65.  
  66.             case "remover":
  67.                 EXEC_TYPE = 3;
  68.                 exec();
  69.                 break;
  70.         }
  71.     }
  72.  
  73.     private void exec() {
  74.         switch (this.EXEC_TYPE) {
  75.             case 1:
  76.                 // <editor-fold defaultstate="collapsed" desc=" Insert - Pilot ">
  77.                 setCategoryData();
  78.                 if (setPilotData()) {
  79.  
  80.                     pilotCategory = new PilotCategory(
  81.                             ID_CATEGORY,
  82.                             CATEGORY
  83.                     );
  84.                     pilot = new Pilot(
  85.                             pilotCategory,
  86.                             ID,
  87.                             FIRST_NAME,
  88.                             LAST_NAME,
  89.                             ENTRY
  90.                     );
  91.                     pilotDAO = new PilotDataAccess(pilot);
  92.                     if (pilotDAO.insert()) {
  93.                         JOptionPane.showMessageDialog(null, "\"" + FIRST_NAME + "\" inserido com sucesso!", "", JOptionPane.PLAIN_MESSAGE);
  94.                     }
  95.                 }
  96.                 break;
  97.             // </editor-fold>
  98.  
  99.             case 2:
  100.                 // <editor-fold defaultstate="collapsed" desc=" Update - Pilot ">
  101.                 setCategoryData();
  102.                 if (setPilotData()) {
  103.                     BEFORE_UPDATE = Integer.parseInt(window.getTable().getModel().getValueAt(window.getTable().getSelectedRow(), 0).toString());
  104.  
  105.                     pilotCategory = new PilotCategory(
  106.                             ID_CATEGORY,
  107.                             CATEGORY
  108.                     );
  109.                     pilot = new Pilot(
  110.                             pilotCategory,
  111.                             ID,
  112.                             FIRST_NAME,
  113.                             LAST_NAME,
  114.                             ENTRY
  115.                     );
  116.                     pilotDAO = new PilotDataAccess(pilot);
  117.                     if (pilotDAO.update(BEFORE_UPDATE)) {
  118.                         JOptionPane.showMessageDialog(null, "Dados Atualizados com sucesso!", "", JOptionPane.INFORMATION_MESSAGE);
  119.                     }
  120.                 }
  121.                 break;
  122.             // </editor-fold>
  123.             case 3:
  124.  
  125.                 // <editor-fold defaultstate="collapsed" desc=" Delete - Pilot ">
  126.                 auxSetCategoryData();
  127.                 if (setPilotData()) {
  128.  
  129.                     pilotCategory = new PilotCategory(
  130.                             ID_CATEGORY,
  131.                             CATEGORY
  132.                     );
  133.                     pilot = new Pilot(
  134.                             pilotCategory,
  135.                             ID,
  136.                             FIRST_NAME,
  137.                             LAST_NAME,
  138.                             ENTRY
  139.                     );
  140.                     int confirm = JOptionPane.showConfirmDialog(null, "Remover o piloto \"" + FIRST_NAME + "\"?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
  141.                     if (confirm == JOptionPane.YES_OPTION) {
  142.                         pilotDAO = new PilotDataAccess(pilot);
  143.                         if (pilotDAO.delete()) {
  144.                             JOptionPane.showMessageDialog(null, "\"" + FIRST_NAME + "\" removido com sucesso!", "", JOptionPane.PLAIN_MESSAGE);
  145.                         }
  146.                     }
  147.                 }
  148.                 break;
  149.             // </editor-fold>
  150.         }
  151.         window.isEditableMode(false);
  152.         updateTable();
  153.         updateComboBox();
  154.     }
  155.  
  156.     private boolean setPilotData() {
  157.         boolean success = false;
  158.         ID = Integer.parseInt(window.getTfID().getText());
  159.  
  160.         FIRST_NAME = window.getTfFName().getText();
  161.         LAST_NAME = window.getTfLName().getText();
  162.         SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy");
  163.         try {
  164.             ENTRY = formater.parse(window.getTfEntry().getText());
  165.             success = true;
  166.         } catch (Exception e) {
  167.             System.out.println("ERRO DE CONVERSAO");
  168.         }
  169.         return success;
  170.     }
  171.  
  172.     private void setCategoryData() {
  173.         CATEGORY = window.getCombobox().getSelectedItem().toString();
  174.         pilotCategoryDAO = new PilotCategoryDataAccess();
  175.         ID_CATEGORY = pilotCategoryDAO.selectByGenre(CATEGORY);
  176.     }
  177.  
  178.     private void auxSetCategoryData() {
  179.         JTable table = window.getTable();
  180.         CATEGORY = table.getModel().getValueAt(table.getSelectedRow(), 4).toString();
  181.         pilotCategoryDAO = new PilotCategoryDataAccess();
  182.         ID_CATEGORY = pilotCategoryDAO.selectByGenre(CATEGORY);
  183.     }
  184.  
  185.     private void updateTable() {
  186.         pilotDAO = new PilotDataAccess();
  187.         window.popTable(pilotDAO.select());
  188.         IDtoGenre();
  189.     }
  190.  
  191.     private void updateComboBox() {
  192.         pilotCategoryDAO = new PilotCategoryDataAccess();
  193.         window.popComboBox(pilotCategoryDAO.select());
  194.     }
  195.  
  196.     private void IDtoGenre() {
  197.         JTable table = window.getTable();
  198.         pilotCategoryDAO = new PilotCategoryDataAccess();
  199.         int rows = table.getRowCount();
  200.         for (int i = 0; i < rows; i++) {
  201.             int id = Integer.parseInt(table.getModel().getValueAt(i, 4).toString());
  202.             table.getModel().setValueAt(pilotCategoryDAO.selectByID(id), i, 4);
  203.         }
  204.     }
  205.  
  206.     @Override
  207.     public void mouseClicked(MouseEvent e) {
  208.         if (e.getSource() instanceof JTable) {
  209.             JTable table = (JTable) e.getSource();
  210.             window.getTfID().setText(table.getModel().getValueAt(table.getSelectedRow(), 0).toString());
  211.             window.getTfFName().setText(table.getModel().getValueAt(table.getSelectedRow(), 1).toString());
  212.             window.getTfLName().setText(table.getModel().getValueAt(table.getSelectedRow(), 2).toString());
  213.             window.getTfEntry().setText(table.getModel().getValueAt(table.getSelectedRow(), 3).toString());
  214.         }
  215.     }
  216.  
  217.     private String GENRE;
  218.  
  219.     @Override public void mousePressed(MouseEvent e) { }
  220.     @Override public void mouseReleased(MouseEvent e) { }
  221.     @Override public void mouseEntered(MouseEvent e) { }
  222.     @Override public void mouseExited(MouseEvent e) { }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement