1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.metis.help;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.event.OceanusEvent;
21 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
22 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
23 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
24 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
25 import io.github.tonywasher.joceanus.tethys.api.control.TethysUIHTMLManager;
26 import io.github.tonywasher.joceanus.tethys.api.control.TethysUIHTMLManager.TethysUIStyleSheetId;
27 import io.github.tonywasher.joceanus.tethys.api.control.TethysUISplitTreeManager;
28 import io.github.tonywasher.joceanus.tethys.api.control.TethysUITreeManager;
29 import io.github.tonywasher.joceanus.tethys.api.control.TethysUITreeManager.TethysUITreeItem;
30 import io.github.tonywasher.joceanus.tethys.api.dialog.TethysUIChildDialog;
31 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
32 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBorderPaneManager;
33
34 import java.util.List;
35
36
37
38
39 public class MetisHelpWindow
40 implements OceanusEventProvider<TethysUIEvent> {
41
42
43
44 protected static final int WINDOW_WIDTH = 900;
45
46
47
48
49 protected static final int WINDOW_HEIGHT = 600;
50
51
52
53
54 private final TethysUIFactory<?> theFactory;
55
56
57
58
59 private final OceanusEventManager<TethysUIEvent> theEventManager;
60
61
62
63
64 private final TethysUISplitTreeManager<MetisHelpEntry> theSplitTree;
65
66
67
68
69 private final TethysUITreeManager<MetisHelpEntry> theTree;
70
71
72
73
74 private TethysUIChildDialog theDialog;
75
76
77
78
79 private final TethysUIHTMLManager theHtml;
80
81
82
83
84
85
86 public MetisHelpWindow(final TethysUIFactory<?> pFactory) {
87
88 theFactory = pFactory;
89
90
91 theEventManager = new OceanusEventManager<>();
92
93
94 theSplitTree = pFactory.controlFactory().newSplitTreeManager();
95 theTree = theSplitTree.getTreeManager();
96 theHtml = theSplitTree.getHTMLManager();
97
98
99 theSplitTree.getEventRegistrar().addEventListener(this::handleSplitTreeAction);
100 }
101
102 @Override
103 public OceanusEventRegistrar<TethysUIEvent> getEventRegistrar() {
104 return theEventManager.getEventRegistrar();
105 }
106
107
108
109
110
111
112 public TethysUISplitTreeManager<MetisHelpEntry> getSplitTreeManager() {
113 return theSplitTree;
114 }
115
116
117
118
119
120
121 public TethysUITreeManager<MetisHelpEntry> getTreeManager() {
122 return theTree;
123 }
124
125
126
127
128
129
130 public TethysUIHTMLManager getHTMLManager() {
131 return theHtml;
132 }
133
134
135
136
137
138
139
140 protected void fireEvent(final TethysUIEvent pEventId,
141 final Object pValue) {
142 theEventManager.fireEvent(pEventId, pValue);
143 }
144
145
146
147
148 public void showDialog() {
149
150 if (theDialog == null) {
151
152 theDialog = theFactory.dialogFactory().newChildDialog();
153 theDialog.setTitle(MetisHelpResource.TITLE.getValue());
154
155
156 final TethysUIBorderPaneManager myPanel = theFactory.paneFactory().newBorderPane();
157 myPanel.setCentre(theSplitTree);
158 myPanel.setPreferredWidth(WINDOW_WIDTH);
159 myPanel.setPreferredHeight(WINDOW_HEIGHT);
160 theDialog.setContent(myPanel);
161
162
163 theDialog.getEventRegistrar().addEventListener(TethysUIEvent.WINDOWCLOSED, e -> {
164 theTree.setVisible(false);
165 fireEvent(TethysUIEvent.WINDOWCLOSED, null);
166 });
167 }
168
169
170 if (!theDialog.isShowing()) {
171
172 theTree.setVisible(true);
173 theDialog.showDialog();
174 }
175 }
176
177
178
179
180 public void hideDialog() {
181
182 if (theDialog != null
183 && theDialog.isShowing()) {
184
185 theDialog.hideDialog();
186 }
187 }
188
189
190
191
192 public void closeWindow() {
193 hideDialog();
194 if (theDialog != null) {
195 theDialog.closeDialog();
196 }
197 }
198
199
200
201
202
203
204
205 public void setModule(final MetisHelpModule pModule) throws OceanusException {
206
207 final List<MetisHelpEntry> myEntries = pModule.getHelpEntries();
208
209
210 final TethysUIStyleSheetId myCSS = pModule.getCSS();
211 if (myCSS != null) {
212 theHtml.setCSSContent(myCSS);
213 }
214
215
216 createTree(pModule.getTitle(), myEntries);
217 }
218
219
220
221
222
223
224 protected void handleSplitTreeAction(final OceanusEvent<TethysUIEvent> pEvent) {
225 switch (pEvent.getEventId()) {
226 case NEWVALUE:
227 handleNewTreeItem(pEvent.getDetails(MetisHelpEntry.class));
228 break;
229 case BUILDPAGE:
230 default:
231 break;
232 }
233 }
234
235
236
237
238
239
240 private void handleNewTreeItem(final MetisHelpEntry pEntry) {
241 if (pEntry != null) {
242 final String myHtml = pEntry.getHtml();
243 if (myHtml != null) {
244 theHtml.setHTMLContent(myHtml, pEntry.getName());
245 }
246 }
247 }
248
249
250
251
252
253
254
255
256 private TethysUITreeItem<MetisHelpEntry> createTree(final String pTitle,
257 final List<MetisHelpEntry> pEntries) {
258
259 final TethysUITreeItem<MetisHelpEntry> myRoot = theTree.getRoot();
260 theTree.setRootName(pTitle);
261 theTree.setRootVisible();
262
263
264 myRoot.removeChildren();
265
266
267 addHelpEntries(myRoot, pEntries);
268
269
270 return myRoot;
271 }
272
273
274
275
276
277
278
279 private void addHelpEntries(final TethysUITreeItem<MetisHelpEntry> pParent,
280 final List<MetisHelpEntry> pEntries) {
281
282 for (MetisHelpEntry myEntry : pEntries) {
283
284 final TethysUITreeItem<MetisHelpEntry> myItem = theTree.addChildItem(pParent, myEntry.getName(), myEntry);
285
286
287 final List<MetisHelpEntry> myChildren = myEntry.getChildren();
288 if (myChildren != null) {
289
290 addHelpEntries(myItem, myChildren);
291 }
292 }
293 }
294 }