Agregada una ventana para realizar el calculo del fondo de la caja, es wip, falta la aprobacion del jefe jajaj

This commit is contained in:
Daniel Cortes
2019-01-18 18:43:53 -03:00
parent 30de826ee2
commit 5a9455eb4b
15 changed files with 1744 additions and 467 deletions

View File

@@ -0,0 +1,191 @@
package danielcortes.xyz.controllers;
import danielcortes.xyz.controllers.actions.NextAction;
import danielcortes.xyz.models.caja.Caja;
import danielcortes.xyz.models.calculo_fondo.CalculoFondo;
import danielcortes.xyz.models.calculo_fondo.CalculoFondoDAO;
import danielcortes.xyz.views.CalcularFondoView;
import danielcortes.xyz.views.components.FondoTableModel;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class CalcularFondoController {
private JComponent parent;
private CalcularFondoView view;
private Caja caja;
private CalculoFondoDAO calculoFondoDAO;
private int editingId;
private boolean editing;
private CalculoFondo editingCalculoFondo;
public CalcularFondoController(JComponent parent, CalcularFondoView view, Caja caja, CalculoFondoDAO calculoFondoDAO) {
this.view = view;
this.parent = parent;
this.caja = caja;
this.calculoFondoDAO = calculoFondoDAO;
this.fillTable();
this.updateResumen();
this.setupViewEvents();
this.updateButtonsEnabled();
this.showView();
}
private void showView() {
JFrame frame = new JFrame("Calculo de Fondo");
frame.setContentPane(view.getContentPanel());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(this.parent);
frame.setVisible(true);
}
private void fillTable() {
FondoTableModel tableModel = this.view.getTableModel();
tableModel.removeRows();
for (CalculoFondo calculoFondo : this.calculoFondoDAO.findByCaja(this.caja)) {
tableModel.addRow(calculoFondo);
}
}
private void setupViewEvents() {
this.view.getValorField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "nextField");
this.view.getDescripcionField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "save");
this.view.getFondoField().getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ENTER"), "updateResumen");
this.view.getValorField().getActionMap().put("nextField", new NextAction(this.view.getDescripcionField()));
this.view.getDescripcionField().getActionMap().put("save", new GuardarAction());
this.view.getFondoField().getActionMap().put("updateResumen", new UpdateResumenAction());
this.view.getTable().getSelectionModel().addListSelectionListener(e -> updateButtonsEnabled());
this.view.getGuardarButton().addActionListener(e -> guardarActionListener());
this.view.getEditarButton().addActionListener(e -> editarActionListener());
this.view.getEliminarButton().addActionListener(e -> eliminarActionListener());
this.view.getTable().addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
JTable table = (JTable) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) {
CalcularFondoController.this.editarActionListener();
}
}
});
}
private void guardarActionListener() {
this.normalizeInput();
int valor = this.view.getValorField().getValue();
String descripcion = this.view.getDescripcionField().getText();
if (editing) {
this.editarCalculoFondo(valor, descripcion);
this.editing = false;
} else {
this.guardarCalculoFondo(valor, descripcion);
}
this.updateResumen();
this.cleanInput();
this.resetFocus();
}
private void editarActionListener() {
int selectedID = this.view.getTable().getSelectedRow();
if (selectedID >= 0) {
int selectedModelID = this.view.getTable().getRowSorter().convertRowIndexToModel(selectedID);
CalculoFondo calculoFondo = this.view.getTableModel().getCalculoFondo(selectedModelID);
this.editingId = selectedModelID;
this.editingCalculoFondo = calculoFondo;
this.editing = true;
this.view.getValorField().setValue(calculoFondo.getValor());
this.view.getDescripcionField().setText(calculoFondo.getDescripcion());
}
}
private void eliminarActionListener() {
int selectedID = this.view.getTable().getSelectedRow();
if (selectedID >= 0) {
CalculoFondo calculoFondo = this.view.getTableModel().getCalculoFondo(selectedID);
this.view.getTableModel().removeRow(selectedID);
this.calculoFondoDAO.deleteCalculoFondo(calculoFondo);
this.updateResumen();
this.updateButtonsEnabled();
this.resetFocus();
}
}
private void guardarCalculoFondo(int valor, String descripcion) {
CalculoFondo calculoFondo = new CalculoFondo();
calculoFondo.setValor(valor);
calculoFondo.setDescripcion(descripcion);
calculoFondo.setCaja(this.caja);
this.calculoFondoDAO.insertCalculoFondo(calculoFondo);
this.view.getTableModel().addRow(calculoFondo);
}
private void editarCalculoFondo(int valor, String descripcion) {
this.editingCalculoFondo.setValor(valor);
this.editingCalculoFondo.setDescripcion(descripcion);
this.editingCalculoFondo.setCaja(this.caja);
this.calculoFondoDAO.updateCalculoFondo(editingCalculoFondo);
this.view.getTableModel().setCalculoFondo(this.editingId, this.editingCalculoFondo);
}
private void updateButtonsEnabled() {
if (this.view.getTable().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 updateResumen() {
int fondo = this.view.getFondoField().getValue();
int suma = this.calculoFondoDAO.getTotalCalculoFondo(this.caja);
this.view.getSumaField().setValue(suma);
this.view.getDiferenciaField().setValue(suma - fondo);
}
private void cleanInput() {
this.view.getValorField().setValue(0);
this.view.getDescripcionField().setText("");
}
private void normalizeInput() {
if (this.view.getDescripcionField().getText() == null) {
this.view.getDescripcionField().setText("");
}
this.view.getDescripcionField().setText(this.view.getDescripcionField().getText().trim());
}
private void resetFocus() {
this.view.getValorField().requestFocus();
}
private class GuardarAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
CalcularFondoController.this.guardarActionListener();
}
}
private class UpdateResumenAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent e) {
CalcularFondoController.this.updateResumen();
}
}
}