TDT4100-project/src/main/java/app/Main.java

103 lines
2.6 KiB
Java
Raw Normal View History

2021-02-12 22:25:41 +01:00
package app;
import java.io.IOException;
2021-04-21 19:01:51 +02:00
import java.util.Optional;
2021-02-19 16:56:30 +01:00
2021-02-12 22:25:41 +01:00
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
2021-02-18 15:18:01 +01:00
import javafx.scene.image.Image;
2021-02-12 22:25:41 +01:00
import javafx.stage.Stage;
2021-02-23 13:30:45 +01:00
import app.events.FileSaveStateChangedEvent;
2021-02-23 00:46:43 +01:00
import app.model.Model;
2021-04-26 22:46:55 +02:00
import app.service.DialogBoxes;
2021-04-20 17:37:50 +02:00
import app.settings.SettingsProvider;
2021-02-23 00:46:43 +01:00
2021-02-12 22:25:41 +01:00
public class Main extends Application {
2021-02-23 00:46:43 +01:00
private Scene scene;
private FXMLLoader fxmlLoader;
private Parent fxmlRoot;
private static final String TITLE = "Banana Editor";
private static final String ICON_PATH = "/graphics/logo.png";
2021-02-12 22:25:41 +01:00
/**
2021-02-23 00:46:43 +01:00
* Boilerplate function to launch the application.
2021-04-20 17:37:50 +02:00
*
2021-03-02 18:50:44 +01:00
* @param args Additional arguments from commandline
2021-02-12 22:25:41 +01:00
*/
public static void main(String[] args) {
launch(args);
}
/**
2021-02-23 00:46:43 +01:00
* Set up a window with title and icon.
2021-02-12 22:25:41 +01:00
*/
2021-02-23 00:46:43 +01:00
private void setupWindow(Stage window) {
window.setTitle(TITLE);
2021-04-15 16:17:46 +02:00
if (window.getIcons().isEmpty())
window.getIcons().add(new Image(getClass().getResourceAsStream(ICON_PATH)));
2021-02-23 00:46:43 +01:00
}
2021-02-18 15:18:01 +01:00
2021-03-02 02:05:50 +01:00
/**
2021-04-20 17:37:50 +02:00
* Loads all FXML documents of the main UI and initializes all correlated
* subcontrollers
*
2021-03-02 02:05:50 +01:00
* @throws IOException
*/
2021-04-26 22:46:55 +02:00
private void loadFXML() {
try {
this.fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/Main.fxml"));
this.fxmlRoot = fxmlLoader.load();
} catch (Exception e) {
DialogBoxes.showErrorMessage("There is an error within the program. Please try to reinstall.");
System.exit(1);
}
2021-02-23 00:46:43 +01:00
}
2021-02-12 22:25:41 +01:00
2021-03-02 02:05:50 +01:00
/**
* Generates a scene for the window, and adds it to {@link Model}
*/
2021-02-23 00:46:43 +01:00
private void createScene() {
this.scene = new Scene(fxmlRoot);
2021-04-15 16:17:46 +02:00
this.scene.setUserData(this.fxmlLoader);
2021-02-23 00:46:43 +01:00
Model.setScene(scene);
}
2021-02-17 21:17:02 +01:00
2021-02-23 00:46:43 +01:00
/**
* Set up default values and settings for the editor.
*/
private void setupDefaults() {
2021-02-24 14:22:14 +01:00
scene.getStylesheets().setAll("", "");
2021-02-23 00:46:43 +01:00
MainController mainController = fxmlLoader.getController();
2021-04-20 17:37:50 +02:00
SettingsProvider SP = new SettingsProvider(mainController.getEventBus());
SP.loadSettings();
2021-04-21 19:01:51 +02:00
Model.setActiveFilePath(Optional.empty());
Model.setProjectPath(Optional.empty());
2021-02-23 13:30:45 +01:00
mainController.getEventBus().post(new FileSaveStateChangedEvent(true));
2021-02-24 11:36:04 +01:00
mainController.setHostServices(getHostServices());
2021-02-23 00:46:43 +01:00
}
/**
* The entrypoint of the application.
2021-04-20 17:37:50 +02:00
*
2021-02-23 00:46:43 +01:00
* @param window The primary window of the application
*/
@Override
public void start(Stage window) throws IOException {
2021-02-20 12:33:56 +01:00
2021-02-23 00:46:43 +01:00
setupWindow(window);
loadFXML();
createScene();
setupDefaults();
2021-02-20 12:33:56 +01:00
2021-02-12 22:25:41 +01:00
window.setScene(scene);
window.show();
}
2021-02-19 16:56:30 +01:00
2021-02-12 22:25:41 +01:00
}