From 18238bd6b843d2ccaac1d4df886c8cfc013b2b56 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Fri, 15 Nov 2019 14:37:52 +0100 Subject: [PATCH] Finalizado soporte de persistencia --- .mes | Bin 0 -> 761 bytes bin/.gitignore | 1 + bin/Logica/Gestion.class | Bin 2301 -> 2334 bytes bin/Logica/Transaccion.class | Bin 1530 -> 1763 bytes src/Ficheros/FicheroBinario.java | 39 +++++++++ src/Logica/Gestion.java | 11 +-- src/Logica/Meses.java | 45 ++++++++++- src/Logica/Transaccion.java | 26 +++--- .../ControladorAniadirVisualizar.java | 63 ++++++++------- .../ControladorPanelLateral.java | 1 - src/VistaControlador/Menu.java | 76 +++++++++++++++--- .../VistaAniadirVisualizar.java | 2 +- src/VistaControlador/VistaPanelLateral.java | 2 +- 13 files changed, 209 insertions(+), 57 deletions(-) create mode 100644 .mes create mode 100644 src/Ficheros/FicheroBinario.java diff --git a/.mes b/.mes new file mode 100644 index 0000000000000000000000000000000000000000..60608bd2876599927a404566227c103e177c3428 GIT binary patch literal 761 zcmaKqziSjh6vy9Yv$y7QXD*STAU5f2cK?8DPLzuW3sE8>IV|RO=k_M-?QCY=o@+o+ zu&@!*h^3Z-f{@ZG`3LMR1VJoqEv$UAKZrIN7?^qUozMI3clP5iaI}JrK*l0u-aW-q z&B9Q~bauS)=lSWkyG!5wfOVOi&MHUtMHvLt|& z+9KI%jN~X1|2_{_Q zhKkR5iX-Uoq+H#Ud5U9Lt+$jGg?MTFPYyeieKJ5fKsBsfH3H8B<<)|tFAv`K^z~%P z1VYlb@wuEeqd>SYN@K;fr0;8K7OCk>8x0J;Q^01`D)(+#IH6*>ZC7o<%8jJkkk=b< zM&#Bu(`dLW>@d>PnCYlSzcX)VE;{uab-G2>#Tw9=ndf!6eAdN4))1V-XgZrz^c!TR YG=8N*f+?KRJPf8-|8e-Pp9FRTULn*aa+ literal 0 HcmV?d00001 diff --git a/bin/.gitignore b/bin/.gitignore index 9bbe4d5..cbd64bd 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,3 +1,4 @@ /IniciarSesion/ /VistaControlador/ /Logica/ +/Ficheros/ diff --git a/bin/Logica/Gestion.class b/bin/Logica/Gestion.class index e72e9f55d8409d43209533585d2f8bdb0d0db6ed..2cc913e14e487ab79d58747313d4b0397888b489 100644 GIT binary patch literal 2334 zcmZ`)TT>iG6#jZy7+8jY+!Agfgv7uSvI#*FvxFpa%NigtOBTUv?=o3BE;BkiP*&dj z3BHNG_{NK68M&m?D$8g8fTrlnVPrCo^oshaen)meg~ZFIfT&#o(k> z7HD1Y_iTBpl*;j(TG8-ul%6hW!*`oYBgl!O9)-QBDXUjpIO-^S^)~0294mmjj z9VrKSNmjUsnEzKzZB|KbDqFe{Bq!j9x(R4{$HX*d1nQM%xspQGDhj#x<7Gm7x)F1@ zZ(yEe9gDxRY9fPosWaR9(u)A`s~6I($zCwAh$N$VPLM?c5AmLXM-?GP%5CDYu3w$? z#g;6UbxD^^Jn>DgwdrhII-wO4?_*WK*pTHVKQ?Q`hN-tgT3JA=oUx0pRb~NOWl6(v zbE>TJs-)H>wGki61YCS#;#05{YgiV|SthE%2#SJEb4uSE zW~lHhnl%itV`})+@)y$$#(4v;sTdM`*k=BmP3DhmEPrIHan#+`iZ;I6`SqNBYB*j< zzC=gzH=J30iS7gRBpVOVr(gZakpm1Qdk--9!uN8K@3V{vdXKY_o7uSSe0Q;x&oaUw zy}!xTbAex6!Vte0LAN@i>Y2_c#`!RkVIC=tdaX9OkAa^!i9rXd^Nk(8Lo?VAIusbe zWm+_duNf-FG0nq0BZnAWNv`grkF#re4PzgJJ809k{?OLHiQrxGB1|uet?x%}iDDb9iY;CB zaR14}m#Q9~#BGKM^f2WU6wRRLB)cP3o!r6QF#Yf8EY7UQHM@tnx3r7%qrYHQ;17%~ zUOvPFlDf2mCcVzj?8*x+^Z(-8SAQMT+Ra*mmuUyClEiB){TOLY;W{tKW4VkUQ94da zwOl&*TO+wKpWJrDUQyq11Hxd_8*E;dMc@OhRv~-vhZAJqtP1r*d=wlO7j^Y`W=}&Q z){lws5cwTE@dfoCYeZUozLZ{DrN~NERan-Z8L zd$-woQ{?y#X}Zh1^&W4gX%sNSwx7jUw0?tme24q^84rAF0%7kRRbh|ZTO>ydyB_hs Pk?Fe8$?vB=vBiG@-TTb_ literal 2301 zcmZ`)T~i!I6g|Bx?6M4jgoOYJUm?V0`B)_+L6=|@0uq75Ko-HT^ez+VxI3e>vy|nV zKfyQAm%Q<28Tm-5RhG~G1%HZF#?v#iFp{-WnZDh(``&xbJ$L!@pI?6mFbNeyMBv<# zy{!r|vtX88Wm_>s1zMlVU70CLYddp4|I{qF0*%|g-z*DUSn~W8R~0ikdTl4m3#)3| zl5WK@1yZkEm|hCftU1yu%R+&vvNL=ZEmulXK+Fr&yS6Kf0(B*wW?|+!=w6pwzH? zb2!ITv-XymKpi>_B++11flr_tosAIaHBgTRZS@;yL_%9v3^e)HRRb5$ zrq8Y!XvKMfmL+AGE0t2-bk=0P=oM=hWHBclrT4*M)ZI}eq_s+6&?&-TQ{0sRXEHVH z(-MXvV30DorJMy>c1g{}R5ePt5yLr+;dKLVV1h{H)|Nnfm>FLM+2PFub#pU@8=6;N zH=#HNZehx=YL3Vp<|AwDY?FK1b%A895(Ju8U0L{iS$+|~juG6dCyqF+HD4PB&AUaj zWLmCGPEJ6F+Cg5@RW3ru|Es_@v#3hSl1>Q88Tf&2e46GA+yz0!m1{bZ;@2t)xcBlc z2Itu(yoY-+ERw9#fvI{8ypJX7%rd{?c%1<8x);!`$zCyVAEV6X+I|%Ye1O#$9#(}I zYPW$k-MmoG*63R-GwUOGGOXN;b^bO_aFK{va8@e}M;PMfA(oIL`)1QI#$Pot9JR5rMMR|?iUN3tKq8!^6Mo)5(yBNKf z)*xf2c&Fcwi(#a>V+K9!%&IM2QH1sgMuR#sT(z%0eTc!IXhfhBX`n!-7tru81OWxc zFwPSV;#;PQaLw{@&&V+{>*hl7Bv;F^Cw)KF0B7iv&Kwr+&3!NH>6)4smVDVZiYw~@gN=GoOn z+`(furhr*a?i{}15AS#M{D1}gjC&rDzBUg|h&&*DPe_i|W*pJKvDkI1gZpjnBiQ*D D+91x~ diff --git a/bin/Logica/Transaccion.class b/bin/Logica/Transaccion.class index dd3367c4994491e173129be69f2caf118e72a8ac..b1cd4c71f62f59197bb08eea19e8091a23be7f89 100644 GIT binary patch literal 1763 zcmZuxU31e$6g}%N#U3S4lMp^q6G95MLNJs7ZA~d9sRPuIf`eyZhKG$U%u+|nNJ<`f z4ru0^86evtNRwddS(?>*8#@BVrNU;&RTqy&l$ zzpL7^x*A9?le{^R_K-?7=UG%*$l3vu3y-hd!{P*I2uipOqKp^M&>uUjl#l~KmW)vu|TjSe~ z^4!2DM_s_$QlTJmphjHQa(_p&URR!q zZV1%MgGuW{84r?FS^}9xzvC8=#t9ooagvhCl~x`CAKS1os-sgj#&AeSr)?a@M>;xV zqlhCq`ozXKCIk*OSmpiA^))wG?RSx*U@a}l&a&WmGZQ^mAshceOJYteY9#XeZR3e@ zWw0&_^8%9xdwgqC^*U})KoyrPT--P8KAnwEaalKC4{qg|z}aNe`i7_t$ydO6T+xlW z$|)rw@o+1x5q!qMPV0kT*r?$;CnGmDT(2WAKMXNYRII+HDQ;+ruLP#b^+7esgJ0Wl zFefn9b)yy?wWWHhBbk%qqpymx7rB89_PrWyMza0=J-HE&B2Vm6Y)!&w&YanyBPTyJ zf54M@=_%P!K}gdyaNoi`ir8;X8!Pxuz>ZHY%P^w(C(FY|t}R$#*4Won&dmGiXp2JA z-wfJrU73$FVVMis3OG$0rg>$N!wFazVVvhbA%y}jy4b|@uvzI~v(mq2rF*$*%u!78 zevD5)5SQh8%6X0B&fGSpoY`%ZoXj?+o%wCdIL8^z{$>cy@?K(EkT}E7B2VHmeiTPY ze2k zP`tK-&-qx|!IwL@`76;WXjM7lz?SIjwuwt|c7a=1j9IH(^|>r5rhjIT1eX%v>@Ij| z7yOi`HF(0&=pSLFe?rEzDJDxBC3+CR41XVI5`+^m;WvCYj#R^fc+XI9MT$4AtBz&f z^-z*JOTTa;! zMiYxf6Cc2bQvY*0mVwUAobCUXf0^HZe*6Nkil;g>hFIBdh#D{MIovwrwVJRk9bty( zK0o5cCbt^Jo$9_>a~TFLyH#~ei8+1Tbsb?f*66h^EYqLvlA|U>K1I(wNaP%@9{!&#D@@hz!wK_Irk8HW%njlFbzz8>LX;j*w^XGVHz;hh(eVUsOu85R_k_v`l))wY#+?zW)wYJG_eP$dUK+jo569 zX5DlmC}3H~)iZtU0u8L7C_BWPZFA=h!=m3-x7TZ4Mf`dZWN}^A{|2?v5ApouDnnSM ze$UCk+Xn97F7=il9GF&}VL1rV*KW`KnxwcdDX3~Q*;2n7f8YZHi&$b9YnX0@#_mWo zMV*r-O$iNOj5l4=;m(<-(Os9<-aY3BUgL%dyY0;{!Dxa9)eVhpWY=yxHM1n-G)H~E z$%?E!=4nbo^go56$yubGCMQv?G>2-X5u#P6$We^ZcbrZwij%nM{C7n2g%0BRg$@$= za0ki!2~r)*epTRk`lc1}2#LtymLy|{6HOXXT%z~6E)_0gflfrKtT45cW;7*z#glH+ zIZSV#e<8F+6$Yg*lSwO0L+vx3Cc_3EdaOlSW!wN!BtM-4 zXMEs+9{5oY{908B)B=hA06p{(Vbu+fETo)7C=5guB??*kuA78M_?T`cpp~iDe`(8P q&o%l;Uz^yXuk5|wXW}zm_>)}n(VqI;MkW9}d%i-2K=2bKQThiwqZB>> diff --git a/src/Ficheros/FicheroBinario.java b/src/Ficheros/FicheroBinario.java new file mode 100644 index 0000000..00bbd60 --- /dev/null +++ b/src/Ficheros/FicheroBinario.java @@ -0,0 +1,39 @@ +package Ficheros; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import Logica.Gestion; +import Logica.Meses; + +public class FicheroBinario { + String directorioGestiones; + String directorioTransacciones; + String directorioMeses; + public FicheroBinario(String nombre) { + this.directorioGestiones=nombre; + } + + public void guardarMeses(Meses mes) { + + } + + public void guardarGestiones(Gestion gestion) throws IOException { + FileOutputStream ficheroSalida = new FileOutputStream(new File(this.directorioGestiones)); + ObjectOutputStream escritor = new ObjectOutputStream(ficheroSalida); + escritor.writeObject(gestion); + escritor.close(); + ficheroSalida.close(); + } + + public void cargarGestiones(Meses meses) throws IOException { + FileInputStream ficheroEntrada = new FileInputStream(new File(this.directorioGestiones)); + ObjectInputStream lector = new ObjectInputStream(ficheroEntrada); + //meses.aniadirGestion(nombre, anio, mes, isPositivo) + } +} diff --git a/src/Logica/Gestion.java b/src/Logica/Gestion.java index 9b683fd..94add3a 100644 --- a/src/Logica/Gestion.java +++ b/src/Logica/Gestion.java @@ -1,14 +1,11 @@ package Logica; -import java.time.Month; -import java.time.Year; +import java.io.Serializable; import java.util.Vector; -public class Gestion{ +public class Gestion implements Serializable{ private Vector gestiones; private float suma; private float total; - private Month mes; - private Year anio; private boolean isPositivo; private String nombre; @@ -55,6 +52,10 @@ public class Gestion{ } } + public boolean esIngreso() { + return this.isPositivo; + } + public void alterarVisibilidad(int elemento) { if(this.gestiones.get(elemento).alterarVisivilidad()) { this.suma += this.gestiones.get(elemento).getDinero(); diff --git a/src/Logica/Meses.java b/src/Logica/Meses.java index 58285a9..1ce8e33 100644 --- a/src/Logica/Meses.java +++ b/src/Logica/Meses.java @@ -1,5 +1,13 @@ package Logica; +import java.io.EOFException; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.time.Month; import java.util.ArrayList; @@ -66,9 +74,44 @@ private ArrayList meses; return this.meses.get(this.mesActual).getTotal(); } + public void guardarMeses(String nombre) throws IOException { + FileOutputStream fichero = new FileOutputStream(nombre); + ObjectOutputStream escritor = new ObjectOutputStream(fichero); + for(Mes mes:this.meses) { + for(Gestion gestion:mes.getGestiones()) { + for(Transaccion transaccion:gestion.getElementos()) { + escritor.writeObject(transaccion); + } + } + + } + escritor.close(); + fichero.close(); + } + + public void cargarMeses(String nombre) throws IOException, ClassNotFoundException { + FileInputStream fichero; + try { + fichero = new FileInputStream(nombre); + }catch(FileNotFoundException e){ + return; + } + + ObjectInputStream lector = new ObjectInputStream(fichero); + try { + while(true) { + Transaccion transaccion = (Transaccion)lector.readObject(); + this.aniadirTransaccion(transaccion, transaccion.getGestion().getNombre(), transaccion.getGestion().esIngreso()); + } + }catch(EOFException e) { + lector.close(); + fichero.close(); + } + } + } -class Mes{ +class Mes implements Serializable{ private int anio; private Month mes; ArrayList gestiones; diff --git a/src/Logica/Transaccion.java b/src/Logica/Transaccion.java index 2dc7689..0cb8030 100644 --- a/src/Logica/Transaccion.java +++ b/src/Logica/Transaccion.java @@ -1,25 +1,25 @@ package Logica; +import java.io.Serializable; import java.time.LocalDate; -public class Transaccion { +public class Transaccion implements Serializable{ + /** + * + */ + private static final long serialVersionUID = 7544113192927092049L; private String nombre; private float dinero; private boolean visible; private LocalDate dia; + private Gestion gestion; - public Transaccion(String nombre, float dinero){ - this.nombre = nombre; - this.dinero = dinero; - this.visible = true; - this.dia = LocalDate.now(); - } - - public Transaccion(String nombre, float dinero, LocalDate dia){ + public Transaccion(String nombre, float dinero, LocalDate dia, Gestion gestion){ this.nombre = nombre; this.dinero = dinero; this.visible = true; this.dia = dia; + this.gestion=gestion; } public String toString() { @@ -42,4 +42,12 @@ public class Transaccion { public LocalDate getDia() { return this.dia; } + + public void aniadirse() { + this.gestion.aniadirGasto(this); + } + + public Gestion getGestion() { + return this.gestion; + } } diff --git a/src/VistaControlador/ControladorAniadirVisualizar.java b/src/VistaControlador/ControladorAniadirVisualizar.java index bf0cfff..498c4eb 100644 --- a/src/VistaControlador/ControladorAniadirVisualizar.java +++ b/src/VistaControlador/ControladorAniadirVisualizar.java @@ -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"); } } diff --git a/src/VistaControlador/ControladorPanelLateral.java b/src/VistaControlador/ControladorPanelLateral.java index ffa5cfa..d3d4312 100644 --- a/src/VistaControlador/ControladorPanelLateral.java +++ b/src/VistaControlador/ControladorPanelLateral.java @@ -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(); diff --git a/src/VistaControlador/Menu.java b/src/VistaControlador/Menu.java index cf9c71e..5420474 100644 --- a/src/VistaControlador/Menu.java +++ b/src/VistaControlador/Menu.java @@ -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 pestanias; ArrayList 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(); @@ -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(); + }else { + this.pestanias.clear(); + } + if(this.controladores == null) { + this.controladores = new ArrayList(); + }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 + } + } + }); + } + + } + } diff --git a/src/VistaControlador/VistaAniadirVisualizar.java b/src/VistaControlador/VistaAniadirVisualizar.java index 3f9deea..52f9146 100644 --- a/src/VistaControlador/VistaAniadirVisualizar.java +++ b/src/VistaControlador/VistaAniadirVisualizar.java @@ -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); diff --git a/src/VistaControlador/VistaPanelLateral.java b/src/VistaControlador/VistaPanelLateral.java index d1591fd..e992e18 100644 --- a/src/VistaControlador/VistaPanelLateral.java +++ b/src/VistaControlador/VistaPanelLateral.java @@ -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");