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.MoneyWiseLoan;
26  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseLoan.MoneyWiseLoanList;
27  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWisePayee;
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.MoneyWiseLoanDialog;
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 Loan Table.
44   */
45  public class MoneyWiseLoanTable
46          extends MoneyWiseAssetTable<MoneyWiseLoan> {
47      /**
48       * The Loan dialog.
49       */
50      private final MoneyWiseLoanDialog theActiveLoan;
51  
52      /**
53       * The edit list.
54       */
55      private MoneyWiseLoanList theLoans;
56  
57      /**
58       * Constructor.
59       *
60       * @param pView    the view
61       * @param pEditSet the editSet
62       * @param pError   the error panel
63       */
64      MoneyWiseLoanTable(final MoneyWiseView pView,
65                         final PrometheusEditSet pEditSet,
66                         final MetisErrorPanel pError) {
67          /* Store parameters */
68          super(pView, pEditSet, pError, MoneyWiseBasicDataType.LOAN);
69  
70          /* register the infoEntry */
71          getEditSet().registerType(MoneyWiseBasicDataType.LOANINFO);
72  
73          /* Access Gui factory */
74          final TethysUIFactory<?> myGuiFactory = pView.getGuiFactory();
75  
76          /* Create a Loan panel */
77          theActiveLoan = new MoneyWiseLoanDialog(myGuiFactory, pEditSet, this);
78          declareItemPanel(theActiveLoan);
79  
80          /* Finish the table */
81          finishTable(true, true, true);
82  
83          /* Add listeners */
84          theActiveLoan.getEventRegistrar().addEventListener(PrometheusDataEvent.ADJUSTVISIBILITY, e -> handlePanelState());
85      }
86  
87      @Override
88      protected boolean isItemEditing() {
89          return theActiveLoan.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("Loans");
97  
98          /* Access list */
99          final MoneyWiseDataSet myData = getView().getData();
100         final MoneyWiseLoanList myBase = myData.getLoans();
101         theLoans = myBase.deriveEditList(getEditSet());
102         getTable().setItems(theLoans.getUnderlyingList());
103 
104         /* Notify panel of refresh */
105         theActiveLoan.refreshData();
106         restoreSelected();
107 
108         /* Complete the task */
109         myTask.end();
110     }
111 
112     @Override
113     public void cancelEditing() {
114         super.cancelEditing();
115         theActiveLoan.setEditable(false);
116     }
117 
118     /**
119      * Select Loan.
120      *
121      * @param pLoan the Loan to select
122      */
123     void selectLoan(final MoneyWiseLoan pLoan) {
124         /* Check whether we need to showAll */
125         checkShowAll(pLoan);
126 
127         /* If we are changing the selection */
128         final MoneyWiseLoan myCurrent = theActiveLoan.getSelectedItem();
129         if (!MetisDataDifference.isEqual(myCurrent, pLoan)) {
130             /* Select the row and ensure that it is visible */
131             getTable().selectRow(pLoan);
132         }
133     }
134 
135     @Override
136     protected void handleRewind() {
137         /* Only action if we are not editing */
138         if (!theActiveLoan.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 (!theActiveLoan.isEditing()) {
154             /* handle the edit transition */
155             setEnabled(true);
156             final MoneyWiseLoan myLoan = theActiveLoan.getSelectedItem();
157             updateTableData();
158             if (myLoan != null) {
159                 getTable().selectRow(myLoan);
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 MoneyWiseLoan pLoan,
173                                      final TethysUIScrollMenu<MoneyWiseAssetCategory> pMenu) {
174         /* Build the menu */
175         theActiveLoan.buildCategoryMenu(pMenu, pLoan);
176     }
177 
178     @Override
179     protected void buildParentMenu(final MoneyWiseLoan pLoan,
180                                    final TethysUIScrollMenu<MoneyWisePayee> pMenu) {
181         /* Build the menu */
182         theActiveLoan.buildParentMenu(pMenu, pLoan);
183     }
184 
185     @Override
186     protected void buildCurrencyMenu(final MoneyWiseLoan pLoan,
187                                      final TethysUIScrollMenu<MoneyWiseCurrency> pMenu) {
188         /* Build the menu */
189         theActiveLoan.buildCurrencyMenu(pMenu, pLoan);
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 MoneyWiseLoan myLoan = theLoans.addNewItem();
205             myLoan.setDefaults();
206 
207             /* Set as new and adjust map */
208             myTask.startTask("incrementVersion");
209             myLoan.setNewVersion();
210             myLoan.adjustMapForItem();
211             getEditSet().incrementVersion();
212 
213             /* Validate the new item */
214             myTask.startTask("validate");
215             myLoan.validate();
216 
217             /* update panel */
218             myTask.startTask("setItem");
219             theActiveLoan.setNewItem(myLoan);
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 loan", e);
229 
230             /* Show the error */
231             setError(myError);
232         }
233     }
234 
235     @Override
236     protected Iterator<PrometheusDataItem> nameSpaceIterator() {
237         return assetNameSpaceIterator();
238     }
239 }