Ahora el modelo de egreso se autocarga con el objeto de tipo egreso en vez de con su id
This commit is contained in:
@@ -31,6 +31,8 @@ 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;
|
||||
@@ -56,21 +58,33 @@ public class CajaController {
|
||||
|
||||
private void fillEgresosTable() {
|
||||
for(Egreso egreso: this.egresoDAO.findAll()){
|
||||
TipoEgreso tipoEgreso = tipoEgresoDAO.findById(egreso.getTipo()).get(0);
|
||||
view.getEgresosTableModel().addRow(egreso);
|
||||
}
|
||||
}
|
||||
|
||||
private void setUpViewEvents(){
|
||||
this.view.getGuardarButton().addActionListener(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();
|
||||
Egreso egreso = this.createEgreso(nro, descripcion, valor, tipo.getId());
|
||||
this.view.getEgresosTableModel().addRow(egreso);
|
||||
this.updateTotalEgresos();
|
||||
});
|
||||
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(){
|
||||
@@ -78,7 +92,7 @@ public class CajaController {
|
||||
this.view.getTotalEgresosField().setText(String.valueOf(total));
|
||||
}
|
||||
|
||||
private Egreso createEgreso(String nro, String descripcion, String valor, int tipo){
|
||||
private Egreso createEgreso(String nro, String descripcion, String valor, TipoEgreso tipo){
|
||||
Egreso egreso = new Egreso();
|
||||
egreso.setValor(Integer.valueOf(valor));
|
||||
egreso.setDescripcion(descripcion);
|
||||
|
||||
@@ -30,9 +30,9 @@ public class Egreso {
|
||||
private String nro;
|
||||
private String descripcion;
|
||||
private int valor;
|
||||
private int tipo;
|
||||
private TipoEgreso tipo;
|
||||
|
||||
public Egreso(int id, String nro, String descripcion, int valor, int tipo) {
|
||||
public Egreso(int id, String nro, String descripcion, int valor, TipoEgreso tipo) {
|
||||
this.id = id;
|
||||
this.nro = nro;
|
||||
this.descripcion = descripcion;
|
||||
@@ -40,7 +40,7 @@ public class Egreso {
|
||||
this.tipo = tipo;
|
||||
}
|
||||
|
||||
public Egreso(String nro, String descripcion, int valor, int tipo) {
|
||||
public Egreso(String nro, String descripcion, int valor, TipoEgreso tipo) {
|
||||
this.nro = nro;
|
||||
this.descripcion = descripcion;
|
||||
this.valor = valor;
|
||||
@@ -81,11 +81,11 @@ public class Egreso {
|
||||
this.valor = valor;
|
||||
}
|
||||
|
||||
public int getTipo() {
|
||||
public TipoEgreso getTipo() {
|
||||
return tipo;
|
||||
}
|
||||
|
||||
public void setTipo(int tipo) {
|
||||
public void setTipo(TipoEgreso tipo) {
|
||||
this.tipo = tipo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ package danielcortes.xyz.models.mysql;
|
||||
import danielcortes.xyz.data.MysqlConnection;
|
||||
import danielcortes.xyz.models.Egreso;
|
||||
import danielcortes.xyz.models.EgresoDAO;
|
||||
import danielcortes.xyz.models.TipoEgreso;
|
||||
import danielcortes.xyz.models.TipoEgresoDAO;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -110,7 +112,7 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
ps.setString(1,egreso.getNro());
|
||||
ps.setString(2,egreso.getDescripcion());
|
||||
ps.setInt(3,egreso.getValor());
|
||||
ps.setInt(4,egreso.getTipo());
|
||||
ps.setInt(4,egreso.getTipo().getId());
|
||||
updates = ps.executeUpdate();
|
||||
ps.close();
|
||||
conn.close();
|
||||
@@ -130,7 +132,7 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
ps.setString(1,egreso.getNro());
|
||||
ps.setString(2,egreso.getDescripcion());
|
||||
ps.setInt(3,egreso.getValor());
|
||||
ps.setInt(4,egreso.getTipo());
|
||||
ps.setInt(4,egreso.getTipo().getId());
|
||||
ps.setInt(5, egreso.getId());
|
||||
updates = ps.executeUpdate();
|
||||
ps.close();
|
||||
@@ -179,16 +181,20 @@ public class MysqlEgresoDAO implements EgresoDAO {
|
||||
return total;
|
||||
}
|
||||
|
||||
|
||||
private List<Egreso> EgresosFromResultSet(ResultSet rs) throws SQLException {
|
||||
ArrayList<Egreso> egresoList = new ArrayList<>();
|
||||
while(rs.next()){
|
||||
int tipoEgresoId = rs.getInt("tipo_id");
|
||||
TipoEgresoDAO tipoEgresoDAO = new MysqlTipoEgresoDAO();
|
||||
TipoEgreso tipoEgreso = tipoEgresoDAO.findById(tipoEgresoId).get(0);
|
||||
|
||||
Egreso egreso = new Egreso();
|
||||
|
||||
egreso.setId(rs.getInt("id"));
|
||||
egreso.setNro(rs.getString("nro"));
|
||||
egreso.setDescripcion(rs.getString("descripcion"));
|
||||
egreso.setValor(rs.getInt("valor"));
|
||||
egreso.setTipo(rs.getInt("tipo_id"));
|
||||
egreso.setTipo(tipoEgreso);
|
||||
egresoList.add(egreso);
|
||||
}
|
||||
return egresoList;
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
<grid id="27dc6" binding="contentPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="15" left="15" bottom="15" right="15"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="1074" height="403"/>
|
||||
<xy x="20" y="20" width="860" height="487"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<grid id="9047a" binding="egresosPanel" layout-manager="GridLayoutManager" row-count="4" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<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">
|
||||
<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="5" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
<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"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
@@ -95,15 +95,23 @@
|
||||
</scrollpane>
|
||||
<component id="c5738" class="javax.swing.JButton" binding="guardarButton" default-binding="true">
|
||||
<constraints>
|
||||
<grid row="1" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
|
||||
<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"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Añadir"/>
|
||||
<text value="&Añadir"/>
|
||||
</properties>
|
||||
</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"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Total Egresos:"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="2b8d0" class="javax.swing.JTextField" binding="totalEgresosField">
|
||||
<constraints>
|
||||
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
|
||||
<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">
|
||||
<preferred-size width="150" height="-1"/>
|
||||
</grid>
|
||||
</constraints>
|
||||
@@ -111,12 +119,12 @@
|
||||
<editable value="false"/>
|
||||
</properties>
|
||||
</component>
|
||||
<component id="3442d" class="javax.swing.JLabel">
|
||||
<component id="ee598" class="javax.swing.JButton" binding="eliminarButton" default-binding="true">
|
||||
<constraints>
|
||||
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
|
||||
<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"/>
|
||||
</constraints>
|
||||
<properties>
|
||||
<text value="Total Egresos:"/>
|
||||
<text value="&Eliminar"/>
|
||||
</properties>
|
||||
</component>
|
||||
</children>
|
||||
@@ -125,5 +133,7 @@
|
||||
</grid>
|
||||
<inspectionSuppressions>
|
||||
<suppress inspection="FormSpellChecking"/>
|
||||
<suppress inspection="I18nForm"/>
|
||||
<suppress inspection="NoLabelFor"/>
|
||||
</inspectionSuppressions>
|
||||
</form>
|
||||
|
||||
@@ -42,6 +42,9 @@ public class EgresosView {
|
||||
private JTextField totalEgresosField;
|
||||
private JComboBox<TipoEgreso> tipoCombo;
|
||||
|
||||
|
||||
private JButton eliminarButton;
|
||||
|
||||
private EgresosTableModel egresosTableModel;
|
||||
|
||||
private void createUIComponents() {
|
||||
@@ -52,6 +55,7 @@ public class EgresosView {
|
||||
egresosTableModel = new EgresosTableModel();
|
||||
egresosTable = new JTable(egresosTableModel);
|
||||
egresosTable.setAutoCreateRowSorter(true);
|
||||
egresosTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||
}
|
||||
|
||||
public JPanel getContentPanel() {
|
||||
@@ -62,6 +66,10 @@ public class EgresosView {
|
||||
return guardarButton;
|
||||
}
|
||||
|
||||
public JButton getEliminarButton() {
|
||||
return eliminarButton;
|
||||
}
|
||||
|
||||
public JTextField getValorField() {
|
||||
return valorField;
|
||||
}
|
||||
@@ -82,7 +90,16 @@ public class EgresosView {
|
||||
return tipoCombo;
|
||||
}
|
||||
|
||||
public JTable getEgresosTable() {
|
||||
return egresosTable;
|
||||
}
|
||||
|
||||
public void setEgresosTable(JTable egresosTable) {
|
||||
this.egresosTable = egresosTable;
|
||||
}
|
||||
|
||||
public EgresosTableModel getEgresosTableModel() {
|
||||
return egresosTableModel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,6 +56,11 @@ public class EgresosTableModel extends AbstractTableModel {
|
||||
this.fireTableRowsInserted(0,getRowCount()-1);
|
||||
}
|
||||
|
||||
public void removeRow(int row){
|
||||
this.rows.remove(row);
|
||||
this.fireTableRowsDeleted(0,getRowCount()-1);
|
||||
}
|
||||
|
||||
public Object getValueAt(int row, int col) {
|
||||
switch (col){
|
||||
case 0:
|
||||
@@ -69,4 +74,8 @@ public class EgresosTableModel extends AbstractTableModel {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Egreso getEgreso(int row){
|
||||
return rows.get(row);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user