Finalizado soporte de persistencia
This commit is contained in:
@@ -18,49 +18,52 @@ public class ControladorAniadirVisualizar implements ActionListener,ChangeListen
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(e.getActionCommand().equals(this.vista.boton.getActionCommand())) {
|
||||
String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso");
|
||||
if(nombre == null) return;
|
||||
if(nombre.equals("")) {
|
||||
JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String dinero = JOptionPane.showInputDialog("Introduce a cuanto asciende");
|
||||
if(dinero == null) return;
|
||||
if(dinero.equals("")) {
|
||||
JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.vista.aniadirElemento(nombre, Float.parseFloat(dinero),this);
|
||||
this.vista.menu.panel.actualizarDatos(this.vista.gestiones);
|
||||
this.vista.menu.panel.revalidate();
|
||||
this.vista.menu.panel.repaint();
|
||||
}catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
}else {
|
||||
JCheckBox pulsado = (JCheckBox)e.getSource();
|
||||
for(int i = 0; i < vista.gestiones.getElementos().size();i++) {
|
||||
if(vista.gestiones.getElementos().get(i).toString().equals(pulsado.getText())) {
|
||||
vista.gestiones.alterarVisibilidad(i);
|
||||
switch(e.getActionCommand()) {
|
||||
case "Aniadir":{
|
||||
String nombre=JOptionPane.showInputDialog("Introduce un nuevo gasto o ingreso");
|
||||
if(nombre == null) return;
|
||||
if(nombre.equals("")) {
|
||||
JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE);
|
||||
return;
|
||||
}
|
||||
String dinero = JOptionPane.showInputDialog("Introduce a cuanto asciende");
|
||||
if(dinero == null) return;
|
||||
if(dinero.equals("")) {
|
||||
JOptionPane.showMessageDialog(null, "Debe introducir algo", "error", JOptionPane.WARNING_MESSAGE);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this.vista.aniadirElemento(nombre, Float.parseFloat(dinero),this);
|
||||
this.vista.menu.panel.actualizarDatos(this.vista.gestiones);
|
||||
this.vista.menu.panel.revalidate();
|
||||
this.vista.menu.panel.repaint();
|
||||
}catch (NumberFormatException ex) {
|
||||
JOptionPane.showMessageDialog(null, "Debe introducir un numero", "error", JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "pulsar":{
|
||||
JCheckBox pulsado = (JCheckBox)e.getSource();
|
||||
for(int i = 0; i < vista.gestiones.getElementos().size();i++) {
|
||||
if(vista.gestiones.getElementos().get(i).toString().equals(pulsado.getText())) {
|
||||
vista.gestiones.alterarVisibilidad(i);
|
||||
this.vista.menu.panel.actualizarDatos(this.vista.gestiones);
|
||||
this.vista.menu.panel.revalidate();
|
||||
this.vista.menu.panel.repaint();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void aniadirListeners() {
|
||||
vista.boton.addActionListener(this);
|
||||
vista.boton.setActionCommand("Aniadir");
|
||||
int contador = 0;
|
||||
for(JCheckBox check:this.vista.transacciones) {
|
||||
check.addActionListener(this);
|
||||
check.setActionCommand("pulsar "+contador);
|
||||
contador++;
|
||||
check.setActionCommand("pulsar");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,6 @@ public class ControladorPanelLateral implements ActionListener, DateChangeListen
|
||||
public void dateChanged(DateChangeEvent arg0) {
|
||||
if(this.mes!=VistaPanelLateral.elegirMes.getDate().getMonth() ||
|
||||
this.anio!=VistaPanelLateral.elegirMes.getDate().getYear()) {
|
||||
System.out.println("Cambia mes");
|
||||
this.menu.cargarGestiones(VistaPanelLateral.elegirMes.getDate().getYear(), VistaPanelLateral.elegirMes.getDate().getMonth());
|
||||
this.mes=VistaPanelLateral.elegirMes.getDate().getMonth();
|
||||
this.anio=VistaPanelLateral.elegirMes.getDate().getYear();
|
||||
|
||||
@@ -3,12 +3,14 @@ package VistaControlador;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTabbedPane;
|
||||
import javax.swing.JTextArea;
|
||||
@@ -17,6 +19,7 @@ import javax.swing.event.ChangeListener;
|
||||
|
||||
import Logica.Gestion;
|
||||
import Logica.Meses;
|
||||
import Logica.Transaccion;
|
||||
|
||||
public class Menu extends JFrame{
|
||||
protected JPanel panelCentral;
|
||||
@@ -24,20 +27,30 @@ public class Menu extends JFrame{
|
||||
protected VistaAniadirVisualizar ingresos;
|
||||
protected VistaAniadirVisualizar gastos;
|
||||
protected VistaPanelLateral panel;
|
||||
String nombreDatos;
|
||||
Meses meses;
|
||||
ArrayList<VistaAniadirVisualizar> pestanias;
|
||||
ArrayList<ControladorAniadirVisualizar> controladores;
|
||||
protected Gestion datosGastos;
|
||||
protected Gestion datosIngresos;
|
||||
public Menu() {
|
||||
this.nombreDatos = ".mes";
|
||||
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
|
||||
|
||||
addWindowListener(new java.awt.event.WindowAdapter() {
|
||||
public void windowClosing(java.awt.event.WindowEvent evt) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
this.pestania = new JTabbedPane();
|
||||
GridBagConstraints constrain = new GridBagConstraints();
|
||||
|
||||
this.cargarGestiones(LocalDate.now().getYear(), LocalDate.now().getMonth());
|
||||
this.cargarPestanias();
|
||||
this.setLayout(new GridBagLayout());
|
||||
GridBagConstraints constrain = new GridBagConstraints();
|
||||
this.panel = new VistaPanelLateral(constrain, this.meses);
|
||||
this.panelCentral = new JPanel();
|
||||
|
||||
|
||||
constrain.fill = GridBagConstraints.VERTICAL;
|
||||
constrain.gridx = 0;
|
||||
constrain.gridy = 0;
|
||||
@@ -49,16 +62,26 @@ public class Menu extends JFrame{
|
||||
setDefaultCloseOperation(3);
|
||||
setLocationRelativeTo(null);
|
||||
ControladorPanelLateral controlador3 = new ControladorPanelLateral(this.panel, this);
|
||||
this.pestania.addChangeListener((ChangeListener)->{
|
||||
if(this.pestania.getTabCount()<0) {
|
||||
this.panel.actualizarDatos(meses.getGestionesActuales().get(this.pestania.getSelectedIndex()));
|
||||
}
|
||||
});
|
||||
this.listenerPestania();
|
||||
this.add(this.panel);
|
||||
}
|
||||
|
||||
private void close(){
|
||||
if (JOptionPane.showConfirmDialog(rootPane, "¿Desea guardar el estado?",
|
||||
"Salir del sistema", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
|
||||
try {
|
||||
this.meses.guardarMeses(this.nombreDatos);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private void iniciarMes(int anio, Month mes) {
|
||||
this.pestania.removeAll();
|
||||
this.listenerPestania();
|
||||
meses.aniadirGestion("Ingresos", anio, mes, true);
|
||||
meses.aniadirGestion("Gastos", anio, mes, false);
|
||||
this.pestanias=new ArrayList<VistaAniadirVisualizar>();
|
||||
@@ -73,6 +96,15 @@ public class Menu extends JFrame{
|
||||
void cargarGestiones(int anio, Month mes) {
|
||||
if(this.meses==null) {
|
||||
this.meses=new Meses();
|
||||
try {
|
||||
meses.cargarMeses(this.nombreDatos);
|
||||
} catch (ClassNotFoundException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.meses.elegirMes(anio, mes);
|
||||
if(this.meses.getGestionesActuales().size() == 0) {
|
||||
@@ -84,8 +116,17 @@ public class Menu extends JFrame{
|
||||
|
||||
private void cargarMes() {
|
||||
this.pestania.removeAll();
|
||||
this.pestanias.clear();
|
||||
this.controladores.clear();
|
||||
this.listenerPestania();
|
||||
if(this.pestanias == null) {
|
||||
this.pestanias = new ArrayList<VistaAniadirVisualizar>();
|
||||
}else {
|
||||
this.pestanias.clear();
|
||||
}
|
||||
if(this.controladores == null) {
|
||||
this.controladores = new ArrayList<ControladorAniadirVisualizar>();
|
||||
}else {
|
||||
this.controladores.clear();
|
||||
}
|
||||
for(Gestion gestion:this.meses.getGestionesActuales()) {
|
||||
VistaAniadirVisualizar vista = new VistaAniadirVisualizar(this, gestion);
|
||||
ControladorAniadirVisualizar controlador = new ControladorAniadirVisualizar(vista);
|
||||
@@ -98,9 +139,11 @@ public class Menu extends JFrame{
|
||||
|
||||
void cargarPestanias() {
|
||||
this.pestania.removeAll();
|
||||
this.listenerPestania();
|
||||
for(VistaAniadirVisualizar vista:this.pestanias) {
|
||||
this.pestania.addTab(vista.getName(),vista);
|
||||
}
|
||||
this.pestania.setSelectedIndex(0);
|
||||
}
|
||||
|
||||
void aniadirGestion(String nombre, boolean sumaOResta) {
|
||||
@@ -111,4 +154,19 @@ public class Menu extends JFrame{
|
||||
this.pestania.addTab(vista.getName(),vista);
|
||||
}
|
||||
|
||||
void listenerPestania(){
|
||||
if(this.pestania.getChangeListeners().length == 1) {
|
||||
this.pestania.addChangeListener((ChangeListener)->{
|
||||
if(this.pestania.getTabCount()>0) {
|
||||
try {
|
||||
this.panel.actualizarDatos(meses.getGestionesActuales().get(this.pestania.getSelectedIndex()));
|
||||
}catch (Exception e) {
|
||||
// TODO: handle exception
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ public class VistaAniadirVisualizar extends JPanel{
|
||||
}
|
||||
|
||||
public void aniadirElemento(String nombre, float dinero, ControladorAniadirVisualizar controlador) {
|
||||
Transaccion transaccion = new Transaccion(nombre, dinero,panelLateral.getDate());
|
||||
Transaccion transaccion = new Transaccion(nombre, dinero,panelLateral.getDate(),this.gestiones);
|
||||
this.gestiones.aniadirGasto(transaccion);
|
||||
JCheckBox check = new JCheckBox(transaccion.toString());
|
||||
check.setSelected(true);
|
||||
|
||||
@@ -25,7 +25,7 @@ public class VistaPanelLateral extends JPanel{
|
||||
VistaPanelLateral(GridBagConstraints constrain, Meses meses){
|
||||
this.setPreferredSize(new Dimension(200,200));
|
||||
this.meses=meses;
|
||||
this.total = new JTextArea();
|
||||
this.total = new JTextArea(String.valueOf(meses.getTotal()));
|
||||
this.gastoEnvio = new JTextArea();
|
||||
this.mostrarEstadisticas = new JButton("Mostrar grafico del mes");
|
||||
this.aniadirGestion = new JButton("Aniadir nueva gestión");
|
||||
|
||||
Reference in New Issue
Block a user