Resubido todo
This commit is contained in:
319
src/danielcortes/xyz/controllers/EgresosController.java
Normal file
319
src/danielcortes/xyz/controllers/EgresosController.java
Normal file
@@ -0,0 +1,319 @@
|
||||
/*
|
||||
* 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.caja.Caja;
|
||||
import danielcortes.xyz.models.egreso.Egreso;
|
||||
import danielcortes.xyz.models.egreso.EgresoDAO;
|
||||
import danielcortes.xyz.models.tipo_egreso.TipoEgreso;
|
||||
import danielcortes.xyz.models.tipo_egreso.TipoEgresoDAO;
|
||||
import danielcortes.xyz.views.EgresosView;
|
||||
import danielcortes.xyz.views.components.EgresosTableModel;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.event.KeyAdapter;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class EgresosController {
|
||||
private EgresosView view;
|
||||
private EgresoDAO egresoDAO;
|
||||
private TipoEgresoDAO tipoEgresoDAO;
|
||||
private Caja caja;
|
||||
|
||||
private int editingId;
|
||||
private boolean editing;
|
||||
private Egreso editingEgreso;
|
||||
|
||||
public EgresosController(EgresosView view, EgresoDAO egresoDAO, TipoEgresoDAO tipoEgresoDAO) {
|
||||
this.view = view;
|
||||
this.egresoDAO = egresoDAO;
|
||||
this.tipoEgresoDAO = tipoEgresoDAO;
|
||||
this.setUpViewEvents();
|
||||
this.fillTipoEgresoCombo();
|
||||
this.updateButtonsEnabled();
|
||||
}
|
||||
|
||||
public EgresoDAO getEgresoDAO() {
|
||||
return egresoDAO;
|
||||
}
|
||||
|
||||
public TipoEgresoDAO getTipoEgresoDAO() {
|
||||
return tipoEgresoDAO;
|
||||
}
|
||||
|
||||
public void updateCaja(Caja caja){
|
||||
this.caja = caja;
|
||||
this.fillEgresosTable();
|
||||
this.updateTotalEgresos();
|
||||
}
|
||||
|
||||
private void fillTipoEgresoCombo() {
|
||||
JComboBox<TipoEgreso> tipoCombo = view.getTipoCombo();
|
||||
for (TipoEgreso tipoEgreso : this.tipoEgresoDAO.findAll()) {
|
||||
tipoCombo.addItem(tipoEgreso);
|
||||
}
|
||||
}
|
||||
|
||||
private void fillEgresosTable() {
|
||||
EgresosTableModel egresosTableModel = view.getEgresosTableModel();
|
||||
egresosTableModel.removeRows();
|
||||
for (Egreso egreso : this.egresoDAO.findByCaja(this.caja)) {
|
||||
egresosTableModel.addRow(egreso);
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpViewEvents() {
|
||||
this.view.getEgresosTable().getSelectionModel().addListSelectionListener(e -> onSelectTableRowListener());
|
||||
this.view.getGuardarButton().addActionListener(e -> guardarActionListener());
|
||||
this.view.getEliminarButton().addActionListener(e -> eliminarActionListener());
|
||||
this.view.getEditarButton().addActionListener(e -> editarActionListener());
|
||||
this.view.getDescripcionField().addActionListener(e -> guardarActionListener());
|
||||
this.view.getNroField().addActionListener(e -> guardarActionListener());
|
||||
this.view.getValorField().addActionListener(e -> guardarActionListener());
|
||||
this.view.getTipoCombo().addKeyListener(new KeyAdapter() {
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
guardarActionListener();
|
||||
}
|
||||
}
|
||||
});
|
||||
this.view.getEgresosTable().addMouseListener(new MouseAdapter() {
|
||||
public void mouseClicked(MouseEvent mouseEvent) {
|
||||
JTable table = (JTable) mouseEvent.getSource();
|
||||
if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) {
|
||||
EgresosController.this.editarActionListener();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void guardarActionListener() {
|
||||
this.normalizeInputs();
|
||||
|
||||
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(editing){
|
||||
this.editarEgreso(nro, descripcion, valor, tipo, this.caja);
|
||||
}else {
|
||||
this.guardarEgreso(nro, descripcion, valor, tipo, this.caja);
|
||||
}
|
||||
this.resetFocus();
|
||||
}
|
||||
|
||||
private void eliminarActionListener() {
|
||||
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();
|
||||
this.updateButtonsEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
private void editarActionListener() {
|
||||
int selectedID = this.view.getEgresosTable().getSelectedRow();
|
||||
if (selectedID >= 0) {
|
||||
Egreso egreso = this.view.getEgresosTableModel().getEgreso(selectedID);
|
||||
|
||||
this.editingId = selectedID;
|
||||
this.editingEgreso = egreso;
|
||||
this.editing = true;
|
||||
|
||||
this.view.getNroField().setText(egreso.getNro());
|
||||
this.view.getDescripcionField().setText(egreso.getDescripcion());
|
||||
this.view.getValorField().setText(String.valueOf(egreso.getValor()));
|
||||
this.view.getTipoCombo().setSelectedItem(egreso.getTipoEgreso());
|
||||
}
|
||||
}
|
||||
|
||||
private void onSelectTableRowListener() {
|
||||
this.view.getEliminarButton().setEnabled(true);
|
||||
this.view.getEditarButton().setEnabled(true);
|
||||
}
|
||||
|
||||
private void updateTotalEgresos() {
|
||||
int total = this.egresoDAO.getTotalEgreso(this.caja);
|
||||
this.view.getTotalEgresosField().setText(String.valueOf(total));
|
||||
}
|
||||
|
||||
private void updateButtonsEnabled() {
|
||||
if (this.view.getEgresosTable().getSelectedRow() >= 0) {
|
||||
this.view.getEliminarButton().setEnabled(true);
|
||||
this.view.getEditarButton().setEnabled(true);
|
||||
} else {
|
||||
this.view.getEliminarButton().setEnabled(false);
|
||||
this.view.getEditarButton().setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void guardarEgreso(String nro, String descripcion, String valor, TipoEgreso tipo, Caja caja) {
|
||||
if (this.validateInput(nro, descripcion, valor, tipo, caja)) {
|
||||
Egreso egreso = new Egreso();
|
||||
egreso.setValor(Integer.valueOf(valor));
|
||||
egreso.setDescripcion(descripcion);
|
||||
egreso.setNro(nro);
|
||||
egreso.setTipoEgreso(tipo);
|
||||
egreso.setCaja(caja);
|
||||
egresoDAO.insertEgreso(egreso);
|
||||
this.view.getEgresosTableModel().addRow(egreso);
|
||||
this.updateTotalEgresos();
|
||||
this.clearInputs();
|
||||
}
|
||||
}
|
||||
|
||||
private void editarEgreso(String nro, String descripcion, String valor, TipoEgreso tipo, Caja caja) {
|
||||
if (this.validateInput(nro, descripcion, valor, tipo, caja)) {
|
||||
this.editingEgreso.setValor(Integer.valueOf(valor));
|
||||
this.editingEgreso.setDescripcion(descripcion);
|
||||
this.editingEgreso.setNro(nro);
|
||||
this.editingEgreso.setTipoEgreso(tipo);
|
||||
egresoDAO.updateEgreso(this.editingEgreso);
|
||||
this.view.getEgresosTableModel().setEgreso(this.editingId, this.editingEgreso);
|
||||
this.updateTotalEgresos();
|
||||
this.clearInputs();
|
||||
this.editing = false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean validateInput(String nro, String descripcion, String valor, TipoEgreso tipoEgreso, Caja caja) {
|
||||
this.hideErrorMessages();
|
||||
|
||||
boolean nroValidation = this.validateNro(nro);
|
||||
boolean descripcionValidation = this.validateDescripcion(descripcion);
|
||||
boolean valorValidation = this.validateValor(valor);
|
||||
boolean tipoEgresoValidation = this.validateTipoEgreso(tipoEgreso);
|
||||
boolean cajaValidation = this.validateCaja(caja);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (valor.length() > 10) {
|
||||
this.view.getErrorValor().setText("El numero ingresado es demasiado largo");
|
||||
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 boolean validateCaja(Caja caja){
|
||||
return caja != null;
|
||||
}
|
||||
|
||||
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("");
|
||||
}
|
||||
|
||||
private void normalizeInputs(){
|
||||
this.view.getValorField().setText(this.view.getValorField().getText().trim());
|
||||
this.view.getNroField().setText(this.view.getNroField().getText().trim());
|
||||
this.view.getDescripcionField().setText(this.view.getDescripcionField().getText().trim());
|
||||
|
||||
}
|
||||
|
||||
private void resetFocus() {
|
||||
this.view.getNroField().requestFocus();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user