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.ThemisFile;
35  import io.github.tonywasher.joceanus.themis.parser.proj.ThemisPackage;
36  
37  import java.util.List;
38  
39  /**
40   * Button to select file from package.
41   */
42  public class ThemisUISourceFileSelect
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<ThemisFile> theButton;
53  
54      /**
55       * The file menu.
56       */
57      private final TethysUIScrollMenu<ThemisFile> theFileMenu;
58  
59      /**
60       * The panel.
61       */
62      private final TethysUIBoxPaneManager thePanel;
63  
64      /**
65       * The current package.
66       */
67      private ThemisPackage thePackage;
68  
69      /**
70       * The current file.
71       */
72      private ThemisFile theFile;
73  
74      /**
75       * Constructor.
76       *
77       * @param pFactory the factory
78       */
79      ThemisUISourceFileSelect(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_FILE.getValue());
86  
87          /* Create the button */
88          theButton = pFactory.buttonFactory().newScrollButton(ThemisFile.class);
89          theFileMenu = 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 -> handleNewFile());
100         theButton.setMenuConfigurator(e -> buildFileMenu());
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 file.
115      *
116      * @return the current file
117      */
118     ThemisFile getCurrentFile() {
119         return theFile;
120     }
121 
122     /**
123      * Set the current package.
124      *
125      * @param pPackage the current package
126      */
127     void setCurrentPackage(final ThemisPackage pPackage) {
128         /* Store the package */
129         thePackage = pPackage;
130 
131         /* Set the default file */
132         final List<ThemisFile> myFiles = thePackage == null ? null : thePackage.getFiles();
133         final ThemisFile myFile = (myFiles == null || myFiles.isEmpty()) ? null : myFiles.getFirst();
134         theButton.setValue(myFile);
135         handleNewFile();
136     }
137 
138     /**
139      * Build the file menu.
140      */
141     private void buildFileMenu() {
142         /* Reset the popUp menu */
143         theFileMenu.removeAllItems();
144 
145         /* Record active item */
146         TethysUIScrollItem<ThemisFile> myActive = null;
147         final ThemisFile myCurr = theButton.getValue();
148 
149         /* Loop through the available files */
150         for (ThemisFile myFile : thePackage.getFiles()) {
151             /* Create a new MenuItem and add it to the popUp */
152             final TethysUIScrollItem<ThemisFile> myItem = theFileMenu.addItem(myFile);
153 
154             /* If this is the active file */
155             if (myFile.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 File.
169      */
170     private void handleNewFile() {
171         /* Select the new file */
172         theFile = theButton.getValue();
173         theEventManager.fireEvent(TethysUIEvent.NEWVALUE);
174     }
175 }