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.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
41
42 public class ThemisUIRefModuleSelect
43 implements OceanusEventProvider<TethysUIEvent>, TethysUIComponent {
44
45
46
47 private final OceanusEventManager<TethysUIEvent> theEventManager;
48
49
50
51
52 private final TethysUIScrollButtonManager<ThemisSolverModule> theButton;
53
54
55
56
57 private final TethysUIScrollMenu<ThemisSolverModule> theModuleMenu;
58
59
60
61
62 private final TethysUIBoxPaneManager thePanel;
63
64
65
66
67 private ThemisSolverProject theProject;
68
69
70
71
72 private ThemisSolverModule theModule;
73
74
75
76
77
78
79 ThemisUIRefModuleSelect(final TethysUIFactory<?> pFactory) {
80
81 theEventManager = new OceanusEventManager<>();
82
83
84 final TethysUIControlFactory myControls = pFactory.controlFactory();
85 final TethysUILabel myPromptLabel = myControls.newLabel(ThemisUIResource.PROMPT_MODULE.getValue());
86
87
88 theButton = pFactory.buttonFactory().newScrollButton(ThemisSolverModule.class);
89 theModuleMenu = theButton.getMenu();
90
91
92 final TethysUIPaneFactory myPanes = pFactory.paneFactory();
93 thePanel = myPanes.newHBoxPane();
94 thePanel.addNode(myPromptLabel);
95 thePanel.addNode(theButton);
96
97
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
115
116
117
118 ThemisSolverModule getCurrentModule() {
119 return theModule;
120 }
121
122
123
124
125
126
127 void setCurrentProject(final ThemisSolverProject pProject) {
128
129 theProject = pProject;
130
131
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
140
141 private void buildModuleMenu() {
142
143 theModuleMenu.removeAllItems();
144
145
146 TethysUIScrollItem<ThemisSolverModule> myActive = null;
147 final ThemisSolverModule myCurr = theButton.getValue();
148
149
150 for (ThemisSolverModule myModule : theProject.getModules()) {
151
152 final TethysUIScrollItem<ThemisSolverModule> myItem = theModuleMenu.addItem(myModule);
153
154
155 if (myModule.equals(myCurr)) {
156
157 myActive = myItem;
158 }
159 }
160
161
162 if (myActive != null) {
163 myActive.scrollToItem();
164 }
165 }
166
167
168
169
170 private void handleNewModule() {
171
172 theModule = theButton.getValue();
173 theEventManager.fireEvent(TethysUIEvent.NEWVALUE);
174 }
175 }