Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
InternalFormatter |
|
| 2.1818181818181817;2.182 |
1 | /* | |
2 | * Created on Apr 11, 2007 by wyatt | |
3 | */ | |
4 | package org.homeunix.thecave.buddi.util; | |
5 | ||
6 | import java.awt.Dimension; | |
7 | ||
8 | import javax.swing.JButton; | |
9 | import javax.swing.JComboBox; | |
10 | import javax.swing.JComponent; | |
11 | ||
12 | import org.homeunix.thecave.buddi.model.Account; | |
13 | import org.homeunix.thecave.buddi.model.AccountType; | |
14 | import org.homeunix.thecave.buddi.model.BudgetCategory; | |
15 | import org.homeunix.thecave.buddi.model.Source; | |
16 | import org.homeunix.thecave.buddi.model.Transaction; | |
17 | ||
18 | 0 | public class InternalFormatter { |
19 | // public static DateFormat getDateFormat(){ | |
20 | // return Formatter.getDateFormat(PrefsModel.getInstance().getDateFormat()); | |
21 | // } | |
22 | // | |
23 | // /** | |
24 | // * Converts a long value (in cents: 10000 == $100.00) to a string | |
25 | // * with proper decimal values, with the user's desired currency | |
26 | // * sign in the user's specified position (whether behind or in front | |
27 | // * of the amount). It is highly recommended that you use this method | |
28 | // * to output monetary values, as it presents the user with a constant | |
29 | // * look for currency. | |
30 | // * | |
31 | // * Note that this method uses HTML font tags to set the color. This | |
32 | // * means that you must wrap all code which uses this with HTML start | |
33 | // * and end tags - you can use the getHtmlWrapper() method to do this. | |
34 | // * | |
35 | // * @param value The currency amount, in cents (as per Buddi's internal | |
36 | // * representation of currency). For instance, to represent the value | |
37 | // * $123.45, you would pass in 12345. | |
38 | // * @param red Wrap the return string in HTML font tags to make it red. | |
39 | // * @param negate Multiply the value by *1 before rendering. | |
40 | // * @return A string with proper decimal places, plus the user's defined | |
41 | // * currency symbol in the correct position (whether before or after the | |
42 | // * amount). Optionally it will be wrapped in red font tags. | |
43 | // */ | |
44 | // public static String getFormattedCurrency(long value, boolean negate, boolean red){ | |
45 | // if (negate) | |
46 | // value *= -1; | |
47 | // | |
48 | // boolean symbolAfterAmount = PrefsModel.getInstance().isShowCurrencyAfterAmount(); | |
49 | // String symbol = PrefsModel.getInstance().getCurrencySign(); | |
50 | // | |
51 | // | |
52 | // String formatted = | |
53 | // (red ? "<font color='red'>" : "") | |
54 | // + (symbolAfterAmount ? "" : symbol) | |
55 | // + Formatter.getDecimalFormat().format((double) value / 100.0) | |
56 | // + (symbolAfterAmount ? " " + symbol : "") | |
57 | // + (red ? "</font>" : ""); | |
58 | // | |
59 | // return formatted; | |
60 | // } | |
61 | // | |
62 | // public static String getFormattedCurrency(long value, boolean negate){ | |
63 | // return getFormattedCurrency(value, negate, isRed(value)); | |
64 | // } | |
65 | // | |
66 | // public static String getFormattedCurrency(long value){ | |
67 | // return getFormattedCurrency(value, false, false); | |
68 | // } | |
69 | // | |
70 | // /** | |
71 | // * Returns the name, formatted as a String. This method complies | |
72 | // * with the Buddi Formatting Standard, and should be used whenever | |
73 | // * possible. | |
74 | // * @param a | |
75 | // * @return | |
76 | // */ | |
77 | // public static String getFormattedNameForAccount(Account a){ | |
78 | // return getFormattedNameGeneric(a.getName(), a.getType().isCredit()); | |
79 | // } | |
80 | // | |
81 | // /** | |
82 | // * Returns the name, formatted as a String. This method complies | |
83 | // * with the Buddi Formatting Standard, and should be used whenever | |
84 | // * possible. | |
85 | // * @param c | |
86 | // * @return | |
87 | // */ | |
88 | // public static String getFormattedNameForCategory(BudgetCategory c){ | |
89 | // return getFormattedNameGeneric(c.getName(), !c.isIncome()); | |
90 | // } | |
91 | // | |
92 | // /** | |
93 | // * Returns the name, formatted as a String. This method complies | |
94 | // * with the Buddi Formatting Standard, and should be used whenever | |
95 | // * possible. | |
96 | // * @param t | |
97 | // * @return | |
98 | // */ | |
99 | // public static String getFormattedNameForType(Type t){ | |
100 | // return getFormattedNameGeneric(t.getName(), t.isCredit()); | |
101 | // } | |
102 | // | |
103 | // /** | |
104 | // * Returns the given string, optionally wrapped in red font tags. | |
105 | // * @param name | |
106 | // * @param isRed | |
107 | // * @return | |
108 | // */ | |
109 | // public static String getFormattedNameGeneric(String name, boolean isRed){ | |
110 | // String formatted = | |
111 | // (isRed ? "<font color='red'>" : "") | |
112 | // + PrefsModel.getInstance().getTranslator().get(name) | |
113 | // + (isRed ? "</font>" : ""); | |
114 | // | |
115 | // return formatted; | |
116 | // } | |
117 | // | |
118 | // public static boolean isRed(Source s){ | |
119 | // if (s instanceof Account){ | |
120 | // return ((Account) s).getType().isCredit(); | |
121 | // } | |
122 | // else if (s instanceof BudgetCategory){ | |
123 | // return !((BudgetCategory) s).isIncome(); | |
124 | // } | |
125 | // else | |
126 | // return false; | |
127 | // } | |
128 | // | |
129 | // public static boolean isRed(Account a, long value){ | |
130 | // if (a.getType().isCredit()) | |
131 | // return value <= 0; | |
132 | // else | |
133 | // return value < 0; | |
134 | // } | |
135 | // | |
136 | // public static boolean isRed(BudgetCategory c, long value){ | |
137 | // if (c.isIncome()) | |
138 | // return value < 0; | |
139 | // else | |
140 | // return value >= 0; | |
141 | // } | |
142 | // | |
143 | // public static boolean isRed(Type t){ | |
144 | // return t.isCredit(); | |
145 | // } | |
146 | // | |
147 | // public static boolean isRed(Type t, long value){ | |
148 | // if (t.isCredit()){ | |
149 | // return value <= 0; | |
150 | // } | |
151 | // else { | |
152 | // return value < 0; | |
153 | // } | |
154 | // } | |
155 | // | |
156 | // public static boolean isRed(Transaction t){ | |
157 | // return !t.isInflow(); | |
158 | // } | |
159 | // | |
160 | // public static boolean isRed(Transaction t, boolean toSelectedAccount){ | |
161 | // if (!toSelectedAccount && t.getAmount() >= 0 | |
162 | // || toSelectedAccount && t.getAmount() < 0) | |
163 | // return true; | |
164 | // else | |
165 | // return false; | |
166 | // } | |
167 | // | |
168 | // public static boolean isRed(long value){ | |
169 | // if (value < 0) | |
170 | // return true; | |
171 | // else | |
172 | // return false; | |
173 | // } | |
174 | ||
175 | public static Dimension getComponentSize(JComponent component, int minWidth){ | |
176 | 32929 | return new Dimension(Math.max(minWidth, component.getPreferredSize().width), Math.max(component.getPreferredSize().height, component.getSize().height)); |
177 | } | |
178 | ||
179 | public static Dimension getComboBoxSize(JComboBox comboBox){ | |
180 | 15125 | return getComponentSize(comboBox, 150); |
181 | } | |
182 | ||
183 | public static Dimension getButtonSize(JButton button){ | |
184 | 13389 | return getComponentSize(button, 100); |
185 | } | |
186 | ||
187 | public static boolean isRed(Source s){ | |
188 | 0 | if (s instanceof Account){ |
189 | 0 | return ((Account) s).getAccountType().isCredit(); |
190 | } | |
191 | 0 | else if (s instanceof BudgetCategory){ |
192 | 0 | return !((BudgetCategory) s).isIncome(); |
193 | } | |
194 | else | |
195 | 0 | return false; |
196 | } | |
197 | ||
198 | public static boolean isRed(Account a, long value){ | |
199 | 0 | return value < 0; |
200 | } | |
201 | ||
202 | public static boolean isRed(BudgetCategory c, long value){ | |
203 | 0 | if (c.isIncome()) |
204 | 0 | return value < 0; |
205 | else | |
206 | 0 | return value >= 0; |
207 | } | |
208 | ||
209 | public static boolean isRed(AccountType t){ | |
210 | 0 | return t.isCredit(); |
211 | } | |
212 | ||
213 | public static boolean isRed(AccountType t, long value){ | |
214 | 0 | return value < 0; |
215 | } | |
216 | ||
217 | public static boolean isRed(Transaction t){ | |
218 | 0 | return !t.isInflow(); |
219 | } | |
220 | ||
221 | public static boolean isRed(Transaction t, boolean toSelectedAccount){ | |
222 | 0 | if (!toSelectedAccount && t.getAmount() >= 0 |
223 | || toSelectedAccount && t.getAmount() < 0) | |
224 | 0 | return true; |
225 | else | |
226 | 0 | return false; |
227 | } | |
228 | ||
229 | public static boolean isRed(long value){ | |
230 | 0 | if (value < 0) |
231 | 0 | return true; |
232 | else | |
233 | 0 | return false; |
234 | } | |
235 | // WMO - Moved to Moss | |
236 | // public static String formatStringToMaxSize(String value, int maxPixelWidth, FontMetrics fm){ | |
237 | // int width; | |
238 | // for(width = 5; width <= value.length() && fm.stringWidth(value.substring(0, width)) < maxPixelWidth; width++); | |
239 | // return Formatter.getStringLengthFormat(width).format(value); | |
240 | // } | |
241 | // | |
242 | // public static String getHtmlWrapper(String htmlToWrap){ | |
243 | // return "<html>" + htmlToWrap + "</html>"; | |
244 | // } | |
245 | } |