/**********************************************************************
Virtual Cache is a program which helps to manage personal finances
Copyright (C) 2009 by Rovinskiy Nikolay
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
package Controllers.ReportViewer;
import Controllers.Main.MainWindowController;
import Controls.CircularDiagram;
import Controls.MultipleChart;
import Controls.MyScrollPane;
import Interfaces.ReportViewer;
import Utils.Card;
import Utils.DateTool;
import Utils.MonthIterator;
import Utils.Transaction;
import java.awt.Graphics;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
/**
*
* @author nrovinskiy
*/
public class ReportViewerController extends ReportViewer
{
private Date dtBegin, dtEnd;
private MainWindowController mwcMain;
public ReportViewerController(MainWindowController main, Date begin, Date end)
{
super((JFrame)main);
mwcMain=main;
dtBegin=begin;
dtEnd=end;
btnSave.addActionListener(new SavePictureListener(mwcMain, this));
jtpAnalisys.addMouseListener(new TabChangeListener(this));
this.setLocation(main.getLocation().x, main.getLocation().y);
CalculateAndShow();
}
private void CalculateAndShow()
{
double dblTotal=0;
Enumeration<String> enReasons=mwcMain.getProfile().getReasons().keys();
Vector<String> vctGroups= new Vector<String>();
while(enReasons.hasMoreElements())
{
String strReson=enReasons.nextElement();
if(!vctGroups.contains(mwcMain.getProfile().getReasons().get(strReson)))
vctGroups.add(mwcMain.getProfile().getReasons().get(strReson));
}
String[] strGroups=new String[vctGroups.size()];
String[] strCardNames=new String[mwcMain.getProfile().getCardNumber()];
vctGroups.toArray(strGroups);
MonthIterator mitTimer=new MonthIterator(dtBegin, dtEnd);
double[][] dblCardRemainings=new double[mwcMain.getProfile().getCardNumber()][mitTimer.getIterationCount()];
initArray(dblCardRemainings);
double[][] dblMonthSpent=new double[vctGroups.size()][mitTimer.getIterationCount()];
initArray(dblMonthSpent);
double[] dblValues=new double[vctGroups.size()];
vctGroups=null;
for(int i=0; i<dblValues.length; i++){dblValues[i]=0;}
Enumeration<Card> enCards=mwcMain.getProfile().getCards();
while(enCards.hasMoreElements())
{
Card crdTemp=enCards.nextElement();
Enumeration<Transaction> enTrans =crdTemp.getAllTransactions();
while(enTrans.hasMoreElements())
{
Transaction tmpTrans=enTrans.nextElement();
if((tmpTrans.getDate().before(dtBegin))||((tmpTrans.getDate().after(dtEnd))))
continue;
for(int i=mitTimer.getIterationNumber(); mitTimer.hasNext(); mitTimer.next())
{
i=mitTimer.getIterationNumber();
if(mitTimer.isInCurrentMonth(tmpTrans.getDate()))
{
for(int k=0; k<strGroups.length; k++)
{
if(strGroups[k].equals(mwcMain.getProfile().getReasons().get(tmpTrans.strReason.trim().toLowerCase())))
dblMonthSpent[k][i]-=tmpTrans.amount;
}
}
}
mitTimer.reset();
for(int i=0; i<strGroups.length; i++)
{
if(tmpTrans.amount>0)
continue;
if(strGroups[i].equals(mwcMain.getProfile().getReasons().get(tmpTrans.strReason.trim().toLowerCase())))
dblValues[i]+=tmpTrans.amount;
}
if(tmpTrans.amount<0)
dblTotal-=tmpTrans.amount;
}
}
//circular diagram 0 tab
jtpAnalisys.addTab("Resourse distribution", new CircularDiagram(strGroups, dblValues));
//jtpAnalisys.setToolTipTextAt(0, "Resourse distribution");
//fill the dates
String[] strDates=new String[mitTimer.getIterationCount()];
for(Date t=mitTimer.getDateOfIteration(); mitTimer.hasNext(); mitTimer.next())
strDates[mitTimer.getIterationNumber()]=DateTool.getDateStr(mitTimer.getDateOfIteration(), "MM.yyyy");
mitTimer.reset();
//Calculade all data
enCards=mwcMain.getProfile().getCards();
int intCardPointer=mwcMain.getProfile().getCardNumber()-1;
double dblMonthBalance;
while(enCards.hasMoreElements())
{
Card crdTemp=enCards.nextElement();
strCardNames[intCardPointer]=crdTemp.getName();
dblMonthBalance=crdTemp.getInitialBalance();
while(mitTimer.hasNext())
{
Enumeration<Transaction> enTranCr=crdTemp.getAllTransactions();
while(enTranCr.hasMoreElements())
{
Transaction trans=enTranCr.nextElement();
if(mitTimer.isInCurrentMonth(trans.getDate()))
{
dblMonthBalance+=trans.amount;
}
}
dblCardRemainings[intCardPointer][mitTimer.getIterationNumber()]=dblMonthBalance;
mitTimer.next();
}
mitTimer.reset();
intCardPointer--;
}
//invert arrays for cards
//amount of money spent to different groups in months tab 1
MyScrollPane jspChart=new MyScrollPane(new MultipleChart(strGroups, dblMonthSpent, strDates, "Spent"));
jtpAnalisys.addTab("Expenses by months", jspChart);
//jtpAnalisys.setToolTipTextAt(1, "Outcome by months");
//percent of money spent to different groups in months tab 2
if(dblTotal>0)
{
for(int i=0; i<dblMonthSpent.length; i++)
for(int k=0; k<dblMonthSpent[i].length; k++)
{
if(dblMonthSpent[i][k]<0)
dblMonthSpent[i][k]=0;
dblMonthSpent[i][k]=dblMonthSpent[i][k]*100/dblTotal;
}
}
else
{
for(int i=0; i<dblMonthSpent.length; i++)
for(int k=0; k<dblMonthSpent[i].length; k++)
{
dblMonthSpent[i][k]=0;
}
}
MultipleChart mtcPercents=new MultipleChart(strGroups, dblMonthSpent, strDates, "Spent %");
mtcPercents.setMaxValue(100);
jspChart=new MyScrollPane(mtcPercents);
jtpAnalisys.addTab("Expenses by months in percent", jspChart);
//jtpAnalisys.setToolTipTextAt(2, "Outcome by monthsin percent");
//amount of money on different credit/debit cards at different months tab 3
jspChart=new MyScrollPane(new MultipleChart(strCardNames, dblCardRemainings, strDates, "Balance"));
jtpAnalisys.addTab("Card balances", jspChart);
//jtpAnalisys.setToolTipTextAt(3, "Card balances");
}
private void initArray(double[][] array)
{
for(int i=0; i<array.length; i++)
for(int k=0; k<array[i].length; k++)
array[i][k]=0;
}
@Override
public void repaint()
{
super.repaint();
CastDraws();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
CastDraws();
}
private void CastDraws()
{
this.setVisible(true);
if(jtpAnalisys.getSelectedIndex()==0)
((CircularDiagram)jtpAnalisys.getComponentAt(0)).Draw();
else
((MultipleChart)(((JScrollPane)jtpAnalisys.getSelectedComponent()).getViewport().getComponent(0))).Draw();
//else if(jtpAnalisys.getSelectedIndex()==2)
//((MultipleChart)((JViewport)((JScrollPane)jtpAnalisys.getComponentAt(2)).getComponent(0)).getComponent(0)).Draw();
//((MultipleChart)((JViewport)((JScrollPane)jtpAnalisys.getComponentAt(3)).getComponent(0)).getComponent(0)).Draw();
}
}