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     * TransactionListModelBase.java
024     *
025     * Created on April 28, 2006, 9:10 AM
026     */
027    
028    package org.jbudget.gui.trans;
029    
030    import java.util.ArrayList;
031    import java.util.Collections;
032    import java.util.Comparator;
033    import java.util.LinkedList;
034    import java.util.List;
035    import javax.swing.event.TableModelEvent;
036    import javax.swing.event.TableModelListener;
037    import javax.swing.table.TableModel;
038    import org.jbudget.Core.Transaction;
039    import org.jbudget.Core.TransactionFilter;
040    import org.jbudget.Core.User;
041    import org.jbudget.gui.FilterChangeDispatcher;
042    import org.jbudget.gui.FilterChangeListener;
043    import org.jbudget.io.DataManager;
044    
045    /**
046     * A model for creating a list of transactions or allocations.
047     * An abstract class that is used to create TableModels for lists (the table
048     * has only one column) of Transactions or Allocations
049     * @author petrov
050     */
051    abstract public class TransactionListModelBase implements TableModel,
052                          TransactionSorterPanel.TransactionSorterListener,
053                          FilterChangeListener,
054                          DataManager.UserListener,
055                          DataManager.DataSourceListener
056    {
057       /** List of listeners */
058       private final List<TransactionListPanelBase.ModelListener> 
059           listeners = new LinkedList<TransactionListPanelBase.ModelListener>();
060      
061       /** List of Transactions */
062       protected List<Transaction> transactions = new ArrayList<Transaction>();
063      
064       /** Comparator used for sorting */
065       private Comparator<Transaction> comparator = null;
066      
067       /** current filter */
068       protected TransactionFilter filter;
069      
070       protected boolean dispose_called = false;
071      
072       //================= Constructors ====================================//
073       
074        /**
075         * Creates a new instance of TransactionListModelBase
076         */
077        public TransactionListModelBase() 
078        {
079            filter = new TransactionFilter();
080            initialize();
081            FilterChangeDispatcher.getInstance().addListener(this);
082            DataManager.getInstance().addUserListener(this);
083            DataManager.getInstance().addDataSourceListener(this);
084        }
085    
086        void dispose() 
087        {
088            FilterChangeDispatcher.getInstance().removeListener(this);
089            DataManager.getInstance().removeUserListener(this);
090            DataManager.getInstance().removeDataSourceListener(this);
091        }
092      
093       //=================== Implementing the interfaces ======================//
094      
095        public void addTableModelListener(TransactionListPanelBase.ModelListener l)
096        {
097            listeners.add(l);
098        }
099    
100        public void addTableModelListener(TableModelListener l)
101        {
102            if (l instanceof TransactionListPanelBase.ModelListener)
103            {
104                listeners.add((TransactionListPanelBase.ModelListener) l);
105            } else
106            {
107                throw new RuntimeException(
108                        "This Model can only be used with TransactionListPanelBase.Table");
109            }
110        }
111    
112        public void removeTableModelListener(TableModelListener l)
113        {
114            listeners.remove(l);
115        }
116    
117        public Class getColumnClass(int index)
118        {
119            return Transaction.class;
120        }
121    
122        public int getColumnCount()
123        {
124            return 1;
125        }
126    
127        public int getRowCount()
128        {
129            return transactions.size();
130        }
131    
132        public String getColumnName(int index)
133        {
134            return "Transactions/Allocations";
135        }
136    
137        public Object getValueAt(int rowIndex, int columnIndex)
138        {
139            return transactions.get(rowIndex);
140        }
141    
142        public boolean isCellEditable(int rowIndex, int columnIndex)
143        {
144            return true;
145        }
146    
147        abstract public void setValueAt(Object aValue, int rowIndex, int columnIndex);
148        
149        /** Called when the user has changed. */
150        public void userChanged(User user)
151        {
152            initialize();
153        }
154        
155        /** Implementing the interface. */
156        public void userRemoved(int uid)
157        {}
158        
159        /** Implementing the interface. */
160        public void userAdded(User user)
161        {}
162        
163        /** Method that is called when the data directory has changed. */
164        public void dataSourceChanged()
165        {
166            DataManager.getInstance().addUserListener(this);
167            DataManager.getInstance().addDataSourceListener(this);
168            filter = new TransactionFilter();
169            initialize();
170        }
171      
172       //================== Other methods ===================================//
173      
174       /** Notifies all listeners that the contents of the list has changed */
175       public void fireTableChanged()
176       {
177        TableModelEvent ev = new TableModelEvent(this);
178        for (TableModelListener l : listeners)
179          l.tableChanged(ev);
180      }
181      
182       /** Forces all listeners to stop editing and save the updated values. */
183       public void fireStopEditing()
184       {
185        for (TransactionListPanelBase.ModelListener l : listeners)
186          l.stopEditing();
187      }
188      
189       /** Returns a list of transactions. */
190       abstract protected List<Transaction> getTransactions();
191      
192       /** Creates a local list of transactions that satisfy the filtering 
193       * requirements and sorts them if the comparator is set
194       */
195       synchronized void initialize()
196       { 
197         transactions.clear();
198         transactions.addAll(getTransactions());
199         
200         if (filter != null)
201           transactions = filter.apply(transactions);
202         
203         if (comparator != null)
204           Collections.sort(transactions, comparator);
205         else
206           Collections.sort(transactions);
207         
208         fireTableChanged();
209       }
210       
211       /** Changes the filter */
212       public void filterUpdated(TransactionFilter newFilter, Object initiator) 
213       {
214          filter = newFilter;
215          initialize();
216       }
217       
218       public TransactionFilter getFilter ()
219       {
220           return filter;
221       }
222       
223       /** Change comparator */
224       public void setComparator (Comparator<Transaction> comparator){
225         if (comparator == this.comparator)
226           return;
227         this.comparator = comparator;
228         initialize();
229       }
230       
231       /** Returns the index of an object */
232       public int indexOf(Object key)
233       {
234         if ( !(key instanceof Transaction))
235           return -1;
236         
237         Transaction t = (Transaction)key;
238         
239         if (comparator == null)
240          return Collections.binarySearch(transactions, t);
241         else
242          return Collections.binarySearch(transactions, t, comparator);
243       }
244    }