1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.metis.toolkit;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
21 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
22 import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogManager;
23 import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogger;
24 import io.github.tonywasher.joceanus.oceanus.profile.OceanusProfile;
25 import io.github.tonywasher.joceanus.metis.data.MetisDataFormatter;
26 import io.github.tonywasher.joceanus.metis.help.MetisHelpWindow;
27 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceEvent;
28 import io.github.tonywasher.joceanus.metis.preference.MetisPreferenceManager;
29 import io.github.tonywasher.joceanus.metis.ui.MetisErrorPanel;
30 import io.github.tonywasher.joceanus.metis.ui.MetisFieldColours.MetisColorPreferences;
31 import io.github.tonywasher.joceanus.metis.ui.MetisPreferenceView;
32 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerEntry;
33 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerManager;
34 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerStandardEntry;
35 import io.github.tonywasher.joceanus.metis.viewer.MetisViewerWindow;
36 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIProgram;
37 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIValueSet;
38 import io.github.tonywasher.joceanus.tethys.api.factory.TethysUIFactory;
39 import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadEvent;
40 import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadManager;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46
47
48
49
50 public class MetisToolkit {
51
52
53
54 private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(MetisToolkit.class);
55
56
57
58
59 private final MetisViewerManager theViewerManager;
60
61
62
63
64 private MetisPreferenceManager thePreferenceManager;
65
66
67
68
69 private final TethysUIFactory<?> theGuiFactory;
70
71
72
73
74 private final TethysUIThreadManager theThreadManager;
75
76
77
78
79 private final MetisViewerEntry theProfileEntry;
80
81
82
83
84 private final MetisViewerEntry theErrorEntry;
85
86
87
88
89 private MetisColorPreferences theColorPreferences;
90
91
92
93
94 private final OceanusDataFormatter theFormatter;
95
96
97
98
99 private final TethysUIProgram theProgram;
100
101
102
103
104
105
106
107 public MetisToolkit(final TethysUIFactory<?> pFactory) throws OceanusException {
108 this(pFactory, true);
109 }
110
111
112
113
114
115
116
117
118 public MetisToolkit(final TethysUIFactory<?> pFactory,
119 final boolean pPreference) throws OceanusException {
120
121 theGuiFactory = pFactory;
122
123
124 theProgram = theGuiFactory.getProgramDefinitions();
125
126
127 theViewerManager = new MetisViewerManager();
128
129
130 theProfileEntry = theViewerManager.getStandardEntry(MetisViewerStandardEntry.PROFILE);
131 theErrorEntry = theViewerManager.getStandardEntry(MetisViewerStandardEntry.ERROR);
132
133
134 setProfile(theGuiFactory.getActiveProfile());
135
136
137 theFormatter = pFactory.getDataFormatter();
138 theFormatter.extendFormatter(new MetisDataFormatter(theFormatter));
139
140
141 theThreadManager = theGuiFactory.threadFactory().newThreadManager();
142 attachToThreadManager();
143
144
145 if (pPreference) {
146
147 thePreferenceManager = new MetisPreferenceManager(theViewerManager);
148
149
150 setUpColors(thePreferenceManager);
151 }
152 }
153
154
155
156
157
158
159 public void setUpColors(final MetisPreferenceManager pPreferenceMgr) {
160
161 thePreferenceManager = pPreferenceMgr;
162 theColorPreferences = pPreferenceMgr.getPreferenceSet(MetisColorPreferences.class);
163
164
165 processColorPreferences();
166
167
168 final OceanusEventRegistrar<MetisPreferenceEvent> myRegistrar = theColorPreferences.getEventRegistrar();
169 myRegistrar.addEventListener(e -> processColorPreferences());
170 }
171
172
173
174
175
176
177 public TethysUIProgram getProgramDefinitions() {
178 return theProgram;
179 }
180
181
182
183
184
185
186 public OceanusDataFormatter getFormatter() {
187 return theFormatter;
188 }
189
190
191
192
193
194
195 public MetisViewerManager getViewerManager() {
196 return theViewerManager;
197 }
198
199
200
201
202
203
204 public MetisPreferenceManager getPreferenceManager() {
205 return thePreferenceManager;
206 }
207
208
209
210
211
212
213 public TethysUIFactory<?> getGuiFactory() {
214 return theGuiFactory;
215 }
216
217
218
219
220
221
222 public TethysUIThreadManager getThreadManager() {
223 return theThreadManager;
224 }
225
226
227
228
229
230
231 public MetisHelpWindow newHelpWindow() {
232 return new MetisHelpWindow(getGuiFactory());
233 }
234
235
236
237
238
239
240
241 public MetisErrorPanel newErrorPanel(final MetisViewerEntry pParent) {
242 return new MetisErrorPanel(theGuiFactory, theViewerManager, pParent);
243 }
244
245
246
247
248
249
250
251 public MetisViewerWindow newViewerWindow() throws OceanusException {
252 return new MetisViewerWindow(getGuiFactory(), theViewerManager);
253 }
254
255
256
257
258
259
260 public MetisPreferenceView newPreferenceView() {
261 return new MetisPreferenceView(getGuiFactory(), thePreferenceManager);
262 }
263
264
265
266
267 private void processColorPreferences() {
268
269 final TethysUIFactory<?> myFactory = getGuiFactory();
270 final TethysUIValueSet myValueSet = myFactory.getValueSet();
271 theColorPreferences.updateValueSet(myValueSet);
272 }
273
274
275
276
277
278
279 private void setProfile(final OceanusProfile pProfile) {
280
281 theProfileEntry.setObject(pProfile);
282 }
283
284
285
286
287
288
289
290 public OceanusProfile getNewProfile(final String pTask) {
291
292 final OceanusProfile myProfile = theGuiFactory.getNewProfile(pTask);
293 setProfile(myProfile);
294
295
296 return myProfile;
297 }
298
299
300
301
302
303
304 public OceanusProfile getActiveProfile() {
305 return theGuiFactory.getActiveProfile();
306 }
307
308
309
310
311
312
313 public OceanusProfile getActiveTask() {
314 return theGuiFactory.getActiveTask();
315 }
316
317
318
319
320
321
322 public static void cleanUpFile(final File pFile) {
323 try {
324 final Path myPath = pFile.toPath();
325 Files.delete(myPath);
326 } catch (IOException e) {
327 LOGGER.error("Failed to delete File", e);
328 }
329 }
330
331
332
333
334 private void attachToThreadManager() {
335
336 final OceanusEventRegistrar<TethysUIThreadEvent> myRegistrar = theThreadManager.getEventRegistrar();
337
338
339 myRegistrar.addEventListener(TethysUIThreadEvent.THREADSTART, e -> {
340
341 theErrorEntry.setObject(null);
342 theErrorEntry.setVisible(false);
343 });
344
345
346 myRegistrar.addEventListener(TethysUIThreadEvent.THREADEND, e -> {
347
348 theProfileEntry.setObject(theThreadManager.getActiveProfile());
349 theProfileEntry.setFocus();
350 });
351
352
353 myRegistrar.addEventListener(TethysUIThreadEvent.THREADERROR, e -> {
354
355 theErrorEntry.setObject(e.getDetails());
356 theErrorEntry.setVisible(true);
357 theErrorEntry.setFocus();
358 });
359 }
360 }