1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.moneywise.ui.controls;
18
19 import io.github.tonywasher.joceanus.oceanus.date.OceanusDate;
20 import io.github.tonywasher.joceanus.oceanus.date.OceanusDateRange;
21 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
22 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
23 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
24 import io.github.tonywasher.joceanus.metis.data.MetisDataDifference;
25 import io.github.tonywasher.joceanus.metis.ui.MetisIcon;
26 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
27 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePortfolio;
28 import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysis;
29 import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisManager;
30 import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisPortfolioBucket;
31 import io.github.tonywasher.joceanus.moneywise.lethe.data.analysis.data.MoneyWiseAnalysisPortfolioBucket.MoneyWiseAnalysisPortfolioBucketList;
32 import io.github.tonywasher.joceanus.moneywise.ui.MoneyWiseUIResource;
33 import io.github.tonywasher.joceanus.moneywise.views.MoneyWiseView;
34 import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataEvent;
35 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIArrowIconId;
36 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
37 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIConstant;
38 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
39 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButton;
40 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButtonFactory;
41 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIDateButtonManager;
42 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIScrollButtonManager;
43 import io.github.tonywasher.joceanus.tethys.api.control.TethysUICheckBox;
44 import io.github.tonywasher.joceanus.tethys.api.control.TethysUIControlFactory;
45 import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
46 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
47 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollItem;
48 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
49 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
50
51 import java.util.Iterator;
52
53
54
55
56 public class MoneyWiseSpotPricesSelect
57 implements OceanusEventProvider<PrometheusDataEvent>, TethysUIComponent {
58
59
60
61 private static final String NLS_DATE = MoneyWiseUIResource.SPOTEVENT_DATE.getValue();
62
63
64
65
66 private static final String NLS_PORT = MoneyWiseBasicDataType.PORTFOLIO.getItemName() + TethysUIConstant.STR_COLON;
67
68
69
70
71 private static final String NLS_CLOSED = MoneyWiseUIResource.UI_PROMPT_SHOWCLOSED.getValue();
72
73
74
75
76 private static final String NLS_TITLE = MoneyWiseUIResource.SPOTPRICE_TITLE.getValue();
77
78
79
80
81 private static final String NLS_NEXTTIP = MoneyWiseUIResource.SPOTPRICE_NEXT.getValue();
82
83
84
85
86 private static final String NLS_PREVTIP = MoneyWiseUIResource.SPOTPRICE_PREV.getValue();
87
88
89
90
91 private final OceanusEventManager<PrometheusDataEvent> theEventManager;
92
93
94
95
96 private final TethysUIBoxPaneManager thePanel;
97
98
99
100
101 private final MoneyWiseView theView;
102
103
104
105
106 private final TethysUIDateButtonManager theDateButton;
107
108
109
110
111 private final TethysUICheckBox theShowClosed;
112
113
114
115
116 private final TethysUIButton theNext;
117
118
119
120
121 private final TethysUIButton thePrev;
122
123
124
125
126 private final TethysUIButton theDownloadButton;
127
128
129
130
131 private final TethysUIScrollButtonManager<MoneyWiseAnalysisPortfolioBucket> thePortButton;
132
133
134
135
136 private final TethysUIScrollMenu<MoneyWiseAnalysisPortfolioBucket> thePortMenu;
137
138
139
140
141 private MoneyWiseAnalysisPortfolioBucketList thePortfolios;
142
143
144
145
146 private MoneyWiseSpotPricesState theState;
147
148
149
150
151 private MoneyWiseSpotPricesState theSavePoint;
152
153
154
155
156 private boolean refreshingData;
157
158
159
160
161
162
163
164 public MoneyWiseSpotPricesSelect(final TethysUIFactory<?> pFactory,
165 final MoneyWiseView pView) {
166
167 theView = pView;
168
169
170 theEventManager = new OceanusEventManager<>();
171
172
173 final TethysUIControlFactory myControls = pFactory.controlFactory();
174 final TethysUILabel myDate = myControls.newLabel(NLS_DATE);
175 final TethysUILabel myPort = myControls.newLabel(NLS_PORT);
176
177
178 theShowClosed = myControls.newCheckBox(NLS_CLOSED);
179
180
181 final TethysUIButtonFactory<?> myButtons = pFactory.buttonFactory();
182 theDateButton = myButtons.newDateButton();
183
184
185 theDownloadButton = myButtons.newButton();
186 MetisIcon.configureDownloadIconButton(theDownloadButton);
187
188
189 theNext = myButtons.newButton();
190 theNext.setIcon(TethysUIArrowIconId.RIGHT);
191 theNext.setToolTip(NLS_NEXTTIP);
192 thePrev = myButtons.newButton();
193 thePrev.setIcon(TethysUIArrowIconId.LEFT);
194 thePrev.setToolTip(NLS_PREVTIP);
195
196
197 thePortButton = myButtons.newScrollButton(MoneyWiseAnalysisPortfolioBucket.class);
198
199
200 theState = new MoneyWiseSpotPricesState();
201
202
203 thePanel = pFactory.paneFactory().newHBoxPane();
204 thePanel.setBorderTitle(NLS_TITLE);
205
206
207 thePanel.addNode(myDate);
208 thePanel.addNode(thePrev);
209 thePanel.addNode(theDateButton);
210 thePanel.addNode(theNext);
211 thePanel.addSpacer();
212 thePanel.addNode(myPort);
213 thePanel.addNode(thePortButton);
214 thePanel.addSpacer();
215 thePanel.addNode(theShowClosed);
216 thePanel.addSpacer();
217 thePanel.addNode(theDownloadButton);
218
219
220 refreshData();
221
222
223 theState.applyState();
224
225
226 thePortMenu = thePortButton.getMenu();
227
228
229 final OceanusEventRegistrar<TethysUIEvent> myRegistrar = thePortButton.getEventRegistrar();
230 myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handleNewPortfolio());
231 thePortButton.setMenuConfigurator(e -> buildPortfolioMenu());
232 theDownloadButton.getEventRegistrar().addEventListener(TethysUIEvent.PRESSED, e -> theEventManager.fireEvent(PrometheusDataEvent.DOWNLOAD));
233 theDateButton.getEventRegistrar().addEventListener(TethysUIEvent.NEWVALUE, e -> handleNewDate());
234 theShowClosed.getEventRegistrar().addEventListener(e -> handleNewClosed());
235 theNext.getEventRegistrar().addEventListener(e -> {
236 theState.setNext();
237 theEventManager.fireEvent(PrometheusDataEvent.SELECTIONCHANGED);
238 });
239 thePrev.getEventRegistrar().addEventListener(e -> {
240 theState.setPrev();
241 theEventManager.fireEvent(PrometheusDataEvent.SELECTIONCHANGED);
242 });
243 }
244
245 @Override
246 public TethysUIComponent getUnderlying() {
247 return thePanel;
248 }
249
250 @Override
251 public OceanusEventRegistrar<PrometheusDataEvent> getEventRegistrar() {
252 return theEventManager.getEventRegistrar();
253 }
254
255
256
257
258
259
260 public OceanusDate getDate() {
261 return theState.getDate();
262 }
263
264
265
266
267
268
269 public final MoneyWisePortfolio getPortfolio() {
270 final MoneyWiseAnalysisPortfolioBucket myBucket = theState.getPortfolio();
271 return myBucket == null
272 ? null
273 : myBucket.getPortfolio();
274 }
275
276
277
278
279
280
281 public boolean getShowClosed() {
282 return theState.showClosed();
283 }
284
285
286
287
288 public final void refreshData() {
289
290 final OceanusDateRange myRange = theView.getRange();
291
292
293 setRange(myRange);
294
295
296 final MoneyWiseAnalysisManager myManager = theView.getAnalysisManager();
297 final MoneyWiseAnalysis myAnalysis = myManager.getAnalysis();
298 thePortfolios = myAnalysis.getPortfolios();
299
300
301 refreshingData = true;
302
303
304 MoneyWiseAnalysisPortfolioBucket myPortfolio = theState.getPortfolio();
305
306
307 myPortfolio = myPortfolio != null
308 ? thePortfolios.getMatchingPortfolio(myPortfolio.getPortfolio())
309 : thePortfolios.getDefaultPortfolio();
310
311
312 theState.setPortfolio(myPortfolio);
313 theState.applyState();
314
315
316 refreshingData = false;
317 }
318
319
320
321
322
323
324 public final void setRange(final OceanusDateRange pRange) {
325 final OceanusDate myStart = (pRange == null)
326 ? null
327 : pRange.getStart();
328 final OceanusDate myEnd = (pRange == null)
329 ? null
330 : pRange.getEnd();
331
332
333 theDateButton.setEarliestDate(myStart);
334 theDateButton.setLatestDate(myEnd);
335 }
336
337 @Override
338 public void setEnabled(final boolean bEnabled) {
339 theNext.setEnabled(bEnabled && (theState.getNextDate() != null));
340 thePrev.setEnabled(bEnabled && (theState.getPrevDate() != null));
341 theDateButton.setEnabled(bEnabled);
342 thePortButton.setEnabled(bEnabled);
343 theDownloadButton.setEnabled(bEnabled);
344 theShowClosed.setEnabled(bEnabled);
345 }
346
347 @Override
348 public void setVisible(final boolean pVisible) {
349 thePanel.setVisible(pVisible);
350 }
351
352
353
354
355 public void createSavePoint() {
356
357 theSavePoint = new MoneyWiseSpotPricesState(theState);
358 }
359
360
361
362
363 public void restoreSavePoint() {
364
365 theState = new MoneyWiseSpotPricesState(theSavePoint);
366
367
368 theState.applyState();
369 }
370
371
372
373
374
375
376
377 public void setAdjacent(final OceanusDate pPrev,
378 final OceanusDate pNext) {
379
380 theState.setAdjacent(pPrev, pNext);
381 }
382
383
384
385
386 private void buildPortfolioMenu() {
387
388 thePortMenu.removeAllItems();
389
390
391 TethysUIScrollItem<MoneyWiseAnalysisPortfolioBucket> myActive = null;
392 final MoneyWiseAnalysisPortfolioBucket myCurr = theState.getPortfolio();
393
394
395 final Iterator<MoneyWiseAnalysisPortfolioBucket> myIterator = thePortfolios.iterator();
396 while (myIterator.hasNext()) {
397 final MoneyWiseAnalysisPortfolioBucket myBucket = myIterator.next();
398
399
400 if (!myBucket.isActive()
401 && !theState.showClosed()) {
402 continue;
403 }
404
405
406 final TethysUIScrollItem<MoneyWiseAnalysisPortfolioBucket> myItem = thePortMenu.addItem(myBucket);
407
408
409 if (myBucket.equals(myCurr)) {
410
411 myActive = myItem;
412 }
413 }
414
415
416 if (myActive != null) {
417 myActive.scrollToItem();
418 }
419 }
420
421
422
423
424 private void handleNewDate() {
425
426 if (theState.setDate(theDateButton)) {
427 theEventManager.fireEvent(PrometheusDataEvent.SELECTIONCHANGED);
428 }
429 }
430
431
432
433
434 private void handleNewPortfolio() {
435
436 if (theState.setPortfolio(thePortButton.getValue())) {
437 theState.applyState();
438 theEventManager.fireEvent(PrometheusDataEvent.SELECTIONCHANGED);
439 }
440 }
441
442
443
444
445 private void handleNewClosed() {
446 if (!refreshingData) {
447 theState.setShowClosed(theShowClosed.isSelected());
448 theEventManager.fireEvent(PrometheusDataEvent.SELECTIONCHANGED);
449 }
450 }
451
452
453
454
455 private final class MoneyWiseSpotPricesState {
456
457
458
459 private MoneyWiseAnalysisPortfolioBucket thePortfolio;
460
461
462
463
464 private OceanusDate theDate;
465
466
467
468
469 private OceanusDate theNextDate;
470
471
472
473
474 private OceanusDate thePrevDate;
475
476
477
478
479 private boolean showClosed;
480
481
482
483
484 private MoneyWiseSpotPricesState() {
485 theDate = new OceanusDate();
486 }
487
488
489
490
491
492
493 private MoneyWiseSpotPricesState(final MoneyWiseSpotPricesState pState) {
494 thePortfolio = pState.getPortfolio();
495 theDate = new OceanusDate(pState.getDate());
496 if (pState.getNextDate() != null) {
497 theNextDate = new OceanusDate(pState.getNextDate());
498 }
499 if (pState.getPrevDate() != null) {
500 thePrevDate = new OceanusDate(pState.getPrevDate());
501 }
502 showClosed = pState.showClosed();
503 }
504
505
506
507
508
509
510 private MoneyWiseAnalysisPortfolioBucket getPortfolio() {
511 return thePortfolio;
512 }
513
514
515
516
517
518
519 private OceanusDate getDate() {
520 return theDate;
521 }
522
523
524
525
526
527
528 private OceanusDate getNextDate() {
529 return theNextDate;
530 }
531
532
533
534
535
536
537 private OceanusDate getPrevDate() {
538 return thePrevDate;
539 }
540
541
542
543
544
545
546 private boolean showClosed() {
547 return showClosed;
548 }
549
550
551
552
553
554
555
556 private boolean setPortfolio(final MoneyWiseAnalysisPortfolioBucket pPortfolio) {
557
558 if (!MetisDataDifference.isEqual(pPortfolio, thePortfolio)) {
559 thePortfolio = pPortfolio;
560 return true;
561 }
562 return false;
563 }
564
565
566
567
568
569
570
571 private boolean setDate(final TethysUIDateButtonManager pButton) {
572
573 final OceanusDate myDate = new OceanusDate(pButton.getSelectedDate());
574 if (!MetisDataDifference.isEqual(myDate, theDate)) {
575 theDate = myDate;
576 return true;
577 }
578 return false;
579 }
580
581
582
583
584 private void setNext() {
585
586 theDate = new OceanusDate(theNextDate);
587 applyState();
588 }
589
590
591
592
593 private void setPrev() {
594
595 theDate = new OceanusDate(thePrevDate);
596 applyState();
597 }
598
599
600
601
602
603
604 private void setShowClosed(final boolean pShowClosed) {
605
606 showClosed = pShowClosed;
607 applyState();
608 }
609
610
611
612
613
614
615
616 private void setAdjacent(final OceanusDate pPrev,
617 final OceanusDate pNext) {
618
619 thePrevDate = pPrev;
620 theNextDate = pNext;
621
622
623 setEnabled(true);
624 }
625
626
627
628
629 private void applyState() {
630
631 setEnabled(true);
632 theDateButton.setSelectedDate(theDate);
633 thePortButton.setValue(thePortfolio);
634 theShowClosed.setSelected(showClosed);
635
636
637 final boolean isToday = MetisDataDifference.isEqual(theDate, new OceanusDate());
638 final boolean isActive = thePortfolio != null && thePortfolio.isActive();
639 theDownloadButton.setVisible(isToday && isActive);
640 }
641 }
642 }