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.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Pane; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class MainFXMLController { private static final Logger LOG = Logger.getLogger(MainFXMLController.class.getName()); @Inject AppLoader appLoader; @FXML private Button home_button; @FXML private AnchorPane mainPane; @FXML private Button myGarden_button; @FXML private Button mySchedule_button; @FXML private Button settings_button; @FXML private Button tutorial_button; /** * go to home pane */ @FXML void goToHome() { showPaneAsMainView("Home.fxml"); styleChangeButton(home_button); } /** * go to my garden pane */ @FXML void goToMyPlants() { showPaneAsMainView("MyGarden.fxml"); styleChangeButton(myGarden_button); } /** * go to the schedule pane */ @FXML void goToMySchedule() { showPaneAsMainView("MySchedule.fxml"); styleChangeButton(mySchedule_button); } /** * open dialog of the settings * @throws IOException exception */ @FXML public void openSettings() throws IOException { Dialog dialog = new Dialog<>(); dialog.setTitle("Settings"); dialog.setHeaderText("Settings"); dialog.setResizable(false); DialogPane dialogPane = dialog.getDialogPane(); ButtonType saveSettings = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE); dialogPane.getButtonTypes().addAll(saveSettings, ButtonType.CANCEL); if (appLoader.loadPaneToDialog("Settings.fxml", dialogPane) instanceof SettingsController controller) { dialog.showAndWait() .ifPresent(button -> { if (button.equals(saveSettings)) { controller.saveSettings(); } }); } } /** * go to Tutorial pane */ public void goToTutorial() { showPaneAsMainView("Tutorial.fxml"); styleChangeButton(tutorial_button); } /** * Updates the mainPane with the selected fxml file. * set HGrow and VGrow to parent AnchorPane. * Sends MainController to other Controllers. * @param fxmlFile string of fxml file */ 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 changeMainViewHandler = (ChangeViewEvent event) -> showPaneAsMainView(event.view()); /** * preload all menu bar panes * @throws IOException exception */ private void preloadPanes() throws IOException { appLoader.loadAndCacheFxml("MyGarden.fxml"); appLoader.loadAndCacheFxml("MySchedule.fxml"); appLoader.loadAndCacheFxml("Plants.fxml"); appLoader.loadAndCacheFxml("Tutorial.fxml"); } private void styleChangeButton(Button button) { //ToDo changeStyle of the menu buttons } /** * loads the default FXML File * {@inheritDoc} */ @AfterInject @SuppressWarnings("unused") public void init() { try { preloadPanes(); showPaneAsMainView("MyGarden.fxml"); styleChangeButton(myGarden_button); } catch (IOException e) { LOG.log(Level.SEVERE, "Failed to load FXML-Pane!", e); } setIconToButton(home_button, "homeIcon.png"); setIconToButton(settings_button, "settingsIcon.png"); Settings.getInstance().getShowTutorialProperty().addListener((observable, oldValue, newValue) -> { tutorial_button.setVisible(newValue); }); tutorial_button.setVisible(Settings.getInstance().getShowTutorial()); } /** * adds icon to given button * @param button the button which get the icon * @param iconFileName file name of icon */ private void setIconToButton(Button button, String iconFileName) { Image img = new Image(String.valueOf(getClass().getResource("icons/" + iconFileName))); ImageView imageView = new ImageView(img); imageView.setFitHeight(20); imageView.setPreserveRatio(true); button.setGraphic(imageView); } }