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.ui;
18  
19  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
20  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
21  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
22  import io.github.tonywasher.joceanus.metis.ui.MetisIcon;
23  import io.github.tonywasher.joceanus.prometheus.views.PrometheusEditSet;
24  import io.github.tonywasher.joceanus.prometheus.views.PrometheusUIEvent;
25  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
26  import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButton;
27  import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButtonFactory;
28  import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
29  import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
30  import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIPaneFactory;
31  
32  /**
33   * Action buttons.
34   */
35  public class PrometheusActionButtons
36          implements OceanusEventProvider<PrometheusUIEvent>, TethysUIComponent {
37      /**
38       * Strut width.
39       */
40      protected static final int STRUT_LENGTH = 5;
41  
42      /**
43       * Text for Box Title.
44       */
45      private static final String NLS_TITLE = PrometheusUIResource.ACTION_TITLE_SAVE.getValue();
46  
47      /**
48       * The Event Manager.
49       */
50      private final OceanusEventManager<PrometheusUIEvent> theEventManager;
51  
52      /**
53       * The update set.
54       */
55      private final PrometheusEditSet theUpdateSet;
56  
57      /**
58       * The panel.
59       */
60      private final TethysUIBoxPaneManager thePanel;
61  
62      /**
63       * The Commit button.
64       */
65      private final TethysUIButton theCommitButton;
66  
67      /**
68       * The Undo button.
69       */
70      private final TethysUIButton theUndoButton;
71  
72      /**
73       * The Reset button.
74       */
75      private final TethysUIButton theResetButton;
76  
77      /**
78       * Constructor.
79       *
80       * @param pFactory   the GUI factory
81       * @param pUpdateSet the update set
82       */
83      public PrometheusActionButtons(final TethysUIFactory<?> pFactory,
84                                     final PrometheusEditSet pUpdateSet) {
85          this(pFactory, pUpdateSet, true);
86      }
87  
88      /**
89       * Constructor.
90       *
91       * @param pFactory    the GUI factory
92       * @param pUpdateSet  the update set
93       * @param pHorizontal is this horizontal panel?
94       */
95      public PrometheusActionButtons(final TethysUIFactory<?> pFactory,
96                                     final PrometheusEditSet pUpdateSet,
97                                     final boolean pHorizontal) {
98          /* Record the update set */
99          theUpdateSet = pUpdateSet;
100 
101         /* Create the event manager */
102         theEventManager = new OceanusEventManager<>();
103 
104         /* Create the buttons */
105         final TethysUIButtonFactory<?> myButtons = pFactory.buttonFactory();
106         theCommitButton = myButtons.newButton();
107         theUndoButton = myButtons.newButton();
108         theResetButton = myButtons.newButton();
109 
110         /* Configure the buttons */
111         MetisIcon.configureCommitIconButton(theCommitButton);
112         MetisIcon.configureUndoIconButton(theUndoButton);
113         MetisIcon.configureResetIconButton(theResetButton);
114 
115         /* Add the listener for item changes */
116         theCommitButton.getEventRegistrar().addEventListener(e -> theEventManager.fireEvent(PrometheusUIEvent.OK));
117         theUndoButton.getEventRegistrar().addEventListener(e -> theEventManager.fireEvent(PrometheusUIEvent.UNDO));
118         theResetButton.getEventRegistrar().addEventListener(e -> theEventManager.fireEvent(PrometheusUIEvent.RESET));
119 
120         /* Create the panel */
121         final TethysUIPaneFactory myPanes = pFactory.paneFactory();
122         thePanel = pHorizontal
123                 ? myPanes.newHBoxPane()
124                 : myPanes.newVBoxPane();
125 
126         /* Define the layout */
127         if (!pHorizontal) {
128             thePanel.addNode(pFactory.controlFactory().newLabel(NLS_TITLE));
129         }
130         thePanel.addNode(theCommitButton);
131         thePanel.addNode(theUndoButton);
132         thePanel.addNode(theResetButton);
133 
134         /* Set border if required */
135         if (pHorizontal) {
136             thePanel.setBorderTitle(NLS_TITLE);
137         }
138 
139         /* Buttons are initially disabled */
140         setEnabled(false);
141     }
142 
143     @Override
144     public TethysUIComponent getUnderlying() {
145         return thePanel;
146     }
147 
148     @Override
149     public OceanusEventRegistrar<PrometheusUIEvent> getEventRegistrar() {
150         return theEventManager.getEventRegistrar();
151     }
152 
153     @Override
154     public void setEnabled(final boolean bEnabled) {
155         /* If the table is locked clear the buttons */
156         if (!bEnabled) {
157             theCommitButton.setEnabled(false);
158             theUndoButton.setEnabled(false);
159             theResetButton.setEnabled(false);
160 
161             /* Else look at the edit state */
162         } else {
163             /* Switch on the edit state */
164             switch (theUpdateSet.getEditState()) {
165                 case CLEAN:
166                     theCommitButton.setEnabled(false);
167                     theUndoButton.setEnabled(false);
168                     theResetButton.setEnabled(theUpdateSet.hasUpdates());
169                     break;
170                 case DIRTY:
171                 case ERROR:
172                     theCommitButton.setEnabled(false);
173                     theUndoButton.setEnabled(true);
174                     theResetButton.setEnabled(true);
175                     break;
176                 case VALID:
177                     theCommitButton.setEnabled(true);
178                     theUndoButton.setEnabled(true);
179                     theResetButton.setEnabled(true);
180                     break;
181                 default:
182                     break;
183             }
184         }
185     }
186 }