Package Controllers.CardPane

Source Code of Controllers.CardPane.CardPaneController

/**********************************************************************
    Virtual Cache is a program which helps to manage personal finances
    Copyright (C) 2013  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.CardPane;

import Controllers.Main.MainWindowController;
import Interfaces.CardPane;
import Utils.Card;
import Utils.DateTool;
import Utils.Transaction;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
*
* @author nrovinskiy
*/
public class CardPaneController extends CardPane
{
    private MainWindowController mwcMain;
    private Card crdInfo;
    private DefaultTableModel dtmData;
    private DecimalFormat dfCurrency;
    public CardPaneController(MainWindowController main, Card card)
    {
        super();
        mwcMain=main;
        crdInfo=card;
        jcbReason.setEditable(true);
        dtmData=(DefaultTableModel)tblTransactions.getModel();//new DefaultTableModel();
        //dtmData.addColumn("ID");
        //dtmData.addColumn("Date");
        //dtmData.addColumn("Amount");
        //dtmData.addColumn("Reason");
        tblTransactions.addMouseListener(new TableListener(txtID));
        txtDate.setText(DateTool.getTodayDate());
        tblTransactions.setModel(dtmData);
        btnAdd.addActionListener(new AddListener(mwcMain, this));
        btnDelete.addActionListener(new DeleteListener(mwcMain, this));
        btnSaveInitBal.addActionListener(new SaveBalanceListener(mwcMain, txtInitial, lblBalanceImpl, crdInfo));
        JRadioButton[] jrbrArray={jrbGot, jrbSpent};
        RadioListener rlSpentGot=new RadioListener(jrbrArray);
        jrbGot.addActionListener(rlSpentGot);
        jrbSpent.addActionListener(rlSpentGot);
        jrbrArray=null;
        dfCurrency=new DecimalFormat("##0.00");
        try
        {
            LoadTransactions();
        }
        catch(ParseException e){}
    }
    public void LoadTransactions() throws ParseException
    {
        //Vector vctRow=new Vector();
        Enumeration<Transaction> enTrans=crdInfo.getAllTransactions();
        while(enTrans.hasMoreElements())
        {
            Vector vctRow=new Vector();
            vctRow.clear();
            Transaction tmpTransaction=enTrans.nextElement();
            vctRow.add(new Integer(tmpTransaction.getID()));
            vctRow.add(tmpTransaction.getTransactionDate());
            vctRow.add(dfCurrency.format(tmpTransaction.amount));
            vctRow.add(tmpTransaction.strReason);
            if(dtmData.getRowCount()==0)
                dtmData.addRow(vctRow);
            else
            {
                int intPosition=0;
                for(int i=0; i<dtmData.getRowCount(); i++)
                {
                    if(DateTool.getDate((String) dtmData.getValueAt(i, 1)).compareTo(tmpTransaction.getDate())>0)
                    {
                        break;
                    }
                    else if(((String) dtmData.getValueAt(i, 1)).equals(tmpTransaction.getTransactionDate())&&(((Integer)dtmData.getValueAt(i, 0)).intValue()>tmpTransaction.getID()))
                    {
                        break;
                    }
                    intPosition=i+1;
                }
                dtmData.insertRow(intPosition, vctRow);
            }
        }
       // vctRow=null;
        enTrans=null;
       
       
        lblBalanceImpl.setText(dfCurrency.format(crdInfo.getBalance()));
    }
    public void addTransaction(Transaction transaction)
    {
        mwcMain.addReason(transaction.strReason);
        crdInfo.addTransaction(transaction);
        //define the place of transaction in terms of it's date
        int intPosition=0;
        for(int i=0; i<dtmData.getRowCount(); i++)
        {
            try
            {
                if(DateTool.getDate((String) dtmData.getValueAt(i, 1)).compareTo(transaction.getDate())>0)
                {
                    break;
                }
            }
            catch(ParseException e){}
            intPosition=i+1;
        }
        Vector vctOneRow=new Vector();
        vctOneRow.add(new Integer(transaction.getID()));
        vctOneRow.add(transaction.getTransactionDate());
        vctOneRow.add(dfCurrency.format(transaction.amount));
        vctOneRow.add(transaction.strReason);
        if(dtmData.getRowCount()>0)
            dtmData.insertRow(intPosition, vctOneRow);
        else
            dtmData.addRow(vctOneRow);
        vctOneRow=null;
        lblBalanceImpl.setText(dfCurrency.format(crdInfo.getBalance()));
        //lblBalanceImpl.setText(Double.toString(crdInfo.getBalance()));
        for(int i=0; i<this.getParent().getComponentCount(); i++)
        {
            ((CardPaneController)this.getParent().getComponent(i)).ReloadReasons();
        }
    }
    public void DeleteTransaction(int ID)
    {
        crdInfo.deleteTransaction(ID);
        for(int i=0; i<dtmData.getRowCount(); i++)
        {
            if(Integer.parseInt(dtmData.getValueAt(i, 0).toString())==ID)
            {
                dtmData.removeRow(i);
            }
        }
        lblBalanceImpl.setText(dfCurrency.format(crdInfo.getBalance()));
        //lblBalanceImpl.setText(Double.toString(crdInfo.getBalance()));
        txtID.setText("");
        mwcMain.LoadReasons();
        for(int i=0; i<this.getParent().getComponentCount(); i++)
        {
            ((CardPaneController)this.getParent().getComponent(i)).ReloadReasons();
        }
    }
    public void ReloadReasons()
    {
        jcbReason.removeAllItems();
       // jcbReason.addItem("");
        for(Iterator<String> i=mwcMain.getReasons().iterator(); i.hasNext();)
        {
            jcbReason.addItem(i.next());
        }
        jcbReason.setSelectedIndex(-1);
    }
    public String getAmount()
    {
        if(jrbSpent.isSelected())
            return new String("-" + txtAmount.getText());
        else
            return txtAmount.getText();
    }
    public String getPurpose(){return (String)jcbReason.getSelectedItem();}
    public String getID(){return txtID.getText();}
    public Card getCard(){return crdInfo;}
    public String getDate(){return txtDate.getText();}
    public JTable getTransactionTable(){return tblTransactions;}
    public DecimalFormat getFormat(){return dfCurrency;}
    public void ClearTransactionBlock()
    {
        //if(!mwcMain.getReasons().contains(jcbReason.getSelectedItem().toString().trim()))
        //    mwcMain.addReason(jcbReason.getSelectedItem().toString().trim());
        jrbGot.setSelected(false);
        jrbSpent.setSelected(true);
        jcbReason.setSelectedIndex(-1);
        txtAmount.setText("");
        txtDate.setText(DateTool.getTodayDate());
    }
    public void UpdateBalance()
    {
        lblBalanceImpl.setText(dfCurrency.format(crdInfo.getBalance()));
    }
}
TOP

Related Classes of Controllers.CardPane.CardPaneController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.