|
1 | 1 | package io.github.gleidsonmt.dashboardfx; |
2 | 2 |
|
3 | | -import javafx.application.Platform; |
4 | 3 | import javafx.scene.Scene; |
| 4 | +import javafx.scene.control.Label; |
| 5 | +import javafx.scene.layout.StackPane; |
| 6 | +import javafx.stage.Stage; |
5 | 7 |
|
6 | 8 | import java.io.File; |
7 | 9 | import java.lang.reflect.InvocationTargetException; |
|
11 | 13 |
|
12 | 14 |
|
13 | 15 | /** |
| 16 | + * Used only in runtime as tool to help development. |
| 17 | + * |
14 | 18 | * @author Gleidson Neves da Silveira | gleidisonmt@gmail.com |
15 | 19 | * Create on 27/08/2025 |
16 | 20 | */ |
17 | | -public class LibrariesTools { |
| 21 | +public final class LibrariesTools { |
18 | 22 |
|
19 | | - public static void showScenicView(Scene scene) { |
20 | | - if (scene == null) return; |
| 23 | + private static Class<?> loadClassJar(String jarDirectory, String fullClass) { |
| 24 | + Class<?> cls = null; |
21 | 25 | try { |
22 | | - // ... inside a method or class |
23 | | - File jarFile = new File("./vendor/scenicview.jar"); |
24 | | - URL[] urls = { jarFile.toURI().toURL() }; |
| 26 | + File jarFile = new File(jarDirectory); |
| 27 | + if (!jarFile.exists()) return null; |
| 28 | + URL[] urls = {jarFile.toURI().toURL()}; |
25 | 29 | URLClassLoader classLoader = new URLClassLoader(urls, ClassLoader.getSystemClassLoader()); |
26 | | - String className = "org.scenicview.ScenicView"; // Fully qualified class name |
| 30 | + Thread.currentThread().setContextClassLoader(classLoader); |
| 31 | + cls = Class.forName(fullClass, true, classLoader); |
| 32 | + } catch (MalformedURLException | ClassNotFoundException _) { |
27 | 33 |
|
28 | | - Platform.runLater(() -> { |
29 | | - ClassLoader originalCtx = Thread.currentThread().getContextClassLoader(); |
30 | | - Thread.currentThread().setContextClassLoader(classLoader); |
31 | | - try { |
32 | | - Class<?> cls = Class.forName(className, true, classLoader); |
33 | | - cls.getMethod("show", Scene.class).invoke(null, scene); |
34 | | - } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | |
35 | | - ClassNotFoundException e) { |
36 | | - throw new RuntimeException(e); |
37 | | - } finally { |
38 | | - Thread.currentThread().setContextClassLoader(originalCtx); |
39 | | - } |
40 | | - }); |
41 | | - System.out.println("[ScenicView] Aberto com sucesso."); |
42 | | - } catch (MalformedURLException e) { |
43 | | - throw new RuntimeException(e); |
44 | 34 | } |
| 35 | + return cls; |
| 36 | + } |
| 37 | + |
| 38 | + public static void addTools(Scene scene) { |
| 39 | + showScenicView(scene); |
| 40 | + listenCss(scene); |
45 | 41 | } |
46 | 42 |
|
47 | | - public static void listenCss(Scene scene) { |
| 43 | + private static void showScenicView(Scene scene) throws RuntimeException { |
48 | 44 | if (scene == null) return; |
| 45 | + ClassLoader originalCtx = Thread.currentThread().getContextClassLoader(); |
| 46 | + Class<?> clazz = loadClassJar("./vendor/scenicview.jar", "org.scenicview.ScenicView"); |
| 47 | + if (clazz == null) return; |
| 48 | + |
49 | 49 | try { |
| 50 | + clazz.getMethod("show", Scene.class).invoke(null, scene); |
| 51 | + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException _) { |
50 | 52 |
|
51 | | - // ... inside a method or class |
52 | | - File jarFile = new File("./vendor/cssfx-11.5.1.jar"); |
53 | | - URL[] urls = {jarFile.toURI().toURL()}; |
54 | | - URLClassLoader classLoader = new URLClassLoader(urls, ClassLoader.getSystemClassLoader()); |
55 | | - String className = "fr.brouillard.oss.cssfx.CSSFX"; // Fully qualified class name |
56 | | - Class<?> cls = Class.forName(className, true, classLoader); |
| 53 | + } finally { |
| 54 | + Thread.currentThread().setContextClassLoader(originalCtx); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static void listenCss(Scene scene) { |
| 59 | + if (scene == null) return; |
| 60 | + Class<?> clazz = loadClassJar("./vendor/cssfx-11.5.1.jar", "fr.brouillard.oss.cssfx.CSSFX"); |
| 61 | + if (clazz == null) return; |
| 62 | + try { |
| 63 | + clazz.getMethod("start", Scene.class).invoke(null, scene); |
| 64 | + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException _) { |
57 | 65 |
|
58 | | - cls.getMethod("start", Scene.class).invoke(null, scene); |
59 | | - } catch (ReflectiveOperationException e) { |
60 | | - System.out.println("[ScenicView] Error on invoking ScenicView.show(Scene): " + e.getMessage()); |
61 | | - } catch (MalformedURLException e) { |
62 | | - throw new RuntimeException(e); |
63 | 66 | } |
64 | 67 | } |
65 | 68 |
|
|
0 commit comments