View Javadoc
1   /*
2    * Prometheus: Application Framework
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.prometheus.preference;
18  
19  import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceSet.MetisPreferenceItem;
20  import io.github.tonywasher.joceanus.metis.ui.MetisPreferenceSetView;
21  import io.github.tonywasher.joceanus.prometheus.preference.PrometheusPreferenceSet.PrometheusByteArrayPreference;
22  import io.github.tonywasher.joceanus.prometheus.preference.PrometheusPreferenceSet.PrometheusCharArrayPreference;
23  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIAlignment;
24  import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
25  import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
26  import io.github.tonywasher.joceanus.tethys.api.field.TethysUIDataEditField.TethysUICharArrayEditField;
27  import io.github.tonywasher.joceanus.tethys.api.field.TethysUIFieldAttribute;
28  import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIGridPaneManager;
29  
30  /**
31   * Panel for editing a preference Set.
32   */
33  public class PrometheusPreferenceSetView
34          extends MetisPreferenceSetView {
35      /**
36       * Constructor.
37       *
38       * @param pFactory       the GUI factory
39       * @param pPreferenceSet the preference set
40       */
41      PrometheusPreferenceSetView(final TethysUIFactory<?> pFactory,
42                                  final PrometheusPreferenceSet pPreferenceSet) {
43          super(pFactory, pPreferenceSet);
44      }
45  
46      @Override
47      protected PreferenceElement allocatePreferenceElement(final MetisPreferenceItem pItem) {
48          if (pItem instanceof PrometheusCharArrayPreference ca) {
49              return new CharArrayPreferenceElement(ca);
50          } else if (pItem instanceof PrometheusByteArrayPreference) {
51              return null;
52          } else {
53              return super.allocatePreferenceElement(pItem);
54          }
55      }
56  
57      /**
58       * CharArray preference element.
59       */
60      private final class CharArrayPreferenceElement
61              implements PreferenceElement {
62          /**
63           * The Preference item.
64           */
65          private final PrometheusCharArrayPreference theItem;
66  
67          /**
68           * The Field item.
69           */
70          private final TethysUICharArrayEditField theField;
71  
72          /**
73           * Constructor.
74           *
75           * @param pItem the item
76           */
77          CharArrayPreferenceElement(final PrometheusCharArrayPreference pItem) {
78              /* Store parameters */
79              theItem = pItem;
80              theField = getFactory().fieldFactory().newCharArrayField();
81              theField.setEditable(true);
82  
83              /* Create the label */
84              final TethysUILabel myLabel = getFactory().controlFactory().newLabel(pItem.getDisplay() + STR_COLON);
85              myLabel.setAlignment(TethysUIAlignment.EAST);
86  
87              /* Add to the Grid Pane */
88              final TethysUIGridPaneManager myGrid = getGrid();
89              myGrid.addCell(myLabel);
90              myGrid.setCellAlignment(myLabel, TethysUIAlignment.EAST);
91              myGrid.addCell(theField);
92              myGrid.setCellColumnSpan(theField, 2);
93              myGrid.allowCellGrowth(theField);
94              myGrid.newRow();
95  
96              /* Create listener */
97              theField.getEventRegistrar().addEventListener(e -> {
98                  pItem.setValue(theField.getValue());
99                  notifyChanges();
100             });
101         }
102 
103         @Override
104         public void updateField() {
105             /* Update the field */
106             theField.setValue(theItem.getValue());
107 
108             /* Set changed indication */
109             theField.setTheAttributeState(TethysUIFieldAttribute.CHANGED, theItem.isChanged());
110             theField.adjustField();
111 
112             /* Handle hidden state */
113             theField.setEnabled(!theItem.isHidden());
114         }
115     }
116 }