1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.moneywise.lethe.ui.controls;
18
19 import io.github.tonywasher.joceanus.oceanus.date.OceanusDateRange;
20 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
21 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
22 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
23 import io.github.tonywasher.joceanus.metis.data.MetisDataDifference;
24 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
25 import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysis;
26 import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisPortfolioBucket;
27 import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisPortfolioBucket.MoneyWiseAnalysisPortfolioBucketList;
28 import io.github.tonywasher.joceanus.moneywise.lethe.views.MoneyWiseAnalysisFilter;
29 import io.github.tonywasher.joceanus.moneywise.lethe.views.MoneyWiseAnalysisFilter.MoneyWiseAnalysisPortfolioCashFilter;
30 import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataEvent;
31 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
32 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIConstant;
33 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
34 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIScrollButtonManager;
35 import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
36 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
37 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollItem;
38 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
39 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
40
41 import java.util.Iterator;
42
43
44
45
46 public class MoneyWisePortfolioAnalysisSelect
47 implements MoneyWiseAnalysisFilterSelection, OceanusEventProvider<PrometheusDataEvent> {
48
49
50
51 private static final String NLS_PORTFOLIO = MoneyWiseBasicDataType.PORTFOLIO.getItemName();
52
53
54
55
56 private final OceanusEventManager<PrometheusDataEvent> theEventManager;
57
58
59
60
61 private final TethysUIBoxPaneManager thePanel;
62
63
64
65
66 private final TethysUIScrollButtonManager<MoneyWiseAnalysisPortfolioBucket> thePortButton;
67
68
69
70
71 private final TethysUIScrollMenu<MoneyWiseAnalysisPortfolioBucket> thePortfolioMenu;
72
73
74
75
76 private MoneyWiseAnalysisPortfolioBucketList thePortfolios;
77
78
79
80
81 private MoneyWisePortfolioState theState;
82
83
84
85
86 private MoneyWisePortfolioState theSavePoint;
87
88
89
90
91
92
93 protected MoneyWisePortfolioAnalysisSelect(final TethysUIFactory<?> pFactory) {
94
95 thePortButton = pFactory.buttonFactory().newScrollButton(MoneyWiseAnalysisPortfolioBucket.class);
96
97
98 theEventManager = new OceanusEventManager<>();
99
100
101 final TethysUILabel myPortLabel = pFactory.controlFactory().newLabel(NLS_PORTFOLIO + TethysUIConstant.STR_COLON);
102
103
104 thePanel = pFactory.paneFactory().newHBoxPane();
105 thePanel.addSpacer();
106 thePanel.addNode(myPortLabel);
107 thePanel.addNode(thePortButton);
108
109
110 theState = new MoneyWisePortfolioState();
111 theState.applyState();
112
113
114 thePortfolioMenu = thePortButton.getMenu();
115
116
117 final OceanusEventRegistrar<TethysUIEvent> myRegistrar = thePortButton.getEventRegistrar();
118 myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handleNewPortfolio());
119 thePortButton.setMenuConfigurator(e -> buildPortfolioMenu());
120 }
121
122 @Override
123 public TethysUIComponent getUnderlying() {
124 return thePanel;
125 }
126
127 @Override
128 public OceanusEventRegistrar<PrometheusDataEvent> getEventRegistrar() {
129 return theEventManager.getEventRegistrar();
130 }
131
132 @Override
133 public MoneyWiseAnalysisPortfolioCashFilter getFilter() {
134 return theState.getFilter();
135 }
136
137 @Override
138 public boolean isAvailable() {
139 return thePortfolios != null
140 && !thePortfolios.isEmpty();
141 }
142
143
144
145
146 public void createSavePoint() {
147
148 theSavePoint = new MoneyWisePortfolioState(theState);
149 }
150
151
152
153
154 public void restoreSavePoint() {
155
156 theState = new MoneyWisePortfolioState(theSavePoint);
157
158
159 theState.applyState();
160 }
161
162 @Override
163 public void setEnabled(final boolean bEnabled) {
164
165 final boolean portAvailable = bEnabled && isAvailable();
166
167
168 thePortButton.setEnabled(portAvailable);
169 }
170
171 @Override
172 public void setVisible(final boolean pVisible) {
173 thePanel.setVisible(pVisible);
174 }
175
176
177
178
179
180
181 public void setAnalysis(final MoneyWiseAnalysis pAnalysis) {
182
183 thePortfolios = pAnalysis.getPortfolios();
184
185
186 MoneyWiseAnalysisPortfolioBucket myPortfolio = theState.getPortfolio();
187
188
189 myPortfolio = myPortfolio != null
190 ? thePortfolios.getMatchingPortfolio(myPortfolio.getPortfolio())
191 : thePortfolios.getDefaultPortfolio();
192
193
194 theState.setThePortfolio(myPortfolio);
195 theState.setDateRange(pAnalysis.getDateRange());
196 theState.applyState();
197 }
198
199 @Override
200 public void setFilter(final MoneyWiseAnalysisFilter<?, ?> pFilter) {
201
202 if (pFilter instanceof MoneyWiseAnalysisPortfolioCashFilter) {
203
204 final MoneyWiseAnalysisPortfolioCashFilter myFilter = (MoneyWiseAnalysisPortfolioCashFilter) pFilter;
205
206
207 MoneyWiseAnalysisPortfolioBucket myPortfolio = myFilter.getPortfolioBucket();
208
209
210 myPortfolio = thePortfolios.getMatchingPortfolio(myPortfolio.getPortfolio());
211
212
213 theState.setThePortfolio(myPortfolio);
214 theState.setDateRange(myFilter.getDateRange());
215 theState.applyState();
216 }
217 }
218
219
220
221
222 private void handleNewPortfolio() {
223
224 if (theState.setPortfolio(thePortButton.getValue())) {
225 theState.applyState();
226 theEventManager.fireEvent(PrometheusDataEvent.SELECTIONCHANGED);
227 }
228 }
229
230
231
232
233 private void buildPortfolioMenu() {
234
235 thePortfolioMenu.removeAllItems();
236
237
238 TethysUIScrollItem<MoneyWiseAnalysisPortfolioBucket> myActive = null;
239 final MoneyWiseAnalysisPortfolioBucket myCurr = theState.getPortfolio();
240
241
242 final Iterator<MoneyWiseAnalysisPortfolioBucket> myIterator = thePortfolios.iterator();
243 while (myIterator.hasNext()) {
244 final MoneyWiseAnalysisPortfolioBucket myBucket = myIterator.next();
245
246
247 final TethysUIScrollItem<MoneyWiseAnalysisPortfolioBucket> myItem = thePortfolioMenu.addItem(myBucket);
248
249
250 if (myBucket.equals(myCurr)) {
251
252 myActive = myItem;
253 }
254 }
255
256
257 if (myActive != null) {
258 myActive.scrollToItem();
259 }
260 }
261
262
263
264
265 private final class MoneyWisePortfolioState {
266
267
268
269 private MoneyWiseAnalysisPortfolioBucket thePortfolio;
270
271
272
273
274 private OceanusDateRange theDateRange;
275
276
277
278
279 private MoneyWiseAnalysisPortfolioCashFilter theFilter;
280
281
282
283
284 private MoneyWisePortfolioState() {
285 }
286
287
288
289
290
291
292 private MoneyWisePortfolioState(final MoneyWisePortfolioState pState) {
293
294 thePortfolio = pState.getPortfolio();
295 theDateRange = pState.getDateRange();
296 theFilter = pState.getFilter();
297 }
298
299
300
301
302
303
304 private MoneyWiseAnalysisPortfolioBucket getPortfolio() {
305 return thePortfolio;
306 }
307
308
309
310
311
312
313 private OceanusDateRange getDateRange() {
314 return theDateRange;
315 }
316
317
318
319
320
321
322 private MoneyWiseAnalysisPortfolioCashFilter getFilter() {
323 return theFilter;
324 }
325
326
327
328
329
330
331
332 private boolean setPortfolio(final MoneyWiseAnalysisPortfolioBucket pPortfolio) {
333
334 if (!MetisDataDifference.isEqual(pPortfolio, thePortfolio)) {
335 setThePortfolio(pPortfolio);
336 return true;
337 }
338 return false;
339 }
340
341
342
343
344
345
346 private void setThePortfolio(final MoneyWiseAnalysisPortfolioBucket pPortfolio) {
347
348 thePortfolio = pPortfolio;
349 if (thePortfolio != null) {
350 theFilter = new MoneyWiseAnalysisPortfolioCashFilter(thePortfolio);
351 theFilter.setDateRange(theDateRange);
352 } else {
353 theFilter = null;
354 }
355 }
356
357
358
359
360
361
362 private void setDateRange(final OceanusDateRange pRange) {
363
364 theDateRange = pRange;
365 if (theFilter != null) {
366 theFilter.setDateRange(theDateRange);
367 }
368 }
369
370
371
372
373 private void applyState() {
374
375 setEnabled(true);
376 thePortButton.setValue(thePortfolio);
377 }
378 }
379 }