package ch.zhaw.gartenverwaltung; import ch.zhaw.gartenverwaltung.bootstrap.AppLoader; import ch.zhaw.gartenverwaltung.bootstrap.Inject; import ch.zhaw.gartenverwaltung.types.HardinessZone; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.*; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import java.io.IOException; import java.net.URL; import java.util.Objects; import java.util.ResourceBundle; /** * Controller class for the Settings.fxml file */ public class SettingsController implements Initializable { Settings settings = Settings.getInstance(); @Inject AppLoader appLoader; @FXML private ComboBox selectHardinessZone_comboBox; @FXML private CheckBox showTutorial_checkBox; @FXML private Button location_button; @FXML private Label location_label; /** * open dialog to set location * @param event event * @throws IOException exception */ @FXML void setLocation(ActionEvent event) throws IOException { openTextFieldDialog(); } /** * save selected values to {@link Settings} */ public void saveSettings() { settings.setShowTutorial(showTutorial_checkBox.isSelected()); settings.setCurrentHardinessZone(selectHardinessZone_comboBox.getValue()); } /** * save default values form {@link Settings} * @param location location * @param resources resources */ @Override public void initialize(URL location, ResourceBundle resources) { showTutorial_checkBox.setSelected(settings.getShowTutorial()); selectHardinessZone_comboBox.getItems().addAll(HardinessZone.values()); selectHardinessZone_comboBox.setValue(settings.getCurrentHardinessZone()); setIconToButton(location_button, "locationIcon.png"); location_label.setText(settings.getLocation()); } /** * adds icon to 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); } /** * opens Dialog to set exception * @throws IOException exception */ private void openTextFieldDialog() throws IOException { Dialog dialog = new Dialog<>(); dialog.setTitle("Set Location of your Garden"); dialog.setHeaderText("set Location of your Garden!"); dialog.setResizable(false); DialogPane dialogPane = dialog.getDialogPane(); dialogPane.getStylesheets().add( Objects.requireNonNull(getClass().getResource("bootstrap/dialogStyle.css")).toExternalForm()); dialogPane.getStyleClass().add("myDialog"); ButtonType save = new ButtonType("Save", ButtonBar.ButtonData.OK_DONE); dialogPane.getButtonTypes().addAll(save, ButtonType.CANCEL); if (appLoader.loadPaneToDialog("TextFieldFormular.fxml", dialogPane) instanceof TextFieldFormularController controller) { controller.setDescription_label("Set"); controller.setValueTextArea(settings.getLocation()); controller.initSaveButton((Button) dialogPane.lookupButton(save)); dialog.setResultConverter(button -> button.equals(save) ? controller.getValue() : null); dialog.showAndWait() .ifPresent(string -> { settings.setLocation(string); location_label.setText(settings.getLocation()); }); } } }