Validacion cuando se guarda un egreso desde la vista

This commit is contained in:
Daniel Cortes
2018-12-21 16:33:25 -03:00
parent 8863e21c5c
commit 1fe2ee082b
5 changed files with 282 additions and 113 deletions

View File

@@ -1,106 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2018 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.controllers;
import danielcortes.xyz.models.Egreso;
import danielcortes.xyz.models.EgresoDAO;
import danielcortes.xyz.models.TipoEgreso;
import danielcortes.xyz.models.TipoEgresoDAO;
import danielcortes.xyz.views.EgresosView;
import javax.swing.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class CajaController {
private EgresosView view;
private EgresoDAO egresoDAO;
private TipoEgresoDAO tipoEgresoDAO;
public CajaController(EgresosView view, EgresoDAO egresoDAO, TipoEgresoDAO tipoEgresoDAO){
this.view = view;
this.egresoDAO = egresoDAO;
this.tipoEgresoDAO = tipoEgresoDAO;
this.setUpViewEvents();
this.fillEgresosTable();
this.fillEgresosTipo();
this.updateTotalEgresos();
}
private void fillEgresosTipo() {
JComboBox<TipoEgreso> tipoCombo = view.getTipoCombo();
for(TipoEgreso tipoEgreso : this.tipoEgresoDAO.findAll()){
tipoCombo.addItem(tipoEgreso);
}
}
private void fillEgresosTable() {
for(Egreso egreso: this.egresoDAO.findAll()){
view.getEgresosTableModel().addRow(egreso);
}
}
private void setUpViewEvents(){
this.view.getGuardarButton().addActionListener(e -> guardarActionListener());
this.view.getEliminarButton().addActionListener(e -> eliminarActionListener());
}
private void guardarActionListener(){
String nro = this.view.getNroField().getText();
String descripcion = this.view.getDescripcionField().getText();
String valor = this.view.getValorField().getText();
TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem();
Egreso egreso = this.createEgreso(nro, descripcion, valor, tipo);
this.view.getEgresosTableModel().addRow(egreso);
this.updateTotalEgresos();
}
private void eliminarActionListener(){
int selectedID = this.view.getEgresosTable().getSelectedRow();
System.out.println(selectedID);
if(selectedID >= 0) {
Egreso egreso = this.view.getEgresosTableModel().getEgreso(selectedID);
this.view.getEgresosTableModel().removeRow(selectedID);
this.egresoDAO.deleteEgreso(egreso);
}
}
private void updateTotalEgresos(){
int total = this.egresoDAO.getTotalEgreso();
this.view.getTotalEgresosField().setText(String.valueOf(total));
}
private Egreso createEgreso(String nro, String descripcion, String valor, TipoEgreso tipo){
Egreso egreso = new Egreso();
egreso.setValor(Integer.valueOf(valor));
egreso.setDescripcion(descripcion);
egreso.setNro(nro);
egreso.setTipo(tipo);
egresoDAO.insertEgreso(egreso);
return egreso;
}
}

View File

@@ -0,0 +1,211 @@
/*
* MIT License
*
* Copyright (c) 2018 Daniel Cortes
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package danielcortes.xyz.controllers;
import danielcortes.xyz.models.Egreso;
import danielcortes.xyz.models.EgresoDAO;
import danielcortes.xyz.models.TipoEgreso;
import danielcortes.xyz.models.TipoEgresoDAO;
import danielcortes.xyz.views.EgresosView;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class EgresosController {
private EgresosView view;
private EgresoDAO egresoDAO;
private TipoEgresoDAO tipoEgresoDAO;
public EgresosController(EgresosView view, EgresoDAO egresoDAO, TipoEgresoDAO tipoEgresoDAO){
this.view = view;
this.egresoDAO = egresoDAO;
this.tipoEgresoDAO = tipoEgresoDAO;
this.setUpViewEvents();
this.fillEgresosTable();
this.fillEgresosTipo();
this.updateTotalEgresos();
}
private void fillEgresosTipo() {
JComboBox<TipoEgreso> tipoCombo = view.getTipoCombo();
for(TipoEgreso tipoEgreso : this.tipoEgresoDAO.findAll()){
tipoCombo.addItem(tipoEgreso);
}
}
private void fillEgresosTable() {
for(Egreso egreso: this.egresoDAO.findAll()){
view.getEgresosTableModel().addRow(egreso);
}
}
private void setUpViewEvents(){
this.view.getEgresosTable().getSelectionModel().addListSelectionListener(this::onSelectTableRowListener);
this.view.getGuardarButton().addActionListener(this::guardarActionListener);
this.view.getEliminarButton().addActionListener(this::eliminarActionListener);
this.view.getDescripcionField().addActionListener(this::guardarActionListener);
this.view.getNroField().addActionListener(this::guardarActionListener);
this.view.getValorField().addActionListener(this::guardarActionListener);
}
private void eliminarActionListener(ActionEvent e){
int selectedID = this.view.getEgresosTable().getSelectedRow();
if(selectedID >= 0) {
Egreso egreso = this.view.getEgresosTableModel().getEgreso(selectedID);
this.view.getEgresosTableModel().removeRow(selectedID);
this.egresoDAO.deleteEgreso(egreso);
this.updateTotalEgresos();
}
}
private void guardarActionListener(ActionEvent e){
String nro = this.view.getNroField().getText();
String descripcion = this.view.getDescripcionField().getText();
String valor = this.view.getValorField().getText();
TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem();
if(this.validateInput(nro, descripcion, valor, tipo)){
Egreso egreso = this.createEgreso(nro, descripcion, valor, tipo);
this.view.getEgresosTableModel().addRow(egreso);
this.updateTotalEgresos();
this.clearInputs();
}
}
private void onSelectTableRowListener(ListSelectionEvent e){
this.view.getEliminarButton().setEnabled(true);
}
private void updateTotalEgresos(){
int total = this.egresoDAO.getTotalEgreso();
this.view.getTotalEgresosField().setText(String.valueOf(total));
}
private Egreso createEgreso(String nro, String descripcion, String valor, TipoEgreso tipo){
Egreso egreso = new Egreso();
egreso.setValor(Integer.valueOf(valor));
egreso.setDescripcion(descripcion);
egreso.setNro(nro);
egreso.setTipo(tipo);
egresoDAO.insertEgreso(egreso);
return egreso;
}
private boolean validateInput(String nro, String descripcion, String valor, TipoEgreso tipoEgreso){
this.hideErrorMessages();
boolean nroValidation = this.validateNro(nro);
boolean descripcionValidation = this.validateDescripcion(descripcion);
boolean valorValidation = this.validateValor(valor);
boolean tipoEgresoValidation = this.validateTipoEgreso(tipoEgreso);
return nroValidation && descripcionValidation && valorValidation && tipoEgresoValidation;
}
private boolean validateNro(String nro){
if(nro == null){
this.view.getErrorNumero().setText("Hubo un problema con los datos");
this.view.getErrorNumero().setVisible(true);
return false;
}
nro = nro.trim();
if(nro.isEmpty()){
this.view.getErrorNumero().setText("El campo esta vacio");
this.view.getErrorNumero().setVisible(true);
return false;
}
return true;
}
private boolean validateDescripcion(String descripcion){
if(descripcion == null){
this.view.getErrorDescripcion().setText("Hubo un problema con los datos");
this.view.getErrorDescripcion().setVisible(true);
return false;
}
descripcion = descripcion.trim();
if(descripcion.isEmpty()){
this.view.getErrorDescripcion().setText("El campo esta vacio");
this.view.getErrorDescripcion().setVisible(true);
return false;
}
return true;
}
private boolean validateValor(String valor){
if(valor == null){
this.view.getErrorValor().setText("Hubo un problema con los datos");
this.view.getErrorValor().setVisible(true);
return false;
}
valor = valor.trim();
if(valor.isEmpty()){
this.view.getErrorValor().setText("El campo esta vacio");
this.view.getErrorValor().setVisible(true);
return false;
}
if(!valor.chars().allMatch(Character::isDigit)){
this.view.getErrorValor().setText("Deben ser numeros");
this.view.getErrorValor().setVisible(true);
return false;
}
return true;
}
private boolean validateTipoEgreso(TipoEgreso tipoEgreso){
if(tipoEgreso == null){
this.view.getErrorTipoEgreso().setText("Hubo un problema con los datos");
this.view.getErrorTipoEgreso().setVisible(true);
return false;
}
return true;
}
private void hideErrorMessages(){
this.view.getErrorTipoEgreso().setVisible(false);
this.view.getErrorValor().setVisible(false);
this.view.getErrorDescripcion().setVisible(false);
this.view.getErrorNumero().setVisible(false);
}
private void clearInputs(){
this.view.getTipoCombo().setSelectedIndex(0);
this.view.getNroField().setText("");
this.view.getValorField().setText("");
this.view.getDescripcionField().setText("");
}
}

View File

@@ -67,6 +67,6 @@ public class ManagerController {
this.view.getCardPanel().add(egresosView.getContentPanel(), "EGRESOS");
CajaController cajaController = new CajaController(egresosView, egresoDAO, tipoEgresoDAO);
EgresosController egresosController = new EgresosController(egresosView, egresoDAO, tipoEgresoDAO);
}
}

View File

@@ -8,7 +8,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="9047a" binding="egresosPanel" layout-manager="GridLayoutManager" row-count="4" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="9047a" binding="egresosPanel" layout-manager="GridLayoutManager" row-count="5" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -82,7 +82,7 @@
</component>
<scrollpane id="65bec">
<constraints>
<grid row="2" column="0" row-span="1" col-span="4" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="4" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -95,7 +95,7 @@
</scrollpane>
<component id="c5738" class="javax.swing.JButton" binding="guardarButton" default-binding="true">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Añadir"/>
@@ -103,7 +103,7 @@
</component>
<component id="3442d" class="javax.swing.JLabel">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Total Egresos:"/>
@@ -111,7 +111,7 @@
</component>
<component id="2b8d0" class="javax.swing.JTextField" binding="totalEgresosField">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="4" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
@@ -121,12 +121,57 @@
</component>
<component id="ee598" class="javax.swing.JButton" binding="eliminarButton" default-binding="true">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="false"/>
<text value="&amp;Eliminar"/>
</properties>
</component>
<component id="33b8a" class="javax.swing.JLabel" binding="errorNumero">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<foreground color="-65536"/>
<text value="Error"/>
<visible value="false"/>
</properties>
</component>
<component id="116af" class="javax.swing.JLabel" binding="errorDescripcion">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<foreground color="-65536"/>
<text value="Error"/>
<visible value="false"/>
</properties>
</component>
<component id="42cb" class="javax.swing.JLabel" binding="errorValor">
<constraints>
<grid row="2" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<foreground color="-65536"/>
<text value="Error"/>
<visible value="false"/>
</properties>
</component>
<component id="e586b" class="javax.swing.JLabel" binding="errorTipoEgreso">
<constraints>
<grid row="2" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
<foreground color="-65536"/>
<text value="Error"/>
<visible value="false"/>
</properties>
</component>
</children>
</grid>
</children>

View File

@@ -44,6 +44,10 @@ public class EgresosView {
private JButton eliminarButton;
private JLabel errorNumero;
private JLabel errorDescripcion;
private JLabel errorValor;
private JLabel errorTipoEgreso;
private EgresosTableModel egresosTableModel;
@@ -102,4 +106,19 @@ public class EgresosView {
return egresosTableModel;
}
public JLabel getErrorNumero() {
return errorNumero;
}
public JLabel getErrorDescripcion() {
return errorDescripcion;
}
public JLabel getErrorValor() {
return errorValor;
}
public JLabel getErrorTipoEgreso() {
return errorTipoEgreso;
}
}