1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.moneywise.ui.panel;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.event.OceanusEvent;
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.oceanus.profile.OceanusProfile;
25 import io.github.tonywasher.joceanus.metis.ui.MetisErrorPanel;
26 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerEntry;
27 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerManager;
28 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
29 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCashCategory;
30 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDepositCategory;
31 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoanCategory;
32 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseRegion;
33 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransCategory;
34 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseTransTag;
35 import io.github.tonywasher.joceanus.moneywise.ui.MoneyWiseGoToId;
36 import io.github.tonywasher.joceanus.moneywise.ui.MoneyWiseUIResource;
37 import io.github.tonywasher.joceanus.moneywise.views.MoneyWiseView;
38 import io.github.tonywasher.joceanus.prometheus.ui.PrometheusActionButtons;
39 import io.github.tonywasher.joceanus.prometheus.ui.PrometheusGoToEvent;
40 import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataEvent;
41 import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
42 import io.github.tonywasher.joceanus.prometheus.views.PrometheusUIEvent;
43 import io.github.tonywasher.joceanus.prometheus.views.PrometheusViewerEntryId;
44 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
45 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
46 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIScrollButtonManager;
47 import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
48 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
49 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
50 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBorderPaneManager;
51 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
52 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUICardPaneManager;
53 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIPaneFactory;
54
55
56
57
58 public class MoneyWiseCategoryPanel
59 implements OceanusEventProvider<PrometheusDataEvent>, TethysUIComponent {
60
61
62
63 private static final String NLS_DATAENTRY = MoneyWiseUIResource.CATEGORY_DATAENTRY.getValue();
64
65
66
67
68 private static final String NLS_SELECT = MoneyWiseUIResource.CATEGORY_TITLE_SELECT.getValue();
69
70
71
72
73 private static final String NLS_DATA = MoneyWiseUIResource.CATEGORY_PROMPT_SELECT.getValue();
74
75
76
77
78 private final OceanusEventManager<PrometheusDataEvent> theEventManager;
79
80
81
82
83 private final MoneyWiseView theView;
84
85
86
87
88 private final TethysUIBorderPaneManager thePanel;
89
90
91
92
93 private final TethysUIScrollButtonManager<PanelName> theSelectButton;
94
95
96
97
98 private final TethysUICardPaneManager<TethysUIComponent> theCardPanel;
99
100
101
102
103 private final TethysUIBorderPaneManager theSelectPanel;
104
105
106
107
108 private final TethysUICardPaneManager<TethysUIComponent> theFilterCardPanel;
109
110
111
112
113 private final MoneyWiseDepositCategoryTable theDepositTable;
114
115
116
117
118 private final MoneyWiseCashCategoryTable theCashTable;
119
120
121
122
123 private final MoneyWiseLoanCategoryTable theLoanTable;
124
125
126
127
128 private final MoneyWiseTransCategoryTable theEventTable;
129
130
131
132
133 private final MoneyWiseTransTagTable theTagTable;
134
135
136
137
138 private final MoneyWiseRegionTable theRegionTable;
139
140
141
142
143 private final PrometheusEditSet theEditSet;
144
145
146
147
148 private final MetisViewerEntry theViewerEntry;
149
150
151
152
153 private final PrometheusActionButtons theActionButtons;
154
155
156
157
158 private final MetisErrorPanel theError;
159
160
161
162
163 private boolean isRefreshing;
164
165
166
167
168 private PanelName theActive;
169
170
171
172
173
174
175 MoneyWiseCategoryPanel(final MoneyWiseView pView) {
176
177 theView = pView;
178
179
180 final TethysUIFactory<?> myFactory = pView.getGuiFactory();
181 final MetisViewerManager myViewer = pView.getViewerManager();
182
183
184 theEventManager = new OceanusEventManager<>();
185
186
187 theEditSet = new PrometheusEditSet(pView);
188
189
190 final TethysUIPaneFactory myPanes = myFactory.paneFactory();
191 thePanel = myPanes.newBorderPane();
192
193
194 final MetisViewerEntry mySection = pView.getViewerEntry(PrometheusViewerEntryId.MAINTENANCE);
195 theViewerEntry = myViewer.newEntry(mySection, NLS_DATAENTRY);
196 theViewerEntry.setTreeObject(theEditSet);
197
198
199 theError = pView.getToolkit().getToolkit().newErrorPanel(theViewerEntry);
200
201
202 theActionButtons = new PrometheusActionButtons(myFactory, theEditSet);
203
204
205 theDepositTable = new MoneyWiseDepositCategoryTable(pView, theEditSet, theError);
206 theCashTable = new MoneyWiseCashCategoryTable(pView, theEditSet, theError);
207 theLoanTable = new MoneyWiseLoanCategoryTable(pView, theEditSet, theError);
208 theEventTable = new MoneyWiseTransCategoryTable(pView, theEditSet, theError);
209 theTagTable = new MoneyWiseTransTagTable(pView, theEditSet, theError);
210 theRegionTable = new MoneyWiseRegionTable(pView, theEditSet, theError);
211
212
213 final TethysUILabel myLabel = myFactory.controlFactory().newLabel(NLS_DATA);
214 theSelectButton = myFactory.buttonFactory().newScrollButton(PanelName.class);
215 buildSelectMenu();
216
217
218 theCardPanel = myPanes.newCardPane();
219
220
221 theCardPanel.addCard(PanelName.DEPOSITS.toString(), theDepositTable);
222 theCardPanel.addCard(PanelName.CASH.toString(), theCashTable);
223 theCardPanel.addCard(PanelName.LOANS.toString(), theLoanTable);
224 theCardPanel.addCard(PanelName.EVENTS.toString(), theEventTable);
225 theCardPanel.addCard(PanelName.EVENTTAGS.toString(), theTagTable);
226 theCardPanel.addCard(PanelName.REGIONS.toString(), theRegionTable);
227 theActive = PanelName.DEPOSITS;
228 theSelectButton.setValue(theActive);
229
230
231 theFilterCardPanel = myPanes.newCardPane();
232
233
234 theFilterCardPanel.addCard(PanelName.DEPOSITS.toString(), theDepositTable.getFilterPanel());
235 theFilterCardPanel.addCard(PanelName.CASH.toString(), theCashTable.getFilterPanel());
236 theFilterCardPanel.addCard(PanelName.LOANS.toString(), theLoanTable.getFilterPanel());
237 theFilterCardPanel.addCard(PanelName.EVENTS.toString(), theEventTable.getFilterPanel());
238 theFilterCardPanel.addCard(PanelName.EVENTTAGS.toString(), theTagTable.getFilterPanel());
239 theFilterCardPanel.addCard(PanelName.REGIONS.toString(), theRegionTable.getFilterPanel());
240
241
242 final TethysUIBoxPaneManager mySelect = myPanes.newHBoxPane();
243 mySelect.addNode(myLabel);
244 mySelect.addNode(theSelectButton);
245
246
247 theSelectPanel = myPanes.newBorderPane();
248 theSelectPanel.setBorderTitle(NLS_SELECT);
249 theSelectPanel.setWest(mySelect);
250 theSelectPanel.setCentre(theFilterCardPanel);
251
252
253 final TethysUIBorderPaneManager myHeader = myPanes.newBorderPane();
254 myHeader.setCentre(theSelectPanel);
255 myHeader.setNorth(theError);
256 myHeader.setEast(theActionButtons);
257
258
259 thePanel.setNorth(myHeader);
260 thePanel.setCentre(theCardPanel);
261
262
263 theActionButtons.setVisible(false);
264
265
266 theSelectButton.getEventRegistrar().addEventListener(TethysUIEvent.NEWVALUE, e -> handleSelection());
267 theError.getEventRegistrar().addEventListener(e -> handleErrorPane());
268 theActionButtons.getEventRegistrar().addEventListener(this::handleActionButtons);
269 setChildListeners(theDepositTable.getEventRegistrar());
270 setChildListeners(theCashTable.getEventRegistrar());
271 setChildListeners(theLoanTable.getEventRegistrar());
272 setChildListeners(theEventTable.getEventRegistrar());
273 setChildListeners(theTagTable.getEventRegistrar());
274 setChildListeners(theRegionTable.getEventRegistrar());
275 }
276
277 @Override
278 public TethysUIComponent getUnderlying() {
279 return thePanel;
280 }
281
282 @Override
283 public OceanusEventRegistrar<PrometheusDataEvent> getEventRegistrar() {
284 return theEventManager.getEventRegistrar();
285 }
286
287
288
289
290
291
292 private void setChildListeners(final OceanusEventRegistrar<PrometheusDataEvent> pRegistrar) {
293 pRegistrar.addEventListener(PrometheusDataEvent.ADJUSTVISIBILITY, e -> {
294 if (!isRefreshing) {
295 setVisibility();
296 }
297 });
298 pRegistrar.addEventListener(PrometheusDataEvent.GOTOWINDOW, this::handleGoToEvent);
299 }
300
301 @Override
302 public void setEnabled(final boolean pEnabled) {
303 theSelectButton.setEnabled(pEnabled);
304 theCardPanel.setEnabled(pEnabled);
305 theFilterCardPanel.setEnabled(pEnabled);
306 }
307
308 @Override
309 public void setVisible(final boolean pVisible) {
310 thePanel.setVisible(pVisible);
311 }
312
313
314
315
316 private void buildSelectMenu() {
317
318 final TethysUIScrollMenu<PanelName> myMenu = theSelectButton.getMenu();
319
320
321 for (PanelName myPanel : PanelName.values()) {
322
323 myMenu.addItem(myPanel);
324 }
325 }
326
327
328
329
330
331
332 public void refreshData() throws OceanusException {
333
334 OceanusProfile myTask = theView.getActiveTask();
335 myTask = myTask.startTask("Categories");
336
337
338 isRefreshing = true;
339 theEditSet.setDataSet(theView.getData());
340
341
342 theDepositTable.refreshData();
343 theCashTable.refreshData();
344 theLoanTable.refreshData();
345 theEventTable.refreshData();
346 theTagTable.refreshData();
347 theRegionTable.refreshData();
348
349
350 isRefreshing = false;
351 setVisibility();
352
353
354 theViewerEntry.setTreeObject(theEditSet);
355
356
357 myTask.end();
358 }
359
360
361
362
363 public void determineFocus() {
364
365 switch (theActive) {
366 case DEPOSITS:
367 theDepositTable.determineFocus(theViewerEntry);
368 break;
369 case CASH:
370 theCashTable.determineFocus(theViewerEntry);
371 break;
372 case LOANS:
373 theLoanTable.determineFocus(theViewerEntry);
374 break;
375 case EVENTS:
376 theEventTable.determineFocus(theViewerEntry);
377 break;
378 case EVENTTAGS:
379 theTagTable.determineFocus(theViewerEntry);
380 break;
381 case REGIONS:
382 theRegionTable.determineFocus(theViewerEntry);
383 break;
384 default:
385 break;
386 }
387 }
388
389
390
391
392
393
394 public boolean hasUpdates() {
395
396 boolean hasUpdates = theDepositTable.hasUpdates();
397 if (!hasUpdates) {
398 hasUpdates = theCashTable.hasUpdates();
399 }
400 if (!hasUpdates) {
401 hasUpdates = theLoanTable.hasUpdates();
402 }
403 if (!hasUpdates) {
404 hasUpdates = theEventTable.hasUpdates();
405 }
406 if (!hasUpdates) {
407 hasUpdates = theTagTable.hasUpdates();
408 }
409 if (!hasUpdates) {
410 hasUpdates = theRegionTable.hasUpdates();
411 }
412
413
414 return hasUpdates;
415 }
416
417
418
419
420
421
422 public boolean hasSession() {
423
424 boolean hasSession = theDepositTable.hasSession();
425 if (!hasSession) {
426 hasSession = theCashTable.hasSession();
427 }
428 if (!hasSession) {
429 hasSession = theLoanTable.hasSession();
430 }
431 if (!hasSession) {
432 hasSession = theEventTable.hasSession();
433 }
434 if (!hasSession) {
435 hasSession = theTagTable.hasSession();
436 }
437 if (!hasSession) {
438 hasSession = theRegionTable.hasSession();
439 }
440
441
442 return hasSession;
443 }
444
445
446
447
448
449
450 public boolean hasErrors() {
451
452 boolean hasErrors = theDepositTable.hasErrors();
453 if (!hasErrors) {
454 hasErrors = theCashTable.hasErrors();
455 }
456 if (!hasErrors) {
457 hasErrors = theLoanTable.hasErrors();
458 }
459 if (!hasErrors) {
460 hasErrors = theEventTable.hasErrors();
461 }
462 if (!hasErrors) {
463 hasErrors = theTagTable.hasErrors();
464 }
465 if (!hasErrors) {
466 hasErrors = theRegionTable.hasErrors();
467 }
468
469
470 return hasErrors;
471 }
472
473
474
475
476
477
478 public boolean isItemEditing() {
479
480 boolean isEditing = theDepositTable.isItemEditing();
481 if (!isEditing) {
482 isEditing = theCashTable.isItemEditing();
483 }
484 if (!isEditing) {
485 isEditing = theLoanTable.isItemEditing();
486 }
487 if (!isEditing) {
488 isEditing = theEventTable.isItemEditing();
489 }
490 if (!isEditing) {
491 isEditing = theTagTable.isItemEditing();
492 }
493 if (!isEditing) {
494 isEditing = theRegionTable.isItemEditing();
495 }
496
497
498 return isEditing;
499 }
500
501
502
503
504
505
506 public void selectCategory(final Object pCategory) {
507
508 if (pCategory instanceof MoneyWiseDepositCategory myCategory) {
509 theDepositTable.selectCategory(myCategory);
510 showPanel(PanelName.DEPOSITS);
511 } else if (pCategory instanceof MoneyWiseCashCategory myCategory) {
512 theCashTable.selectCategory(myCategory);
513 showPanel(PanelName.CASH);
514 } else if (pCategory instanceof MoneyWiseLoanCategory myCategory) {
515 theLoanTable.selectCategory(myCategory);
516 showPanel(PanelName.LOANS);
517 } else if (pCategory instanceof MoneyWiseTransCategory myCategory) {
518 theEventTable.selectCategory(myCategory);
519 showPanel(PanelName.EVENTS);
520 }
521 }
522
523
524
525
526
527
528 public void selectTag(final Object pTag) {
529
530 if (pTag instanceof MoneyWiseTransTag myTag) {
531 theTagTable.selectTag(myTag);
532 showPanel(PanelName.EVENTTAGS);
533 }
534 }
535
536
537
538
539
540
541 public void selectRegion(final Object pRegion) {
542
543 if (pRegion instanceof MoneyWiseRegion myRegion) {
544 theRegionTable.selectRegion(myRegion);
545 showPanel(PanelName.REGIONS);
546 }
547 }
548
549
550
551
552
553
554 private void showPanel(final PanelName pName) {
555
556 final String myName = pName.toString();
557
558
559 theCardPanel.selectCard(myName);
560 theFilterCardPanel.selectCard(myName);
561
562
563 theActive = pName;
564 theSelectButton.setFixedText(myName);
565
566
567 determineFocus();
568 }
569
570
571
572
573 protected void setVisibility() {
574
575 final boolean hasUpdates = hasUpdates();
576 final boolean isItemEditing = isItemEditing();
577
578
579 theActionButtons.setEnabled(true);
580 theActionButtons.setVisible(hasUpdates && !isItemEditing);
581
582
583 theSelectButton.setEnabled(!isItemEditing);
584 theFilterCardPanel.setEnabled(!isItemEditing);
585
586
587 theEventManager.fireEvent(PrometheusDataEvent.ADJUSTVISIBILITY);
588 }
589
590
591
592
593 private void cancelEditing() {
594
595 theDepositTable.cancelEditing();
596 theCashTable.cancelEditing();
597 theLoanTable.cancelEditing();
598 theEventTable.cancelEditing();
599 theTagTable.cancelEditing();
600 theRegionTable.cancelEditing();
601 }
602
603
604
605
606 private void handleErrorPane() {
607
608 final boolean isError = theError.hasError();
609
610
611 theSelectPanel.setVisible(!isError);
612
613
614 theCardPanel.setEnabled(!isError);
615
616
617 theActionButtons.setEnabled(!isError);
618 }
619
620
621
622
623 private void handleSelection() {
624
625 cancelEditing();
626
627
628 showPanel(theSelectButton.getValue());
629 }
630
631
632
633
634
635
636 private void handleActionButtons(final OceanusEvent<PrometheusUIEvent> pEvent) {
637
638 cancelEditing();
639
640
641 theEditSet.processCommand(pEvent.getEventId(), theError);
642 }
643
644
645
646
647
648
649 private void handleGoToEvent(final OceanusEvent<PrometheusDataEvent> pEvent) {
650
651 @SuppressWarnings("unchecked") final PrometheusGoToEvent<MoneyWiseGoToId> myEvent = pEvent.getDetails(PrometheusGoToEvent.class);
652
653
654 switch (myEvent.getId()) {
655
656 case STATEMENT:
657 case ACCOUNT:
658 case STATIC:
659 theEventManager.cascadeEvent(pEvent);
660 break;
661
662
663 case CATEGORY:
664 selectCategory(myEvent.getDetails());
665 break;
666 case TAG:
667 selectTag(pEvent.getDetails());
668 break;
669 case REGION:
670 selectRegion(pEvent.getDetails());
671 break;
672 default:
673 break;
674 }
675 }
676
677
678
679
680 private enum PanelName {
681
682
683
684 DEPOSITS(MoneyWiseBasicDataType.DEPOSITCATEGORY),
685
686
687
688
689 CASH(MoneyWiseBasicDataType.CASHCATEGORY),
690
691
692
693
694 LOANS(MoneyWiseBasicDataType.LOANCATEGORY),
695
696
697
698
699 EVENTS(MoneyWiseBasicDataType.TRANSCATEGORY),
700
701
702
703
704 EVENTTAGS(MoneyWiseBasicDataType.TRANSTAG),
705
706
707
708
709 REGIONS(MoneyWiseBasicDataType.REGION);
710
711
712
713
714 private final String theName;
715
716
717
718
719
720
721 PanelName(final MoneyWiseBasicDataType pDataType) {
722 theName = pDataType.getListName();
723 }
724
725 @Override
726 public String toString() {
727
728 return theName;
729 }
730 }
731 }