Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aap.controller;
- import aap.view.*;
- 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.JButton;
- import javax.swing.JOptionPane;
- import javax.swing.JTable;
- public class MainWindowController implements ActionListener {
- private MainWindow window;
- private CategoryController categoryControll;
- private PilotController pilotControll;
- private PilotWindow pilotWindow;
- private CategoryWindow categoryWindow;
- private PilotDataAccess pilotDAO;
- private AirplaneDataAccess airplaneDAO;
- private TravelDataAccess travelDAO;
- private Travel travel;
- private Pilot pilot;
- private Airplane airplane;
- private AirplaneCategoryDataAccess airplaneCategoryDAO;
- public MainWindowController(MainWindow window) {
- this.window = window;
- //window.getBtAirplane().addActionListener(this);
- window.getBtAirplane().setEnabled(false);
- window.getBtAirplaneCategory().addActionListener(this);
- window.getBtPilot().addActionListener(this);
- window.getBtPilotCategory().addActionListener(this);
- window.getBtNew().addActionListener(this);
- window.getBtCancel().addActionListener(this);
- window.getBtInsert().addActionListener(this);
- refreshTable();
- window.setVisible(true);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- JButton b = (JButton) e.getSource();
- switch (b.getText().toLowerCase()) {
- case "pilotos":
- execPilotWindow();
- break;
- case "categoria de pilotos":
- execCategoryWindow("pilot");
- break;
- case "categoria de aviões":
- execCategoryWindow("airplane");
- break;
- case "nova":
- execNewTravel();
- break;
- case "inserir":
- execInsert();
- break;
- case "cancelamento":
- if(EXEC_TYPE == 10) {
- window.isNewMode(false);
- } else {
- execCancel();
- }
- break;
- }
- }
- private int EXEC_TYPE = 0;
- private int PILOT_BREVET, PLANE_ID, TOTAL_PASSENGERS;
- private String LOCATION;
- private Date TRAVEL_DATE;
- public boolean setData() {
- boolean success = false;
- try {
- pilotDAO = new PilotDataAccess();
- PILOT_BREVET = pilotDAO.selectByName(window.getPilotCombo().getSelectedItem().toString());
- PLANE_ID = Integer.parseInt(window.getAirplaneCombo().getSelectedItem().toString());
- TOTAL_PASSENGERS = Integer.parseInt(window.getTfPassengers().getText());
- LOCATION = window.getTfLocation().getText();
- SimpleDateFormat formater = new SimpleDateFormat("dd-MM-yyyy");
- TRAVEL_DATE = formater.parse(window.getTfDate().getText());
- pilot = new Pilot(
- null,
- PILOT_BREVET,
- null, null, null
- );
- airplane = new Airplane(
- null,
- PLANE_ID,
- TOTAL_PASSENGERS
- );
- travel = new Travel(
- pilot,
- airplane,
- TRAVEL_DATE,
- LOCATION,
- TOTAL_PASSENGERS
- );
- success = true;
- } catch (Exception e) {
- JOptionPane.showMessageDialog(null, "Verifique os dados antes de prosseguir.", "", JOptionPane.ERROR_MESSAGE);
- System.out.println("ERRO DE CONVERSAO");
- }
- return success;
- }
- public void execPilotWindow() {
- pilotWindow = new PilotWindow(window, true);
- pilotControll = new PilotController(pilotWindow);
- }
- public void execCategoryWindow(String type) {
- if (categoryControll == null) {
- categoryWindow = new CategoryWindow();
- categoryControll = new CategoryController(categoryWindow);
- }
- if (type.equals("airplane")) {
- categoryWindow.changeCategory("airplane");
- } else {
- categoryWindow.changeCategory("pilot");
- }
- categoryControll.updateComboBox();
- categoryWindow.setVisible(true);
- }
- public void execNewTravel() {
- window.isNewMode(true);
- refreshComboBox();
- EXEC_TYPE = 10;
- }
- public void execInsert() {
- window.isNewMode(false);
- if(setData()) {
- travelDAO = new TravelDataAccess(travel);
- if(travelDAO.insert())
- JOptionPane.showMessageDialog(null, "Viagem registrada!", "", JOptionPane.PLAIN_MESSAGE);
- }
- window.tfNormalState();
- refreshTable();
- }
- public void execCancel() {
- boolean selected = false;
- JTable table = window.getTable();
- for(int i = 0; i < table.getRowCount(); i++) {
- if(table.isRowSelected(i)) {
- window.getBtNew().setEnabled(false);
- table.setEnabled(false);
- selected = true;
- String pilotName = table.getModel().getValueAt(i, 0).toString();
- pilotDAO = new PilotDataAccess();
- PILOT_BREVET = pilotDAO.selectByName(pilotName);
- PLANE_ID = Integer.parseInt(table.getModel().getValueAt(i, 1).toString());
- pilot = new Pilot(
- null,
- PILOT_BREVET,
- null, null, null
- );
- airplane = new Airplane(
- null,
- PLANE_ID,
- TOTAL_PASSENGERS
- );
- travel = new Travel(
- pilot,
- airplane,
- null,
- null,
- TOTAL_PASSENGERS
- );
- int confirm = JOptionPane.showConfirmDialog(null, "Remover esta viagem?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
- if( confirm == JOptionPane.YES_OPTION ) {
- travelDAO = new TravelDataAccess(travel);
- if( travelDAO.delete() )
- JOptionPane.showConfirmDialog(null, "Viagem removida!", "", JOptionPane.PLAIN_MESSAGE);
- }
- }
- }
- if(!selected)
- JOptionPane.showConfirmDialog(null, "Selecione uma Viagem antes de clicar em cancelar", "", JOptionPane.PLAIN_MESSAGE);
- window.isNewMode(false);
- refreshTable();
- }
- private void refreshTable() {
- travelDAO = new TravelDataAccess();
- window.popTable(travelDAO.select());
- BrevetToName();
- //IDToGenre();
- }
- private void refreshComboBox() {
- pilotDAO = new PilotDataAccess();
- window.popComboBox(pilotDAO.selectAllPilotNames(), 1);
- airplaneDAO = new AirplaneDataAccess();
- window.popComboBox(airplaneDAO.selectAllPlaneIDs(), 2);
- }
- private void BrevetToName() {
- JTable table = window.getTable();
- int rows = table.getRowCount();
- pilotDAO = new PilotDataAccess();
- for (int i = 0; i < rows; i++) {
- int brevet = Integer.parseInt(table.getModel().getValueAt(i, 0).toString());
- table.getModel().setValueAt(pilotDAO.selectByID(brevet), i, 0);
- }
- }
- /*private void IDToGenre() {
- JTable table = window.getTable();
- int rows = table.getRowCount();
- airplaneCategoryDAO = new AirplaneCategoryDataAccess();
- for (int i = 0; i < rows; i++) {
- int id = Integer.parseInt(table.getModel().getValueAt(i, 1).toString());
- table.getModel().setValueAt(airplaneCategoryDAO.selectByID(id), i, 1);
- }
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement