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