View Javadoc
1   /*
2    * Metis: Java Data Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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   * Metis Toolkit.
51   */
52  public class MetisToolkit {
53      /**
54       * Logger.
55       */
56      private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(MetisToolkit.class);
57  
58      /**
59       * Viewer Manager.
60       */
61      private final MetisViewerManager theViewerManager;
62  
63      /**
64       * Preference Manager.
65       */
66      private MetisPreferenceManager thePreferenceManager;
67  
68      /**
69       * GUI Factory.
70       */
71      private final TethysUIFactory<?> theGuiFactory;
72  
73      /**
74       * Thread Manager.
75       */
76      private final TethysUIThreadManager theThreadManager;
77  
78      /**
79       * The Profile Viewer Entry.
80       */
81      private final MetisViewerEntry theProfileEntry;
82  
83      /**
84       * The Error Viewer Entry.
85       */
86      private final MetisViewerEntry theErrorEntry;
87  
88      /**
89       * Colour Preferences.
90       */
91      private MetisColorPreferences theColorPreferences;
92  
93      /**
94       * The formatter.
95       */
96      private final OceanusDataFormatter theFormatter;
97  
98      /**
99       * Program Definition.
100      */
101     private final TethysUIProgram theProgram;
102 
103     /**
104      * Constructor.
105      *
106      * @param pFactory the GUI factory
107      * @throws OceanusException on error
108      */
109     public MetisToolkit(final TethysUIFactory<?> pFactory) throws OceanusException {
110         this(pFactory, true);
111     }
112 
113     /**
114      * Constructor.
115      *
116      * @param pFactory    the GUI factory
117      * @param pPreference creat preference manager
118      * @throws OceanusException on error
119      */
120     public MetisToolkit(final TethysUIFactory<?> pFactory,
121                         final boolean pPreference) throws OceanusException {
122         /* Store parameters */
123         theGuiFactory = pFactory;
124 
125         /* Store program definitions */
126         theProgram = theGuiFactory.getProgramDefinitions();
127 
128         /* Create the viewer */
129         theViewerManager = new MetisViewerManagerImpl();
130 
131         /* Access the profile entries */
132         theProfileEntry = theViewerManager.getStandardEntry(MetisViewerStandardEntry.PROFILE);
133         theErrorEntry = theViewerManager.getStandardEntry(MetisViewerStandardEntry.ERROR);
134 
135         /* Record the profile */
136         setProfile(theGuiFactory.getActiveProfile());
137 
138         /* Access and extend the formatter */
139         theFormatter = pFactory.getDataFormatter();
140         theFormatter.extendFormatter(new MetisDataFormatter(theFormatter));
141 
142         /* create the thread manager */
143         theThreadManager = theGuiFactory.threadFactory().newThreadManager();
144         attachToThreadManager();
145 
146         /* If we are setting up a MetisPreferenceManager */
147         if (pPreference) {
148             /* Create the preference manager */
149             thePreferenceManager = new MetisPreferenceManager(theViewerManager);
150 
151             /* Set up colors */
152             setUpColors(thePreferenceManager);
153         }
154     }
155 
156     /**
157      * Set up colors.
158      *
159      * @param pPreferenceMgr the preference manager
160      */
161     public void setUpColors(final MetisPreferenceManager pPreferenceMgr) {
162         /* Access the Colour Preferences */
163         thePreferenceManager = pPreferenceMgr;
164         theColorPreferences = pPreferenceMgr.getPreferenceSet(MetisColorPreferences.class);
165 
166         /* Process the colour preferences */
167         processColorPreferences();
168 
169         /* Create listener */
170         final OceanusEventRegistrar<MetisPreferenceEvent> myRegistrar = theColorPreferences.getEventRegistrar();
171         myRegistrar.addEventListener(e -> processColorPreferences());
172     }
173 
174     /**
175      * Obtain the Program Definitions.
176      *
177      * @return the definitions
178      */
179     public TethysUIProgram getProgramDefinitions() {
180         return theProgram;
181     }
182 
183     /**
184      * Obtain the Data Formatter.
185      *
186      * @return the formatter
187      */
188     public OceanusDataFormatter getFormatter() {
189         return theFormatter;
190     }
191 
192     /**
193      * Obtain the Viewer Manager.
194      *
195      * @return the viewer
196      */
197     public MetisViewerManager getViewerManager() {
198         return theViewerManager;
199     }
200 
201     /**
202      * Obtain the Preference Manager.
203      *
204      * @return the preferences
205      */
206     public MetisPreferenceManager getPreferenceManager() {
207         return thePreferenceManager;
208     }
209 
210     /**
211      * Obtain the GUI Factory.
212      *
213      * @return the factory
214      */
215     public TethysUIFactory<?> getGuiFactory() {
216         return theGuiFactory;
217     }
218 
219     /**
220      * Obtain the Thread Manager.
221      *
222      * @return the factory
223      */
224     public TethysUIThreadManager getThreadManager() {
225         return theThreadManager;
226     }
227 
228     /**
229      * Create a Help Window.
230      *
231      * @return the help Window
232      */
233     public MetisHelpWindow newHelpWindow() {
234         return new MetisHelpWindow(getGuiFactory());
235     }
236 
237     /**
238      * Create an ErrorPanel.
239      *
240      * @param pParent the parent viewer entry
241      * @return the error panel
242      */
243     public MetisErrorPanel newErrorPanel(final MetisViewerEntry pParent) {
244         return new MetisErrorPanel(theGuiFactory, theViewerManager, pParent);
245     }
246 
247     /**
248      * Create a Viewer Window.
249      *
250      * @return the viewer Window
251      * @throws OceanusException on error
252      */
253     public MetisViewerWindow newViewerWindow() throws OceanusException {
254         return new MetisViewerWindowImpl(getGuiFactory(), theViewerManager);
255     }
256 
257     /**
258      * Create a new Preference View.
259      *
260      * @return the view
261      */
262     public MetisPreferenceView newPreferenceView() {
263         return new MetisPreferenceView(getGuiFactory(), thePreferenceManager);
264     }
265 
266     /**
267      * Process Colour preferences.
268      */
269     private void processColorPreferences() {
270         /* Update the value Set with the preferences */
271         final TethysUIFactory<?> myFactory = getGuiFactory();
272         final TethysUIValueSet myValueSet = myFactory.getValueSet();
273         theColorPreferences.updateValueSet(myValueSet);
274     }
275 
276     /**
277      * Set profile.
278      *
279      * @param pProfile the profile
280      */
281     private void setProfile(final OceanusProfile pProfile) {
282         /* Update the Profile Viewer entry */
283         theProfileEntry.setObject(pProfile);
284     }
285 
286     /**
287      * Create new profile.
288      *
289      * @param pTask the name of the task
290      * @return the new profile
291      */
292     public OceanusProfile getNewProfile(final String pTask) {
293         /* Create a new profile */
294         final OceanusProfile myProfile = theGuiFactory.getNewProfile(pTask);
295         setProfile(myProfile);
296 
297         /* Return the new profile */
298         return myProfile;
299     }
300 
301     /**
302      * Obtain the active profile.
303      *
304      * @return the active profile
305      */
306     public OceanusProfile getActiveProfile() {
307         return theGuiFactory.getActiveProfile();
308     }
309 
310     /**
311      * Obtain the active task.
312      *
313      * @return the active task
314      */
315     public OceanusProfile getActiveTask() {
316         return theGuiFactory.getActiveTask();
317     }
318 
319     /**
320      * Delete a file on error exit.
321      *
322      * @param pFile the file to delete
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      * Attach to threadManager.
335      */
336     private void attachToThreadManager() {
337         /* Access the event registrar */
338         final OceanusEventRegistrar<TethysUIThreadEvent> myRegistrar = theThreadManager.getEventRegistrar();
339 
340         /* Add Thread start support */
341         myRegistrar.addEventListener(TethysUIThreadEvent.THREADSTART, e -> {
342             /* Tasks for event handler */
343             theErrorEntry.setObject(null);
344             theErrorEntry.setVisible(false);
345         });
346 
347         /* Add Thread end support */
348         myRegistrar.addEventListener(TethysUIThreadEvent.THREADEND, e -> {
349             /* Tasks for event handler */
350             theProfileEntry.setObject(theThreadManager.getActiveProfile());
351             theProfileEntry.setFocus();
352         });
353 
354         /* Add Thread error support */
355         myRegistrar.addEventListener(TethysUIThreadEvent.THREADERROR, e -> {
356             /* Tasks for event handler */
357             theErrorEntry.setObject(e.getDetails());
358             theErrorEntry.setVisible(true);
359             theErrorEntry.setFocus();
360         });
361     }
362 }