1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.metis.ui;
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.oceanus.logger.OceanusLogManager;
25 import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogger;
26 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceEvent;
27 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceManager;
28 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceResource;
29 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceSet;
30 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIComponent;
31 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
32 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIGenericWrapper;
33 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButton;
34 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIButtonFactory;
35 import io.github.tonywasher.joceanus.tethys.api.button.TethysUIScrollButtonManager;
36 import io.github.tonywasher.joceanus.tethys.api.control.TethysUILabel;
37 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
38 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollItem;
39 import io.github.tonywasher.joceanus.tethys.api.menu.TethysUIScrollMenu;
40 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBorderPaneManager;
41 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIBoxPaneManager;
42 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUICardPaneManager;
43 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIPaneFactory;
44 import io.github.tonywasher.joceanus.tethys.api.pane.TethysUIScrollPaneManager;
45
46 import java.util.ArrayList;
47 import java.util.List;
48
49
50
51
52 public class MetisPreferenceView
53 implements OceanusEventProvider<MetisPreferenceEvent>, TethysUIComponent {
54
55
56
57 private static final String NLS_OK = MetisPreferenceResource.UI_BUTTON_OK.getValue();
58
59
60
61
62 private static final String NLS_RESET = MetisPreferenceResource.UI_BUTTON_RESET.getValue();
63
64
65
66
67 private static final String NLS_SAVE = MetisPreferenceResource.UI_TITLE_SAVE.getValue();
68
69
70
71
72 private static final String NLS_SELECT = MetisPreferenceResource.UI_TITLE_SELECT.getValue();
73
74
75
76
77 private static final String NLS_SET = MetisPreferenceResource.UI_LABEL_SET.getValue();
78
79
80
81
82 private static final String ERROR_STORE = MetisPreferenceResource.UI_ERROR_STORE.getValue();
83
84
85
86
87 private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(MetisPreferenceView.class);
88
89
90
91
92 private final OceanusEventManager<MetisPreferenceEvent> theEventManager;
93
94
95
96
97 private final TethysUIFactory<?> theGuiFactory;
98
99
100
101
102 private final TethysUIBorderPaneManager thePane;
103
104
105
106
107 private final TethysUIScrollButtonManager<TethysUIGenericWrapper> theSelectButton;
108
109
110
111
112 private final TethysUIScrollMenu<TethysUIGenericWrapper> thePrefMenu;
113
114
115
116
117 private final TethysUICardPaneManager<MetisPreferenceSetView> theProperties;
118
119
120
121
122 private final TethysUIBoxPaneManager theButtons;
123
124
125
126
127 private final List<MetisPreferenceSetView> theViews;
128
129
130
131
132
133
134
135 public MetisPreferenceView(final TethysUIFactory<?> pFactory,
136 final MetisPreferenceManager pPreferenceMgr) {
137
138 theGuiFactory = pFactory;
139
140
141 theEventManager = new OceanusEventManager<>();
142
143
144 final TethysUIButtonFactory<?> myButtons = theGuiFactory.buttonFactory();
145 final TethysUIButton myOKButton = myButtons.newButton();
146 myOKButton.setTextOnly();
147 myOKButton.setText(NLS_OK);
148 final TethysUIButton myResetButton = myButtons.newButton();
149 myResetButton.setTextOnly();
150 myResetButton.setText(NLS_RESET);
151
152
153 myOKButton.getEventRegistrar().addEventListener(e -> saveUpdates());
154 myResetButton.getEventRegistrar().addEventListener(e -> resetUpdates());
155
156
157 final TethysUIPaneFactory myPanes = theGuiFactory.paneFactory();
158 theButtons = myPanes.newHBoxPane();
159 theButtons.setBorderTitle(NLS_SAVE);
160 theButtons.addSpacer();
161 theButtons.addNode(myOKButton);
162 theButtons.addSpacer();
163 theButtons.addNode(myResetButton);
164 theButtons.addSpacer();
165
166
167 theProperties = myPanes.newCardPane();
168
169
170 theViews = new ArrayList<>();
171
172
173 for (MetisPreferenceSet mySet : pPreferenceMgr.getPreferenceSets()) {
174
175 registerSet(mySet);
176 }
177
178
179 pPreferenceMgr.getEventRegistrar().addEventListener(this::handleNewPropertySet);
180
181
182 final TethysUILabel myLabel = theGuiFactory.controlFactory().newLabel(NLS_SET);
183 theSelectButton = myButtons.newScrollButton(TethysUIGenericWrapper.class);
184 thePrefMenu = theSelectButton.getMenu();
185
186
187 final TethysUIBoxPaneManager mySelection = myPanes.newHBoxPane();
188 mySelection.setBorderTitle(NLS_SELECT);
189
190
191 mySelection.addNode(myLabel);
192 mySelection.addNode(theSelectButton);
193 mySelection.addSpacer();
194
195
196 final OceanusEventRegistrar<TethysUIEvent> myRegistrar = theSelectButton.getEventRegistrar();
197 myRegistrar.addEventListener(TethysUIEvent.NEWVALUE, e -> handlePropertySetSelect());
198 theSelectButton.setMenuConfigurator(c -> buildPreferenceMenu());
199
200
201 final TethysUIScrollPaneManager myScrollPane = myPanes.newScrollPane();
202 myScrollPane.setContent(theProperties);
203
204
205 thePane = myPanes.newBorderPane();
206 thePane.setNorth(mySelection);
207 thePane.setCentre(myScrollPane);
208 thePane.setSouth(theButtons);
209
210
211 setSelectText();
212 setVisibility();
213 }
214
215 @Override
216 public TethysUIComponent getUnderlying() {
217 return thePane;
218 }
219
220 @Override
221 public OceanusEventRegistrar<MetisPreferenceEvent> getEventRegistrar() {
222 return theEventManager.getEventRegistrar();
223 }
224
225
226
227
228 private void handlePropertySetSelect() {
229 final MetisPreferenceSetView myView = (MetisPreferenceSetView) theSelectButton.getValue().getData();
230 theProperties.selectCard(myView.toString());
231 }
232
233
234
235
236
237
238 private void handleNewPropertySet(final OceanusEvent<MetisPreferenceEvent> pEvent) {
239
240 final MetisPreferenceSet mySet = pEvent.getDetails(MetisPreferenceSet.class);
241
242
243 registerSet(mySet);
244
245
246 setVisibility();
247 }
248
249
250
251
252
253
254 private void registerSet(final MetisPreferenceSet pSet) {
255
256 if (!pSet.isHidden()) {
257
258 final MetisPreferenceSetView myView = createView(theGuiFactory, pSet);
259
260
261 theProperties.addCard(myView.toString(), myView);
262 theViews.add(myView);
263
264
265 myView.getEventRegistrar().addEventListener(e -> {
266 setVisibility();
267 theEventManager.fireEvent(MetisPreferenceEvent.PREFCHANGED);
268 });
269 }
270 }
271
272
273
274
275
276
277
278
279 protected MetisPreferenceSetView createView(final TethysUIFactory<?> pFactory,
280 final MetisPreferenceSet pSet) {
281
282 return new MetisPreferenceSetView(pFactory, pSet);
283 }
284
285
286
287
288 public void determineFocus() {
289
290 final MetisPreferenceSetView myPanel = theProperties.getActiveCard();
291 if (myPanel != null) {
292 myPanel.determineFocus();
293 }
294 }
295
296
297
298
299
300
301 public boolean hasUpdates() {
302 final MetisPreferenceSetView myView = theProperties.getActiveCard();
303 return (myView != null)
304 && myView.hasChanges();
305 }
306
307
308
309
310
311
312 public boolean hasSession() {
313 return hasUpdates();
314 }
315
316
317
318
319 private void saveUpdates() {
320 try {
321 final MetisPreferenceSetView myView = theProperties.getActiveCard();
322 myView.storeChanges();
323 } catch (OceanusException e) {
324 LOGGER.error(ERROR_STORE, e);
325 }
326
327
328 setVisibility();
329
330
331 theEventManager.fireEvent(MetisPreferenceEvent.PREFCHANGED);
332 }
333
334
335
336
337 private void resetUpdates() {
338
339 final MetisPreferenceSetView myView = theProperties.getActiveCard();
340 myView.resetChanges();
341
342
343 setVisibility();
344
345
346 theEventManager.fireEvent(MetisPreferenceEvent.PREFCHANGED);
347 }
348
349
350
351
352 private void setVisibility() {
353
354 final MetisPreferenceSetView myView = theProperties.getActiveCard();
355 theSelectButton.setEnabled((myView != null)
356 && !myView.hasChanges());
357
358
359 theButtons.setVisible((myView != null)
360 && myView.hasChanges());
361 }
362
363
364
365
366 private void setSelectText() {
367
368 theSelectButton.setValue(new TethysUIGenericWrapper(theProperties.getActiveCard()));
369 }
370
371
372
373
374 private void buildPreferenceMenu() {
375
376 thePrefMenu.removeAllItems();
377
378
379 TethysUIScrollItem<?> myActive = null;
380 final String myActiveName = theProperties.getActiveName();
381
382
383 for (MetisPreferenceSetView myView : theViews) {
384
385 final TethysUIScrollItem<?> myItem = thePrefMenu.addItem(new TethysUIGenericWrapper(myView));
386
387
388 if (myView.toString().equals(myActiveName)) {
389
390 myActive = myItem;
391 }
392 }
393
394
395 if (myActive != null) {
396 myActive.scrollToItem();
397 }
398 }
399 }