I am working in JavaFX and I created util class which is responsible for creating new window. I called this class WindowUtil. Depending on the button that was clicked I want to:
Open new window
Send some data to controller of this window (this is optional)
The problem is that code responsible for opening a new window is always the same among overloaded methods so I'd like to somehow write this code only once.
Below I present these methods. Some of them are overloaded and some of them are not.
public static void loadWindow(String path, String appName) {
try {
Stage subWindow = new Stage();
subWindow.setResizable(false);
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(WindowUtil.class.getResource(path).openStream());
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void loadWindowAndSendData(String path, String appName, ConnectionData connectionData) {
try {
Stage subWindow = new Stage();
subWindow.setResizable(false);
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(WindowUtil.class.getResource(path).openStream());
ConnectionDataProvider controller = loader.getController();
controller.getConnectionData(connectionData);
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void loadWindowAndSendData(String path, String appName, ConnectionData connectionData, Data data,
OrderedItemsData orderedItemsList) {
try {
Stage subWindow = new Stage();
subWindow.setResizable(false);
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(WindowUtil.class.getResource(path).openStream());
ProcessingController processingController = (ProcessingController) loader.getController();
processingController.getData(connectionData, data, orderedItemsList);
Scene scene = new Scene(parent);
processingController.startConnection();
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void loadWindowAndSendData(String path, String appName, StoredItem rowData,
OrderedItemsData orderedItemsData) {
try {
Stage subWindow = new Stage();
subWindow.setResizable(false);
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(WindowUtil.class.getResource(path).openStream());
SpecifyQuantityController quantityController = (SpecifyQuantityController) loader.getController();
quantityController.getData(rowData, orderedItemsData);
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void loadWindowAndSendData(String path, String appName, Data data) {
try {
Stage subWindow = new Stage();
subWindow.setResizable(false);
subWindow.initModality(Modality.APPLICATION_MODAL);
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(WindowUtil.class.getResource(path).openStream());
DataProvider controller = loader.getController();
controller.getData(data);
Scene scene = new Scene(parent);
subWindow.setScene(scene);
subWindow.setTitle(appName);
subWindow.show();
} catch (IOException e) {
e.printStackTrace();
}
}
As you can see:
-first 5 lines of each method are always the same,
-last 4 lines of each method are also always the same.