refactor: removed MainFXMLController from dependencies

Replaced MainFXMLController-based scene-changing with event-based scene-changing to remove cyclic dependency
This commit is contained in:
David Guler
2022-11-15 08:40:42 +01:00
parent 5ef3f6c587
commit 2b7cec7e6a
10 changed files with 110 additions and 40 deletions
@@ -1,21 +1,25 @@
package ch.zhaw.gartenverwaltung;
import ch.zhaw.gartenverwaltung.bootstrap.AfterInject;
import ch.zhaw.gartenverwaltung.bootstrap.AppLoader;
import ch.zhaw.gartenverwaltung.bootstrap.ChangeViewEvent;
import ch.zhaw.gartenverwaltung.bootstrap.Inject;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MainFXMLController implements Initializable {
public class MainFXMLController {
private static final Logger LOG = Logger.getLogger(MainFXMLController.class.getName());
private final AppLoader appLoader = new AppLoader(this);
@Inject
AppLoader appLoader;
@FXML
private Button home_button;
@@ -31,29 +35,26 @@ public class MainFXMLController implements Initializable {
@FXML
private Button plants_button;
public MainFXMLController() throws IOException {
}
@FXML
void goToHome() throws IOException {
void goToHome() {
showPaneAsMainView("Home.fxml");
styleChangeButton(home_button);
}
@FXML
void goToMyPlants() throws IOException {
void goToMyPlants() {
showPaneAsMainView("MyGarden.fxml");
styleChangeButton(myGarden_button);
}
@FXML
void goToMySchedule() throws IOException {
void goToMySchedule() {
showPaneAsMainView("MySchedule.fxml");
styleChangeButton(mySchedule_button);
}
@FXML
void goToPlants() throws IOException {
void goToPlants() {
showPaneAsMainView("Plants.fxml");
styleChangeButton(plants_button);
}
@@ -63,15 +64,22 @@ public class MainFXMLController implements Initializable {
* set HGrow and VGrow to parent AnchorPane.
* Sends MainController to other Controllers.
* @param fxmlFile string of fxml file
* @throws IOException exception when file does not exist
*/
public void showPaneAsMainView(String fxmlFile) throws IOException {
Pane anchorPane = appLoader.loadPane(fxmlFile);
mainPane.getChildren().setAll(anchorPane);
anchorPane.prefWidthProperty().bind(mainPane.widthProperty());
anchorPane.prefHeightProperty().bind(mainPane.heightProperty());
public void showPaneAsMainView(String fxmlFile) {
try {
Pane anchorPane = appLoader.loadPane(fxmlFile);
mainPane.getChildren().setAll(anchorPane);
anchorPane.prefWidthProperty().bind(mainPane.widthProperty());
anchorPane.prefHeightProperty().bind(mainPane.heightProperty());
anchorPane.removeEventHandler(ChangeViewEvent.CHANGE_MAIN_VIEW, changeMainViewHandler);
anchorPane.addEventHandler(ChangeViewEvent.CHANGE_MAIN_VIEW, changeMainViewHandler);
} catch (IOException e) {
LOG.log(Level.SEVERE, "Could not load pane.", e);
}
}
private final EventHandler<ChangeViewEvent> changeMainViewHandler = (ChangeViewEvent event) -> showPaneAsMainView(event.view());
private void preloadPanes() throws IOException {
appLoader.loadAndCacheFxml("MyGarden.fxml");
@@ -87,8 +95,9 @@ public class MainFXMLController implements Initializable {
* loads the default FXML File
* {@inheritDoc}
*/
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
@AfterInject
@SuppressWarnings("unused")
public void init() {
try {
preloadPanes();
showPaneAsMainView("Home.fxml");