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.base.ThemisChar;
35 import io.github.tonywasher.joceanus.themis.parser.proj.ThemisModule;
36 import io.github.tonywasher.joceanus.themis.parser.proj.ThemisPackage;
37
38
39
40
41 public class ThemisUISourcePackageSelect
42 implements OceanusEventProvider<TethysUIEvent>, TethysUIComponent {
43
44
45
46 private final OceanusEventManager<TethysUIEvent> theEventManager;
47
48
49
50
51 private final TethysUIScrollButtonManager<ThemisPackage> theButton;
52
53
54
55
56 private final TethysUIScrollMenu<ThemisPackage> thePackageMenu;
57
58
59
60
61 private final ThemisUISourceFileSelect theFileSelect;
62
63
64
65
66 private final TethysUILabel thePrefixLabel;
67
68
69
70
71 private final TethysUIBoxPaneManager thePanel;
72
73
74
75
76 private ThemisModule theModule;
77
78
79
80
81 private ThemisPackage thePackage;
82
83
84
85
86 private String thePrefix;
87
88
89
90
91
92
93
94 ThemisUISourcePackageSelect(final TethysUIFactory<?> pFactory,
95 final ThemisUISourceFileSelect pFileSelect) {
96
97 theFileSelect = pFileSelect;
98
99
100 theEventManager = new OceanusEventManager<>();
101
102
103 final TethysUIControlFactory myControls = pFactory.controlFactory();
104 thePrefixLabel = myControls.newLabel();
105 final TethysUILabel myPromptLabel = myControls.newLabel(ThemisUIResource.PROMPT_PACKAGE.getValue());
106
107
108 theButton = pFactory.buttonFactory().newScrollButton(ThemisPackage.class);
109 thePackageMenu = theButton.getMenu();
110
111
112 final TethysUIPaneFactory myPanes = pFactory.paneFactory();
113 thePanel = myPanes.newHBoxPane();
114 thePanel.addNode(myPromptLabel);
115 thePanel.addNode(thePrefixLabel);
116 thePanel.addNode(theButton);
117
118
119 final OceanusEventRegistrar<TethysUIEvent> myRegistrar = theButton.getEventRegistrar();
120 myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handleNewPackage());
121 theButton.setMenuConfigurator(e -> buildPackageMenu());
122 }
123
124 @Override
125 public OceanusEventRegistrar<TethysUIEvent> getEventRegistrar() {
126 return theEventManager.getEventRegistrar();
127 }
128
129 @Override
130 public TethysUIComponent getUnderlying() {
131 return thePanel;
132 }
133
134
135
136
137
138
139 ThemisPackage getCurrentPackage() {
140 return thePackage;
141 }
142
143
144
145
146
147
148 void setCurrentModule(final ThemisModule pModule) {
149
150 theModule = pModule;
151
152
153 determinePrefix();
154
155
156 final ThemisPackage myPackage = getDefaultPackage();
157 theButton.setValue(myPackage, getDisplayName(myPackage));
158 handleNewPackage();
159 }
160
161
162
163
164
165
166 private ThemisPackage getDefaultPackage() {
167
168 if (theModule != null) {
169
170 for (ThemisPackage myPackage : theModule.getPackages()) {
171
172 if (skipPackage(myPackage)) {
173 continue;
174 }
175
176
177 return myPackage;
178 }
179 }
180
181
182 return null;
183 }
184
185
186
187
188 private void determinePrefix() {
189
190 thePrefix = null;
191 theButton.setVisible(false);
192
193
194 if (theModule != null) {
195
196 for (ThemisPackage myPackage : theModule.getPackages()) {
197
198 adjustPrefix(myPackage);
199 }
200 }
201
202
203 thePrefixLabel.setText(thePrefix);
204 thePrefixLabel.setVisible(thePrefix != null && !thePrefix.isEmpty());
205 }
206
207
208
209
210
211
212 private void adjustPrefix(final ThemisPackage pPackage) {
213
214 if (skipPackage(pPackage)) {
215 return;
216 }
217
218
219 final String myName = pPackage.getPackage();
220 if (thePrefix == null) {
221 thePrefix = myName;
222
223
224 } else {
225
226 theButton.setVisible(true);
227
228
229 thePrefix = getCommonPrefix(myName, thePrefix);
230 }
231 }
232
233
234
235
236
237
238
239
240 private String getCommonPrefix(final String pFirst,
241 final String pSecond) {
242 if (pFirst.equals(pSecond)) {
243 return pFirst;
244 }
245 return pFirst.length() >= pSecond.length()
246 ? getCommonPrefix(getParentName(pFirst), pSecond)
247 : getCommonPrefix(pFirst, getParentName(pSecond));
248 }
249
250
251
252
253
254
255
256 private String getParentName(final String pName) {
257
258 final int iIndex = pName.lastIndexOf(ThemisChar.PERIOD);
259 return iIndex == -1 ? "" : pName.substring(0, iIndex);
260 }
261
262
263
264
265
266
267
268 private String getDisplayName(final ThemisPackage pPackage) {
269 final int myPrefixLen = thePrefix == null ? 0 : thePrefix.length();
270 return pPackage == null ? "" : pPackage.getPackage().substring(myPrefixLen);
271 }
272
273
274
275
276 private void buildPackageMenu() {
277
278 thePackageMenu.removeAllItems();
279
280
281 TethysUIScrollItem<ThemisPackage> myActive = null;
282 final ThemisPackage myCurr = theButton.getValue();
283
284
285 for (ThemisPackage myPackage : theModule.getPackages()) {
286
287 if (skipPackage(myPackage)) {
288 continue;
289 }
290
291
292 final TethysUIScrollItem<ThemisPackage> myItem = thePackageMenu.addItem(myPackage, getDisplayName(myPackage));
293
294
295 if (myPackage.equals(myCurr)) {
296
297 myActive = myItem;
298 }
299 }
300
301
302 if (myActive != null) {
303 myActive.scrollToItem();
304 }
305 }
306
307
308
309
310
311
312
313 private boolean skipPackage(final ThemisPackage pPackage) {
314
315 return pPackage.isPlaceHolder();
316 }
317
318
319
320
321 private void handleNewPackage() {
322
323 thePackage = theButton.getValue();
324 theEventManager.fireEvent(TethysUIEvent.NEWVALUE);
325 theFileSelect.setCurrentPackage(thePackage);
326 }
327 }