My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright (c) 2008, Kevin Stembridge
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.moneydance.modules.features.forecaster.homepage;

import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;

import com.moneydance.apps.md.controller.FeatureModuleContext;
import com.moneydance.apps.md.model.Account;
import com.moneydance.apps.md.model.CurrencyType;
import com.moneydance.apps.md.model.RootAccount;
import com.moneydance.apps.md.view.HomePageView;
import com.moneydance.modules.features.forecaster.AbstractForecasterController;
import com.moneydance.modules.features.forecaster.DataSetManager;
import com.moneydance.modules.features.forecaster.FavouriteInfo;
import com.moneydance.modules.features.forecaster.FuturePeriod;
import com.moneydance.modules.features.forecaster.Main;
import com.moneydance.modules.features.forecaster.PreferencesKey;
import com.moneydance.modules.features.forecaster.accountmarker.AccountMarkers;
import com.moneydance.modules.features.forecaster.lang.NullArgumentException;
import com.moneydance.modules.features.forecaster.util.AccountHelper;


public class ForecasterHomePageController extends AbstractForecasterController implements HomePageView {


private static final String VIEW_ID = "com.moneydance.modules.features.forecaster.homepage";

private final Action settingsOkAction = new SettingsDialogOkAction();
private final Action settingsApplyAction = new SettingsDialogApplyAction();
private final Action settingsCancelAction = new SettingsDialogCancelAction();
private final Set<Account> selectedAccounts = new HashSet<Account>();
private ForecasterHomePage homePage;

private HomePageSettingsDialog settingsDialog;
private FuturePeriod futurePeriod = new FuturePeriod(3, FuturePeriod.Unit.MONTHS);
private boolean includeOverdueReminders = true;
private boolean markZeroBalance = true;
private boolean showAccountMarkers = true;
private boolean showTotal = true;


public ForecasterHomePageController(FeatureModuleContext featureModuleContext, AccountHelper accountHelper) {

super(featureModuleContext, new DataSetManager(), accountHelper);

}


public JComponent getGUIView(RootAccount rootAccount) {

if (this.homePage == null) {

Action showSettingsAction = new AbstractAction() {

private static final long serialVersionUID = 2569294241436545562L;

public void actionPerformed(ActionEvent e) {
onShowSettingsActionPerformed();
}

};

this.homePage = new ForecasterHomePage(getDatasetManager(), getBaseCurrencyType(), showSettingsAction);

FavouriteInfo lastVisibleForecast = getLastVisibleForecast(rootAccount);
loadFavouriteInfo(lastVisibleForecast);
loadSettings();

}

return this.homePage;

}


private FavouriteInfo getLastVisibleForecast(RootAccount rootAccount) {

String lastVisibleForecastString = rootAccount.getParameter(PreferencesKey.PREFS_KEY_LAST_VISIBLE_FORECAST_HOME_PAGE);

if (lastVisibleForecastString == null || "".equals(lastVisibleForecastString.trim())) {
return null;
}

try {
return FavouriteInfo.getInstance(lastVisibleForecastString);
} catch (RuntimeException e) {
rootAccount.removeParameter(PreferencesKey.PREFS_KEY_LAST_VISIBLE_FORECAST_HOME_PAGE);
return null;
}

}


private void onShowSettingsActionPerformed() {

this.settingsDialog = new HomePageSettingsDialog(this.settingsOkAction, this.settingsApplyAction, this.settingsCancelAction);

this.settingsDialog.setAvailableAccountNames(getAllAccounts());
this.settingsDialog.setSelectedAccountNames(this.selectedAccounts);
this.settingsDialog.setFuturePeriod(this.futurePeriod);
this.settingsDialog.setIncludeOverdueReminders(this.includeOverdueReminders);
this.settingsDialog.setMarkZeroBalance(this.markZeroBalance);
this.settingsDialog.setShowMarkers(this.showAccountMarkers);
this.settingsDialog.setShowTotal(this.showTotal);

this.settingsDialog.setVisible(true);

}


public String getID() {

return VIEW_ID;

}


public void refresh() {

generateForecast(this.selectedAccounts, this.futurePeriod, this.includeOverdueReminders, this.showTotal);

}


public void reset() {

//TODO remove
// System.out.println("BEGIN: reset()");

}


public void setActive(boolean active) {

if (active == false) {
detachListeners();
} else {
//Do nothing. Listeners will be reattached when the next forecast is generated.
}

}


@Override
public String toString() {

return Main.APP_TITLE;

}


@Override
protected void updateFutureDate(Calendar futureDate) {

if (this.homePage != null) {
this.homePage.updateChartDateRange(futureDate);
}

}


@Override
protected void updateDisplayCurrency(CurrencyType displayCurrencyType) {

if (this.homePage != null) {
this.homePage.updateDisplayCurrency(displayCurrencyType);
}

}


@Override
protected void onCloseForecaster(RootAccount rootAccount) {

saveSettings();

}


private void loadSettings() {

RootAccount rootAccount = getFeatureModuleContext().getRootAccount();

this.includeOverdueReminders = rootAccount.getBooleanParameter(PreferencesKey.PREFS_KEY_INCLUDE_OVERDUE_REMINDERS_HOME_PAGE, true);
this.markZeroBalance = rootAccount.getBooleanParameter(PreferencesKey.PREFS_KEY_MARK_ZERO_BALANCE_HOME_PAGE, true);
this.showAccountMarkers = rootAccount.getBooleanParameter(PreferencesKey.PREFS_KEY_SHOW_ACCOUNT_MARKERS_HOME_PAGE, true);
this.showTotal = rootAccount.getBooleanParameter(PreferencesKey.PREFS_KEY_SHOW_TOTAL_HOME_PAGE, true);
this.homePage.setZeroBalanceMarkerVisible(this.markZeroBalance);
this.homePage.setShowAccountMarkersVisible(this.showAccountMarkers);

}


private void saveSettings() {

RootAccount rootAccount = getFeatureModuleContext().getRootAccount();
Set<String> accountNames = AccountHelper.getAccountNames(this.selectedAccounts);
FavouriteInfo lastVisibleFavouriteInfo = new FavouriteInfo("last_visible_forecast_home_page",
accountNames,
this.futurePeriod);

String lastVisibleFavouriteMemento = lastVisibleFavouriteInfo.getMemento();

rootAccount.setParameterNoNotify(PreferencesKey.PREFS_KEY_LAST_VISIBLE_FORECAST_HOME_PAGE, lastVisibleFavouriteMemento);
rootAccount.setParameter(PreferencesKey.PREFS_KEY_INCLUDE_OVERDUE_REMINDERS_HOME_PAGE, this.includeOverdueReminders);
rootAccount.setParameter(PreferencesKey.PREFS_KEY_MARK_ZERO_BALANCE_HOME_PAGE, this.markZeroBalance);
rootAccount.setParameter(PreferencesKey.PREFS_KEY_SHOW_ACCOUNT_MARKERS_HOME_PAGE, this.showAccountMarkers);
rootAccount.setParameter(PreferencesKey.PREFS_KEY_SHOW_TOTAL_HOME_PAGE, this.showTotal);

}


@Override
protected void setSelectedAccounts(Set<Account> accountNames) {

this.selectedAccounts.clear();
this.selectedAccounts.addAll(accountNames);

if (this.settingsDialog != null) {
this.settingsDialog.setSelectedAccountNames(accountNames);
}

}


@Override
protected void setFuturePeriod(FuturePeriod futurePeriod) {

NullArgumentException.throwIfNull(futurePeriod, "futurePeriod");

this.futurePeriod = futurePeriod;

if (this.settingsDialog != null) {
this.settingsDialog.setFuturePeriod(futurePeriod);
}

}


@Override
protected boolean isIncludeOverdueReminders() {

return this.includeOverdueReminders;

}


@Override
protected boolean isMarkZeroBalance() {

return this.markZeroBalance;

}


@Override
protected boolean isShowAccountMarkers() {

return this.showAccountMarkers;

}


@Override
protected boolean isShowTotal() {

return this.showTotal;

}


private class SettingsDialogOkAction extends AbstractAction {

private static final long serialVersionUID = 6571616417101427136L;


SettingsDialogOkAction() {
super("OK");
}


public void actionPerformed(ActionEvent e) {
onSettingsOkActionPerformed();
}

}


private class SettingsDialogApplyAction extends AbstractAction {


private static final long serialVersionUID = 4208640628163028867L;


SettingsDialogApplyAction() {
super("Apply");
}


public void actionPerformed(ActionEvent e) {
onSettingsApplyActionPerformed();
}

}


private class SettingsDialogCancelAction extends AbstractAction {

private static final long serialVersionUID = -8940044325200468411L;


SettingsDialogCancelAction() {
super("Cancel");
}


public void actionPerformed(ActionEvent e) {
onSettingsCancelActionPerformed();
}

}


private void onSettingsApplyActionPerformed() {

updateSettings();
refresh();

}


private void onSettingsOkActionPerformed() {

updateSettings();
refresh();
destroySettingsDialog();

}


private void updateSettings() {

if (this.settingsDialog == null) {
return;
}

List<Account> selectedAccounts = this.settingsDialog.getSelectedAccounts();
this.selectedAccounts.clear();
this.selectedAccounts.addAll(selectedAccounts);

this.futurePeriod = this.settingsDialog.getSelectedFuturePeriod();
this.includeOverdueReminders = this.settingsDialog.isIncludeOverdueReminders();
this.markZeroBalance = this.settingsDialog.isMarkZeroBalance();
this.showAccountMarkers = this.settingsDialog.isShowMarkers();
this.showTotal = this.settingsDialog.isShowTotal();
this.homePage.setZeroBalanceMarkerVisible(this.markZeroBalance);
this.homePage.setShowAccountMarkersVisible(this.showAccountMarkers);

saveSettings();

}


private void onSettingsCancelActionPerformed() {

if (this.settingsDialog == null) {
return;
}

destroySettingsDialog();

}


private void destroySettingsDialog() {

this.settingsDialog.setVisible(false);
this.settingsDialog.dispose();
this.settingsDialog = null;

}


@Override
protected void resetAccountMarkers(List<AccountMarkers> markers) {

this.homePage.resetAccountMarkers(markers);

}


@Override
protected void updateMarkersForAccount(Account account, AccountMarkers markers) {

this.homePage.updateMarkersForAccount(account, markers);

}


}

Change log

r117 by kevin.stembridge.dev on Jan 9, 2012   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r116 by kevin.stembridge.dev on Jan 9, 2012   Diff
Added preference to show/hide balance
total (patch submitted by Mark Lasby)
r107 by kevin.stembridge.dev on Dec 20, 2011   Diff
Account markers
r106 by kevin.stembridge.dev on Dec 20, 2011   Diff
Account markers
All revisions of this file

File info

Size: 13221 bytes, 429 lines
Powered by Google Project Hosting