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.dialog;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.metis.data.MetisDataItem.MetisDataFieldId;
21  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseBasicDataType;
22  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseRegion;
23  import io.github.tonywasher.joceanus.moneywise.data.basic.MoneyWiseRegion.MoneyWiseRegionList;
24  import io.github.tonywasher.joceanus.moneywise.ui.base.MoneyWiseBaseTable;
25  import io.github.tonywasher.joceanus.moneywise.ui.base.MoneyWiseItemPanel;
26  import io.github.tonywasher.joceanus.prometheus.data.PrometheusDataResource;
27  import io.github.tonywasher.joceanus.prometheus.ui.fieldset.PrometheusFieldSet;
28  import io.github.tonywasher.joceanus.prometheus.ui.fieldset.PrometheusFieldSetEvent;
29  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
30  import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
31  import io.github.tonywasher.joceanus.tethys.api.field.TethysUIDataEditField.TethysUIStringEditField;
32  import io.github.tonywasher.joceanus.tethys.api.field.TethysUIFieldFactory;
33  
34  /**
35   * Panel to display/edit/create a Region.
36   */
37  public class MoneyWiseRegionDialog
38          extends MoneyWiseItemPanel<MoneyWiseRegion> {
39      /**
40       * Constructor.
41       *
42       * @param pFactory the GUI factory
43       * @param pEditSet the edit set
44       * @param pOwner   the owning table
45       */
46      public MoneyWiseRegionDialog(final TethysUIFactory<?> pFactory,
47                                   final PrometheusEditSet pEditSet,
48                                   final MoneyWiseBaseTable<MoneyWiseRegion> pOwner) {
49          /* Initialise the panel */
50          super(pFactory, pEditSet, pOwner);
51  
52          /* Access the fieldSet */
53          final PrometheusFieldSet<MoneyWiseRegion> myFieldSet = getFieldSet();
54  
55          /* Create the text fields */
56          final TethysUIFieldFactory myFields = pFactory.fieldFactory();
57          final TethysUIStringEditField myName = myFields.newStringField();
58          final TethysUIStringEditField myDesc = myFields.newStringField();
59  
60          /* Assign the fields to the panel */
61          myFieldSet.addField(PrometheusDataResource.DATAITEM_FIELD_NAME, myName, MoneyWiseRegion::getName);
62          myFieldSet.addField(PrometheusDataResource.DATAITEM_FIELD_DESC, myDesc, MoneyWiseRegion::getDesc);
63  
64          /* Configure name checks */
65          myName.setValidator(this::isValidName);
66          myName.setReporter(pOwner::showValidateError);
67  
68          /* Configure description checks */
69          myDesc.setValidator(this::isValidDesc);
70          myDesc.setReporter(pOwner::showValidateError);
71      }
72  
73      @Override
74      public void refreshData() {
75          /* If we have an item */
76          final MoneyWiseRegion myItem = getItem();
77          if (myItem != null) {
78              final MoneyWiseRegionList myRegions = getDataList(MoneyWiseBasicDataType.REGION, MoneyWiseRegionList.class);
79              setItem(myRegions.findItemById(myItem.getIndexedId()));
80          }
81  
82          /* Make sure that the item is not editable */
83          setEditable(false);
84      }
85  
86      @Override
87      protected void adjustFields(final boolean isEditable) {
88          /* Access the fieldSet */
89          final PrometheusFieldSet<MoneyWiseRegion> myFieldSet = getFieldSet();
90  
91          /* Determine whether the description field should be visible */
92          final boolean bShowDesc = isEditable || getItem().getDesc() != null;
93          myFieldSet.setFieldVisible(PrometheusDataResource.DATAITEM_FIELD_DESC, bShowDesc);
94      }
95  
96      @Override
97      protected void updateField(final PrometheusFieldSetEvent pUpdate) throws OceanusException {
98          /* Access the field */
99          final MetisDataFieldId myField = pUpdate.getFieldId();
100         final MoneyWiseRegion myRegion = getItem();
101 
102         /* Process updates */
103         if (PrometheusDataResource.DATAITEM_FIELD_NAME.equals(myField)) {
104             /* Update the Name */
105             myRegion.setName(pUpdate.getValue(String.class));
106         } else if (PrometheusDataResource.DATAITEM_FIELD_DESC.equals(myField)) {
107             /* Update the Description */
108             myRegion.setDescription(pUpdate.getValue(String.class));
109         }
110     }
111 
112     @Override
113     protected void declareGoToItems(final boolean pUpdates) {
114         /* No GoTo items */
115     }
116 }