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