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 * BudgetTreeRenderer.java
024 *
025 * Created on April 1, 2006, 5:56 PM
026 */
027
028 package org.jbudget.gui.budget;
029
030 import java.awt.Color;
031 import org.jbudget.Core.Account;
032 import org.jbudget.Core.AutomaticTransaction;
033 import org.jbudget.Core.Budget;
034 import org.jbudget.Core.ExpenseAllocation;
035 import org.jbudget.Core.ExpenseCategory;
036 import org.jbudget.Core.IncomeSource;
037 import org.jbudget.Core.MoneyPit;
038 import java.awt.Component;
039 import java.util.List;
040 import javax.swing.tree.*;
041 import javax.swing.*;
042 import org.jbudget.Core.User;
043 import org.jbudget.gui.GuiPreferences;
044 import org.jbudget.util.CurrencyFormater;
045
046
047 /**
048 *
049 * @author petrov
050 */
051 class BudgetTreeRenderer extends JLabel implements TreeCellRenderer
052 {
053 /** The Budget instance */
054 private final Budget theBudget;
055 /** if true the allocation amounts will accumulate. A category will
056 * display the sum of all allocations assigned to itself and all
057 * its subcategories.
058 */
059 boolean cumulative = false;
060
061 private static Icon categoryIcon;
062 private static Icon accountIcon;
063 private static Icon incomeIcon;
064 private static Icon allocationIcon;
065 private static Icon transferToIcon;
066 private static Icon transferFromIcon;
067
068 private static Color allocationColor;
069 private static Color transferToColor;
070 private static Color transferFromColor;
071 private static Color foregroundColor;
072 private static Color backgroundColor;
073 private static Color altBackgroundColor;
074
075 /** Creates a new instance of BudgetTreeRenderer */
076 BudgetTreeRenderer(Budget budget)
077 {
078 theBudget = budget;
079 updateUI();
080 setOpaque(false);
081 }
082
083
084 @Override
085 public void updateUI()
086 {
087 super.updateUI();
088
089 allocationColor = UIManager.getColor(GuiPreferences.budgetAllocationKey);
090 transferToColor = UIManager.getColor(GuiPreferences.budgetTransferToKey);
091 transferFromColor = UIManager.getColor(GuiPreferences.budgetTransferFromKey);
092 foregroundColor = UIManager.getColor(GuiPreferences.tableFgKey);
093 backgroundColor = UIManager.getColor(GuiPreferences.tableBgKey);
094 altBackgroundColor = UIManager.getColor(GuiPreferences.tableAltBgKey);
095
096 categoryIcon = UIManager.getIcon(GuiPreferences.iconExpenseCategoryKey);
097 accountIcon = UIManager.getIcon(GuiPreferences.iconAccountKey);
098 incomeIcon = UIManager.getIcon(GuiPreferences.iconIncomeSourceKey);
099 allocationIcon = UIManager.getIcon(GuiPreferences.iconBudgetAllocKey);
100 transferToIcon = UIManager.getIcon(GuiPreferences.iconBudgetToKey);
101 transferFromIcon = UIManager.getIcon(GuiPreferences.iconBudgetFromKey);
102 }
103
104
105 public Component getTreeCellRendererComponent(
106 JTree tree,
107 Object value,
108 boolean sel,
109 boolean expanded,
110 boolean leaf,
111 int row,
112 boolean hasFocus)
113 {
114 setForeground(foregroundColor);
115
116 if (value == BudgetTreeModel.accounts)
117 {
118 setIcon(accountIcon);
119 setText(BudgetTreeModel.accounts.toString());
120 }
121 else if (value == BudgetTreeModel.expenseCategories)
122 {
123 setIcon(categoryIcon);
124 setText(BudgetTreeModel.expenseCategories.toString());
125 }
126 else if (value == BudgetTreeModel.incomeSources)
127 {
128 setIcon(incomeIcon);
129 setText(BudgetTreeModel.incomeSources.toString());
130 }
131 else if (value instanceof ExpenseCategory)
132 {
133 ExpenseCategory bc = (ExpenseCategory)value;
134 long allocationAmount = theBudget.monthlyAllocationAmount(bc,cumulative);
135 String allocationString = CurrencyFormater.format(allocationAmount);
136 long transactionAmount = -theBudget.monthlyAutoTrAmountDest(bc,cumulative);
137 String transactionString = CurrencyFormater.format(transactionAmount);
138 StringBuffer text = new StringBuffer();
139 text.append("<html><b>" + bc.getName() + "</b>");
140
141 if (bc.getSelectUsers())
142 {
143 text.append("(");
144 List<User> users = bc.getHolders();
145 if (!users.isEmpty())
146 {
147 text.append(users.get(0).getName());
148 if (users.size()>=2)
149 {
150 text.append(", ");
151 text.append(users.get(1).getName());
152 if (users.size()>2)
153 text.append(", ...");
154 }
155 }
156 else
157 text.append(" ");
158 text.append(")");
159 }
160
161 text.append(" [+<font color=#217523>" + allocationString +
162 "</font>] ");
163 if (transactionAmount < 0)
164 text.append(" [<font color=#c43127>" + transactionString +
165 "</font>]");
166 else
167 text.append(" [+<font color=#217523>" + transactionString +
168 "</font>] ");
169 text.append("</html>");
170 setIcon(categoryIcon);
171 setText(text.toString());
172 }
173 else if (value instanceof Account)
174 {
175 Account account = (Account) value;
176 long inAmount =
177 theBudget.monthlyAutoTrAmountDest(new MoneyPit(account));
178 long outAmount =
179 theBudget.monthlyAutoTrAmountSource(new MoneyPit(account));
180 // Making sure that there are no negative amounts.
181 if (inAmount < 0)
182 {
183 outAmount -= inAmount;
184 inAmount = 0L;
185 }
186 if (outAmount < 0)
187 {
188 inAmount -= outAmount;
189 outAmount = 0;
190 }
191 // Now making out amount negative
192 outAmount = -outAmount;
193 String inString = CurrencyFormater.format(inAmount);
194 String outString = CurrencyFormater.format(outAmount);
195
196
197 StringBuffer text = new StringBuffer();
198 text.append("<html><b>" + account.getName() + "</b>");
199 List<User> users = account.getAccountHolders();
200 if (!users.isEmpty())
201 {
202 text.append("(");
203 text.append(users.get(0).getName());
204 if (users.size()>=2)
205 {
206 text.append(", ");
207 text.append(users.get(1).getName());
208 if (users.size()>2)
209 text.append(", ...");
210 }
211 text.append(")");
212 }
213 text.append(" [+<font color=#217523>" + inString +
214 "</font>] ");
215 text.append(" [<font color=#c43127>" + outString +
216 "</font>]");
217 text.append("</html>");
218 setIcon(accountIcon);
219 setText(text.toString());
220 }
221 else if (value instanceof IncomeSource)
222 {
223 IncomeSource incomeSource = (IncomeSource) value;
224
225 long inAmount =
226 incomeSource.getMonthlyAmount();
227 long outAmount =
228 theBudget.monthlyAutoTrAmountSource(
229 new MoneyPit(incomeSource,theBudget));
230 // Making sure that there are no negative amounts.
231 if (inAmount < 0)
232 {
233 outAmount -= inAmount;
234 inAmount = 0L;
235 }
236 if (outAmount < 0)
237 {
238 inAmount -= outAmount;
239 outAmount = 0;
240 }
241 // Now making out amount negative
242 outAmount = -outAmount;
243 String inString = CurrencyFormater.format(inAmount);
244 String outString = CurrencyFormater.format(outAmount);
245
246
247 StringBuffer text = new StringBuffer();
248 text.append("<html><b>" + incomeSource.getName() + "</b>");
249 text.append(" [+<font color=#217523>" + inString +
250 "</font>] ");
251 text.append(" [<font color=#c43127>" + outString +
252 "</font>]");
253 text.append("</html>");
254 setIcon(incomeIcon);
255 setText(text.toString());
256 }
257 else if (value instanceof ExpenseAllocation)
258 {
259 setForeground(allocationColor);
260 ExpenseAllocation expAlloc = (ExpenseAllocation)value;
261 String text = "Allocation: " +
262 CurrencyFormater.format(expAlloc.getAmount());
263 AutomaticTransaction.Type type = expAlloc.getType();
264 if (type == AutomaticTransaction.Type.DAILY)
265 text += "/day";
266 else if (type == AutomaticTransaction.Type.WEEKLY)
267 text += "/week";
268 else if (type == AutomaticTransaction.Type.MONTHLY)
269 text += "/month";
270 else if (type == AutomaticTransaction.Type.YEARLY)
271 text += "/year";
272 setText(text);
273 setIcon(allocationIcon);
274 }
275 else if (value instanceof AutomaticTransaction)
276 {
277 setForeground(transferFromColor);
278 AutomaticTransaction autoTr = (AutomaticTransaction)value;
279 String text = "Auto. Trans.: " +
280 CurrencyFormater.format(autoTr.getAmount());
281 AutomaticTransaction.Type type = autoTr.getType();
282 if (type == AutomaticTransaction.Type.DAILY)
283 text += "/day";
284 else if (type == AutomaticTransaction.Type.WEEKLY)
285 text += "/week";
286 else if (type == AutomaticTransaction.Type.MONTHLY)
287 text += "/month";
288 else if (type == AutomaticTransaction.Type.YEARLY)
289 text += "/year";
290 setText(text);
291 // Automatic transactions can be shown in two places since they have a
292 // a source and a destination. Now we have to figure out which is it and
293 // choose an appropriate icon.
294 TreePath path = tree.getPathForRow(row);
295 MoneyPit source = autoTr.getSource();
296 MoneyPit dest = autoTr.getDestination();
297 Icon icon = null;
298
299 if (path != null)
300 {
301 Object parent = path.getParentPath().getLastPathComponent();
302
303 if (source != MoneyPit.NONE && parent.equals(source.getContent()))
304 {
305 if (source.getType() == MoneyPit.Type.EXPENSE)
306 {
307 icon = transferToIcon;
308 setForeground(transferToColor);
309 }
310 else
311 icon = transferFromIcon;
312 }
313 else if (dest != MoneyPit.NONE && parent.equals(dest.getContent()))
314 {
315 if (dest.getType() == MoneyPit.Type.EXPENSE)
316 icon = transferFromIcon;
317 else
318 {
319 icon = transferToIcon;
320 setForeground(transferToColor);
321 }
322 }
323
324 setIcon(icon);
325 }
326 }
327 else
328 {
329 this.setText(value.toString());
330 }
331
332 return this;
333 }
334
335 }