Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aap.controller;
- import aap.view.CategoryWindow;
- import app.dao.*;
- import app.model.*;
- import java.awt.event.*;
- import javax.swing.JButton;
- import javax.swing.JOptionPane;
- public class CategoryController implements ActionListener {
- private CategoryWindow window;
- private AirplaneCategoryDataAccess airplaneDAO;
- private AirplaneCategory airplaneCategory;
- private PilotCategoryDataAccess pilotDAO;
- private PilotCategory pilotCategory;
- public CategoryController(CategoryWindow window) {
- this.window = window;
- window.getBtInsertCategory().addActionListener(this);
- window.getBtDeleteCategory().addActionListener(this);
- window.getBtEditCategory().addActionListener(this);
- updateComboBox();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- JButton b = (JButton) e.getSource();
- switch (b.getText().toLowerCase()) {
- case "inserir": insert(); break;
- case "remover": delete(); break;
- case "editar": edit(); break;
- case "atualizar": update(); break;
- }
- }
- public void updateComboBox() {
- if (window.getCategoryInInteger() == 1) {
- airplaneDAO = new AirplaneCategoryDataAccess();
- window.popComboBox(airplaneDAO.select());
- } else {
- pilotDAO = new PilotCategoryDataAccess();
- window.popComboBox(pilotDAO.select());
- }
- }
- public void updateAndShowWindow(String type) {
- if (this.window == null) {
- this.window = new CategoryWindow();
- } else {
- window.changeCategory(type);
- }
- if (window.getCategoryInInteger() == 1) {
- airplaneDAO = new AirplaneCategoryDataAccess();
- window.popComboBox(airplaneDAO.select());
- } else {
- pilotDAO = new PilotCategoryDataAccess();
- window.popComboBox(pilotDAO.select());
- }
- window.setVisible(true);
- }
- private void updateIDAndGenre() {
- this.ID = Integer.parseInt(window.getTfID().getText());
- this.GENRE = window.getTfNewCategory().getText();
- }
- private void setTextFieldNames() {
- window.getTfID().setText(Integer.toString(ID));
- window.getTfNewCategory().setText(GENRE);
- }
- private void insert() {
- boolean success;
- // <editor-fold defaultstate="collapsed" desc=" Insert - Airplane / Pilot ">
- updateIDAndGenre();
- if (window.getCategoryInInteger() == 1) {
- this.airplaneCategory = new AirplaneCategory(
- ID,
- GENRE
- );
- airplaneDAO = new AirplaneCategoryDataAccess(airplaneCategory);
- success = airplaneDAO.insert();
- } else {
- this.pilotCategory = new PilotCategory(
- ID,
- GENRE
- );
- pilotDAO = new PilotCategoryDataAccess(pilotCategory);
- success = pilotDAO.insert();
- }
- if (success) {
- updateComboBox();
- window.getTfID().setText(window.getTfID().getName());
- window.getTfNewCategory().setText(window.getTfID().getName());
- JOptionPane.showMessageDialog(null, GENRE + " inserido com sucesso!", "", JOptionPane.INFORMATION_MESSAGE);
- }
- //</editor-fold>
- }
- private void delete() {
- boolean success;
- // <editor-fold defaultstate="collapsed" desc=" Delete - Airplane / Pilot ">
- GENRE = window.getCombobox().getSelectedItem().toString();
- // 1 = sim, 2 não
- int confirm = JOptionPane.showConfirmDialog(null, "Remover a categoria \"" + GENRE + "\"?", "", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
- if (confirm == JOptionPane.YES_OPTION) {
- if (window.getCategoryInInteger() == 1) {
- this.airplaneCategory = new AirplaneCategory(0, GENRE);
- airplaneDAO = new AirplaneCategoryDataAccess(airplaneCategory);
- success = airplaneDAO.delete();
- } else {
- this.pilotCategory = new PilotCategory(0, GENRE);
- pilotDAO = new PilotCategoryDataAccess(pilotCategory);
- success = pilotDAO.delete();
- }
- if (success) {
- updateComboBox();
- JOptionPane.showMessageDialog(null, "\"" + GENRE + "\" removido com sucesso!", "", JOptionPane.OK_OPTION);
- }
- }
- //</editor-fold>
- }
- private void edit() {
- boolean success;
- // <editor-fold defaultstate="collapsed" desc=" Edit - Airplane / Pilot ">
- window.isEditableMode(true);
- BEFORE_EDIT = GENRE = window.getCombobox().getSelectedItem().toString();
- int index = window.getCombobox().getSelectedIndex();
- if (window.getCategoryInInteger() == 1) {
- airplaneDAO = new AirplaneCategoryDataAccess();
- ID = window.getIDInComboBox(airplaneDAO.select(), index);
- } else {
- pilotDAO = new PilotCategoryDataAccess();
- ID = window.getIDInComboBox(pilotDAO.select(), index);
- }
- setTextFieldNames();
- //</editor-fold>
- }
- private void update() {
- boolean success;
- // <editor-fold defaultstate="collapsed" desc=" Update - Airplane / Pilot ">
- updateIDAndGenre();
- if (window.getCategoryInInteger() == 1) {
- airplaneCategory = new AirplaneCategory(
- ID,
- GENRE
- );
- airplaneDAO = new AirplaneCategoryDataAccess(airplaneCategory);
- success = airplaneDAO.update(BEFORE_EDIT);
- } else {
- pilotCategory = new PilotCategory(
- ID,
- GENRE
- );
- pilotDAO = new PilotCategoryDataAccess(pilotCategory);
- success = pilotDAO.update(BEFORE_EDIT);
- }
- if (success) {
- updateComboBox();
- JOptionPane.showMessageDialog(null, "Dados Atualizados com sucesso!", "", JOptionPane.INFORMATION_MESSAGE);
- }
- window.isEditableMode(false);
- window.getTfID().setText(window.getTfID().getName());
- window.getTfNewCategory().setText(window.getTfNewCategory().getName());
- //</editor-fold>
- }
- private int ID;
- private String GENRE;
- private String BEFORE_EDIT;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement