Package presenter

Source Code of presenter.AssociarColunasImportacaoPresenter

package presenter;

import business.ContainerArvores;
import view.AssociaColunasImportacaoView;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import org.jdesktop.swingx.JXErrorPane;
import org.jdesktop.swingx.error.ErrorInfo;

/**
*
* @author vaio
*/
public final class AssociarColunasImportacaoPresenter {

    private AssociaColunasImportacaoView view;
    private ArrayList<String> fileHeader;
    private List<String> projectHeader;
    private ContainerArvores cenario;
    private Map<Integer, Integer> dataAssign = new TreeMap<Integer, Integer>();
    private boolean importData = false;
    private DefaultListModel lmArquivo;
    private DefaultListModel lmProject;
    private DefaultListModel lmAssociacao = new DefaultListModel();

    /**
     *
     * @return
     */
    public boolean isImportData() {
        boolean temp = importData;
        importData = false;
        return temp;
    }

    /**
     *
     * @param pFileHeader
     * @param pCenario
     */
    public AssociarColunasImportacaoPresenter(ArrayList<String> pFileHeader, ContainerArvores pCenario) {
        this.view = new AssociaColunasImportacaoView();
        this.fileHeader = pFileHeader;
        this.cenario = pCenario;
        this.projectHeader = pCenario.getHeadersNames(true);
        view.getLstAssociacao().setModel(new DefaultListModel());

        view.getLstArquivo().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        view.getLstProjeto().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        lmArquivo = new DefaultListModel();

        for (String header : this.fileHeader) {
            lmArquivo.addElement(header);
        }

        carregaColunasProjeto();

        view.getLstArquivo().setModel(lmArquivo);

        view.getBtnNovaColuna().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String coluna = JOptionPane.showInputDialog("Informe o nome da coluna");
                if (coluna.length() > 0) {
                    try {
                        cenario.addColumnData(coluna);
                        carregaColunasProjeto();
                        System.out.println();
                    } catch (Exception ex) {
                        ErrorInfo info = new ErrorInfo("Erro", ex.getMessage(), null, "category", ex, Level.SEVERE, null);
                        JXErrorPane.showDialog(view, info);

                    }
                }
            }
        });

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

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                if (view.getLstArquivo().getSelectedIndex() >= 0 && view.getLstProjeto().getSelectedIndex() >= 0) {
                    int colunaArquivo = view.getLstArquivo().getSelectedIndex();
                    int colunaProjeto = view.getLstProjeto().getSelectedIndex() + 1;
                    dataAssign.put(colunaArquivo, colunaProjeto);
                    String associacao = lmArquivo.get(colunaArquivo) + " -> " + lmProject.get(colunaProjeto - 1);
                    lmAssociacao.addElement(associacao);
                    view.getLstAssociacao().setModel(lmAssociacao);
                } else {
                    JOptionPane.showMessageDialog(view, "Selecione o dado do arquivo que deseja associar ao dado do projeto");
                }
            }
        });

        view.getBtnRemover().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                int selecionado = view.getLstAssociacao().getSelectedIndex();
                if (selecionado >= 0) {
                    String elemento = (String) view.getLstAssociacao().getSelectedValue();
                    lmAssociacao.removeElement(elemento);
                    elemento = elemento.substring(0, elemento.indexOf(" -> "));
                    dataAssign.remove(fileHeader.indexOf(elemento));
                    System.err.print("");
                } else {
                    JOptionPane.showMessageDialog(view, "Selecione uma associação.");
                }

            }
        });

        view.getBtnCancelar().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                importData = false;
                view.setVisible(false);
                view.dispose();
            }
        });

        view.getBtnImportar().addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (!dataAssign.isEmpty()) {
                    //if (dataAssign.size() == projectHeader.size()) {
                    if (true) {
                        importData = true;
                        view.setVisible(false);
                        view.dispose();
                    } else {
                        if (JOptionPane.showConfirmDialog(null, "Existem dados do projeto ainda não associados. "
                                + "Deseja cancelar a importação?", "Importação", JOptionPane.OK_CANCEL_OPTION) == 0) {
                            importData = false;
                            view.setVisible(false);
                            view.dispose();
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(view, "Faça pelo menos uma associação antes de importar");
                }
            }
        });
        view.setVisible(true);
    }

    /**
     *
     * @return
     */
    public Map<Integer, Integer> getDataAssign() {
        return dataAssign;
    }

    /**
     *
     * @return
     */
    public ArrayList<String> getFileHeader() {
        return fileHeader;
    }

    /**
     *
     * @return
     */
    public List<String> getProjectHeader() {
        return projectHeader;
    }

    /**
     *
     */
    public void carregaColunasProjeto() {
        lmProject = new DefaultListModel();
        this.projectHeader = cenario.getHeadersNames(true);
        this.projectHeader.remove("ARV");
        for (String header : this.projectHeader) {
            lmProject.addElement(header);
        }
        view.getLstProjeto().setModel(lmProject);
    }

    /**
     *
     * @return
     */
    public ContainerArvores getProjeto() {
        return cenario;
    }
}
TOP

Related Classes of presenter.AssociarColunasImportacaoPresenter

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.