1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.moneywise.atlas.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.atlas.data.analysis.buckets.MoneyWiseXAnalysis;
25 import io.github.tonywasher.joceanus.moneywise.atlas.data.analysis.buckets.MoneyWiseXAnalysisPortfolioBucket;
26 import io.github.tonywasher.joceanus.moneywise.atlas.data.analysis.buckets.MoneyWiseXAnalysisPortfolioBucket.MoneyWiseXAnalysisPortfolioBucketList;
27 import io.github.tonywasher.joceanus.moneywise.atlas.views.MoneyWiseXAnalysisFilter;
28 import io.github.tonywasher.joceanus.moneywise.atlas.views.MoneyWiseXAnalysisFilter.MoneyWiseXAnalysisPortfolioCashFilter;
29 import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
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 MoneyWiseXPortfolioAnalysisSelect
47 implements MoneyWiseXAnalysisFilterSelection, 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<MoneyWiseXAnalysisPortfolioBucket> thePortButton;
67
68
69
70
71 private final TethysUIScrollMenu<MoneyWiseXAnalysisPortfolioBucket> thePortfolioMenu;
72
73
74
75
76 private MoneyWiseXAnalysisPortfolioBucketList 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 MoneyWiseXPortfolioAnalysisSelect(final TethysUIFactory<?> pFactory) {
94
95 thePortButton = pFactory.buttonFactory().newScrollButton(MoneyWiseXAnalysisPortfolioBucket.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 MoneyWiseXAnalysisPortfolioCashFilter 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 MoneyWiseXAnalysis pAnalysis) {
182
183 thePortfolios = pAnalysis.getPortfolios();
184
185
186 MoneyWiseXAnalysisPortfolioBucket 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 MoneyWiseXAnalysisFilter<?, ?> pFilter) {
201
202 if (pFilter instanceof MoneyWiseXAnalysisPortfolioCashFilter myFilter) {
203
204 MoneyWiseXAnalysisPortfolioBucket myPortfolio = myFilter.getPortfolioBucket();
205
206
207 myPortfolio = thePortfolios.getMatchingPortfolio(myPortfolio.getPortfolio());
208
209
210 theState.setThePortfolio(myPortfolio);
211 theState.setDateRange(myFilter.getDateRange());
212 theState.applyState();
213 }
214 }
215
216
217
218
219 private void handleNewPortfolio() {
220
221 if (theState.setPortfolio(thePortButton.getValue())) {
222 theState.applyState();
223 theEventManager.fireEvent(PrometheusDataEvent.SELECTIONCHANGED);
224 }
225 }
226
227
228
229
230 private void buildPortfolioMenu() {
231
232 thePortfolioMenu.removeAllItems();
233
234
235 TethysUIScrollItem<MoneyWiseXAnalysisPortfolioBucket> myActive = null;
236 final MoneyWiseXAnalysisPortfolioBucket myCurr = theState.getPortfolio();
237
238
239 final Iterator<MoneyWiseXAnalysisPortfolioBucket> myIterator = thePortfolios.iterator();
240 while (myIterator.hasNext()) {
241 final MoneyWiseXAnalysisPortfolioBucket myBucket = myIterator.next();
242
243
244 final TethysUIScrollItem<MoneyWiseXAnalysisPortfolioBucket> myItem = thePortfolioMenu.addItem(myBucket);
245
246
247 if (myBucket.equals(myCurr)) {
248
249 myActive = myItem;
250 }
251 }
252
253
254 if (myActive != null) {
255 myActive.scrollToItem();
256 }
257 }
258
259
260
261
262 private final class MoneyWisePortfolioState {
263
264
265
266 private MoneyWiseXAnalysisPortfolioBucket thePortfolio;
267
268
269
270
271 private OceanusDateRange theDateRange;
272
273
274
275
276 private MoneyWiseXAnalysisPortfolioCashFilter theFilter;
277
278
279
280
281 private MoneyWisePortfolioState() {
282 }
283
284
285
286
287
288
289 private MoneyWisePortfolioState(final MoneyWisePortfolioState pState) {
290
291 thePortfolio = pState.getPortfolio();
292 theDateRange = pState.getDateRange();
293 theFilter = pState.getFilter();
294 }
295
296
297
298
299
300
301 private MoneyWiseXAnalysisPortfolioBucket getPortfolio() {
302 return thePortfolio;
303 }
304
305
306
307
308
309
310 private OceanusDateRange getDateRange() {
311 return theDateRange;
312 }
313
314
315
316
317
318
319 private MoneyWiseXAnalysisPortfolioCashFilter getFilter() {
320 return theFilter;
321 }
322
323
324
325
326
327
328
329 private boolean setPortfolio(final MoneyWiseXAnalysisPortfolioBucket pPortfolio) {
330
331 if (!MetisDataDifference.isEqual(pPortfolio, thePortfolio)) {
332 setThePortfolio(pPortfolio);
333 return true;
334 }
335 return false;
336 }
337
338
339
340
341
342
343 private void setThePortfolio(final MoneyWiseXAnalysisPortfolioBucket pPortfolio) {
344
345 thePortfolio = pPortfolio;
346 if (thePortfolio != null) {
347 theFilter = new MoneyWiseXAnalysisPortfolioCashFilter(thePortfolio);
348 theFilter.setDateRange(theDateRange);
349 } else {
350 theFilter = null;
351 }
352 }
353
354
355
356
357
358
359 private void setDateRange(final OceanusDateRange pRange) {
360
361 theDateRange = pRange;
362 if (theFilter != null) {
363 theFilter.setDateRange(theDateRange);
364 }
365 }
366
367
368
369
370 private void applyState() {
371
372 setEnabled(true);
373 thePortButton.setValue(thePortfolio);
374 }
375 }
376 }