Coverage Report - org.homeunix.thecave.buddi.model.swing.MyAccountTreeTableModel
 
Classes in this File Line Coverage Branch Coverage Complexity
MyAccountTreeTableModel
39%
26/66
34%
18/52
5.556
 
 1  
 /*
 2  
  * Created on Aug 4, 2007 by wyatt
 3  
  */
 4  
 package org.homeunix.thecave.buddi.model.swing;
 5  
 
 6  
 import java.util.List;
 7  
 
 8  
 import org.homeunix.thecave.buddi.i18n.BuddiKeys;
 9  
 import org.homeunix.thecave.buddi.model.Account;
 10  
 import org.homeunix.thecave.buddi.model.AccountType;
 11  
 import org.homeunix.thecave.buddi.model.Document;
 12  
 import org.homeunix.thecave.buddi.model.impl.FilteredLists;
 13  
 import org.homeunix.thecave.buddi.model.prefs.PrefsModel;
 14  
 import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
 15  
 
 16  
 public class MyAccountTreeTableModel extends AbstractTreeTableModel {
 17  
 
 18  
         private final Document model;
 19  
         private final Object root;
 20  
 
 21  
         public MyAccountTreeTableModel(Document model) {
 22  3034
                 super(new Object());
 23  3034
                 this.model = model;
 24  3034
                 this.root = getRoot();                
 25  3034
         }
 26  
 
 27  
         public int getColumnCount() {
 28  22430
                 int columns = 2;
 29  22430
                 if (PrefsModel.getInstance().isShowOverdraft() || PrefsModel.getInstance().isShowCreditRemaining())
 30  28
                         columns++;
 31  22430
                 if (PrefsModel.getInstance().isShowInterestRates())
 32  14
                         columns++;
 33  22430
                 return columns;
 34  
         }
 35  
 
 36  
         @Override
 37  
         public String getColumnName(int column) {
 38  6338
                 if (column == 0)
 39  3166
                         return PrefsModel.getInstance().getTranslator().get(BuddiKeys.ACCOUNT);
 40  3172
                 if (column == 1)
 41  3166
                         return PrefsModel.getInstance().getTranslator().get(BuddiKeys.AMOUNT);
 42  
                 
 43  
                 //Depending on which of overdraft/credit and interest options are enabled, we
 44  
                 // may have 3 or 4 columns.  If overdraft/credit is enabled, column index 2 is
 45  
                 // used for it; otherwise, it is used for interest rate.
 46  6
                 if (column == 2){
 47  6
                         if (PrefsModel.getInstance().isShowOverdraft() || PrefsModel.getInstance().isShowCreditRemaining())
 48  4
                                 return PrefsModel.getInstance().getTranslator().get(BuddiKeys.AVAILABLE_FUNDS);
 49  
                         else
 50  2
                                 return PrefsModel.getInstance().getTranslator().get(BuddiKeys.INTEREST_RATE);        
 51  
                 }
 52  
                 //If there is a column index 3, it will always be for interest rate.
 53  0
                 if (column == 3)
 54  0
                         return PrefsModel.getInstance().getTranslator().get(BuddiKeys.INTEREST_RATE);
 55  0
                 return "";
 56  
         }
 57  
 
 58  
         public Object getValueAt(Object node, int column) {
 59  3034
                 return node;
 60  
         }
 61  
 
 62  
         public Object getChild(Object parent, int childIndex) {
 63  0
                 if (parent.equals(root)){
 64  0
                         if (PrefsModel.getInstance().isShowFlatAccounts()){
 65  0
                                 return new FilteredLists.AccountListFilteredByDeleted(model, model.getAccounts()).get(childIndex);
 66  
                         }
 67  
                         else{
 68  0
                                 List<AccountType> types = new FilteredLists.TypeListFilteredByAccounts(model);
 69  0
                                 if (childIndex < types.size())
 70  0
                                         return types.get(childIndex);
 71  
                         }
 72  
                 }
 73  0
                 if (parent instanceof AccountType){
 74  0
                         List<Account> accounts = new FilteredLists.AccountListFilteredByDeleted(model, new FilteredLists.AccountListFilteredByType(model, model.getAccounts(), (AccountType) parent));
 75  0
                         if (childIndex < accounts.size())
 76  0
                                 return accounts.get(childIndex);
 77  
                 }
 78  0
                 return null;
 79  
         }
 80  
 
 81  
         public int getChildCount(Object parent) {
 82  15578
                 if (parent.equals(root)){
 83  15578
                         if (PrefsModel.getInstance().isShowFlatAccounts()){
 84  6
                                 return new FilteredLists.AccountListFilteredByDeleted(model, model.getAccounts()).size();
 85  
                         }
 86  
                         else{
 87  15572
                                 List<AccountType> types = new FilteredLists.TypeListFilteredByAccounts(model);
 88  15572
                                 return types.size();
 89  
                         }
 90  
                 }
 91  0
                 if (parent instanceof AccountType){
 92  0
                         List<Account> accounts = new FilteredLists.AccountListFilteredByDeleted(model, new FilteredLists.AccountListFilteredByType(model, model.getAccounts(), (AccountType) parent));
 93  0
                         return accounts.size();
 94  
                 }
 95  
 
 96  0
                 return 0;
 97  
         }
 98  
 
 99  
         public int getIndexOfChild(Object parent, Object child) {
 100  0
                 if (parent == null || child == null)
 101  0
                         return -1;
 102  
 
 103  
 //                if (parent.equals(root) && child instanceof AccountType){
 104  0
                 if (parent.equals(root)){
 105  0
                         if (PrefsModel.getInstance().isShowFlatAccounts()){
 106  0
                                 return new FilteredLists.AccountListFilteredByDeleted(model, model.getAccounts()).indexOf(child);
 107  
                         }
 108  
                         else{
 109  0
                                 List<AccountType> types = new FilteredLists.TypeListFilteredByAccounts(model);
 110  0
                                 return types.indexOf(child);
 111  
                         }
 112  
                 }
 113  
 
 114  0
                 if (parent instanceof AccountType && child instanceof Account){
 115  0
                         List<Account> accounts = new FilteredLists.AccountListFilteredByDeleted(model, new FilteredLists.AccountListFilteredByType(model, model.getAccounts(), (AccountType) parent));
 116  0
                         return accounts.indexOf(child);
 117  
                 }
 118  0
                 return -1;
 119  
         }
 120  
 
 121  
         public void fireStructureChanged(){
 122  132
                 modelSupport.fireNewRoot();
 123  132
         }
 124  
 
 125  
         @Override
 126  
         public String toString() {
 127  0
                 StringBuilder sb = new StringBuilder();
 128  
 
 129  0
                 for (int i = 0; i < getChildCount(root); i++) {
 130  0
                         Object o1 = getChild(root, i);
 131  0
                         sb.append(o1).append(" [");
 132  0
                         for (int j = 0; j < getChildCount(o1); j++){
 133  0
                                 Object o2 = getChild(o1, j);
 134  0
                                 sb.append(o2);
 135  0
                                 if (j < (getChildCount(o1) - 1))
 136  0
                                         sb.append(", ");
 137  
                         }
 138  0
                         sb.append("]  ");
 139  
                 }
 140  
 
 141  0
                 return sb.toString();
 142  
         }
 143  
 }