Guest User

DuvidaCombo

a guest
Mar 13th, 2017
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.95 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package jschool.gui;
  7.  
  8. import java.util.ArrayList;
  9. import javax.swing.DefaultComboBoxModel;
  10. import jschool.dao.CidadeDAO;
  11. import jschool.dao.InstituicaoDAO;
  12. import jschool.dao.EstadoDAO;
  13. import jschool.entidade.Cidade;
  14. import jschool.entidade.Estado;
  15. import jschool.entidade.IEntidade;
  16. import jschool.entidade.Instituicao;
  17. import jschool.infra.Formatador;
  18. import jschool.infra.Mensagem;
  19.  
  20. /**
  21.  *
  22.  * @author andre
  23.  */
  24. public class JIFrameInstituicaoEnsino extends javax.swing.JInternalFrame {
  25.    
  26.     /**
  27.      * Creates new form JIFrameInstituicaoEnsino
  28.      */
  29.     public JIFrameInstituicaoEnsino() {
  30.         initComponents();
  31.         carregarEstados();
  32.         carregarInstituicao();
  33.     }
  34.  
  35.     private void carregarInstituicao() {
  36.         Instituicao instituicao = (Instituicao) new InstituicaoDAO().consultarId(0);
  37.         if (instituicao != null) {
  38.             jtfId.setText(Integer.toString(instituicao.getId()));
  39.             jtfNome.setText(instituicao.getNome());
  40.             jtfCnpj.setText(instituicao.getCnpj());
  41.             jtfIe.setText(instituicao.getInscricaoEstadual());
  42.             jtfTelefone.setText(instituicao.getTelefone());
  43.             jtfEmail.setText(instituicao.getEmail());
  44.             jtfSite.setText(instituicao.getSite());
  45.             jtfLogradouro.setText(instituicao.getLogradouro());
  46.             jtfNumero.setText(instituicao.getNumero());
  47.             jtfBairro.setText(instituicao.getBairro());
  48.             jtfComplemento.setText(instituicao.getComplemento());
  49.             jcbUf.setSelectedItem(instituicao.getCidade().getEstado());
  50.             jcbCidade.setSelectedItem(instituicao.getCidade());
  51.         } else {
  52.             Mensagem.erro("Problema ao carregar instituição de ensino");
  53.             jbtConfirmar.setEnabled(false);
  54.         }
  55.     }
  56.  
  57.     private void salvarInstituicao() {
  58.         Instituicao instituicao = new Instituicao();
  59.         instituicao.setId(Integer.parseInt(jtfId.getText()));
  60.         instituicao.setNome(jtfNome.getText());
  61.         instituicao.setCnpj(Formatador.removerPontuacaoCnpj(jtfCnpj.getText()));
  62.         instituicao.setInscricaoEstadual(jtfIe.getText());
  63.         instituicao.setTelefone(Formatador.removerPontuacaoTelefone(jtfTelefone.getText()));
  64.         instituicao.setEmail(jtfEmail.getText());
  65.         instituicao.setSite(jtfSite.getText());
  66.         instituicao.setLogradouro(jtfLogradouro.getText());
  67.         instituicao.setNumero(jtfNumero.getText());
  68.         instituicao.setComplemento(jtfComplemento.getText());
  69.         instituicao.setBairro(jtfBairro.getText());
  70.         instituicao.setCidade((Cidade)jcbCidade.getSelectedItem());
  71.        
  72.         boolean resultado = new InstituicaoDAO().salvar(instituicao);
  73.         if (resultado) {
  74.             Mensagem.sucesso("Alterações registradas com sucesso");
  75.         } else {
  76.             Mensagem.erro("Problema ao registrar alterações");
  77.         }
  78.     }
  79.  
  80.     private void carregarEstados() {
  81.         DefaultComboBoxModel modelo = new DefaultComboBoxModel();
  82.        
  83.         ArrayList<IEntidade> estados = new EstadoDAO().consultarTodos();
  84.         for (int i = 0; i < estados.size(); i++) {
  85.             Estado e = (Estado) estados.get(i);
  86.             modelo.addElement(e);
  87.         }
  88.  
  89.         jcbUf.setModel(modelo);
  90.     }
  91.  
  92.     private void carregarCidades() {
  93.         Estado estado = (Estado) jcbUf.getSelectedItem();
  94.         if (estado.getId() > 0) {
  95.             DefaultComboBoxModel modelo = new DefaultComboBoxModel();
  96.            
  97.             ArrayList<IEntidade> cidades = new CidadeDAO().consultarCidadesPorEstado(estado);
  98.             for (int i = 0; i < cidades.size(); i++) {
  99.                 Cidade cid = (Cidade) cidades.get(i);
  100.                 modelo.addElement(cid);
  101.             }
  102.            
  103.             jcbCidade.setModel(modelo);
  104.         } else {
  105.             Mensagem.aviso("Selecione um estado");
  106.         }
  107.     }
  108.  
  109.     /**
  110.      * This method is called from within the constructor to initialize the form.
  111.      * WARNING: Do NOT modify this code. The content of this method is always
  112.      * regenerated by the Form Editor.
  113.      */
  114.     @SuppressWarnings("unchecked")
  115.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  116.     private void initComponents() {
  117.  
  118.         jLabel1 = new javax.swing.JLabel();
  119.         jLabel2 = new javax.swing.JLabel();
  120.         jLabel3 = new javax.swing.JLabel();
  121.         jLabel4 = new javax.swing.JLabel();
  122.         jLabel5 = new javax.swing.JLabel();
  123.         jLabel6 = new javax.swing.JLabel();
  124.         jtfNome = new javax.swing.JTextField();
  125.         jtfIe = new javax.swing.JTextField();
  126.         jtfEmail = new javax.swing.JTextField();
  127.         jtfSite = new javax.swing.JTextField();
  128.         jLabel7 = new javax.swing.JLabel();
  129.         jLabel8 = new javax.swing.JLabel();
  130.         jLabel9 = new javax.swing.JLabel();
  131.         jtfLogradouro = new javax.swing.JTextField();
  132.         jtfNumero = new javax.swing.JTextField();
  133.         jtfBairro = new javax.swing.JTextField();
  134.         jtfComplemento = new javax.swing.JTextField();
  135.         jLabel10 = new javax.swing.JLabel();
  136.         jLabel11 = new javax.swing.JLabel();
  137.         jbtConfirmar = new javax.swing.JButton();
  138.         jbtCancelar = new javax.swing.JButton();
  139.         jtfCnpj = new javax.swing.JFormattedTextField();
  140.         jtfTelefone = new javax.swing.JFormattedTextField();
  141.         jLabel12 = new javax.swing.JLabel();
  142.         jtfId = new javax.swing.JTextField();
  143.         jLabel13 = new javax.swing.JLabel();
  144.         jcbUf = new javax.swing.JComboBox<>();
  145.         jcbCidade = new javax.swing.JComboBox<>();
  146.  
  147.         setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
  148.         setClosable(true);
  149.         setTitle("Instituição de Ensino");
  150.  
  151.         jLabel1.setText("Nome");
  152.  
  153.         jLabel2.setText("CNPJ");
  154.  
  155.         jLabel3.setText("IE");
  156.  
  157.         jLabel4.setText("Telefone");
  158.  
  159.         jLabel5.setText("Email");
  160.  
  161.         jLabel6.setText("Site");
  162.  
  163.         jLabel7.setText("Logradouro");
  164.  
  165.         jLabel8.setText("Número");
  166.  
  167.         jLabel9.setText("Bairro");
  168.  
  169.         jLabel10.setText("Complemento");
  170.  
  171.         jLabel11.setText("Cidade");
  172.  
  173.         jbtConfirmar.setText("Confirmar");
  174.         jbtConfirmar.addActionListener(new java.awt.event.ActionListener() {
  175.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  176.                 jbtConfirmarActionPerformed(evt);
  177.             }
  178.         });
  179.  
  180.         jbtCancelar.setText("Cancelar");
  181.         jbtCancelar.addActionListener(new java.awt.event.ActionListener() {
  182.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  183.                 jbtCancelarActionPerformed(evt);
  184.             }
  185.         });
  186.  
  187.         try {
  188.             jtfCnpj.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.###/####-##")));
  189.         } catch (java.text.ParseException ex) {
  190.             ex.printStackTrace();
  191.         }
  192.  
  193.         try {
  194.             jtfTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
  195.         } catch (java.text.ParseException ex) {
  196.             ex.printStackTrace();
  197.         }
  198.  
  199.         jLabel12.setText("Código");
  200.  
  201.         jtfId.setEditable(false);
  202.  
  203.         jLabel13.setText("UF");
  204.  
  205.         jcbUf.addItemListener(new java.awt.event.ItemListener() {
  206.             public void itemStateChanged(java.awt.event.ItemEvent evt) {
  207.                 jcbUfItemStateChanged(evt);
  208.             }
  209.         });
  210.  
  211.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  212.         getContentPane().setLayout(layout);
  213.         layout.setHorizontalGroup(
  214.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  215.             .addGroup(layout.createSequentialGroup()
  216.                 .addContainerGap()
  217.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  218.                     .addGroup(layout.createSequentialGroup()
  219.                         .addGap(0, 0, Short.MAX_VALUE)
  220.                         .addComponent(jbtConfirmar)
  221.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  222.                         .addComponent(jbtCancelar))
  223.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  224.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  225.                             .addComponent(jLabel4)
  226.                             .addComponent(jLabel5)
  227.                             .addComponent(jLabel6))
  228.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  229.                             .addGroup(layout.createSequentialGroup()
  230.                                 .addGap(78, 78, 78)
  231.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  232.                                     .addComponent(jtfEmail)
  233.                                     .addComponent(jtfSite)))
  234.                             .addGroup(layout.createSequentialGroup()
  235.                                 .addGap(79, 79, 79)
  236.                                 .addComponent(jtfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
  237.                                 .addGap(0, 0, Short.MAX_VALUE))))
  238.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  239.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  240.                             .addComponent(jLabel1)
  241.                             .addComponent(jLabel12))
  242.                         .addGap(93, 93, 93)
  243.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  244.                             .addComponent(jtfNome, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE)
  245.                             .addComponent(jtfId, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
  246.                         .addGap(0, 2, Short.MAX_VALUE))
  247.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  248.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  249.                             .addComponent(jLabel7)
  250.                             .addComponent(jLabel9)
  251.                             .addComponent(jLabel10)
  252.                             .addComponent(jLabel11)
  253.                             .addComponent(jLabel13))
  254.                         .addGap(42, 42, 42)
  255.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  256.                             .addComponent(jtfBairro, javax.swing.GroupLayout.Alignment.TRAILING)
  257.                             .addComponent(jtfLogradouro)
  258.                             .addComponent(jtfComplemento)
  259.                             .addComponent(jcbCidade, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  260.                             .addGroup(layout.createSequentialGroup()
  261.                                 .addComponent(jcbUf, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
  262.                                 .addGap(0, 0, Short.MAX_VALUE)))))
  263.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  264.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  265.                     .addComponent(jLabel3)
  266.                     .addComponent(jLabel2)
  267.                     .addComponent(jLabel8))
  268.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  269.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  270.                     .addComponent(jtfIe)
  271.                     .addComponent(jtfNumero)
  272.                     .addComponent(jtfCnpj, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))
  273.                 .addContainerGap())
  274.         );
  275.  
  276.         layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbtCancelar, jbtConfirmar});
  277.  
  278.         layout.setVerticalGroup(
  279.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  280.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  281.                 .addContainerGap()
  282.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  283.                     .addComponent(jLabel12)
  284.                     .addComponent(jtfId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  285.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  286.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  287.                     .addComponent(jLabel1)
  288.                     .addComponent(jtfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  289.                     .addComponent(jLabel2)
  290.                     .addComponent(jtfCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  291.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  292.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  293.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  294.                         .addComponent(jLabel3)
  295.                         .addComponent(jtfIe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  296.                     .addGroup(layout.createSequentialGroup()
  297.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  298.                             .addComponent(jLabel4)
  299.                             .addComponent(jtfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  300.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  301.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  302.                             .addComponent(jLabel5)
  303.                             .addComponent(jtfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  304.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  305.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  306.                             .addComponent(jLabel6)
  307.                             .addComponent(jtfSite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
  308.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  309.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  310.                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  311.                         .addComponent(jLabel7)
  312.                         .addComponent(jLabel8)
  313.                         .addComponent(jtfNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  314.                     .addComponent(jtfLogradouro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  315.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  316.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  317.                     .addComponent(jLabel9)
  318.                     .addComponent(jtfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  319.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  320.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  321.                     .addComponent(jtfComplemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  322.                     .addComponent(jLabel10))
  323.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  324.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  325.                     .addComponent(jLabel13)
  326.                     .addComponent(jcbUf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  327.                 .addGap(5, 5, 5)
  328.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  329.                     .addComponent(jLabel11)
  330.                     .addComponent(jcbCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  331.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  332.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  333.                     .addComponent(jbtConfirmar)
  334.                     .addComponent(jbtCancelar))
  335.                 .addContainerGap())
  336.         );
  337.  
  338.         layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jbtCancelar, jbtConfirmar});
  339.  
  340.         pack();
  341.     }// </editor-fold>                        
  342.  
  343.     private void jbtCancelarActionPerformed(java.awt.event.ActionEvent evt) {                                            
  344.         this.dispose();
  345.     }                                          
  346.  
  347.     private void jbtConfirmarActionPerformed(java.awt.event.ActionEvent evt) {                                            
  348.         salvarInstituicao();
  349.         this.dispose();
  350.     }                                            
  351.  
  352.     private void jcbUfItemStateChanged(java.awt.event.ItemEvent evt) {                                      
  353.         carregarCidades();
  354.     }                                      
  355.  
  356.     // Variables declaration - do not modify                    
  357.     private javax.swing.JLabel jLabel1;
  358.     private javax.swing.JLabel jLabel10;
  359.     private javax.swing.JLabel jLabel11;
  360.     private javax.swing.JLabel jLabel12;
  361.     private javax.swing.JLabel jLabel13;
  362.     private javax.swing.JLabel jLabel2;
  363.     private javax.swing.JLabel jLabel3;
  364.     private javax.swing.JLabel jLabel4;
  365.     private javax.swing.JLabel jLabel5;
  366.     private javax.swing.JLabel jLabel6;
  367.     private javax.swing.JLabel jLabel7;
  368.     private javax.swing.JLabel jLabel8;
  369.     private javax.swing.JLabel jLabel9;
  370.     private javax.swing.JButton jbtCancelar;
  371.     private javax.swing.JButton jbtConfirmar;
  372.     private javax.swing.JComboBox<String> jcbCidade;
  373.     private javax.swing.JComboBox<String> jcbUf;
  374.     private javax.swing.JTextField jtfBairro;
  375.     private javax.swing.JFormattedTextField jtfCnpj;
  376.     private javax.swing.JTextField jtfComplemento;
  377.     private javax.swing.JTextField jtfEmail;
  378.     private javax.swing.JTextField jtfId;
  379.     private javax.swing.JTextField jtfIe;
  380.     private javax.swing.JTextField jtfLogradouro;
  381.     private javax.swing.JTextField jtfNome;
  382.     private javax.swing.JTextField jtfNumero;
  383.     private javax.swing.JTextField jtfSite;
  384.     private javax.swing.JFormattedTextField jtfTelefone;
  385.     // End of variables declaration                  
  386. }
Advertisement
Add Comment
Please, Sign In to add comment