Package presenter

Source Code of presenter.ModelosPresenter

package presenter;

import business.Modelo;
import dao.ModelosDAO;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.error.ErrorInfo;
import org.netbeans.validation.api.Problem;
import org.netbeans.validation.api.Severity;
import org.netbeans.validation.api.Validator;
import org.netbeans.validation.api.builtin.Validators;
import org.netbeans.validation.api.ui.ValidationPanel;
import view.ModelosView;
import view.helpers.WindowClose;
import view.states.modelos.Navegacao;

/**
*
* @author Helder
*/
public final class ModelosPresenter implements Observer {

    private ModelosView view;
    private ExpressaoPresenter expPres;
    private DefaultTableModel mod;
    private ModelosDAO modelosDAO;
    private Modelo modelo;
    private List<Modelo> modelos;

    public ModelosPresenter() {

        Object[] linha = null;

        this.view = new ModelosView(null);
        this.view.setLocationRelativeTo(null);

        view.getTxtNome().setName("Nome do modelo");
        view.getTxtExpressao().setName("Expressão");

        final ValidationPanel pnl = new ValidationPanel();
        view.setContentPane(pnl);
        pnl.setInnerComponent(view.getPnlCampos());

        // O campo Nome não pode ser vazio
        Validator<javax.swing.text.Document> vldVazio = Validators.forDocument(true, Validators.REQUIRE_NON_EMPTY_STRING);
        pnl.getValidationGroup().add(view.getTxtNome(), vldVazio);
        pnl.getValidationGroup().add(view.getTxtExpressao(), vldVazio);
        pnl.addChangeListener(new ChangeListener() {

            public void stateChanged(ChangeEvent e) {
                Problem p = pnl.getProblem();
                boolean enable = p == null ? true : p.severity() != Severity.FATAL;
                view.getBtnConfModelo().setEnabled(enable);
            }
        });


        modelosDAO = new ModelosDAO();

        modelosDAO.connect();
        try {
            modelos = modelosDAO.getAll("id", true);
        } catch (Exception ex) {
            ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
            JXErrorPane.showDialog(null, info);
        }
        Iterator it = modelos.iterator();
        mod = (DefaultTableModel) view.getTableModelos().getModel();
        linha = new Object[3];
        while (it.hasNext()) {
            modelo = (Modelo) it.next();
            linha[0] = modelo.getNome();
            linha[1] = modelo.getExpressao();
            linha[2] = view.getListTipoMod().getModel().getElementAt(modelo.getTipoExp());
            mod.addRow(linha);
        }

        this.view.getTableModelos().addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(final MouseEvent e) {
                int selected = view.getTableModelos().getSelectedRow();
                if (view.getTableModelos().getSelectedRowCount() > 0) {
                    view.getTxtNome().setText(view.getTableModelos().getValueAt(selected, 0).toString());
                    view.getTxtExpressao().setText(view.getTableModelos().getValueAt(selected, 1).toString());
                    view.getListTipoMod().setSelectedValue(view.getTableModelos().getValueAt(selected, 2), false);
                }
            }
        });
        modelosDAO.close();

        this.view.setViewState(new Navegacao(view));

        view.getBtnCarregarExp().addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                List<String> variaveis = new ArrayList();

                variaveis.add("VAR01");
                variaveis.add("VAR02");
                variaveis.add("VAR03");
                variaveis.add("VAR04");
                variaveis.add("VAR05");
                variaveis.add("VAR06");
                variaveis.add("VAR07");
                variaveis.add("VAR08");
                variaveis.add("VAR09");
                variaveis.add("VAR010");
                variaveis.add("VAR011");
                variaveis.add("VAR012");

                expPres = new ExpressaoPresenter(variaveis, true, view.getTxtExpressao().getText());
                view.getTxtExpressao().setText(expPres.getExpressao());
            }
        });

        view.getBtnIncModelo().addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                view.getTxtNome().setText(null);
                view.getTxtExpressao().setText(null);
                view.getViewState().acionaIncluir();
            }
        });

        view.getBtnExcModelo().addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                int pos;

                pos = view.getTableModelos().getSelectedRow();
                if (pos >= 0) {
                    if (JOptionPane.showConfirmDialog(view, "Confirma a exclusão?", "Exclusão", JOptionPane.YES_NO_OPTION) == 0) {
                        try {
                            modelosDAO.connect();
                            Modelo m = modelosDAO.find(modelos.get(pos).getId());
                            modelosDAO.delete(m);
                            mod.removeRow(pos);
                            modelos.remove(pos);
                            modelosDAO.close();
                            view.getViewState().acionaConfirma();
                            view.getViewState().habilitar();
                        } catch (Exception ex) {
                            ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
                            JXErrorPane.showDialog(null, info);
                        }
                    }
                }
            }
        });

        view.getBtnAltModelo().addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                view.getViewState().acionaAlterar();
            }
        });

        view.getBtnCancModelo().addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                view.getViewState().acionaCancela();
                view.getViewState().habilitar();
                if (mod.getRowCount() > 0) {
                    view.getTxtNome().setText(view.getTableModelos().getValueAt(0, 0).toString());
                    view.getTxtExpressao().setText(view.getTableModelos().getValueAt(0, 1).toString());
                    view.getListTipoMod().setSelectedIndex(modelo.getTipoExp());
                }
            }
        });

        view.getBtnConfModelo().addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {

                Object linha[] = new Object[4];
                int linhaAtual;

                linhaAtual = view.getTableModelos().getSelectedRow();
                if (view.getViewState().getClass().getName().toLowerCase().equals("view.states.modelos.inclusao")) {
                    view.getViewState().acionaConfirma();
                    if (view.getViewState().getClass().getName().toLowerCase().equals("view.states.modelos.navegacao")) {
                        try {
                            modelosDAO.connect();
                            modelo = new Modelo();
                            modelo.setId(modelosDAO.getNewId());
                            modelo.setNome(view.getTxtNome().getText());
                            modelo.setExpressao(view.getTxtExpressao().getText());
                            modelo.setTipoExp(view.getListTipoMod().getSelectedIndex());
                            modelosDAO.save(modelo);
                            linha[0] = modelo.getNome();
                            linha[1] = modelo.getExpressao();
                            //linha[2] = Integer.toString(modelo.getTipoExp());
                            linha[2] = view.getListTipoMod().getModel().getElementAt(modelo.getTipoExp());
                            mod.addRow(linha);
                            modelos.add(modelo);
                            modelosDAO.close();
                        } catch (Exception ex) {
                            ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
                            JXErrorPane.showDialog(null, info);
                        }
                    }
                }
                if (view.getViewState().getClass().getName().toLowerCase().equals("view.states.modelos.alteracao")) {
                    view.getViewState().acionaConfirma();
                    if (view.getViewState().getClass().getName().toLowerCase().equals("view.states.modelos.navegacao")) {
                        try {
                            modelosDAO.connect();
                            modelo = modelosDAO.find(modelos.get(linhaAtual).getId());
                            modelo.setNome(view.getTxtNome().getText());
                            modelo.setExpressao(view.getTxtExpressao().getText());
                            modelo.setTipoExp(view.getListTipoMod().getSelectedIndex());
                            modelosDAO.update(modelo, false, modelo.getId());
                            mod.setValueAt(modelo.getNome(), linhaAtual, 0);
                            mod.setValueAt(modelo.getExpressao(), linhaAtual, 1);
                            mod.setValueAt(view.getListTipoMod().getModel().getElementAt(modelo.getTipoExp()), linhaAtual, 2);
                            modelosDAO.close();
                        } catch (Exception ex) {
                            ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
                            JXErrorPane.showDialog(null, info);
                        }
                    }
                }

            }
        });

        view.getBtnRetornar().addActionListener(new java.awt.event.ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                view.setVisible(false);
                view.dispose();
            }
        });

        view.getTableModelos().addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                view.getViewState().habilitar();
            }
        });

        view.addWindowListener(new WindowClose(view));

        this.view.setModal(true);
        this.view.setVisible(true);
    }

    @Override
    public void update(Observable o, Object arg) {
    }
}
TOP

Related Classes of presenter.ModelosPresenter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.