001    
002    /*****************************************************************************
003    
004        jBudget.
005        Copyright (C) 2006  Dmitri Petrov
006    
007        This program is free software: you can redistribute it and/or modify
008        it under the terms of the GNU General Public License as published by
009        the Free Software Foundation, either version 3 of the License, or
010        (at your option) any later version.
011    
012        This program is distributed in the hope that it will be useful,
013        but WITHOUT ANY WARRANTY; without even the implied warranty of
014        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015        GNU General Public License for more details.
016    
017        You should have received a copy of the GNU General Public License
018        along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     
020    *****************************************************************************/
021    
022    /*
023     * TransactionListModel.java
024     *
025     * Created on July 11, 2006, 8:26 AM
026     */
027    package org.jbudget.gui.trans;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    import org.jbudget.Core.MoneyPit;
032    import org.jbudget.Core.Transaction;
033    import org.jbudget.io.DataManager;
034    
035    /**
036     * A TableModel for displaying a list of Transactions.
037     *
038     * @author petrov
039     */
040    public class TransactionListModel extends TransactionListModelBase
041            implements DataManager.TransactionListener
042    {
043    
044        public TransactionListModel()
045        {
046            super();
047            DataManager.getInstance().addTransactionListener(this);
048        }
049    
050        @Override
051        public void dispose()
052        {
053            dispose_called = true;
054            DataManager.getInstance().removeTransactionListener(this);
055            super.dispose();
056        }
057    
058        @Override
059        public void finalize()
060        {
061            if (!dispose_called) {
062                System.out.println("Dispose was not called for TransactionListModel");
063                dispose();
064            }
065        }
066    
067        protected List<Transaction> getTransactions()
068        {
069            return DataManager.getInstance().getTransactions(filter);
070        }
071    
072        public void setValueAt(Object aValue, int rowIndex, int columnIndex)
073        {
074            Transaction newValue = (Transaction) aValue;
075            Transaction oldInstance = transactions.get(rowIndex);
076            transactions.set(rowIndex, newValue);
077    
078            DataManager.getInstance().removeTransaction(oldInstance);
079            DataManager.getInstance().addTransaction(newValue);
080    
081            fireTableChanged();
082        }
083    
084        @Override
085        public String getColumnName(int index)
086        {
087            return "Transactions";
088        }
089    
090        //===========  Implementing Interfaces =================================//
091        /** Called when a transaction is added to the month. */
092        public void transactionAdded(Transaction transaction)
093        {
094            List<Transaction> l = new ArrayList<Transaction>(1);
095            l.add(transaction);
096            if (!filter.apply(l).isEmpty()) {
097                initialize();
098            }
099        }
100    
101        /** Called when a transaction is removed from the month. */
102        public void transactionRemoved(List<Transaction.Transfer> transfers,
103                                       int cDay, int cMonth, int cYear)
104        {
105            initialize();
106        }
107        
108        /** Method that is called when the data directory has changed. */
109        @Override
110        public void dataSourceChanged()
111        {
112            DataManager.getInstance().addTransactionListener(this);
113            super.dataSourceChanged();
114        }
115    }