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.ThemisFile;
35 import io.github.tonywasher.joceanus.themis.parser.proj.ThemisPackage;
36
37 import java.util.List;
38
39
40
41
42 public class ThemisUISourceFileSelect
43 implements OceanusEventProvider<TethysUIEvent>, TethysUIComponent {
44
45
46
47 private final OceanusEventManager<TethysUIEvent> theEventManager;
48
49
50
51
52 private final TethysUIScrollButtonManager<ThemisFile> theButton;
53
54
55
56
57 private final TethysUIScrollMenu<ThemisFile> theFileMenu;
58
59
60
61
62 private final TethysUIBoxPaneManager thePanel;
63
64
65
66
67 private ThemisPackage thePackage;
68
69
70
71
72 private ThemisFile theFile;
73
74
75
76
77
78
79 ThemisUISourceFileSelect(final TethysUIFactory<?> pFactory) {
80
81 theEventManager = new OceanusEventManager<>();
82
83
84 final TethysUIControlFactory myControls = pFactory.controlFactory();
85 final TethysUILabel myPromptLabel = myControls.newLabel(ThemisUIResource.PROMPT_FILE.getValue());
86
87
88 theButton = pFactory.buttonFactory().newScrollButton(ThemisFile.class);
89 theFileMenu = 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 -> 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
115
116
117
118 ThemisFile getCurrentFile() {
119 return theFile;
120 }
121
122
123
124
125
126
127 void setCurrentPackage(final ThemisPackage pPackage) {
128
129 thePackage = pPackage;
130
131
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
140
141 private void buildFileMenu() {
142
143 theFileMenu.removeAllItems();
144
145
146 TethysUIScrollItem<ThemisFile> myActive = null;
147 final ThemisFile myCurr = theButton.getValue();
148
149
150 for (ThemisFile myFile : thePackage.getFiles()) {
151
152 final TethysUIScrollItem<ThemisFile> myItem = theFileMenu.addItem(myFile);
153
154
155 if (myFile.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 handleNewFile() {
171
172 theFile = theButton.getValue();
173 theEventManager.fireEvent(TethysUIEvent.NEWVALUE);
174 }
175 }