Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** === USAGE EXAMPLE IN MAIN CLASS ===
- package me.rehabcz.testfx;
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- import me.rehabcz.testfx.base.SceneLoader;
- public class Main extends Application {
- public static SceneLoader sceneLoader;
- @Override
- public void start(Stage stage) {
- Scene scene = sceneLoader.load("example"); // <-- Scene identifier as directory name
- stage.setTitle("Hello!");
- stage.setScene(scene);
- stage.setHeight(500);
- stage.setWidth(800);
- stage.setResizable(false);
- stage.show();
- }
- public static void main(String[] args) {
- sceneLoader = new SceneLoader(Main.class); // <-- Retrieves Main class namespace for resource discovery
- sceneLoader.setRoot("stages"); // <-- Root folder in resources
- launch();
- }
- }
- */
- /** === EXAMPLE FOLDER STRUCTURE OF RESOURCES ===
- - me.rehabcz.textfx
- - stages <-- Referenced by setRoot() method
- - example <-- Scene directory (directory name is identifier of stage)
- - *.fxml <-- First fxml get's registered (other fxml files ignored)
- - *.css <-- All css files are loaded
- */
- package me.rehabcz.testfx.base;
- import io.github.classgraph.ClassGraph;
- import io.github.classgraph.ScanResult;
- import javafx.application.Application;
- import javafx.fxml.FXMLLoader;
- import javafx.scene.Scene;
- import java.io.IOException;
- import java.net.URL;
- import java.util.*;
- /**
- * Utility class for automation scenes lookup for JavaFX
- * it uses ClassGraph library under the hood
- * @author rehabcz
- * @version 1.0
- */
- public class SceneLoader {
- /**
- * Provided namespace for referencing resources
- */
- protected Class<? extends Application> namespace;
- /**
- * Root folder name that contains scenes directories
- */
- protected String root;
- public SceneLoader(Class<? extends Application> namespace) {
- this.namespace = namespace;
- }
- public SceneLoader() {
- this.namespace = null;
- }
- /**
- * Sets root folder for definitions of scenes inside resources
- * @param directory Scenes resource folder name
- */
- public void setRoot(String directory) {
- this.root = directory;
- }
- private String namespace() {
- String base = this.namespace.getPackageName();
- if (this.root != null)
- base = base + '.' + this.root;
- return base;
- }
- public Scene load(String directory){
- return prebuildScene(this.namespace() + '.' + directory);
- }
- public Scene load(Class<? extends Application> namespace, String directory) {
- return prebuildScene(namespace.getPackageName() + '.' + directory);
- }
- private Scene prebuildScene(String _package) {
- ArrayList<URL> _files = new ArrayList<>();
- try {
- ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages(_package).scan();
- _files = (ArrayList<URL>) scanResult.getAllResources().getURLs();
- scanResult.close();
- }
- catch (Exception ignored) {}
- return prebuildScene(_files);
- }
- private Scene prebuildScene(ArrayList<URL> files) {
- try {
- FXMLLoader loader = null;
- ArrayList<String> stylesheets = new ArrayList<>();
- for (URL file: files) {
- if (com.google.common.io.Files.getFileExtension(file.getFile()).equals("fxml")) {
- loader = new FXMLLoader(file);
- }
- if (com.google.common.io.Files.getFileExtension(file.getFile()).equals("css")) {
- stylesheets.add(file.toExternalForm());
- }
- }
- if (loader != null) {
- Scene scene = new Scene(loader.load());
- for (String css : stylesheets) {
- scene.getStylesheets().add(css);
- }
- return scene;
- }
- } catch (IOException ignored) {}
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement