1 | |
|
2 | |
|
3 | |
|
4 | |
package org.homeunix.thecave.buddi.view.swing; |
5 | |
|
6 | |
import java.awt.Component; |
7 | |
|
8 | |
import javax.swing.JTable; |
9 | |
import javax.swing.table.DefaultTableCellRenderer; |
10 | |
|
11 | |
import org.homeunix.thecave.buddi.model.BudgetCategory; |
12 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
13 | |
import org.homeunix.thecave.buddi.util.InternalFormatter; |
14 | |
|
15 | 12628 | public class MyBudgetTableAmountCellRenderer extends DefaultTableCellRenderer { |
16 | |
public static final long serialVersionUID = 0; |
17 | |
|
18 | 12628 | private final StringBuilder sb = new StringBuilder(); |
19 | |
|
20 | |
@Override |
21 | |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
22 | 9570 | super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); |
23 | |
|
24 | 9570 | if (value instanceof Object[]){ |
25 | 9570 | Object[] values = (Object[]) value; |
26 | |
|
27 | 9570 | if (sb.length() > 0) |
28 | 9195 | sb.delete(0, sb.length()); |
29 | |
|
30 | |
|
31 | 9570 | if ((Long) values[1] == 0 && (Long) values[2] == 0){ |
32 | |
|
33 | 9570 | sb.append("---"); |
34 | |
} |
35 | |
else { |
36 | |
|
37 | 0 | TextFormatter.appendFormattedCurrency(sb, (Long) values[1], |
38 | |
InternalFormatter.isRed((BudgetCategory) values[0], (Long) values[1]), false); |
39 | |
|
40 | |
|
41 | 0 | if (!((Long) values[2]).equals((Long) values[1]) && (Long) values[2] != 0){ |
42 | 0 | sb.append(" ("); |
43 | 0 | TextFormatter.appendFormattedCurrency(sb, |
44 | |
(Long) values[2], |
45 | |
InternalFormatter.isRed((BudgetCategory) values[0], (Long) values[2]), |
46 | |
false); |
47 | 0 | sb.append(")"); |
48 | |
} |
49 | |
} |
50 | |
|
51 | 9570 | sb.append(" / "); |
52 | 9570 | final Long actual = (Long) values[4]; |
53 | 9570 | if(actual != 0) { |
54 | 0 | TextFormatter.appendFormattedCurrency(sb, actual, actual < 0, actual < 0); |
55 | |
} else { |
56 | 9570 | sb.append("---"); |
57 | |
} |
58 | |
|
59 | 9570 | final Long actualIncludingSubs = (Long) values[5]; |
60 | 9570 | if(actualIncludingSubs != (long)actual) { |
61 | 0 | sb.append(" ("); |
62 | 0 | TextFormatter.appendFormattedCurrency(sb, actualIncludingSubs, actualIncludingSubs < 0, |
63 | |
actualIncludingSubs < 0); |
64 | 0 | sb.append(")"); |
65 | |
} |
66 | |
|
67 | 9570 | for (int i = 0; i < ((Integer) values[3]); i++){ |
68 | 0 | sb.insert(0, "    "); |
69 | |
} |
70 | |
|
71 | 9570 | sb.insert(0, "<html>"); |
72 | 9570 | sb.append("</html>"); |
73 | 9570 | this.setText(sb.toString()); |
74 | |
} |
75 | |
|
76 | 9570 | if (hasFocus && isSelected) { |
77 | 0 | table.editCellAt(row,column); |
78 | 0 | table.getCellEditor(row, column).getTableCellEditorComponent(table, value, isSelected, row, column).requestFocus(); |
79 | |
} |
80 | |
|
81 | 9570 | return this; |
82 | |
} |
83 | |
} |