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.ArrayList;
031    import java.util.LinkedList;
032    import java.util.List;
033    import javax.swing.event.TableModelListener;
034    import javax.swing.table.AbstractTableModel;
035    import org.jbudget.Core.Account;
036    import org.jbudget.Core.Budget;
037    import org.jbudget.Core.User;
038    import org.jbudget.io.Converter;
039    import org.jbudget.io.DataManager;
040    import org.jbudget.util.Pair;
041    
042    /**
043     * A list model for selecting an account.
044     *
045     * @author petrov
046     */
047    public class AccountSelectorModel extends AbstractTableModel
048    {
049      /** Used when showing accounts registered with a particular budget */
050      private final Budget budget;
051      
052      public static final int SHOW_ALL = 1;
053      public static final int SHOW_UNUSED = 2;
054      public static final int SHOW_REGISTERED = 3;
055      public static final int SHOW_UNREGISTERED = 4;
056      
057      /** Type of list */
058      private final int listType;
059      
060      /** should we show hidden accounts */
061      private boolean showHidden = false;
062      
063      /** List of displayed accounts */
064      private List<Account> availableAccounts = new ArrayList<Account>();
065      
066      /** Column names */
067      private String[] columnNames = {"name", "id", "holders"};
068      
069      private DataManager.AccountListener listener = 
070          new DataManager.AccountListener(){
071        public void accountAdded(Account account){
072          initialize();
073          int accountID = account.getID();
074          int i = 0;
075          for (Account a : availableAccounts){
076            if (a.getID() == accountID){
077              fireTableRowsInserted(i, i);
078              break;
079            }
080            i++;
081          }
082        }
083        
084        public void accountUpdated(Account account){
085          fireTableDataChanged();
086        }
087        
088        public void accountRemoved(int accountID){
089          int i = 0;
090          for (Account a : availableAccounts){
091            if (a.getID() == accountID)
092              break;
093            i++;
094          }
095          if (i == availableAccounts.size())
096            return;
097          initialize();
098          fireTableRowsDeleted(i, i);
099        }
100      };
101      
102      /** Guard variable */
103      private boolean wasDisposed = false;
104      
105          
106      /** Creates a new instance of BudgetSelectorModel. Configures the model to
107       *  show all or a subset of all accounts known to the 
108       * {@link org.petrov.dmitri.jbudget.io.DataManager DataManager}
109       * @param showUnused if true only accounts that are not registered with any
110       *        budget are shown. In the opposite case all accounts are shown.
111       */
112      public AccountSelectorModel(boolean showUnused){
113        DataManager.getInstance().addAccountListener(listener);
114        if (showUnused)
115          listType = SHOW_UNUSED;
116        else
117          listType = SHOW_ALL;
118        budget = null;
119        initialize();
120      }
121      
122      /** Creates a new instance of BudgetSelectorModel. Configures the model to 
123       *  show accounts registered with a particular budget, or all accounts that
124       *  have not been registered with the budget. 
125       * @param showUnused if true accounts that are not registered with the
126       *        budget are shown. In the opposite case all accounts registered
127       *        with the budget are shown.
128       */
129      public AccountSelectorModel(Budget budget, boolean showUnused){
130        DataManager.getInstance().addAccountListener(listener);
131        if (showUnused)
132          listType = SHOW_UNREGISTERED;
133        else
134          listType = SHOW_REGISTERED;
135        this.budget = budget;
136       initialize();
137      }
138      
139      
140      /** Frees resources */
141      public void dispose(){
142        DataManager.getInstance().removeAccountListener(listener);
143        wasDisposed = true;
144      }
145      
146      public void finalize(){
147        if (!wasDisposed){
148          System.err.println("Dispose was not called for AccountSelectorModel");
149          dispose();
150        }
151      }
152      
153      /** Gets a list of available budgets from the DataManager */
154      private void initialize (){
155        List<Pair<Integer,String>> accountsInfo = 
156            new ArrayList<Pair<Integer,String>>();
157        if (listType == SHOW_ALL){
158          accountsInfo.addAll(DataManager.getInstance().getValidAccounts());
159        } 
160        else if (listType == SHOW_UNUSED)
161        {
162          DataManager mgr = DataManager.getInstance();
163          for (Pair<Integer,String> info : mgr.getValidAccounts())
164            if (mgr.isUnusedAccount(info.first().intValue()))
165              accountsInfo.add(info);
166        }
167        else if (listType == SHOW_REGISTERED)
168        {
169          accountsInfo.addAll(
170              Converter.getInfo(budget.getRegisteredAccounts()));
171        } 
172        else if (listType == SHOW_UNREGISTERED)
173        {
174          accountsInfo.addAll(DataManager.getInstance().getValidAccounts());
175          accountsInfo.removeAll(
176              Converter.getInfo(budget.getRegisteredAccounts()));
177        }
178        
179        availableAccounts.clear();
180        
181        for (Pair<Integer,String> info : accountsInfo)
182        {
183          Account account =
184              DataManager.getInstance().getAccount(info.first().intValue());
185          if (showHidden || (!account.isHidded()))
186            availableAccounts.add(account);
187        }  
188      }
189      
190      //================ IMPLEMENTING THE INTERFACE ===========================//
191      
192      /** Number of columns. */
193      public int getColumnCount() {
194        return 3;
195      }
196      
197      /** Number of rows. */
198      public int getRowCount() {
199        return availableAccounts.size();
200      }
201      
202      /** Get column name. */
203      public String getColumnName(int columnIndex) {
204        return columnNames[columnIndex];
205      }
206      
207      public Class<?> getColumnClass(int columnIndex){
208        switch (columnIndex)
209        {
210          case 0:
211            return String.class;
212          case 1:
213            return Integer.class;
214          case 2:
215            return List.class;
216        }
217        return Object.class;
218      }
219      
220      public Object getValueAt(int rowIndex, int columnIndex) {
221        Account account = availableAccounts.get(rowIndex);
222        switch(columnIndex)
223        {
224          case 0:
225            return account.getName();
226          case 1:
227            return new Integer(account.getID());
228          case 2:
229            List<Integer> uids = account.getAccountHolderUids();
230            List<User> users = new ArrayList<User>(uids.size());
231            for (Integer uid : uids)
232              users.add(DataManager.getInstance().getUser(uid));
233            return users;
234        }
235        return null;
236      }
237      
238      public void setValueAt(Object aValue,
239                    int rowIndex,
240                    int columnIndex)
241      {
242        throw new IllegalStateException("Editing not supported");
243      }
244      
245      public boolean isCellEditable(int rowIndex, int columnIndex){
246        return false;
247      }
248      // ======================================================================//
249     
250    }