package ch.makery.address; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.prefs.Preferences; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Dialogs; import javafx.scene.image.Image; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.BorderPane; import javafx.stage.Modality; import javafx.stage.Stage; import ch.makery.address.AbscenceListController; import ch.makery.address.model.Permission; import ch.makery.address.model.Person; import ch.makery.address.util.FileUtil; import ch.makery.address.AbscenceEditController; import com.thoughtworks.xstream.XStream; public class MainApp extends Application { private Stage primaryStage; private BorderPane rootLayout; /** * The data as an observable list of Persons. */ private ObservableList personData = FXCollections.observableArrayList(); private ObservableList PermissionData = FXCollections.observableArrayList(); /** * Constructor */ public MainApp() { // Add some sample data personData.add(new Person("Hans", "Muster")); personData.add(new Person("Ruth", "Mueller")); personData.add(new Person("Heinz", "Kurz")); personData.add(new Person("Cornelia", "Meier")); personData.add(new Person("Werner", "Meyer")); personData.add(new Person("Lydia", "Kunz")); personData.add(new Person("Anna", "Best")); personData.add(new Person("Stefan", "Meier")); personData.add(new Person("Martin", "Mueller")); //************************************************************* PermissionData.add(new Permission("Anoh", "Jean")); PermissionData.add(new Permission("Ezan", "Paul")); PermissionData.add(new Permission("Ezan", "Jean")); PermissionData.add(new Permission("Anoh", "Paul")); PermissionData.add(new Permission("Ezan", "Germain")); PermissionData.add(new Permission("Anoh", "Germain")); //************************************************************* } @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; this.primaryStage.setTitle("GRH-ALDIS"); this.primaryStage.getIcons().add(new Image("file:resources/images/address_book_32.png")); try { // Load the root layout from the fxml file FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/RootLayout.fxml")); rootLayout = (BorderPane) loader.load(); Scene scene = new Scene(rootLayout); primaryStage.setScene(scene); // Give the controller access to the main app RootLayoutController controller = loader.getController(); controller.setMainApp(this); primaryStage.show(); } catch (IOException e) { // Exception gets thrown if the fxml file could not be loaded e.printStackTrace(); } showPersonOverview(); // Try to load last opened person file File filePerson = getPersonFilePath(); if (filePerson != null) { loadPersonDataFromFile(filePerson); } } /** * Returns the data as an observable list of Persons. * @return */ public ObservableList getPersonData() { return personData; } //*************************************************** /** * Returns the data as an observable list of Permission * @return */ public ObservableList getFormPermissionData() { return PermissionData; } //**************************************************** //**************************************************** /** * Returns the main stage. * @return */ public Stage getPrimaryStage() { return primaryStage; } /** * Shows the person overview page in the center of the root layout. */ public void showPersonOverview() { try { // Load the fxml file and set into the center of the main layout FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/PersonOverview.fxml")); AnchorPane overviewPage = (AnchorPane) loader.load(); rootLayout.setCenter(overviewPage); // Give the controller access to the main app PersonOverviewController controller = loader.getController(); controller.setMainApp(this); } catch (IOException e) { // Exception gets thrown if the fxml file could not be loaded e.printStackTrace(); } } //****************************************************************************************************** public Stage primaryStagePerm; public void showPermissionListe(){ //this.primaryStagePerm = primaryStagePerm; //this.primaryStagePerm.setTitle("Permission"); //this.primaryStagePerm.getIcons().add(new Image("file:resources/images/")); // Load the root layout from the fxml file try { FXMLLoader loader = new FXMLLoader(PermissionListController.class.getResource("view/PermissionListRoot.fxml")); BorderPane borderPane = (BorderPane) loader.load(); Stage dialogStage = new Stage(); dialogStage.setTitle("Liste des Permissions"); dialogStage.initModality(Modality.WINDOW_MODAL); dialogStage.getIcons().add(new Image("file:resources/images/aldhis_list.png")); dialogStage.initOwner(primaryStage); Scene scene = new Scene(borderPane); dialogStage.setScene(scene); dialogStage.show(); FXMLLoader loader2 = new FXMLLoader(PermissionListController.class.getResource("view/PermissionListView.fxml")); AnchorPane overviewPage = (AnchorPane) loader2.load(); borderPane.setCenter(overviewPage); // Give the controller access to the main app PermissionListController controller = loader2.getController(); controller.setMainApp(this); } catch (IOException e) { // TODO Bloc catch généré automatiquement e.printStackTrace(); } File filePermission = getFormPermissionFilePath(); if (filePermission != null) { loadFormPermissionDataFromFile(filePermission); } } //****************************************************************************************************** public void showAbscenceListe(){ //this.primaryStageAbsc = primaryStageAbsc; //this.primaryStageAbsc.setTitle("Abscence"); //this.primaryStageAbsc.getIcons().add(new Image("file:resources/images/")); // Load the root layout from the fxml file try { FXMLLoader loader = new FXMLLoader(AbscenceListController.class.getResource("view/AbscenceListRoot.fxml")); BorderPane borderPane = (BorderPane) loader.load(); Stage dialogStage = new Stage(); dialogStage.setTitle("Liste des Abscences"); dialogStage.initModality(Modality.WINDOW_MODAL); dialogStage.getIcons().add(new Image("file:resources/images/aldhis_list.png")); dialogStage.initOwner(primaryStage); Scene scene = new Scene(borderPane); dialogStage.setScene(scene); dialogStage.show(); FXMLLoader loader2 = new FXMLLoader(AbscenceListController.class.getResource("view/AbscenceListView.fxml")); AnchorPane overviewPage = (AnchorPane) loader2.load(); borderPane.setCenter(overviewPage); // Give the controller access to the main app AbscenceListController controller = loader2.getController(); controller.setMainApp(this); } catch (IOException e) { // TODO Bloc catch généré automatiquement e.printStackTrace(); } /*File fileAbscence = abscenceListController.getAbscenceFilePath(); if (fileAbscence != null) { abscenceListController.loadAbscenceDataFromFile(fileAbscence); } */ } public boolean showNouvelleAbscence(){ try { FXMLLoader loader2 = new FXMLLoader(AbscenceListController.class.getResource("view/AbscenceEdit.fxml")); AnchorPane page = (AnchorPane) loader2.load(); Stage dialogStage = new Stage(); dialogStage.setTitle("Formulaire Abscence"); dialogStage.initModality(Modality.WINDOW_MODAL); dialogStage.initOwner(dialogStage); Scene scene = new Scene(page); dialogStage.setScene(scene); dialogStage.show(); // Set the permission formulaire into the controller AbscenceEditController controller = loader2.getController(); controller.setDialogStage(dialogStage); return controller.isValideClicked(); } catch (IOException e) { // TODO Bloc catch généré automatiquement e.printStackTrace(); return false; } } //****************************************************************************************************** /** * Opens a dialog to edit details for the specified person. If the user * clicks OK, the changes are saved into the provided person object and * true is returned. * * @param person the person object to be edited * @return true if the user clicked OK, false otherwise. */ public boolean showPersonEditDialog(Person person) { try { // Load the fxml file and create a new stage for the popup FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/PersonEditDialog.fxml")); AnchorPane page = (AnchorPane) loader.load(); Stage dialogStage = new Stage(); dialogStage.setTitle("Edit Person"); dialogStage.initModality(Modality.WINDOW_MODAL); dialogStage.getIcons().add(new Image("file:resources/images/edit.png")); dialogStage.initOwner(primaryStage); Scene scene = new Scene(page); dialogStage.setScene(scene); // Set the person into the controller PersonEditDialogController controller = loader.getController(); controller.setDialogStage(dialogStage); controller.setPerson(person); // Show the dialog and wait until the user closes it dialogStage.showAndWait(); return controller.isOkClicked(); } catch (IOException e) { // Exception gets thrown if the fxml file could not be loaded e.printStackTrace(); return false; } } /** * Returns the person file preference, i.e. the file that was last opened. * The preference is read from the OS specific registry. If no such * preference can be found, null is returned. * * @return */ public File getPersonFilePath() { Preferences prefs = Preferences.userNodeForPackage(MainApp.class); String filePath = prefs.get("filePath", null); if (filePath != null) { return new File(filePath); } else { return null; } } //******************************************************************************* public File getFormPermissionFilePath() { Preferences prefs = Preferences.userNodeForPackage(PermissionListController.class); String filePath = prefs.get("filePath", null); if (filePath != null) { return new File(filePath); } else { return null; } } //************************************************************************************* //************************************************************************************* /** * Sets the file path of the currently loaded file. * The path is persisted in the OS specific registry. * * @param file the file or null to remove the path */ public void setPersonFilePath(File filePerson) { Preferences prefs = Preferences.userNodeForPackage(MainApp.class); if (filePerson != null) { prefs.put("filePath", filePerson.getPath()); // Update the stage title primaryStage.setTitle("Base de Données ALDIS - " + filePerson.getName()); } else { prefs.remove("filePath"); // Update the stage title primaryStage.setTitle("Base de Données ALDIS"); } } //************************************************************************************* public void setFormPermissionFilePath(File filePermission) { Preferences prefs = Preferences.userNodeForPackage(PermissionListController.class); if (filePermission != null) { prefs.put("filePath", filePermission.getPath()); // Update the stage title primaryStage.setTitle("Base de Données ALDIS - " + filePermission.getName()); } else { prefs.remove("filePath"); // Update the stage title primaryStage.setTitle("Base de Données ALDIS"); } } //************************************************************************************* //************************************************************************************* /** * Loads person data from the specified file. The current person data will * be replaced. * * @param file */ @SuppressWarnings("unchecked") public void loadPersonDataFromFile(File filePerson) { XStream xstreamPerson = new XStream(); xstreamPerson.alias("person", Person.class); try { String xml = FileUtil.readFile(filePerson); ArrayList personList = (ArrayList) xstreamPerson.fromXML(xml); personData.clear(); personData.addAll(personList); setPersonFilePath(filePerson); } catch (Exception e) { // catches ANY exception Dialogs.showErrorDialog(primaryStage, "Could not load data from file:\n" + filePerson.getPath(), "Could not load data", "Error", e); } } //***************************************************************************************** @SuppressWarnings("unchecked") public void loadFormPermissionDataFromFile(File filePermission) { XStream xstreamPermission = new XStream(); xstreamPermission.alias("formPermission", Permission.class); try { String xml = FileUtil.readFile(filePermission); ArrayList formPermissionList = (ArrayList) xstreamPermission.fromXML(xml); PermissionData.clear(); PermissionData.addAll(formPermissionList); setFormPermissionFilePath(filePermission); } catch (Exception e) { // catches ANY exception Dialogs.showErrorDialog(primaryStage, "Could not load data from file:\n" + filePermission.getPath(), "Could not load data", "Error", e); } } //****************************************************************************************************************** //****************************************************************************************************************** /** * Saves the current person data to the specified file. * * @param file */ public void savePersonDataToFile(File filePerson) { XStream xstreamPerson = new XStream(); xstreamPerson.alias("person", Person.class); // Convert ObservableList to a normal ArrayList ArrayList personList = new ArrayList<>(personData); String xml = xstreamPerson.toXML(personList); try { FileUtil.saveFile(xml, filePerson); setPersonFilePath(filePerson); } catch (Exception e) { // catches ANY exception Dialogs.showErrorDialog(primaryStage, "Could not save data to file:\n" + filePerson.getPath(), "Could not save data", "Error", e); } } //******************************************************************************************* public void saveFormPermissionDataToFile(File filePermission) { XStream xstreamPermission = new XStream(); xstreamPermission.alias("formPermission", Permission.class); // Convert ObservableList to a normal ArrayList ArrayList formPermissionList = new ArrayList<>(PermissionData); String xml = xstreamPermission.toXML(formPermissionList); try { FileUtil.saveFile(xml, filePermission); setFormPermissionFilePath(filePermission); } catch (Exception e) { // catches ANY exception Dialogs.showErrorDialog(primaryStage, "Could not save data to file:\n" + filePermission.getPath(), "Could not save data", "Error", e); } } //******************************************************************************************* //********************************************************************************************* /** * Opens a dialog to show birthday statistics. */ public void showBirthdayStatistics() { try { // Load the fxml file and create a new stage for the popup FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("view/BirthdayStatistics.fxml")); AnchorPane page = (AnchorPane) loader.load(); Stage dialogStage = new Stage(); dialogStage.setTitle("Birthday Statistics"); dialogStage.initModality(Modality.WINDOW_MODAL); dialogStage.initOwner(primaryStage); Scene scene = new Scene(page); dialogStage.setScene(scene); // Set the persons into the controller BirthdayStatisticsController controller = loader.getController(); controller.setPersonData(personData); dialogStage.show(); } catch (IOException e) { // Exception gets thrown if the fxml file could not be loaded e.printStackTrace(); } } //Parametrage de Permission /**** * * @param showNouvellePermission * @return */ public boolean showNouvellePermission(){ try { FXMLLoader loader2 = new FXMLLoader(PermissionListController.class.getResource("view/PermissionEdit.fxml")); AnchorPane page = (AnchorPane) loader2.load(); Stage dialogStage = new Stage(); dialogStage.setTitle("Formulaire Permission"); dialogStage.initModality(Modality.WINDOW_MODAL); dialogStage.initOwner(primaryStage); Scene scene = new Scene(page); dialogStage.setScene(scene); dialogStage.show(); // Set the permission formulaire into the controller PermissionEditController controller = loader2.getController(); controller.setDialogStage(dialogStage); return controller.isValideClicked(); } catch (IOException e) { // TODO Bloc catch généré automatiquement e.printStackTrace(); return false; } } public static void main(String[] args) { launch(args); } }