1 | |
|
2 | |
|
3 | |
|
4 | |
package org.homeunix.thecave.buddi.model.prefs; |
5 | |
|
6 | |
import java.awt.Dimension; |
7 | |
import java.awt.Point; |
8 | |
import java.beans.XMLDecoder; |
9 | |
import java.beans.XMLEncoder; |
10 | |
import java.io.ByteArrayOutputStream; |
11 | |
import java.io.File; |
12 | |
import java.io.FileInputStream; |
13 | |
import java.io.FileNotFoundException; |
14 | |
import java.io.FileOutputStream; |
15 | |
import java.util.HashMap; |
16 | |
import java.util.LinkedList; |
17 | |
import java.util.List; |
18 | |
import java.util.logging.Level; |
19 | |
import java.util.logging.Logger; |
20 | |
|
21 | |
import org.homeunix.thecave.buddi.Buddi; |
22 | |
import org.homeunix.thecave.buddi.Const; |
23 | |
import org.homeunix.thecave.buddi.i18n.BuddiTranslator; |
24 | |
import org.homeunix.thecave.buddi.util.FileFunctions; |
25 | |
|
26 | |
import ca.digitalcave.moss.common.OperatingSystemUtil; |
27 | |
import ca.digitalcave.moss.common.Version; |
28 | |
|
29 | 2986 | public class PrefsModel { |
30 | |
|
31 | |
public static PrefsModel getInstance() { |
32 | 3187772 | return SingletonHolder.instance; |
33 | |
} |
34 | |
|
35 | 3187772 | private static class SingletonHolder { |
36 | 2986 | private static PrefsModel instance = new PrefsModel(); |
37 | |
} |
38 | |
|
39 | |
private PrefsModelBean prefsModel; |
40 | |
private BuddiTranslator translate; |
41 | |
|
42 | |
|
43 | |
|
44 | 2986 | private static File prefsFile = OperatingSystemUtil.getUserFile(Const.PROJECT_NAME, Const.PREFERENCE_FILE_NAME); |
45 | |
|
46 | 2986 | private PrefsModel() { |
47 | |
try { |
48 | 2986 | XMLDecoder prefsDecoder = new XMLDecoder(new FileInputStream(prefsFile)); |
49 | 2986 | prefsModel = (PrefsModelBean) prefsDecoder.readObject(); |
50 | 2986 | if (prefsModel == null) |
51 | 0 | throw new Exception("Error loading preferences - creating new file."); |
52 | |
} |
53 | 0 | catch (RuntimeException re){ |
54 | 0 | newPrefsFile(); |
55 | |
} |
56 | 0 | catch (Exception e){ |
57 | 0 | newPrefsFile(); |
58 | 2986 | } |
59 | 2986 | } |
60 | |
|
61 | |
private void newPrefsFile(){ |
62 | |
|
63 | 0 | prefsModel = new PrefsModelBean(); |
64 | 0 | prefsModel.setCurrencySign("$"); |
65 | 0 | prefsModel.setLanguage("English"); |
66 | 0 | prefsModel.setNumberOfBackups(10); |
67 | 0 | prefsModel.setShowAutoComplete(true); |
68 | 0 | prefsModel.setShowDeleted(true); |
69 | 0 | prefsModel.setSendCrashReports(true); |
70 | 0 | prefsModel.setShowUpdateNotifications(true); |
71 | 0 | prefsModel.setShowTooltips(true); |
72 | |
|
73 | |
|
74 | |
|
75 | 0 | save(); |
76 | 0 | } |
77 | |
|
78 | |
public void save() { |
79 | 226 | if (prefsFile != null){ |
80 | |
try { |
81 | 226 | if (!prefsFile.getParentFile().exists()) |
82 | 0 | prefsFile.getParentFile().mkdirs(); |
83 | 226 | XMLEncoder encoder = new XMLEncoder(new FileOutputStream(prefsFile)); |
84 | 226 | encoder.writeObject(prefsModel); |
85 | 226 | encoder.flush(); |
86 | 226 | encoder.close(); |
87 | |
} |
88 | 0 | catch (FileNotFoundException fnfe){ |
89 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Problem saving preferences file: ", fnfe); |
90 | 226 | } |
91 | |
} |
92 | 226 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public String saveToString(){ |
100 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
101 | 0 | XMLEncoder encoder = new XMLEncoder(baos); |
102 | 0 | encoder.writeObject(prefsModel); |
103 | 0 | encoder.flush(); |
104 | 0 | encoder.close(); |
105 | |
|
106 | 0 | return baos.toString(); |
107 | |
} |
108 | |
|
109 | |
public BuddiTranslator getTranslator(){ |
110 | 2495526 | if (translate == null) |
111 | 2986 | translate = new BuddiTranslator(); |
112 | 2495526 | return translate; |
113 | |
} |
114 | |
|
115 | |
public int getAutosaveDelay() { |
116 | 695 | return prefsModel.getAutosaveDelay(); |
117 | |
} |
118 | |
|
119 | |
public void setAutosaveDelay(int autosaveDelay) { |
120 | 43 | prefsModel.setAutosaveDelay(autosaveDelay); |
121 | 43 | } |
122 | |
|
123 | |
public boolean isShowFlatAccounts() { |
124 | 16273 | return prefsModel.isShowFlatAccounts(); |
125 | |
} |
126 | |
public void setShowFlatAccounts(boolean showFlatAccounts) { |
127 | 43 | prefsModel.setShowFlatAccounts(showFlatAccounts); |
128 | 43 | } |
129 | |
public boolean isShowFlatBudget() { |
130 | 57621 | return prefsModel.isShowFlatBudget(); |
131 | |
} |
132 | |
public void setShowFlatBudget(boolean showFlatBudget) { |
133 | 43 | prefsModel.setShowFlatBudget(showFlatBudget); |
134 | 43 | } |
135 | |
public boolean isShowFlatBudgetInSourceCombobox() { |
136 | 27068 | return prefsModel.isShowFlatBudgetInSourceCombobox(); |
137 | |
} |
138 | |
public void setShowFlatBudgetInSourceCombobox(boolean showFlatBudgetInSourceCombobox) { |
139 | 43 | prefsModel.setShowFlatBudgetInSourceCombobox(showFlatBudgetInSourceCombobox); |
140 | 43 | } |
141 | |
|
142 | |
public Version getLastVersion(){ |
143 | 5954 | if (prefsModel.getLastVersion() == null) |
144 | 0 | return null; |
145 | 5954 | return new Version(prefsModel.getLastVersion()); |
146 | |
} |
147 | |
|
148 | |
public void updateVersion(){ |
149 | 0 | prefsModel.setLastVersion(Buddi.getVersion().toString()); |
150 | 0 | } |
151 | |
|
152 | |
public String getCurrencySign() { |
153 | 29980 | return prefsModel.getCurrencySign(); |
154 | |
} |
155 | |
|
156 | |
public void setCurrencySign(String currencySign) { |
157 | 43 | if (currencySign != null && currencySign.length() > 0) |
158 | 43 | prefsModel.setCurrencySign(currencySign); |
159 | 43 | } |
160 | |
|
161 | |
public String getDateFormat() { |
162 | 9793 | if (prefsModel.getDateFormat() == null || prefsModel.getDateFormat().length() == 0) |
163 | 0 | prefsModel.setDateFormat("yyyy/MM/dd"); |
164 | 9793 | return prefsModel.getDateFormat(); |
165 | |
} |
166 | |
|
167 | |
public void setDateFormat(String dateFormat) { |
168 | 43 | if (dateFormat != null && dateFormat.length() > 0) |
169 | 43 | prefsModel.setDateFormat(dateFormat); |
170 | 43 | } |
171 | |
|
172 | |
public String getLanguage() { |
173 | 6873 | return (prefsModel.getLanguage() != null && prefsModel.getLanguage().length() > 0 |
174 | |
? prefsModel.getLanguage() |
175 | |
: "English"); |
176 | |
} |
177 | |
|
178 | |
public void setLanguage(String language) { |
179 | 43 | if (language != null && language.length() > 0) |
180 | 43 | prefsModel.setLanguage(language); |
181 | 43 | } |
182 | |
|
183 | |
public List<File> getLastDataFiles() { |
184 | 2986 | if (prefsModel.getLastDataFiles() == null || prefsModel.getLastDataFiles().size() == 0) |
185 | 2986 | return null; |
186 | 0 | List<File> files = new LinkedList<File>(); |
187 | 0 | for (String s : prefsModel.getLastDataFiles()) { |
188 | 0 | files.add(new File(s)); |
189 | |
} |
190 | 0 | return files; |
191 | |
} |
192 | |
|
193 | |
public void setLastDataFiles(List<File> lastDataFiles) { |
194 | 22 | if (lastDataFiles != null){ |
195 | 22 | List<String> strings = new LinkedList<String>(); |
196 | 22 | for (File file : lastDataFiles) { |
197 | 22 | strings.add(file + ""); |
198 | |
} |
199 | 22 | prefsModel.setLastDataFiles(strings); |
200 | |
} |
201 | 22 | } |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public int getNumberOfBackups() { |
332 | 695 | return prefsModel.getNumberOfBackups(); |
333 | |
} |
334 | |
|
335 | |
public void setNumberOfBackups(int numberOfBackups) { |
336 | 43 | prefsModel.setNumberOfBackups(numberOfBackups); |
337 | 43 | } |
338 | |
|
339 | |
public String getProxyServer() { |
340 | 695 | return (prefsModel.getProxyServer() != null ? prefsModel.getProxyServer() : ""); |
341 | |
} |
342 | |
|
343 | |
public void setProxyServer(String proxyServer) { |
344 | 43 | if (proxyServer != null) |
345 | 43 | prefsModel.setProxyServer(proxyServer); |
346 | 43 | } |
347 | |
|
348 | |
public int getPort() { |
349 | 695 | return prefsModel.getPort(); |
350 | |
} |
351 | |
|
352 | |
public void setPort(int port) { |
353 | 43 | prefsModel.setPort(port); |
354 | 43 | } |
355 | |
|
356 | |
|
357 | |
public boolean isShowAutoComplete() { |
358 | 1860 | return prefsModel.isShowAutoComplete(); |
359 | |
} |
360 | |
|
361 | |
public void setShowAutoComplete(boolean showAutoComplete) { |
362 | 43 | prefsModel.setShowAutoComplete(showAutoComplete); |
363 | 43 | } |
364 | |
|
365 | |
public boolean isShowCleared() { |
366 | 5335 | return prefsModel.isShowCleared(); |
367 | |
} |
368 | |
|
369 | |
public void setShowCleared(boolean showCleared) { |
370 | 43 | prefsModel.setShowCleared(showCleared); |
371 | 43 | } |
372 | |
|
373 | |
public boolean isShowCurrencyAfterAmount() { |
374 | 29285 | return prefsModel.isShowCurrencyAfterAmount(); |
375 | |
} |
376 | |
|
377 | |
public void setShowCurrencyAfterAmount(boolean showCurrencyAfterAmount) { |
378 | 43 | prefsModel.setShowCurrencyAfterAmount(showCurrencyAfterAmount); |
379 | 43 | } |
380 | |
|
381 | |
public boolean isShowDeleted() { |
382 | 170585 | return prefsModel.isShowDeleted(); |
383 | |
} |
384 | |
|
385 | |
public void setShowDeleted(boolean showDeleted) { |
386 | 43 | prefsModel.setShowDeleted(showDeleted); |
387 | 43 | } |
388 | |
|
389 | |
public boolean isShowPromptAtStartup() { |
390 | 3681 | return prefsModel.isShowPromptAtStartup(); |
391 | |
} |
392 | |
|
393 | |
public void setShowPromptAtStartup(boolean showPromptAtStartup) { |
394 | 43 | prefsModel.setShowPromptAtStartup(showPromptAtStartup); |
395 | 43 | } |
396 | |
|
397 | |
public boolean isShowProxySettings() { |
398 | 713 | return prefsModel.isShowProxySettings(); |
399 | |
} |
400 | |
|
401 | |
public void setShowProxySettings(boolean showProxySettings) { |
402 | 43 | prefsModel.setShowProxySettings(showProxySettings); |
403 | 43 | } |
404 | |
|
405 | |
public boolean isShowReconciled() { |
406 | 5335 | return prefsModel.isShowReconciled(); |
407 | |
} |
408 | |
|
409 | |
public void setShowReconciled(boolean showReconciled) { |
410 | 43 | prefsModel.setShowReconciled(showReconciled); |
411 | 43 | } |
412 | |
|
413 | |
public boolean isShowUpdateNotifications() { |
414 | 6658 | return prefsModel.isShowUpdateNotifications(); |
415 | |
} |
416 | |
|
417 | |
public void setShowUpdateNotifications(boolean showUpdateNotifications) { |
418 | 43 | prefsModel.setShowUpdateNotifications(showUpdateNotifications); |
419 | 43 | } |
420 | |
|
421 | |
public int getNumberOfBudgetColumns() { |
422 | 197102 | return prefsModel.getNumberOfBudgetColumns(); |
423 | |
} |
424 | |
|
425 | |
public void setNumberOfBudgetColumns(int numberOfBudgetColumns) { |
426 | 0 | prefsModel.setNumberOfBudgetColumns(numberOfBudgetColumns); |
427 | 0 | } |
428 | |
|
429 | |
public static void setPrefsFile(File file){ |
430 | 0 | if (file != null && FileFunctions.isFolderWritable(file)){ |
431 | 0 | PrefsModel.prefsFile = file; |
432 | |
} |
433 | 0 | } |
434 | |
|
435 | |
public boolean isSendCrashReports() { |
436 | 0 | return prefsModel.isSendCrashReports(); |
437 | |
} |
438 | |
|
439 | |
public void setSendCrashReports(boolean sendCrashReports) { |
440 | 0 | prefsModel.setSendCrashReports(sendCrashReports); |
441 | 0 | } |
442 | |
|
443 | |
public String getPluginPreference(String key){ |
444 | 0 | return prefsModel.getPluginPreferences().get(key); |
445 | |
} |
446 | |
|
447 | |
public void putPluginPreference(String key, String value){ |
448 | 0 | prefsModel.getPluginPreferences().put(key, value); |
449 | 0 | } |
450 | |
|
451 | |
public List<String> getPluginListPreference(String key){ |
452 | 0 | if (prefsModel.getPluginListPreferences() == null) |
453 | 0 | prefsModel.setPluginListPreferences(new HashMap<String, List<String>>()); |
454 | 0 | return prefsModel.getPluginListPreferences().get(key); |
455 | |
} |
456 | |
|
457 | |
public void putPluginListPreference(String key, List<String> value){ |
458 | 0 | if (prefsModel.getPluginListPreferences() == null) |
459 | 0 | prefsModel.setPluginListPreferences(new HashMap<String, List<String>>()); |
460 | 0 | prefsModel.getPluginListPreferences().put(key, value); |
461 | 0 | } |
462 | |
|
463 | |
public boolean isDontShowNegativeSign() { |
464 | 29242 | return prefsModel.isShowNegativeSign(); |
465 | |
} |
466 | |
|
467 | |
public void setShowNegativeSign(boolean showNegativeSign) { |
468 | 43 | prefsModel.setShowNegativeSign(showNegativeSign); |
469 | 43 | } |
470 | |
|
471 | |
public boolean isShowCreditRemaining() { |
472 | 23115 | return prefsModel.isShowCreditRemaining(); |
473 | |
} |
474 | |
|
475 | |
public void setShowCreditRemaining(boolean showCreditRemaining) { |
476 | 43 | prefsModel.setShowCreditRemaining(showCreditRemaining); |
477 | 43 | } |
478 | |
|
479 | |
public boolean isShowOverdraft() { |
480 | 23131 | return prefsModel.isShowOverdraft(); |
481 | |
} |
482 | |
|
483 | |
public void setShowOverdraft(boolean showOverdraft) { |
484 | 43 | prefsModel.setShowOverdraft(showOverdraft); |
485 | 43 | } |
486 | |
|
487 | |
public boolean isShowTooltips() { |
488 | 3025 | return prefsModel.isShowTooltips(); |
489 | |
} |
490 | |
|
491 | |
public void setShowTooltips(boolean showTooltips) { |
492 | 43 | prefsModel.setShowTooltips(showTooltips); |
493 | 43 | } |
494 | |
|
495 | |
|
496 | |
|
497 | |
|
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
public void putWindowLocation(String uid, Point location){ |
506 | 161 | prefsModel.getWindowLocation().put(uid, location); |
507 | 161 | } |
508 | |
|
509 | |
|
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
public Point getWindowLocation(String uid){ |
519 | 5565 | Point p = prefsModel.getWindowLocation().get(uid); |
520 | 5565 | if (p != null) |
521 | 3720 | return p; |
522 | 1845 | return new Point(100, 100); |
523 | |
} |
524 | |
|
525 | |
|
526 | |
|
527 | |
|
528 | |
|
529 | |
|
530 | |
|
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
public void putWindowSize(String uid, Dimension size){ |
536 | 0 | prefsModel.getWindowSize().put(uid, size); |
537 | 0 | } |
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
|
548 | |
public Dimension getWindowSize(String uid){ |
549 | 3025 | Dimension d = prefsModel.getWindowSize().get(uid); |
550 | 3025 | if (d != null) |
551 | 3025 | return d; |
552 | 0 | return new Dimension(640, 480); |
553 | |
} |
554 | |
|
555 | |
public boolean isSearchPaneVisible() { |
556 | 0 | return prefsModel.isSearchPaneVisible(); |
557 | |
} |
558 | |
|
559 | |
public void setSearchPaneVisible(boolean searchPaneVisible) { |
560 | 0 | prefsModel.setSearchPaneVisible(searchPaneVisible); |
561 | 0 | } |
562 | |
|
563 | |
public boolean isTotalPaneVisible() { |
564 | 0 | return prefsModel.isTotalPaneVisible(); |
565 | |
} |
566 | |
|
567 | |
public void setTotalPaneVisible(boolean totalPaneVisible) { |
568 | 0 | prefsModel.setTotalPaneVisible(totalPaneVisible); |
569 | 0 | } |
570 | |
|
571 | |
public String getTransactionCellRenderer() { |
572 | 695 | return prefsModel.getTransactionCellRenderer(); |
573 | |
} |
574 | |
|
575 | |
public void setTransactionCellRenderer(String transactionCellRenderer) { |
576 | 43 | prefsModel.setTransactionCellRenderer(transactionCellRenderer); |
577 | 43 | } |
578 | |
|
579 | |
public String getClearedFilter() { |
580 | 0 | return prefsModel.getClearedFilter(); |
581 | |
} |
582 | |
|
583 | |
public void setClearedFilter(String clearedFilter) { |
584 | 0 | prefsModel.setClearedFilter(clearedFilter); |
585 | 0 | } |
586 | |
|
587 | |
public String getDateFilter() { |
588 | 0 | return prefsModel.getDateFilter(); |
589 | |
} |
590 | |
|
591 | |
public void setDateFilter(String dateFilter) { |
592 | 0 | prefsModel.setDateFilter(dateFilter); |
593 | 0 | } |
594 | |
|
595 | |
public String getReconciledFilter() { |
596 | 0 | return prefsModel.getReconciledFilter(); |
597 | |
} |
598 | |
|
599 | |
public void setReconciledFilter(String reconciledFilter) { |
600 | 0 | prefsModel.setReconciledFilter(reconciledFilter); |
601 | 0 | } |
602 | |
|
603 | |
public String getSearchText() { |
604 | 0 | return prefsModel.getSearchText(); |
605 | |
} |
606 | |
|
607 | |
public void setSearchText(String searchText) { |
608 | 0 | prefsModel.setSearchText(searchText); |
609 | 0 | } |
610 | |
|
611 | |
public int getLastDeleteOption() { |
612 | 0 | return prefsModel.getLastDeleteOption(); |
613 | |
} |
614 | |
public void setLastDeleteOption(int lastDeleteOption) { |
615 | 0 | prefsModel.setLastDeleteOption(lastDeleteOption); |
616 | 0 | } |
617 | |
|
618 | |
public String getAvailableVersion() { |
619 | 0 | return prefsModel.getAvailableVersion(); |
620 | |
} |
621 | |
public void setAvailableVersion(String availableVersion) { |
622 | 0 | prefsModel.setAvailableVersion(availableVersion); |
623 | 0 | } |
624 | |
|
625 | |
public boolean isShowInterestRates(){ |
626 | 23125 | return prefsModel.isShowInterestRates(); |
627 | |
} |
628 | |
|
629 | |
public void setShowInterestRates(boolean showInterestRates){ |
630 | 43 | prefsModel.setShowInterestRates(showInterestRates); |
631 | 43 | } |
632 | |
} |
633 | |
|