Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pokedex.controll;
- import java.awt.Component;
- import java.awt.event.*;
- import java.util.ArrayList;
- import javax.swing.*;
- import pokedex.dao.PokedexDataAccess;
- import pokedex.model.Pokemon;
- import pokedex.view.MainWindow;
- public class PokedexController extends MouseAdapter implements ActionListener, FocusListener, ItemListener {
- private ArrayList<Pokemon> POKELIST;
- private ArrayList<String> POKELISTNAME;
- private PokedexDataAccess data;
- private MainWindow window;
- public PokedexController(MainWindow window) {
- this.window = window;
- for (int i = 0; i < window.MAXBUTTONS; i++) {
- setListener(window.getButtonByIndex(i));
- }
- for (int i = 0; i < window.MAXTEXTFIELDS - 1; i++) {
- setListener(window.getTextFieldByIndex(i));
- }
- setListener(window.getTextArea());
- //window.getComboBox().addActionListener(this);
- window.getComboBox().addItemListener(this);
- data = new PokedexDataAccess();
- uptComboBox(0);
- }
- public void uptComboBox(int select) {
- data.select();
- POKELIST = data.getPokeList();
- POKELISTNAME = data.getPokelistname();
- if (window.getComboBox().getItemCount() > 0)
- window.getComboBox().removeAllItems();
- window.getComboBox().addItem("-- POKÉMONS DISPONÍVEIS --");
- for (String pokename : POKELISTNAME) {
- window.getComboBox().addItem(pokename);
- }
- window.getComboBox().setSelectedIndex(select);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- Object o = e.getSource();
- if (o instanceof JButton) {
- JButton button = (JButton) e.getSource();
- switch (button.getText().toLowerCase()) {
- case "inserir":
- EXEC_TYPE = 1;
- setNewPokemonAndExec();
- break;
- case "atualizar":
- EXEC_TYPE = 2;
- window.setTextFieldStatus(true);
- window.turnAllEditable(true);
- window.turnConfirmButton(true);
- break;
- case "deletar":
- window.setTextFieldStatus(true);
- window.turnAllEditable(false);
- window.turnConfirmButton(true);
- EXEC_TYPE = 3;
- break;
- case "confirmar":
- window.turnAllEditable(false);
- window.componentVisible(window.getActionPanel(), false);
- setNewPokemonAndExec();
- break;
- case "voltar":
- window.turnConfirmButton(false);
- window.turnAllEditable(false);
- break;
- }
- }
- if (o instanceof JComboBox) {
- // só pra não bugar quando for chamado
- }
- }
- // flag
- int EXEC_TYPE = 0;
- private boolean exec() {
- switch (EXEC_TYPE) {
- case 1:
- data.insert();
- break;
- case 2:
- data.update();
- break;
- case 3:
- data.delete();
- break;
- }
- window.setTextFieldStatus(false);
- window.getComboBox().removeAllItems();
- uptComboBox(0);
- window.turnConfirmButton(false);
- window.getConfirmLabel().setVisible(false);
- return false;
- }
- private void setNewPokemonAndExec() {
- Pokemon p = new Pokemon();
- p.setID(Integer.parseInt(window.getTextFieldByIndex(0).getText()));
- p.setName(window.getTextFieldByIndex(1).getText());
- p.setType(window.getTextFieldByIndex(2).getText());
- p.setWeak(window.getTextFieldByIndex(3).getText());
- //pokemon.setVantage(window.getTextFieldByIndex(4).getText());
- p.setDescription(window.getTextArea().getText());
- data = new PokedexDataAccess(p);
- if (exec()) {
- EXEC_TYPE = 0;
- }
- }
- public final void setListener(Component c) {
- if (c instanceof JButton) {
- ((JButton) c).addActionListener(this);
- } else {
- c.addFocusListener(this);
- }
- c.addMouseListener(this);
- }
- @Override
- public void mouseEntered(MouseEvent e) {
- Object o = e.getSource();
- if (o instanceof JButton) {
- window.buttonHover((JButton) o, true);
- } else if (o instanceof JTextField) {
- if (window.getTextFieldStatus()) {
- window.textFieldHover((JTextField) o, true);
- } else if (o instanceof JTextArea) {
- if (window.getTextFieldStatus()) {
- window.textAreaHover((JTextArea) o, true);
- }
- }
- }
- }
- @Override
- public void mouseExited(MouseEvent e) {
- Object o = e.getSource();
- if (o instanceof JButton) {
- window.buttonHover((JButton) o, false);
- } else if (o instanceof JTextField) {
- window.textFieldHover((JTextField) o, false);
- } else if (o instanceof JTextArea) {
- window.textAreaHover((JTextArea) o, false);
- }
- }
- @Override
- public void focusGained(FocusEvent e) {
- Object o = e.getSource();
- if (o instanceof JTextField) {
- if (window.getTextFieldStatus()) {
- window.textFieldFocus((JTextField) o, true);
- }
- } else if (window.getTextFieldStatus()) {
- window.textAreaFocus((JTextArea) o, true);
- }
- }
- @Override
- public void focusLost(FocusEvent e) {
- Object o = e.getSource();
- if (o instanceof JTextField) {
- if (window.getTextFieldStatus()) {
- window.textFieldFocus((JTextField) o, false);
- }
- } else if (window.getTextFieldStatus()) {
- window.textAreaFocus((JTextArea) o, false);
- }
- }
- @Override
- public void itemStateChanged(ItemEvent e) {
- if (e.getStateChange() == ItemEvent.SELECTED) {
- int item = window.getComboBox().getSelectedIndex();
- window.setTextFieldName(); // volta o texto escrito em component.getname
- /*if (item == 0) {
- pokemonExists(false);
- } else {
- pokemonExists(true);*/
- Pokemon p = POKELIST.get(item);
- window.getTextFieldByIndex(0).setText(Integer.toString(p.getID()));
- window.getTextFieldByIndex(1).setText(p.getName());
- window.getTextFieldByIndex(2).setText(p.getType());
- window.getTextFieldByIndex(3).setText(p.getWeak());
- //window.getTextFieldByIndex(4).setText(p.getVantage());
- window.getTextArea().setText(p.getDescription());
- //}
- System.out.println(item);
- JOptionPane.showMessageDialog(null, window.getComboBox().getItemCount());
- }
- }
- /* CONTROLE */
- public void pokemonExists(boolean state) {
- if (!state) {
- window.componentVisible(window.getButtonByIndex(0), true);
- window.componentVisible(window.getActionPanel(), false);
- window.turnAllEditable(true);
- } else {
- window.componentVisible(window.getButtonByIndex(0), false);
- window.componentVisible(window.getActionPanel(), true);
- window.turnAllEditable(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement