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
028 package org.jbudget.gui.trans;
029
030 import java.util.ArrayList;
031 import java.util.List;
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 AllocationsListModel extends TransactionListModelBase
041 implements DataManager.AllocationListener
042 {
043
044 public AllocationsListModel()
045 {
046 DataManager.getInstance().addAllocationListener(this);
047 }
048
049 @Override
050 public void dispose()
051 {
052 dispose_called = true;
053 DataManager.getInstance().removeAllocationListener(this);
054 super.dispose();
055 }
056
057 @Override
058 public void finalize()
059 {
060 if (!dispose_called)
061 {
062 System.out.println("Dispose was not called for AllocationListModel");
063 dispose();
064 }
065 }
066
067 protected List<Transaction> getTransactions()
068 {
069 return DataManager.getInstance().getAllocations(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().removeAllocation(oldInstance);
079 DataManager.getInstance().addAllocation(newValue);
080
081 fireTableChanged();
082 }
083
084 @Override
085 public String getColumnName(int index)
086 {
087 return "Allocations";
088 }
089
090 //=========== Implementing Interfaces =================================//
091
092 /** Called when an allocation is added to the month. */
093 public void allocationAdded(Transaction allocation)
094 {
095 List<Transaction> l = new ArrayList<Transaction>(1);
096 l.add(allocation);
097 if (!filter.apply(l).isEmpty())
098 initialize();
099 }
100
101 /** Called when an allocation is removed from the month. */
102 public void allocationRemoved(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().addAllocationListener(this);
113 super.dataSourceChanged();
114 }
115 }