View Javadoc
1   /*
2    * MoneyWise: Finance Application
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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.profile.OceanusProfile;
21  import io.github.tonywasher.joceanus.metis.data.MetisDataDifference;
22  import io.github.tonywasher.joceanus.metis.ui.MetisErrorPanel;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
24  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataSet;
25  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePayee;
26  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePortfolio;
27  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePortfolio.MoneyWisePortfolioList;
28  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAssetCategory;
29  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
30  import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
31  import io.github.tonywasher.joceanus.moneywise.ui.base.MoneyWiseAssetTable;
32  import io.github.tonywasher.joceanus.moneywise.ui.dialog.MoneyWisePortfolioDialog;
33  import io.github.tonywasher.joceanus.moneywise.views.MoneyWiseView;
34  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
35  import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataEvent;
36  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
37  import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
38  import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
39  
40  import java.util.Iterator;
41  
42  /**
43   * MoneyWise Portfolio Table.
44   */
45  public class MoneyWisePortfolioTable
46          extends MoneyWiseAssetTable<MoneyWisePortfolio> {
47      /**
48       * The Portfolio dialog.
49       */
50      private final MoneyWisePortfolioDialog theActivePortfolio;
51  
52      /**
53       * The edit list.
54       */
55      private MoneyWisePortfolioList thePortfolios;
56  
57      /**
58       * Constructor.
59       *
60       * @param pView    the view
61       * @param pEditSet the editSet
62       * @param pError   the error panel
63       */
64      MoneyWisePortfolioTable(final MoneyWiseView pView,
65                              final PrometheusEditSet pEditSet,
66                              final MetisErrorPanel pError) {
67          /* Store parameters */
68          super(pView, pEditSet, pError, MoneyWiseBasicDataType.PORTFOLIO);
69  
70          /* register the infoEntry */
71          getEditSet().registerType(MoneyWiseBasicDataType.PORTFOLIOINFO);
72  
73          /* Access Gui factory */
74          final TethysUIFactory<?> myGuiFactory = pView.getGuiFactory();
75  
76          /* Create a portfolio panel */
77          theActivePortfolio = new MoneyWisePortfolioDialog(myGuiFactory, pEditSet, this);
78          declareItemPanel(theActivePortfolio);
79  
80          /* Finish the table */
81          finishTable(true, true, true);
82  
83          /* Add listeners */
84          theActivePortfolio.getEventRegistrar().addEventListener(PrometheusDataEvent.ADJUSTVISIBILITY, e -> handlePanelState());
85      }
86  
87      @Override
88      protected boolean isItemEditing() {
89          return theActivePortfolio.isEditing();
90      }
91  
92      @Override
93      protected void refreshData() throws OceanusException {
94          /* Obtain the active profile */
95          OceanusProfile myTask = getView().getActiveTask();
96          myTask = myTask.startTask("Portfolios");
97  
98          /* Access list */
99          final MoneyWiseDataSet myData = getView().getData();
100         final MoneyWisePortfolioList myBase = myData.getPortfolios();
101         thePortfolios = myBase.deriveEditList(getEditSet());
102         getTable().setItems(thePortfolios.getUnderlyingList());
103 
104         /* Notify panel of refresh */
105         theActivePortfolio.refreshData();
106         restoreSelected();
107 
108         /* Complete the task */
109         myTask.end();
110     }
111 
112     @Override
113     public void cancelEditing() {
114         super.cancelEditing();
115         theActivePortfolio.setEditable(false);
116     }
117 
118     /**
119      * Select portfolio.
120      *
121      * @param pPortfolio the portfolio to select
122      */
123     void selectPortfolio(final MoneyWisePortfolio pPortfolio) {
124         /* Check whether we need to showAll */
125         checkShowAll(pPortfolio);
126 
127         /* If we are changing the selection */
128         final MoneyWisePortfolio myCurrent = theActivePortfolio.getSelectedItem();
129         if (!MetisDataDifference.isEqual(myCurrent, pPortfolio)) {
130             /* Select the row and ensure that it is visible */
131             getTable().selectRow(pPortfolio);
132         }
133     }
134 
135     @Override
136     protected void handleRewind() {
137         /* Only action if we are not editing */
138         if (!theActivePortfolio.isEditing()) {
139             /* Handle the reWind */
140             setEnabled(true);
141             super.handleRewind();
142         }
143 
144         /* Adjust for changes */
145         notifyChanges();
146     }
147 
148     /**
149      * Handle panel state.
150      */
151     private void handlePanelState() {
152         /* Only action if we are not editing */
153         if (!theActivePortfolio.isEditing()) {
154             /* handle the edit transition */
155             setEnabled(true);
156             final MoneyWisePortfolio myPortfolio = theActivePortfolio.getSelectedItem();
157             updateTableData();
158             if (myPortfolio != null) {
159                 getTable().selectRow(myPortfolio);
160             } else {
161                 restoreSelected();
162             }
163         } else {
164             getTable().cancelEditing();
165         }
166 
167         /* Note changes */
168         notifyChanges();
169     }
170 
171     @Override
172     protected void buildCategoryMenu(final MoneyWisePortfolio pPortfolio,
173                                      final TethysUIScrollMenu<MoneyWiseAssetCategory> pMenu) {
174         /* Build the menu */
175         theActivePortfolio.buildTypeMenu(pMenu, pPortfolio);
176     }
177 
178     @Override
179     protected void buildParentMenu(final MoneyWisePortfolio pPortfolio,
180                                    final TethysUIScrollMenu<MoneyWisePayee> pMenu) {
181         /* Build the menu */
182         theActivePortfolio.buildParentMenu(pMenu, pPortfolio);
183     }
184 
185     @Override
186     protected void buildCurrencyMenu(final MoneyWisePortfolio pPortfolio,
187                                      final TethysUIScrollMenu<MoneyWiseCurrency> pMenu) {
188         /* Build the menu */
189         theActivePortfolio.buildCurrencyMenu(pMenu, pPortfolio);
190     }
191 
192     @Override
193     protected void addNewItem() {
194         /* Protect against Exceptions */
195         try {
196             /* Make sure that we have finished editing */
197             cancelEditing();
198 
199             /* Create a new profile */
200             final OceanusProfile myTask = getView().getNewProfile("addNewItem");
201 
202             /* Create the new asset */
203             myTask.startTask("buildItem");
204             final MoneyWisePortfolio myPortfolio = thePortfolios.addNewItem();
205             myPortfolio.setDefaults();
206 
207             /* Set as new and adjust map */
208             myTask.startTask("incrementVersion");
209             myPortfolio.setNewVersion();
210             myPortfolio.adjustMapForItem();
211             getEditSet().incrementVersion();
212 
213             /* Validate the new item */
214             myTask.startTask("validate");
215             myPortfolio.validate();
216 
217             /* update panel */
218             myTask.startTask("setItem");
219             theActivePortfolio.setNewItem(myPortfolio);
220 
221             /* Lock the table */
222             setTableEnabled(false);
223             myTask.end();
224 
225             /* Handle Exceptions */
226         } catch (OceanusException e) {
227             /* Build the error */
228             final OceanusException myError = new MoneyWiseDataException("Failed to create new portfolio", e);
229 
230             /* Show the error */
231             setError(myError);
232         }
233     }
234 
235     @Override
236     protected Iterator<PrometheusDataItem> nameSpaceIterator() {
237         return assetNameSpaceIterator();
238     }
239 }