/** Create the panel. */ public PanelCadastroAbastecimento(final int idAbastecimentoSelecionado) { JLabel lblCadastroAbastecimento = new JLabel(" Cadastro de Abastecimentos\r\n"); lblCadastroAbastecimento.setIcon(new ImageIcon("imagens\\2895_32x32.png")); lblCadastroAbastecimento.setFont(new Font("Tahoma", Font.PLAIN, 20)); JLabel lblData = new JLabel("Data"); lblData.setFont(new Font("Tahoma", Font.PLAIN, 15)); lblData.setHorizontalAlignment(SwingConstants.LEFT); MaskFormatter data = null; try { data = new MaskFormatter("##/##/####"); } catch (ParseException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } final JCalendar cmbData = new JCalendar(); JLabel lblHodometro = new JLabel("Hod\u00F4metro"); lblHodometro.setFont(new Font("Tahoma", Font.PLAIN, 15)); JLabel lblPlaca = new JLabel("Placa"); lblPlaca.setFont(new Font("Tahoma", Font.PLAIN, 15)); textFieldHodometro = new JTextField(); textFieldHodometro.setDocument(new IntegerDocument(6)); textFieldHodometro.setFont(new Font("Tahoma", Font.PLAIN, 15)); textFieldHodometro.setColumns(10); MBVeiculo mbVeiculo = MBVeiculo.getInstance(); comboBoxPlaca = new JComboBox<Veiculo>(); DefaultComboBoxModel<Veiculo> modeloComboBox; JLabel lblVeiculo = new JLabel("Veiculo"); lblVeiculo.setFont(new Font("Tahoma", Font.PLAIN, 15)); final JLabel lblModelo = new JLabel(""); comboBoxPlaca.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { Veiculo v = (Veiculo) comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()); lblModelo.setText(v.getModelo().getNome()); lblModelo.setFont(new Font("Tahoma", Font.PLAIN, 15)); } }); try { modeloComboBox = new DefaultComboBoxModel<Veiculo>(new Vector(mbVeiculo.listarVeiculos())); comboBoxPlaca.setModel(modeloComboBox); comboBoxPlaca.setSelectedIndex(0); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Veiculo veiculoSelecionado = (Veiculo) comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()); comboBoxPlaca.setFont(new Font("Tahoma", Font.PLAIN, 15)); JButton btnCancelar = new JButton("Cancelar"); btnCancelar.setIcon(new ImageIcon("imagens\\7464_32x32.png")); btnCancelar.setFont(new Font("Tahoma", Font.PLAIN, 15)); btnCancelar.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { PanelListagemAbastecimento(); } }); JButton btnSalvar = new JButton("Salvar"); btnSalvar.setIcon(new ImageIcon("imagens\\7484_16x16.png")); btnSalvar.setFont(new Font("Tahoma", Font.PLAIN, 15)); btnSalvar.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { Util util = Util.getInstance(); java.sql.Timestamp data = new java.sql.Timestamp(util.getCMBData(cmbData).getTime()); MBAbastecimento mbAbastecimento = MBAbastecimento.getInstance(); MBVeiculo mbVeiculo = MBVeiculo.getInstance(); Abastecimento a; try { a = new Abastecimento( new Integer(idAbastecimentoSelecionado), mbVeiculo.retornarVeiculo( comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()).getIdveiculo()), Integer.parseInt(Util.mascaraHodometro(textFieldHodometro.getText())), data); } catch (NumberFormatException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ParseException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { a = new Abastecimento( new Integer(idAbastecimentoSelecionado), mbVeiculo.retornarVeiculo( comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()).getIdveiculo()), Integer.parseInt(Util.mascaraHodometro(textFieldHodometro.getText())), data); if (idAbastecimentoSelecionado == 0) { if (a.getIdabastecimento() == 0) { a.setIdabastecimento(null); } String retorno = mbAbastecimento.inserir(a); mbVeiculo.AtualizarOdometro( Integer.parseInt(textFieldHodometro.getText()), comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()).getIdveiculo()); mbTipoServiçoModelo.atualizaStatusVeiculo( mbVeiculo.retornarVeiculo( comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()).getIdveiculo())); if (retorno.equals("ok")) { JOptionPane.showMessageDialog(null, "Cadastro inserido!"); // PanelListagemAbastecimento(); } else { JOptionPane.showMessageDialog(null, retorno); } } else { String retorno = mbAbastecimento.editar(a); mbVeiculo.AtualizarOdometro( Integer.parseInt(textFieldHodometro.getText()), comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()).getIdveiculo()); mbTipoServiçoModelo.atualizaStatusVeiculo( mbVeiculo.retornarVeiculo( comboBoxPlaca.getItemAt(comboBoxPlaca.getSelectedIndex()).getIdveiculo())); if (retorno.equals("ok")) { // PanelListagemAbastecimento(); } else { JOptionPane.showMessageDialog(null, retorno); } } } catch (Exception e) { // TODO: handle exception } PanelListagemAbastecimento(); } }); GroupLayout groupLayout = new GroupLayout(this); groupLayout.setHorizontalGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addGroup( groupLayout .createSequentialGroup() .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent(lblCadastroAbastecimento) .addGroup( groupLayout .createSequentialGroup() .addContainerGap() .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent(lblHodometro) .addComponent(lblData) .addComponent( lblPlaca, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE) .addComponent(lblVeiculo)) .addGap(53) .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent(lblModelo) .addGroup( groupLayout .createSequentialGroup() .addGroup( groupLayout .createParallelGroup( Alignment.LEADING, false) .addComponent( cmbData, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( comboBoxPlaca, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( textFieldHodometro, 0, 0, Short.MAX_VALUE) .addComponent( btnSalvar, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)) .addGap(10) .addComponent( btnCancelar, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE))))) .addContainerGap(92, Short.MAX_VALUE))); groupLayout.setVerticalGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addGroup( groupLayout .createSequentialGroup() .addContainerGap() .addComponent(lblCadastroAbastecimento) .addGap(28) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent(lblData) .addComponent( cmbData, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent(lblHodometro) .addComponent( textFieldHodometro, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent(lblPlaca) .addComponent( comboBoxPlaca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent(lblVeiculo) .addComponent(lblModelo)) .addGap(57) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent(btnSalvar) .addComponent(btnCancelar)) .addGap(36))); setLayout(groupLayout); if (idAbastecimentoSelecionado > 0) { MBAbastecimento mbAbastecimento = MBAbastecimento.getInstance(); Util util = Util.getInstance(); try { Abastecimento a = mbAbastecimento.retornarAbastecimento(idAbastecimentoSelecionado); String d = a.getData2().toString().substring(8, 10) + "/" + a.getData2().toString().substring(5, 7) + "/" + a.getData2().toString().substring(0, 4); cmbData.setSelectedItem(d); textFieldHodometro.setText(a.getKmOdometro().toString()); boolean aux = false; int i = 0; while (aux == false) { aux = mbVeiculo.listarVeiculos().get(i).getIdveiculo() == a.getVeiculo().getIdveiculo(); if (aux == true) break; i++; } comboBoxPlaca.setSelectedIndex(i); } catch (Exception e) { JOptionPane.showMessageDialog(null, "erro - " + e); // TODO: handle exception } } }
public PanelRelatorioTotalGasto() { JScrollPane scrollPane = new JScrollPane(); JButton btnPesquisar = new JButton("Pesquisar"); btnPesquisar.setIcon(new ImageIcon("imagens\\8391_16x16.png")); btnPesquisar.setFont(new Font("Tahoma", Font.PLAIN, 15)); cmbDataInicio.addActionListener( new ActionListener() { //// nao sei pq ta chamando dois eventos public void actionPerformed(ActionEvent arg0) { if (cmbDataInicio.getSelectedItem() != null) { flagCMBDataInicio--; // flag decrementa quando e recebido uma acao no combo if (flagCMBDataInicio == 0) { flagCMBDataInicio = 2; if (cmbDataFinal.getSelectedItem() != null) { Date dataInicio = util.getCMBData(cmbDataInicio); Date dataFinal = util.getCMBData(cmbDataFinal); if (dataInicio.getTime() <= dataFinal.getTime()) { // se data inicio for menor que final atualiza combo AtualizaComboData(); } } } } } }); cmbDataFinal.addActionListener( new ActionListener() { // nao sei pq ta chamando dois eventos public void actionPerformed(ActionEvent arg0) { if (cmbDataFinal.getSelectedItem() != null) { flagCMBDataFinal--; // decrementa quando recebe uma acao no combo if (flagCMBDataFinal == 0) { flagCMBDataFinal = 2; // volta ao valor inicial if (cmbDataInicio.getSelectedItem() != null) { Date dataInicio = util.getCMBData(cmbDataInicio); Date dataFinal = util.getCMBData(cmbDataFinal); if (dataInicio.getTime() <= dataFinal .getTime()) { // se data inicio for menor que data final atualiza combo AtualizaComboData(); } } } } } }); cmbMarca.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (flagCMBMarca && cmbMarca.getSelectedIndex() == 0) { System.out.println("igual 0 "); cmbModelo.setSelectedIndex(0); cmbPlaca.setSelectedIndex(0); } if (cmbMarca.getSelectedIndex() > 0) { System.out.println("igual 1"); flagCMBMarca = true; } } }); cmbPlaca.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (cmbPlaca.getSelectedIndex() > 0) { AtualizaComboPlaca(cmbPlaca.getSelectedIndex()); flagCMBPlaca = true; } if (cmbPlaca.getSelectedIndex() == 0 && flagCMBPlaca) { AtualizaComboPlaca(cmbPlaca.getSelectedIndex()); } } }); btnPesquisar.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent arg0) { System.out.println(cmbDataInicio.getSelectedIndex()); try { if (cmbDataInicio.getSelectedIndex() < 0 || cmbDataFinal.getSelectedIndex() < 0) { JOptionPane.showMessageDialog(null, "Favor preecher o campo data"); } else { gerarConsulta(); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); if (!usuarioLogado.ehAdministrador()) { btnPesquisar.setVisible(false); } else { btnPesquisar.setVisible(true); } table = new JTable(); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) {} }); table.setModel( new DefaultTableModel( new Object[][] {}, new String[] { "Placa", "Od\u00F4metro", "Fornecedor", "Tipo Servico", "Data", "Valor" })); table.getColumnModel().getColumn(0).setPreferredWidth(51); table.getColumnModel().getColumn(2).setPreferredWidth(170); table.getColumnModel().getColumn(3).setPreferredWidth(141); table.getColumnModel().getColumn(4).setPreferredWidth(54); table.getColumnModel().getColumn(5).setPreferredWidth(83); table.setAutoCreateRowSorter(true); scrollPane.setViewportView(table); JButton btnImprimirRelatrio = new JButton("Imprimir Relat\u00F3rio"); btnImprimirRelatrio.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { listaServicos.clear(); for (int i = 0; i < table.getRowCount(); i++) { totalGastoServicos veiculoServico = new totalGastoServicos(); veiculoServico.setPlaca(table.getValueAt(i, 0).toString()); veiculoServico.setOdometro(table.getValueAt(i, 1).toString()); veiculoServico.setFornecedor(table.getValueAt(i, 2).toString()); veiculoServico.setTipoServico(table.getValueAt(i, 3).toString()); veiculoServico.setData(table.getValueAt(i, 4).toString()); veiculoServico.setValor(table.getValueAt(i, 5).toString()); veiculoServico.setTotal(txtTotal.getText().toString()); listaServicos.add(veiculoServico); } JasperReport report; try { report = JasperCompileManager.compileReport("relatorios/totalGastoServicos.jrxml"); JasperPrint print = JasperFillManager.fillReport( report, null, new JRBeanCollectionDataSource(listaServicos)); JasperViewer jrviewer = new JasperViewer(print, false); jrviewer.show(); System.out.println("Relatório gerado."); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); btnImprimirRelatrio.setFont(new Font("Tahoma", Font.PLAIN, 15)); JLabel lblTotal = new JLabel("TOTAL:"); lblTotal.setFont(new Font("Tahoma", Font.PLAIN, 15)); txtTotal = new JTextField(); txtTotal.setHorizontalAlignment(SwingConstants.RIGHT); txtTotal.setForeground(Color.RED); txtTotal.setFont(new Font("Tahoma", Font.BOLD, 14)); txtTotal.setEditable(false); txtTotal.setColumns(10); GroupLayout groupLayout = new GroupLayout(this); groupLayout.setHorizontalGroup( groupLayout .createParallelGroup(Alignment.TRAILING) .addGroup( groupLayout .createSequentialGroup() .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addGroup( groupLayout .createSequentialGroup() .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addGroup( groupLayout .createSequentialGroup() .addGap(10) .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent( cmbDataInicio, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE) .addComponent( lblInicio, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent( cmbDataFinal, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE) .addComponent( lblFim, GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup( groupLayout .createParallelGroup(Alignment.TRAILING) .addComponent( cmbPlaca, 0, 79, Short.MAX_VALUE) .addComponent( lblPlaca, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE))) .addGroup( groupLayout .createSequentialGroup() .addContainerGap() .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent( cmbTipoServico, 0, 237, Short.MAX_VALUE) .addComponent( lblTipoDeServico, GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)))) .addPreferredGap(ComponentPlacement.RELATED) .addGroup( groupLayout .createParallelGroup(Alignment.TRAILING) .addGroup( groupLayout .createSequentialGroup() .addGroup( groupLayout .createParallelGroup(Alignment.TRAILING) .addComponent( lblModelo, GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE) .addComponent( cmbMarca, 0, 67, Short.MAX_VALUE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup( groupLayout .createParallelGroup(Alignment.TRAILING) .addComponent( lblMarca, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( cmbModelo, 0, 90, Short.MAX_VALUE)) .addPreferredGap(ComponentPlacement.RELATED) .addGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addComponent( lblMotorista, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( cmbMotorista, 0, 104, Short.MAX_VALUE))) .addComponent(cmbFornecedor, 0, 273, Short.MAX_VALUE) .addComponent( lblFornecedor, GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE))) .addGroup( groupLayout .createSequentialGroup() .addContainerGap() .addGroup( groupLayout .createParallelGroup(Alignment.TRAILING) .addComponent( scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE) .addGroup( groupLayout .createSequentialGroup() .addComponent( btnPesquisar, GroupLayout.PREFERRED_SIZE, 139, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(btnImprimirRelatrio) .addGap(18) .addComponent( lblTotal, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE) .addPreferredGap( ComponentPlacement.RELATED, 9, Short.MAX_VALUE) .addComponent( txtTotal, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE))))) .addGap(1))); groupLayout.setVerticalGroup( groupLayout .createParallelGroup(Alignment.LEADING) .addGroup( groupLayout .createSequentialGroup() .addGap(11) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent( lblInicio, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( lblFim, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblPlaca) .addComponent(lblMarca) .addComponent(lblModelo) .addComponent(lblMotorista)) .addGap(6) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent( cmbDataInicio, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( cmbDataFinal, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( cmbModelo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent( cmbMotorista, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent( cmbMarca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent( cmbPlaca, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(6) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent(lblTipoDeServico) .addComponent(lblFornecedor)) .addGap(1) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent( cmbTipoServico, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent( cmbFornecedor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(ComponentPlacement.UNRELATED) .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE) .addPreferredGap(ComponentPlacement.RELATED) .addGroup( groupLayout .createParallelGroup(Alignment.BASELINE) .addComponent(btnPesquisar) .addComponent(btnImprimirRelatrio) .addComponent( txtTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(lblTotal)) .addContainerGap())); setLayout(groupLayout); }