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.source;
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.parser.proj.ThemisModule;
35  import io.github.tonywasher.joceanus.themis.parser.proj.ThemisProject;
36  
37  import java.util.List;
38  
39  /**
40   * Button to select module from project.
41   */
42  public class ThemisUISourceModuleSelect
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<ThemisModule> theButton;
53  
54      /**
55       * The module menu.
56       */
57      private final TethysUIScrollMenu<ThemisModule> theModuleMenu;
58  
59      /**
60       * The package select.
61       */
62      private final ThemisUISourcePackageSelect thePackageSelect;
63  
64      /**
65       * The panel.
66       */
67      private final TethysUIBoxPaneManager thePanel;
68  
69      /**
70       * The current project.
71       */
72      private ThemisProject theProject;
73  
74      /**
75       * The current module.
76       */
77      private ThemisModule theModule;
78  
79      /**
80       * Constructor.
81       *
82       * @param pFactory       the factory
83       * @param pPackageSelect the package selection button
84       */
85      ThemisUISourceModuleSelect(final TethysUIFactory<?> pFactory,
86                                 final ThemisUISourcePackageSelect pPackageSelect) {
87          /* Store the package select */
88          thePackageSelect = pPackageSelect;
89  
90          /* Create Event Manager */
91          theEventManager = new OceanusEventManager<>();
92  
93          /* Create the label */
94          final TethysUIControlFactory myControls = pFactory.controlFactory();
95          final TethysUILabel myPromptLabel = myControls.newLabel(ThemisUIResource.PROMPT_MODULE.getValue());
96  
97          /* Create the button */
98          theButton = pFactory.buttonFactory().newScrollButton(ThemisModule.class);
99          theModuleMenu = theButton.getMenu();
100 
101         /* Create the panel */
102         final TethysUIPaneFactory myPanes = pFactory.paneFactory();
103         thePanel = myPanes.newHBoxPane();
104         thePanel.addNode(myPromptLabel);
105         thePanel.addNode(theButton);
106 
107         /* Set listeners */
108         final OceanusEventRegistrar<TethysUIEvent> myRegistrar = theButton.getEventRegistrar();
109         myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handleNewModule());
110         theButton.setMenuConfigurator(e -> buildModuleMenu());
111     }
112 
113     @Override
114     public OceanusEventRegistrar<TethysUIEvent> getEventRegistrar() {
115         return theEventManager.getEventRegistrar();
116     }
117 
118     @Override
119     public TethysUIComponent getUnderlying() {
120         return thePanel;
121     }
122 
123     /**
124      * Obtain the current module.
125      *
126      * @return the current module
127      */
128     ThemisModule getCurrentModule() {
129         return theModule;
130     }
131 
132     /**
133      * Set the current project.
134      *
135      * @param pProject the current project
136      */
137     void setCurrentProject(final ThemisProject pProject) {
138         /* Store the project */
139         theProject = pProject;
140 
141         /* Set the default module */
142         final List<ThemisModule> myModules = theProject == null ? null : theProject.getModules();
143         final ThemisModule myModule = (myModules == null || myModules.isEmpty()) ? null : myModules.getFirst();
144         theButton.setValue(myModule);
145         handleNewModule();
146     }
147 
148     /**
149      * Build the package menu.
150      */
151     private void buildModuleMenu() {
152         /* Reset the popUp menu */
153         theModuleMenu.removeAllItems();
154 
155         /* Record active item */
156         TethysUIScrollItem<ThemisModule> myActive = null;
157         final ThemisModule myCurr = theButton.getValue();
158 
159         /* Loop through the available modules */
160         for (ThemisModule myModule : theProject.getModules()) {
161             /* Create a new MenuItem and add it to the popUp */
162             final TethysUIScrollItem<ThemisModule> myItem = theModuleMenu.addItem(myModule);
163 
164             /* If this is the active module */
165             if (myModule.equals(myCurr)) {
166                 /* Record it */
167                 myActive = myItem;
168             }
169         }
170 
171         /* Ensure active item is visible */
172         if (myActive != null) {
173             myActive.scrollToItem();
174         }
175     }
176 
177     /**
178      * Handle new Module.
179      */
180     private void handleNewModule() {
181         /* Select the new module */
182         theModule = theButton.getValue();
183         theEventManager.fireEvent(TethysUIEvent.NEWVALUE);
184         thePackageSelect.setCurrentModule(theModule);
185     }
186 }