View Javadoc
1   /*
2    * Themis: Java Project Framework
3    * Copyright 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  
18  package io.github.tonywasher.joceanus.themis.gui.reference;
19  
20  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
21  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
22  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
23  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
24  import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
25  import io.github.tonywasher.joceanus.tethys.api.button.TethysUIScrollButtonManager;
26  import io.github.tonywasher.joceanus.tethys.api.control.TethysUIControlFactory;
27  import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
28  import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
29  import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollItem;
30  import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
31  import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
32  import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIPaneFactory;
33  import io.github.tonywasher.joceanus.themis.gui.base.ThemisUIResource;
34  import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverModule;
35  import io.github.tonywasher.joceanus.themis.solver.proj.ThemisSolverProject;
36  
37  import java.util.List;
38  
39  /**
40   * Button to select module from project.
41   */
42  public class ThemisUIRefModuleSelect
43          implements OceanusEventProvider<TethysUIEvent>, TethysUIComponent {
44      /**
45       * The Event Manager.
46       */
47      private final OceanusEventManager<TethysUIEvent> theEventManager;
48  
49      /**
50       * The scroll button.
51       */
52      private final TethysUIScrollButtonManager<ThemisSolverModule> theButton;
53  
54      /**
55       * The module menu.
56       */
57      private final TethysUIScrollMenu<ThemisSolverModule> theModuleMenu;
58  
59      /**
60       * The panel.
61       */
62      private final TethysUIBoxPaneManager thePanel;
63  
64      /**
65       * The current project.
66       */
67      private ThemisSolverProject theProject;
68  
69      /**
70       * The current module.
71       */
72      private ThemisSolverModule theModule;
73  
74      /**
75       * Constructor.
76       *
77       * @param pFactory the factory
78       */
79      ThemisUIRefModuleSelect(final TethysUIFactory<?> pFactory) {
80          /* Create Event Manager */
81          theEventManager = new OceanusEventManager<>();
82  
83          /* Create the label */
84          final TethysUIControlFactory myControls = pFactory.controlFactory();
85          final TethysUILabel myPromptLabel = myControls.newLabel(ThemisUIResource.PROMPT_MODULE.getValue());
86  
87          /* Create the button */
88          theButton = pFactory.buttonFactory().newScrollButton(ThemisSolverModule.class);
89          theModuleMenu = theButton.getMenu();
90  
91          /* Create the panel */
92          final TethysUIPaneFactory myPanes = pFactory.paneFactory();
93          thePanel = myPanes.newHBoxPane();
94          thePanel.addNode(myPromptLabel);
95          thePanel.addNode(theButton);
96  
97          /* Set listeners */
98          final OceanusEventRegistrar<TethysUIEvent> myRegistrar = theButton.getEventRegistrar();
99          myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handleNewModule());
100         theButton.setMenuConfigurator(e -> buildModuleMenu());
101     }
102 
103     @Override
104     public OceanusEventRegistrar<TethysUIEvent> getEventRegistrar() {
105         return theEventManager.getEventRegistrar();
106     }
107 
108     @Override
109     public TethysUIComponent getUnderlying() {
110         return thePanel;
111     }
112 
113     /**
114      * Obtain the current module.
115      *
116      * @return the current module
117      */
118     ThemisSolverModule getCurrentModule() {
119         return theModule;
120     }
121 
122     /**
123      * Set the current project.
124      *
125      * @param pProject the current project
126      */
127     void setCurrentProject(final ThemisSolverProject pProject) {
128         /* Store the project */
129         theProject = pProject;
130 
131         /* Set the default module */
132         final List<ThemisSolverModule> myModules = theProject == null ? null : theProject.getModules();
133         final ThemisSolverModule myModule = (myModules == null || myModules.isEmpty()) ? null : myModules.getFirst();
134         theButton.setValue(myModule);
135         handleNewModule();
136     }
137 
138     /**
139      * Build the package menu.
140      */
141     private void buildModuleMenu() {
142         /* Reset the popUp menu */
143         theModuleMenu.removeAllItems();
144 
145         /* Record active item */
146         TethysUIScrollItem<ThemisSolverModule> myActive = null;
147         final ThemisSolverModule myCurr = theButton.getValue();
148 
149         /* Loop through the available modules */
150         for (ThemisSolverModule myModule : theProject.getModules()) {
151             /* Create a new MenuItem and add it to the popUp */
152             final TethysUIScrollItem<ThemisSolverModule> myItem = theModuleMenu.addItem(myModule);
153 
154             /* If this is the active module */
155             if (myModule.equals(myCurr)) {
156                 /* Record it */
157                 myActive = myItem;
158             }
159         }
160 
161         /* Ensure active item is visible */
162         if (myActive != null) {
163             myActive.scrollToItem();
164         }
165     }
166 
167     /**
168      * Handle new Module.
169      */
170     private void handleNewModule() {
171         /* Select the new module */
172         theModule = theButton.getValue();
173         theEventManager.fireEvent(TethysUIEvent.NEWVALUE);
174     }
175 }