Created
March 23, 2020 12:51
-
-
Save jacoblockard99/d2d15a541a4e80dcee0b3f1360a827a0 to your computer and use it in GitHub Desktop.
A utility class that quickly loads FXML files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.jacoblockard.blogger.util; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import java.io.IOException; | |
import java.net.URL; | |
/** | |
* Contains methods for conveniently loading FXML files. Note that this class is designed for very specific use cases; | |
* for any kind of complex FXML loading functionality, the JavaFX FXMLLoader should be used instead. | |
* | |
* @version 0.1.1 | |
* @author Jacob Lockard | |
*/ | |
public class FXMLQuickLoader { | |
/** | |
* Loads the FXML file at the given location, potentially with the given controller, controller factory, and root. | |
* | |
* @param location A URL object representing the location of the FXML file. | |
* @param controller An Object representing the controller to associate with the loaded file, or null if no | |
* controller is desired. | |
* @param root An Object representing the root to associate with the loaded file, or null if no root is desired. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent load(URL location, Object controller, Object root) throws IOException { | |
FXMLLoader loader; | |
// Initialize the FXMLLoader. | |
loader = new FXMLLoader(); | |
// Set its location. | |
loader.setLocation(location); | |
// Check if the controller exists. | |
if (controller != null) { | |
// If it does, set the loader's controller. | |
loader.setController(controller); | |
} | |
// Check if the root exists. | |
if (root != null) { | |
// If it does, set the loader's root. | |
loader.setRoot(root); | |
} | |
// Load the FXML file. | |
return loader.load(); | |
} | |
/** | |
* Loads the FXML file at the given file path, potentially with the given controller, controller factory, and root. | |
* | |
* @param locationPath A String object representing the file path to the FXML file. | |
* @param controller An Object representing the controller to associate with the loaded file, or null if no | |
* controller is desired. | |
* @param root An Object representing the root to associate with the loaded file, or null if no root is desired. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent load(String locationPath, Object controller, Object root) throws IOException { | |
return load(FXMLQuickLoader.class.getResource(locationPath), controller, root); | |
} | |
/** | |
* Loads the FXML file at the given location, potentially with the given controller. | |
* | |
* @param location A URL object representing the location of the FXML file. | |
* @param controller An Object representing the controller to associate with the loaded file, or null if no | |
* controller is desired. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent load(URL location, Object controller) throws IOException { | |
return load(location, controller, null); | |
} | |
/** | |
* Loads the FXML file at the given file path, potentially with the given controller. | |
* | |
* @param locationPath A String object representing the file path to the FXML file. | |
* @param controller An Object representing the controller to associate with the loaded file, or null if no | |
* controller is desired. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent load(String locationPath, Object controller) throws IOException { | |
return load(FXMLQuickLoader.class.getResource(locationPath), controller); | |
} | |
/** | |
* Loads the FXML file at the given location with no controller. | |
* | |
* @param location A URL object representing the location of the FXML file. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent load(URL location) throws IOException { | |
return load(location, null); | |
} | |
/** | |
* Loads the FXML file at the given file path with no controller. | |
* | |
* @param locationPath A String object representing the file path to the FXML file. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent load(String locationPath) throws IOException { | |
return load(locationPath, new Object()); | |
} | |
/** | |
* Loads the FXML file at the given location into the given control. | |
* | |
* @param control A Parent object representing the control to load the FXML file into. | |
* @param location A URL object representing the location of the FXML file. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent loadToControl(Parent control, URL location) throws IOException { | |
return load(location, control, control); | |
} | |
/** | |
* Loads the FXML file at the given file path into the given control. | |
* | |
* @param control A Parent object representing the control to load the FXML file into. | |
* @param locationPath A String object representing the file path to the FXML file. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent loadToControl(Parent control, String locationPath) throws IOException { | |
return loadToControl(control, FXMLQuickLoader.class.getResource(locationPath)); | |
} | |
/** | |
* Loads the FXML file at the default file path into the given control. This method assumes that the FXML file has | |
* a name equal to the simple name of the control's class, is a resource, and is contained in a directory called | |
* "fxml". | |
* | |
* @param control An Object that extends Parent representing the control to load the FXML file into. | |
* @return a Parent object representing the result of loading the FXML file. | |
*/ | |
public static Parent loadToControl(Parent control) throws IOException { | |
return loadToControl(control, "/fxml/" + fxmlName(control.getClass()) + ".fxml"); | |
} | |
/** | |
* Gets the name of an FXML file for the given class. | |
* | |
* @param type The class to create an FXML name for. | |
* @return a String object representing the name of the FXML file. | |
*/ | |
private static String fxmlName(Class type) { | |
return type.getSimpleName() | |
.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2") | |
.replaceAll("([a-z])([A-Z])", "$1_$2") | |
.toLowerCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment