Skip to content

Commit

Permalink
Edição de Usuário
Browse files Browse the repository at this point in the history
  • Loading branch information
higorcavalcanti committed Jun 18, 2016
1 parent d340e54 commit 95a8482
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/com/spej/dao/Dao.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.spej.dao;

import com.spej.persistencia.ConnectionFactory;
import com.spej.view.Mensagem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -106,7 +107,6 @@ public T getByPreparedStatement( PreparedStatement stmt ) {
//throw new RuntimeException("Usuario não encontrado!");
if(rs.next()) {
T result = this.byResultSet(rs);

stmt.close();
return result;
}
Expand Down
16 changes: 16 additions & 0 deletions src/com/spej/dao/DepartamentoDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.spej.dao;

import com.spej.model.Departamento;
import com.spej.view.Mensagem;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -106,4 +107,19 @@ public Departamento byResultSet(ResultSet rs) {
}
return d;
}

public Departamento getById(int id) {
String sql = "SELECT * " +
"FROM Departamentos " +
"WHERE id = ? " +
"LIMIT 1";
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setInt(1, id);
return this.getByPreparedStatement(stmt);
}
catch(SQLException e) {
throw new RuntimeException("Erro desconhecido!\nMensagem:\n" + e.getMessage());
}
}
}
2 changes: 1 addition & 1 deletion src/com/spej/model/Departamento.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public void setNome(String nome) {

@Override
public String toString() {
return this.getNome();
return this.getId() + " - " + this.getNome();
}
}
1 change: 0 additions & 1 deletion src/com/spej/model/DepartamentoComboBoxModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ public void addListDataListener(ListDataListener l) {
public void removeListDataListener(ListDataListener l) {

}

}
18 changes: 17 additions & 1 deletion src/com/spej/model/DepartamentoTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,24 @@ public Object getValueAt(int rowIndex, int columnIndex) {
return "";
}

@Override
public String getColumnName(int column) {
if(column == 0) return "ID";
else return "Nome";
}


@Override
public boolean isCellEditable(int row, int col) {
return false;
}
}

public Departamento getRow(int rowIndex) {
try {
return this.deps.get(rowIndex);
}
catch(IndexOutOfBoundsException e) {
}
return null;
}
}
7 changes: 7 additions & 0 deletions src/com/spej/model/PontoTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ else if(columnIndex == 2) {
return "";
}

@Override
public String getColumnName(int column) {
if(column == 0) return "Matricula";
else if(column == 1) return "Nome";
else return "Horário de entrada";
}

@Override
public boolean isCellEditable(int row, int col) {
return false;
Expand Down
68 changes: 68 additions & 0 deletions src/com/spej/model/UsuarioTableModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.spej.model;

import com.spej.dao.UsuarioDao;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
*
* @author Higor
*/
public class UsuarioTableModel extends AbstractTableModel {

private ArrayList<Usuario> users;

public UsuarioTableModel() {
this( new UsuarioDao().getAll("matricula > 0") );
}
public UsuarioTableModel(ArrayList<Usuario> users) {
super();
this.users = users;
}

@Override
public int getRowCount() {
return this.users.size();
}
@Override
public int getColumnCount() {
return 2;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
try {
if(columnIndex == 0) return this.users.get(rowIndex).getMatricula();
else if(columnIndex == 1) return this.users.get(rowIndex).getNome();
}
catch(IndexOutOfBoundsException e) {
}
return "";
}

@Override
public String getColumnName(int column) {
if(column == 0) return "Matricula";
else return "Nome";
}


@Override
public boolean isCellEditable(int row, int col) {
return false;
}

public Usuario getRowObject(int rowIndex) {
try {
return this.users.get(rowIndex);
}
catch(IndexOutOfBoundsException e) {
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/com/spej/view/CadastrarUsuario.form
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@
</Component>
<Component class="javax.swing.JButton" name="jBotaoAcao">
<Properties>
<Property name="text" type="java.lang.String" value="Cadastrar"/>
<Property name="text" type="java.lang.String" value="Salvar"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jBotaoAcaoActionPerformed"/>
Expand Down
60 changes: 43 additions & 17 deletions src/com/spej/view/CadastrarUsuario.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.spej.view;

import com.spej.controller.UsuarioController;
import com.spej.dao.DepartamentoDao;
import com.spej.model.Departamento;
import com.spej.model.DepartamentoComboBoxModel;
import com.spej.model.Usuario;
Expand All @@ -19,24 +20,45 @@
*/
public class CadastrarUsuario extends javax.swing.JDialog {

private Usuario user;
private boolean criando;

/**
* Creates new form CadastroFuncionario
*/
public CadastrarUsuario() {
this( new Usuario() );
this.criando = true;
}
public CadastrarUsuario(Usuario usuario) {

this.criando = false;
this.user = usuario;

initComponents();
setLocationRelativeTo( null ); // Centralizar a tela no meio
setLocationRelativeTo( null ); // Centralizar a tela no meio

limparCampos();
}

//Limpa os campos após o cadastro
public void limparCampos(){
jTextMatricula.setText(null);
jTextNome.setText(null);
jDataNascimento.setText(null);
jTextCargo.setText(null);
jTextEmail.setText(null);
jTextUsername.setText(null);
jPasswordField1.setText(null);
jComboDepartamento.setSelectedItem(null);

SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
String date = DATE_FORMAT.format( this.user.getNascimento().getTime() );

DepartamentoDao dd = new DepartamentoDao();
Departamento d = dd.getById( this.user.getDepartamento() );

jTextMatricula.setText( this.user.getMatricula() + "" );
jTextNome.setText( this.user.getNome() );
jDataNascimento.setText( date );
jTextCargo.setText( this.user.getCargo() );
jTextEmail.setText( this.user.getEmail() );
jTextUsername.setText( this.user.getUsername() );
jPasswordField1.setText( this.user.getPassword() );
jComboDepartamento.getModel().setSelectedItem( d );

}


Expand All @@ -61,7 +83,7 @@ private void initComponents() {
jTextCargo = new javax.swing.JTextField();
jLabelCargo = new javax.swing.JLabel();
jLabelDepartamento = new javax.swing.JLabel();
jComboDepartamento = new javax.swing.JComboBox<>();
jComboDepartamento = new javax.swing.JComboBox<DepartamentoComboBoxModel>();
jBotaoAcao = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabelUsuario = new javax.swing.JLabel();
Expand Down Expand Up @@ -136,7 +158,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

jBotaoAcao.setText("Cadastrar");
jBotaoAcao.setText("Salvar");
jBotaoAcao.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jBotaoAcaoActionPerformed(evt);
Expand Down Expand Up @@ -335,16 +357,20 @@ private void jBotaoAcaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI
usuario.setNascimento( new java.sql.Date(dateFormat.parse(jDataNascimento.getText()).getTime()) );
usuario.setEmail( jTextEmail.getText() );

uc.insert(usuario);

Mensagem.sucesso(this, "Usuário cadastrado com sucesso!");
limparCampos();
if(this.criando) {
uc.insert(usuario);
}
else {
uc.update(user, usuario);
}
Mensagem.sucesso(this, "Usuário " + (criando ? "cadastrado" : "editado") + " com sucesso!");
dispose();
}
catch(RuntimeException e) {
Mensagem.erro(this, e.getMessage(), "Falha ao cadastrar usuário");
Mensagem.erro(this, e.getMessage(), "Falha ao " + (criando ? "criar" : "editar") + " usuário");
}
catch(Exception e) {
Mensagem.erro(this, "Erro desconhecido!\n" + e.getMessage(), "Falha ao cadastrar usuário");
Mensagem.erro(this, "Erro desconhecido!\n" + e.getMessage(), "Falha ao " + (criando ? "criar" : "editar") + " usuário");
}
}

Expand Down
17 changes: 10 additions & 7 deletions src/com/spej/view/GerenciarUsuario.form
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,12 @@

<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTable" name="jTb">
<Component class="javax.swing.JTable" name="jTableUsuarios">
<Properties>
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.editors2.TableModelEditor">
<Table columnCount="2" rowCount="10">
<Column editable="true" title="C&#xf3;digo" type="java.lang.Object"/>
<Column editable="true" title="Nome" type="java.lang.Object"/>
</Table>
<Property name="model" type="javax.swing.table.TableModel" editor="org.netbeans.modules.form.RADConnectionPropertyEditor">
<Connection code=" new com.spej.model.UsuarioTableModel() " type="code"/>
</Property>
<Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor">
<Property name="columnModel" type="javax.swing.table.TableColumnModel" editor="org.netbeans.modules.form.editors2.TableColumnModelEditor" postCode="jTableUsuarios.getSelectionModel().addListSelectionListener(new ListSelectionListener(){&#xa; public void valueChanged(ListSelectionEvent event) {&#xa; if(jTableUsuarios.getSelectedRowCount() &gt; 0) {&#xa; jBotaoEditar.setEnabled(true); &#xa; jBotaoExcluir.setEnabled(true);&#xa; } else {&#xa; jBotaoEditar.setEnabled(false); &#xa; jBotaoExcluir.setEnabled(false); &#xa; }&#xa; }&#xa;});">
<TableColumnModel selectionModel="0">
<Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true">
<Title/>
Expand All @@ -203,12 +200,18 @@
<Property name="text" type="java.lang.String" value="Editar"/>
<Property name="enabled" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jBotaoEditarActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="jBotaoExcluir">
<Properties>
<Property name="text" type="java.lang.String" value="Excluir"/>
<Property name="enabled" type="boolean" value="false"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jBotaoExcluirActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="jBotaoBuscar">
<Properties>
Expand Down
Loading

0 comments on commit 95a8482

Please sign in to comment.