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.MoneyWiseCash;
25  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseCash.MoneyWiseCashList;
26  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseDataSet;
27  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseAssetCategory;
28  import io.github.tonywasher.joceanus.moneywise.data.statics.MoneyWiseCurrency;
29  import io.github.tonywasher.joceanus.moneywise.exc.MoneyWiseDataException;
30  import io.github.tonywasher.joceanus.moneywise.ui.base.MoneyWiseAssetTable;
31  import io.github.tonywasher.joceanus.moneywise.ui.dialog.MoneyWiseCashDialog;
32  import io.github.tonywasher.joceanus.moneywise.views.MoneyWiseView;
33  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataItem;
34  import io.github.tonywasher.joceanus.prometheus.views.PrometheusDataEvent;
35  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
36  import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
37  import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
38  
39  import java.util.Iterator;
40  
41  /**
42   * MoneyWise Cash Table.
43   */
44  public class MoneyWiseCashTable
45          extends MoneyWiseAssetTable<MoneyWiseCash> {
46      /**
47       * The Cash dialog.
48       */
49      private final MoneyWiseCashDialog theActiveCash;
50  
51      /**
52       * The edit list.
53       */
54      private MoneyWiseCashList theCash;
55  
56      /**
57       * Constructor.
58       *
59       * @param pView    the view
60       * @param pEditSet the editSet
61       * @param pError   the error panel
62       */
63      MoneyWiseCashTable(final MoneyWiseView pView,
64                         final PrometheusEditSet pEditSet,
65                         final MetisErrorPanel pError) {
66          /* Store parameters */
67          super(pView, pEditSet, pError, MoneyWiseBasicDataType.CASH);
68  
69          /* register the infoEntry */
70          getEditSet().registerType(MoneyWiseBasicDataType.CASHINFO);
71  
72          /* Access Gui factory */
73          final TethysUIFactory<?> myGuiFactory = pView.getGuiFactory();
74  
75          /* Create a Cash panel */
76          theActiveCash = new MoneyWiseCashDialog(myGuiFactory, pEditSet, this);
77          declareItemPanel(theActiveCash);
78  
79          /* Finish the table */
80          finishTable(false, true, true);
81  
82          /* Add listeners */
83          theActiveCash.getEventRegistrar().addEventListener(PrometheusDataEvent.ADJUSTVISIBILITY, e -> handlePanelState());
84      }
85  
86      @Override
87      protected boolean isItemEditing() {
88          return theActiveCash.isEditing();
89      }
90  
91      @Override
92      protected void refreshData() throws OceanusException {
93          /* Obtain the active profile */
94          OceanusProfile myTask = getView().getActiveTask();
95          myTask = myTask.startTask("Cashs");
96  
97          /* Access list */
98          final MoneyWiseDataSet myData = getView().getData();
99          final MoneyWiseCashList myBase = myData.getCash();
100         theCash = myBase.deriveEditList(getEditSet());
101         getTable().setItems(theCash.getUnderlyingList());
102 
103         /* Notify panel of refresh */
104         theActiveCash.refreshData();
105         restoreSelected();
106 
107         /* Complete the task */
108         myTask.end();
109     }
110 
111     @Override
112     public void cancelEditing() {
113         super.cancelEditing();
114         theActiveCash.setEditable(false);
115     }
116 
117     /**
118      * Select Cash.
119      *
120      * @param pCash the Cash to select
121      */
122     void selectCash(final MoneyWiseCash pCash) {
123         /* Check whether we need to showAll */
124         checkShowAll(pCash);
125 
126         /* If we are changing the selection */
127         final MoneyWiseCash myCurrent = theActiveCash.getSelectedItem();
128         if (!MetisDataDifference.isEqual(myCurrent, pCash)) {
129             /* Select the row and ensure that it is visible */
130             getTable().selectRow(pCash);
131         }
132     }
133 
134     @Override
135     protected void handleRewind() {
136         /* Only action if we are not editing */
137         if (!theActiveCash.isEditing()) {
138             /* Handle the reWind */
139             setEnabled(true);
140             super.handleRewind();
141         }
142 
143         /* Adjust for changes */
144         notifyChanges();
145     }
146 
147     /**
148      * Handle panel state.
149      */
150     private void handlePanelState() {
151         /* Only action if we are not editing */
152         if (!theActiveCash.isEditing()) {
153             /* handle the edit transition */
154             setEnabled(true);
155             final MoneyWiseCash myCash = theActiveCash.getSelectedItem();
156             updateTableData();
157             if (myCash != null) {
158                 getTable().selectRow(myCash);
159             } else {
160                 restoreSelected();
161             }
162         } else {
163             getTable().cancelEditing();
164         }
165 
166         /* Note changes */
167         notifyChanges();
168     }
169 
170     @Override
171     protected void buildCategoryMenu(final MoneyWiseCash pCash,
172                                      final TethysUIScrollMenu<MoneyWiseAssetCategory> pMenu) {
173         /* Build the menu */
174         theActiveCash.buildCategoryMenu(pMenu, pCash);
175     }
176 
177     @Override
178     protected void buildCurrencyMenu(final MoneyWiseCash pCash,
179                                      final TethysUIScrollMenu<MoneyWiseCurrency> pMenu) {
180         /* Build the menu */
181         theActiveCash.buildCurrencyMenu(pMenu, pCash);
182     }
183 
184     @Override
185     protected void addNewItem() {
186         /* Protect against Exceptions */
187         try {
188             /* Make sure that we have finished editing */
189             cancelEditing();
190 
191             /* Create a new profile */
192             final OceanusProfile myTask = getView().getNewProfile("addNewItem");
193 
194             /* Create the new asset */
195             myTask.startTask("buildItem");
196             final MoneyWiseCash myCash = theCash.addNewItem();
197             myCash.setDefaults();
198 
199             /* Set as new and adjust map */
200             myTask.startTask("incrementVersion");
201             myCash.setNewVersion();
202             myCash.adjustMapForItem();
203             getEditSet().incrementVersion();
204 
205             /* Validate the new item */
206             myTask.startTask("validate");
207             myCash.validate();
208 
209             /* update panel */
210             myTask.startTask("setItem");
211             theActiveCash.setNewItem(myCash);
212 
213             /* Lock the table */
214             setTableEnabled(false);
215             myTask.end();
216 
217             /* Handle Exceptions */
218         } catch (OceanusException e) {
219             /* Build the error */
220             final OceanusException myError = new MoneyWiseDataException("Failed to create new cash", e);
221 
222             /* Show the error */
223             setError(myError);
224         }
225     }
226 
227     @Override
228     protected Iterator<PrometheusDataItem> nameSpaceIterator() {
229         return assetNameSpaceIterator();
230     }
231 }
232