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     * TransactionSorterPanel.java
024     *
025     * Created on May 3, 2006, 7:03 AM
026     */
027    
028    package org.jbudget.gui.trans;
029    
030    import java.awt.Dimension;
031    import java.awt.event.ActionEvent;
032    import java.awt.event.ActionListener;
033    import java.util.Comparator;
034    import java.util.LinkedList;
035    import java.util.List;
036    import javax.swing.BorderFactory;
037    import javax.swing.border.EtchedBorder;
038    import org.jbudget.Core.Transaction;
039    import org.jbudget.gui.templ.TransactionSorterPanelTemplate;
040    
041    /**
042     * Creates a panel that allows user to choose how transactions are
043     * sorted.
044     * @author petrov
045     */
046    public class TransactionSorterPanel extends TransactionSorterPanelTemplate
047    {
048      /** A list of objects that will have their comparators changed in reaction
049       *  to the gui actions
050       */
051      private final List<TransactionSorterListener> listeners = 
052          new LinkedList<TransactionSorterListener>();
053       
054      /** Creates a new instance of TransactionSorterPanel */
055      public TransactionSorterPanel()
056      {
057        super();
058        
059        setPreferredSize(new Dimension(450, 100));
060        setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
061        
062        defaultRB.addActionListener(new ActionListener(){
063          public void actionPerformed(ActionEvent ev){
064            notifyAllListeners(null);
065          }
066        });
067        
068        dateRB.addActionListener(new ActionListener(){
069          public void actionPerformed(ActionEvent ev){
070            notifyAllListeners(Transaction.getDateComparator());
071          }
072        });
073        
074        sourceRB.addActionListener(new ActionListener(){
075          public void actionPerformed(ActionEvent ev){
076            notifyAllListeners(Transaction.getSourceComparator());
077          }
078        });
079        
080        destinationRB.addActionListener(new ActionListener(){
081          public void actionPerformed(ActionEvent ev){
082            notifyAllListeners(Transaction.getDestinationComparator());
083          }
084        });
085        
086        uidRB.addActionListener(new ActionListener(){
087          public void actionPerformed(ActionEvent ev){
088            notifyAllListeners(Transaction.getUidComparator());
089          }
090        });
091      }
092      
093      public void addListener(TransactionSorterListener l) {listeners.add(l);}
094      public void removeListener(TransactionSorterListener l) {
095        listeners.remove(l);
096      }
097      
098      /** Changes the comparator for all registered listeners to the current
099       * value of the comparator agrument.
100       */
101      public void notifyAllListeners(Comparator<Transaction> comparator){
102        for (TransactionSorterListener l : listeners)
103          l.setComparator(comparator);
104      }
105      
106      
107      public interface TransactionSorterListener
108      {
109        /** Change the comparator */
110        void setComparator (Comparator<Transaction> comparator);
111      }
112      
113    }