Cambiado el egresotablemodel para que acepte un egreso como modelo para llenar la tabla

This commit is contained in:
Daniel Cortes
2018-12-20 23:16:30 -03:00
parent dd52ef3975
commit bc777d3e4c
3 changed files with 36 additions and 60 deletions

View File

@@ -57,12 +57,7 @@ public class CajaController {
private void fillEgresosTable() { private void fillEgresosTable() {
for(Egreso egreso: this.egresoDAO.findAll()){ for(Egreso egreso: this.egresoDAO.findAll()){
TipoEgreso tipoEgreso = tipoEgresoDAO.findById(egreso.getTipo()).get(0); TipoEgreso tipoEgreso = tipoEgresoDAO.findById(egreso.getTipo()).get(0);
view.getEgresosTableModel().addRow(new String[]{ view.getEgresosTableModel().addRow(egreso);
egreso.getNro(),
egreso.getDescripcion(),
String.valueOf(egreso.getValor()),
tipoEgreso.getNombre()
});
} }
} }
@@ -72,15 +67,10 @@ public class CajaController {
String descripcion = this.view.getDescripcionField().getText(); String descripcion = this.view.getDescripcionField().getText();
String valor = this.view.getValorField().getText(); String valor = this.view.getValorField().getText();
TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem(); TipoEgreso tipo = (TipoEgreso) this.view.getTipoCombo().getSelectedItem();
this.addToTable(nro, descripcion, valor, tipo.getNombre()); Egreso egreso = this.createEgreso(nro, descripcion, valor, tipo.getId());
this.createEgreso(nro, descripcion, valor, tipo.getId()); this.view.getEgresosTableModel().addRow(egreso);
this.updateTotalEgresos(); this.updateTotalEgresos();
}); });
}
private void addToTable(String nro, String descripcion, String valor, String tipo){
view.getEgresosTableModel().addRow(new String[]{nro,descripcion,valor,tipo});
} }
private void updateTotalEgresos(){ private void updateTotalEgresos(){
@@ -88,13 +78,14 @@ public class CajaController {
this.view.getTotalEgresosField().setText(String.valueOf(total)); this.view.getTotalEgresosField().setText(String.valueOf(total));
} }
private void createEgreso(String nro, String descripcion, String valor, int tipo){ private Egreso createEgreso(String nro, String descripcion, String valor, int tipo){
Egreso egreso = new Egreso(); Egreso egreso = new Egreso();
egreso.setValor(Integer.valueOf(valor)); egreso.setValor(Integer.valueOf(valor));
egreso.setDescripcion(descripcion); egreso.setDescripcion(descripcion);
egreso.setNro(nro); egreso.setNro(nro);
egreso.setTipo(tipo); egreso.setTipo(tipo);
egresoDAO.insertEgreso(egreso); egresoDAO.insertEgreso(egreso);
return egreso;
} }

View File

@@ -24,12 +24,19 @@
package danielcortes.xyz.views.components; package danielcortes.xyz.views.components;
public class EgresosTableModel extends TableModel { import danielcortes.xyz.models.Egreso;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
public class EgresosTableModel extends AbstractTableModel {
private String[] columns; private String[] columns;
private ArrayList<Egreso> rows;
public EgresosTableModel(){ public EgresosTableModel(){
super(); super();
this.columns = new String[]{"", "Descripcion", "Valor", "Tipo"}; this.columns = new String[]{"", "Descripcion", "Valor", "Tipo"};
this.rows = new ArrayList<>();
} }
public String getColumnName(int col) { public String getColumnName(int col) {
@@ -39,4 +46,27 @@ public class EgresosTableModel extends TableModel {
public int getColumnCount() { public int getColumnCount() {
return columns.length; return columns.length;
} }
public int getRowCount() {
return rows.size();
}
public void addRow(Egreso egreso) {
rows.add(egreso);
this.fireTableRowsInserted(0,getRowCount()-1);
}
public Object getValueAt(int row, int col) {
switch (col){
case 0:
return rows.get(row).getNro();
case 1:
return rows.get(row).getDescripcion();
case 2:
return rows.get(row).getValor();
case 3:
return rows.get(row).getTipo();
}
return null;
}
} }

View File

@@ -1,45 +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.views.components;
import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
public abstract class TableModel extends AbstractTableModel {
private ArrayList<String[]> rows = new ArrayList<>();
public int getRowCount() {
return rows.size();
}
public void addRow(String[] data){
rows.add(data);
this.fireTableRowsInserted(0,getRowCount()-1);
}
public Object getValueAt(int row, int col) {
return rows.get(row)[col];
}
}