1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42 public class ThemisUISourceModuleSelect
43 implements OceanusEventProvider<TethysUIEvent>, TethysUIComponent {
44
45
46
47 private final OceanusEventManager<TethysUIEvent> theEventManager;
48
49
50
51
52 private final TethysUIScrollButtonManager<ThemisModule> theButton;
53
54
55
56
57 private final TethysUIScrollMenu<ThemisModule> theModuleMenu;
58
59
60
61
62 private final ThemisUISourcePackageSelect thePackageSelect;
63
64
65
66
67 private final TethysUIBoxPaneManager thePanel;
68
69
70
71
72 private ThemisProject theProject;
73
74
75
76
77 private ThemisModule theModule;
78
79
80
81
82
83
84
85 ThemisUISourceModuleSelect(final TethysUIFactory<?> pFactory,
86 final ThemisUISourcePackageSelect pPackageSelect) {
87
88 thePackageSelect = pPackageSelect;
89
90
91 theEventManager = new OceanusEventManager<>();
92
93
94 final TethysUIControlFactory myControls = pFactory.controlFactory();
95 final TethysUILabel myPromptLabel = myControls.newLabel(ThemisUIResource.PROMPT_MODULE.getValue());
96
97
98 theButton = pFactory.buttonFactory().newScrollButton(ThemisModule.class);
99 theModuleMenu = theButton.getMenu();
100
101
102 final TethysUIPaneFactory myPanes = pFactory.paneFactory();
103 thePanel = myPanes.newHBoxPane();
104 thePanel.addNode(myPromptLabel);
105 thePanel.addNode(theButton);
106
107
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
125
126
127
128 ThemisModule getCurrentModule() {
129 return theModule;
130 }
131
132
133
134
135
136
137 void setCurrentProject(final ThemisProject pProject) {
138
139 theProject = pProject;
140
141
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
150
151 private void buildModuleMenu() {
152
153 theModuleMenu.removeAllItems();
154
155
156 TethysUIScrollItem<ThemisModule> myActive = null;
157 final ThemisModule myCurr = theButton.getValue();
158
159
160 for (ThemisModule myModule : theProject.getModules()) {
161
162 final TethysUIScrollItem<ThemisModule> myItem = theModuleMenu.addItem(myModule);
163
164
165 if (myModule.equals(myCurr)) {
166
167 myActive = myItem;
168 }
169 }
170
171
172 if (myActive != null) {
173 myActive.scrollToItem();
174 }
175 }
176
177
178
179
180 private void handleNewModule() {
181
182 theModule = theButton.getValue();
183 theEventManager.fireEvent(TethysUIEvent.NEWVALUE);
184 thePackageSelect.setCurrentModule(theModule);
185 }
186 }