| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.model.swing; |
| 5 | |
|
| 6 | |
import java.util.ArrayList; |
| 7 | |
import java.util.Collections; |
| 8 | |
import java.util.HashMap; |
| 9 | |
import java.util.List; |
| 10 | |
import java.util.Map; |
| 11 | |
|
| 12 | |
import javax.swing.ComboBoxModel; |
| 13 | |
import javax.swing.DefaultComboBoxModel; |
| 14 | |
import javax.swing.event.ListDataListener; |
| 15 | |
|
| 16 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 17 | |
import org.homeunix.thecave.buddi.model.Account; |
| 18 | |
import org.homeunix.thecave.buddi.model.BudgetCategory; |
| 19 | |
import org.homeunix.thecave.buddi.model.Document; |
| 20 | |
import org.homeunix.thecave.buddi.model.Source; |
| 21 | |
import org.homeunix.thecave.buddi.model.impl.FilteredLists; |
| 22 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 23 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 24 | |
|
| 25 | |
import ca.digitalcave.moss.application.document.DocumentChangeEvent; |
| 26 | |
import ca.digitalcave.moss.application.document.DocumentChangeListener; |
| 27 | |
|
| 28 | |
public class SourceComboBoxModel implements ComboBoxModel { |
| 29 | |
private static final long serialVersionUID = 0; |
| 30 | |
|
| 31 | |
private final Document model; |
| 32 | |
private final boolean includeIncome; |
| 33 | |
private final boolean includeSplit; |
| 34 | |
|
| 35 | |
private final Source ignoredSource; |
| 36 | |
|
| 37 | 2433 | private final List<Account> accounts = new ArrayList<Account>(); |
| 38 | 2433 | private final List<BudgetCategory> budgetCategories = new ArrayList<BudgetCategory>(); |
| 39 | |
|
| 40 | 2433 | private final Map<Object, Integer> levelMappings = new HashMap<Object, Integer>(); |
| 41 | |
|
| 42 | |
private final DefaultComboBoxModel comboBoxModel; |
| 43 | |
|
| 44 | |
private final DocumentChangeListener listener; |
| 45 | |
|
| 46 | 2433 | public SourceComboBoxModel(Document model, boolean includeIncome, boolean includeSplit, Source ignoredSource) { |
| 47 | 2433 | this.model = model; |
| 48 | 2433 | this.includeIncome = includeIncome; |
| 49 | 2433 | this.includeSplit = includeSplit; |
| 50 | 2433 | this.comboBoxModel = new DefaultComboBoxModel(); |
| 51 | 2433 | this.ignoredSource = ignoredSource; |
| 52 | 2433 | updateComboBoxModel(null); |
| 53 | 2433 | listener = new DocumentChangeListener(){ |
| 54 | |
public void documentChange(DocumentChangeEvent event) { |
| 55 | 2 | updateComboBoxModel(null); |
| 56 | 2 | } |
| 57 | |
}; |
| 58 | 2433 | model.addDocumentChangeListener(listener); |
| 59 | 2433 | } |
| 60 | |
|
| 61 | |
public Object getSelectedItem() { |
| 62 | 12525 | return getComboBoxModel().getSelectedItem(); |
| 63 | |
} |
| 64 | |
|
| 65 | |
public void setSelectedItem(Object anItem) { |
| 66 | 4872 | getComboBoxModel().setSelectedItem(anItem); |
| 67 | 4872 | } |
| 68 | |
|
| 69 | |
public void addListDataListener(ListDataListener l) { |
| 70 | 7438 | getComboBoxModel().addListDataListener(l); |
| 71 | 7438 | } |
| 72 | |
|
| 73 | |
public Object getElementAt(int index) { |
| 74 | 28648 | return getComboBoxModel().getElementAt(index); |
| 75 | |
} |
| 76 | |
|
| 77 | |
public int getSize() { |
| 78 | 14654 | return getComboBoxModel().getSize(); |
| 79 | |
} |
| 80 | |
|
| 81 | |
public void removeListDataListener(ListDataListener l) { |
| 82 | 0 | getComboBoxModel().removeListDataListener(l); |
| 83 | 0 | } |
| 84 | |
|
| 85 | |
private DefaultComboBoxModel getComboBoxModel(){ |
| 86 | 94510 | return comboBoxModel; |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public boolean contains(Source source){ |
| 96 | 0 | return (accounts.contains(source) || budgetCategories.contains(source)); |
| 97 | |
} |
| 98 | |
|
| 99 | |
public int getLevel(Object item){ |
| 100 | 13651 | if (levelMappings.containsKey(item)) |
| 101 | 13651 | return levelMappings.get(item); |
| 102 | 0 | return 0; |
| 103 | |
} |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public void updateComboBoxModel(Source source){ |
| 112 | 2435 | Object selected = getSelectedItem(); |
| 113 | |
|
| 114 | 2435 | accounts.clear(); |
| 115 | |
|
| 116 | 2435 | for (Account a : model.getAccounts()) { |
| 117 | 0 | if (!a.isDeleted() || PrefsModel.getInstance().isShowDeleted() || a.equals(source)){ |
| 118 | 0 | if (!a.equals(ignoredSource)) |
| 119 | 0 | accounts.add(a); |
| 120 | |
} |
| 121 | |
} |
| 122 | 2435 | budgetCategories.clear(); |
| 123 | 2435 | for (BudgetCategory bc : model.getBudgetCategories()) { |
| 124 | 24350 | if (!bc.isDeleted() || PrefsModel.getInstance().isShowDeleted() || bc.equals(source)){ |
| 125 | 24350 | if (!bc.equals(ignoredSource)) |
| 126 | 24350 | budgetCategories.add(bc); |
| 127 | |
} |
| 128 | |
} |
| 129 | |
|
| 130 | 2435 | Collections.sort(accounts); |
| 131 | 2435 | Collections.sort(budgetCategories); |
| 132 | |
|
| 133 | 2435 | getComboBoxModel().removeAllElements(); |
| 134 | 2435 | getComboBoxModel().addElement(TextFormatter.getTranslation(BuddiKeys.ACCOUNTS_COMBOBOX_HEADER)); |
| 135 | 2435 | for (Account a : accounts) { |
| 136 | 0 | getComboBoxModel().addElement(a); |
| 137 | |
} |
| 138 | 2435 | getComboBoxModel().addElement(" "); |
| 139 | 2435 | getComboBoxModel().addElement(TextFormatter.getTranslation(BuddiKeys.BUDGET_CATEGORIES_COMBOBOX_HEADER)); |
| 140 | |
|
| 141 | 2435 | addBudgetCategories(null, 0); |
| 142 | |
|
| 143 | 2435 | if (includeSplit){ |
| 144 | 2332 | getComboBoxModel().addElement(" "); |
| 145 | 2332 | getComboBoxModel().addElement(BuddiKeys.SPLITS.toString()); |
| 146 | |
} |
| 147 | |
|
| 148 | 2435 | setSelectedItem(selected); |
| 149 | 2435 | } |
| 150 | |
|
| 151 | |
private void addBudgetCategories(BudgetCategory parent, int level){ |
| 152 | |
final List<BudgetCategory> siblings; |
| 153 | 14404 | if (PrefsModel.getInstance().isShowFlatBudgetInSourceCombobox()) { |
| 154 | 0 | siblings = budgetCategories; |
| 155 | |
} |
| 156 | |
else { |
| 157 | 14404 | siblings = new FilteredLists.BudgetCategoryListFilteredByParent(model, budgetCategories, parent); |
| 158 | |
} |
| 159 | 14404 | for (BudgetCategory bc : siblings) { |
| 160 | 24350 | if (bc.isIncome() == includeIncome){ |
| 161 | 11969 | getComboBoxModel().addElement(bc); |
| 162 | 11969 | levelMappings.put(bc, level); |
| 163 | 11969 | if (!PrefsModel.getInstance().isShowFlatBudgetInSourceCombobox()){ |
| 164 | 11969 | addBudgetCategories(bc, level + 1); |
| 165 | |
} |
| 166 | |
} |
| 167 | |
} |
| 168 | 14404 | } |
| 169 | |
} |