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     * BudgetSelectorModel.java
024     *
025     * Created on June 5, 2006, 7:08 PM
026     */
027    
028    package org.jbudget.gui.budget;
029    
030    import java.util.List;
031    import javax.swing.AbstractListModel;
032    import org.jbudget.Core.Budget;
033    import org.jbudget.io.DataManager;
034    import org.jbudget.util.Pair;
035    
036    /**
037     * A list model for selecting budget.
038     *
039     * @author petrov
040     */
041    public class BudgetSelectorModel extends AbstractListModel
042    {
043      /** if true only unused budgets are shown */
044      private final boolean showUnused;
045      
046      private List<Pair<Integer,String>> availableBudgets;
047      
048      private DataManager.BudgetListener listener = 
049          new DataManager.BudgetListener(){
050        public void budgetAdded(Budget budget){
051          initialize();
052          int budgetID = budget.getID();
053          int i = 0;
054          for (Pair<Integer,String> budgetInfo : availableBudgets){
055            if (budgetInfo.first().intValue() == budgetID){
056              fireIntervalAdded(this, i, i);
057              break;
058            }
059            i++;
060          }
061        }
062        
063        public void budgetRemoved(int budgetID){
064          int i = 0;
065          for (Pair<Integer,String> budgetInfo : availableBudgets){
066            if (budgetInfo.first().intValue() == budgetID)
067              break;
068            i++;
069          }
070          if (i == availableBudgets.size())
071            return;
072          initialize();
073          fireIntervalRemoved(this, i, i);
074        }
075      };
076      
077      /** Guard variable */
078      private boolean wasDisposed = false;
079          
080      /** Creates a new instance of BudgetSelectorModel */
081      public BudgetSelectorModel(boolean showUnused){
082        DataManager.getInstance().addBudgetListener(listener);
083        this.showUnused = showUnused;
084        initialize();
085      }
086      
087      /** Frees resources */
088      public void dispose(){
089        DataManager.getInstance().removeBudgetListener(listener);
090        wasDisposed = true;
091      }
092      
093      public void finalize(){
094        if (!wasDisposed){
095          System.err.println("Dispose was not called for BudgetSelectorModel");
096          dispose();
097        }
098      }
099      
100      /** Gets a list of available budgets from the DataManager */
101      private void initialize (){
102        if (showUnused)
103          availableBudgets = DataManager.getInstance().getUnusedBudgets();
104        else
105          availableBudgets = DataManager.getInstance().getValidBudgets();
106      }
107      
108      /** Returns the element at the appropriate location */
109      public Object getElementAt(int i){
110        return availableBudgets.get(i);
111      }
112      
113      /** Returns the number of available budgets */
114      public int getSize(){
115        return availableBudgets.size();
116      }
117     
118    }