Advertisement
RehabCZ

JavaFX Scene loader

Sep 26th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.10 KB | Source Code | 0 0
  1. /** === USAGE EXAMPLE IN MAIN CLASS ===
  2. package me.rehabcz.testfx;
  3.  
  4. import javafx.application.Application;
  5. import javafx.scene.Scene;
  6. import javafx.stage.Stage;
  7. import me.rehabcz.testfx.base.SceneLoader;
  8.  
  9. public class Main extends Application {
  10.  
  11.     public static SceneLoader sceneLoader;
  12.  
  13.     @Override
  14.     public void start(Stage stage) {
  15.         Scene scene = sceneLoader.load("example"); // <-- Scene identifier as directory name
  16.         stage.setTitle("Hello!");
  17.         stage.setScene(scene);
  18.         stage.setHeight(500);
  19.         stage.setWidth(800);
  20.         stage.setResizable(false);
  21.         stage.show();
  22.     }
  23.  
  24.     public static void main(String[] args) {
  25.         sceneLoader = new SceneLoader(Main.class); // <-- Retrieves Main class namespace for resource discovery
  26.         sceneLoader.setRoot("stages"); // <-- Root folder in resources
  27.         launch();
  28.     }
  29. }
  30. */
  31.  
  32. /** === EXAMPLE FOLDER STRUCTURE OF RESOURCES ===
  33.     - me.rehabcz.textfx
  34.         - stages <-- Referenced by setRoot() method
  35.             - example <-- Scene directory (directory name is identifier of stage)
  36.                 - *.fxml <-- First fxml get's registered (other fxml files ignored)
  37.                 - *.css  <-- All css files are loaded
  38. */
  39.  
  40. package me.rehabcz.testfx.base;
  41.  
  42. import io.github.classgraph.ClassGraph;
  43. import io.github.classgraph.ScanResult;
  44. import javafx.application.Application;
  45. import javafx.fxml.FXMLLoader;
  46. import javafx.scene.Scene;
  47.  
  48. import java.io.IOException;
  49. import java.net.URL;
  50. import java.util.*;
  51.  
  52. /**
  53.  * Utility class for automation scenes lookup for JavaFX
  54.  * it uses ClassGraph library under the hood
  55.  * @author rehabcz
  56.  * @version 1.0
  57.  */
  58. public class SceneLoader {
  59.  
  60.     /**
  61.      * Provided namespace for referencing resources
  62.      */
  63.     protected Class<? extends Application> namespace;
  64.  
  65.     /**
  66.      * Root folder name that contains scenes directories
  67.      */
  68.     protected String root;
  69.  
  70.     public SceneLoader(Class<? extends Application> namespace) {
  71.         this.namespace = namespace;
  72.     }
  73.  
  74.     public SceneLoader() {
  75.         this.namespace = null;
  76.     }
  77.  
  78.     /**
  79.      * Sets root folder for definitions of scenes inside resources
  80.      * @param directory Scenes resource folder name
  81.      */
  82.     public void setRoot(String directory) {
  83.         this.root = directory;
  84.     }
  85.  
  86.     private String namespace() {
  87.         String base = this.namespace.getPackageName();
  88.         if (this.root != null)
  89.             base = base + '.' + this.root;
  90.         return base;
  91.     }
  92.  
  93.     public Scene load(String directory){
  94.         return prebuildScene(this.namespace() + '.' + directory);
  95.     }
  96.  
  97.     public Scene load(Class<? extends Application> namespace, String directory) {
  98.         return prebuildScene(namespace.getPackageName() + '.' + directory);
  99.     }
  100.  
  101.     private Scene prebuildScene(String _package) {
  102.         ArrayList<URL> _files = new ArrayList<>();
  103.         try {
  104.             ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages(_package).scan();
  105.             _files = (ArrayList<URL>) scanResult.getAllResources().getURLs();
  106.             scanResult.close();
  107.         }
  108.         catch (Exception ignored) {}
  109.         return prebuildScene(_files);
  110.     }
  111.  
  112.     private Scene prebuildScene(ArrayList<URL> files) {
  113.         try {
  114.             FXMLLoader loader = null;
  115.             ArrayList<String> stylesheets = new ArrayList<>();
  116.             for (URL file: files) {
  117.  
  118.                 if (com.google.common.io.Files.getFileExtension(file.getFile()).equals("fxml")) {
  119.                     loader = new FXMLLoader(file);
  120.                 }
  121.                 if (com.google.common.io.Files.getFileExtension(file.getFile()).equals("css")) {
  122.                     stylesheets.add(file.toExternalForm());
  123.                 }
  124.             }
  125.  
  126.             if (loader != null) {
  127.                 Scene scene = new Scene(loader.load());
  128.                 for (String css : stylesheets) {
  129.                     scene.getStylesheets().add(css);
  130.                 }
  131.                 return scene;
  132.             }
  133.         } catch (IOException ignored) {}
  134.         return null;
  135.     }
  136.  
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement