package ch.makery.address; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.prefs.Preferences; import com.thoughtworks.xstream.XStream; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.control.Dialogs; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.AnchorPane; import javafx.stage.Modality; import javafx.stage.Stage; import ch.makery.address.model.Abscence; import ch.makery.address.MainApp; import ch.makery.address.util.CalendarUtil; import ch.makery.address.util.FileUtil; /** * * @author Anohjp * */ public class AbscenceListController { @FXML protected TableView abscenceTable; @FXML private TableColumn nomPersColumn; @FXML private TableColumn prenomPersColumn; @FXML private Label nomPersLabel; @FXML private Label prenomPersLabel; @FXML private Label fonctionLabel; @FXML private Label dateDebutLabel; @FXML private Label dateFinLabel; @FXML private Label dureeLabel; @FXML private Label motifLabel; private ObservableList abscenceData = FXCollections.observableArrayList(); //private Stage primaryStageAbsc; // Creation d constructeur public AbscenceListController(){ abscenceData.add(new Abscence("Jean", "Ezan")); abscenceData.add(new Abscence("Paul", "Anoh")); abscenceData.add(new Abscence("Germain", "Ezan")); abscenceData.add(new Abscence("Jean", "Anoh")); abscenceData.add(new Abscence("Paul", "Ezan")); abscenceData.add(new Abscence("Germain", "Anoh")); } @FXML private void initialize() { // Initialise la table permission nomPersColumn.setCellValueFactory(new PropertyValueFactory("nomPers")); prenomPersColumn.setCellValueFactory(new PropertyValueFactory("prenomPers")); // Auto resize columns abscenceTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); // effacer permission showAbscenceDetails(null); // Listen for selection changes/ Ecouteur de changement de selection abscenceTable.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, Abscence oldValue, Abscence newValue) { showAbscenceDetails(newValue); } }); } /** * Is called by the main application to give a reference back to itself. * * @param mainApp */ public void setMainApp(MainApp mainApp) { // Add observable list data to the table abscenceTable.setItems(getAbscenceData()); } /** * Returns the data as an observable list of Permission * @return */ public ObservableList getAbscenceData() { return abscenceData; } public File getAbscenceFilePath() { Preferences prefs = Preferences.userNodeForPackage(AbscenceListController.class); String filePath = prefs.get("filePath", null); if (filePath != null) { return new File(filePath); } else { return null; } } //private Stage primaryStage; public void setAbscenceFilePath(File file) { Preferences prefs = Preferences.userNodeForPackage(AbscenceListController.class); if (file != null) { prefs.put("filePath", file.getPath()); // Update the stage title //primaryStage.setTitle("Abscence - " + file.getName()); } else { prefs.remove("filePath"); // Update the stage title //primaryStage.setTitle("Abscence"); } } @SuppressWarnings("unchecked") public void loadAbscenceDataFromFile(File file) { XStream xstreamAbscence = new XStream(); xstreamAbscence.alias("abscence", Abscence.class); try { String xml = FileUtil.readFile(file); ArrayList abscenceList = (ArrayList) xstreamAbscence.fromXML(xml); abscenceData.clear(); abscenceData.addAll(abscenceList); setAbscenceFilePath(file); } catch (Exception e) { // catches ANY exception Dialogs.showErrorDialog(null, "Could not load data from file:\n" + file.getPath(), "Could not load data", "Error", e); } } public void saveAbscenceDataToFile(File file) { XStream xstream = new XStream(); xstream.alias("abscence", Abscence.class); // Convert ObservableList to a normal ArrayList ArrayList abscenceList = new ArrayList<>(abscenceData); String xml = xstream.toXML(abscenceList); try { FileUtil.saveFile(xml, file); setAbscenceFilePath(file); } catch (Exception e) { // catches ANY exception Dialogs.showErrorDialog(null, "Could not save data to file:\n" + file.getPath(), "Could not save data", "Error", e); } } /** * Fills all text fields to show details about the permission. * If the specified permission is null, all text fields are cleared. * * @param permission the permission or null */ protected void showAbscenceDetails(Abscence abscence) { if (abscence != null) { nomPersLabel.setText(abscence.getNomPers()); prenomPersLabel.setText(abscence.getPrenomPers()); fonctionLabel.setText(abscence.getFonction()); dateDebutLabel.setText(CalendarUtil.format(abscence.getDateDebut())); dateFinLabel.setText(CalendarUtil.format(abscence.getDateFin())); dureeLabel.setText(Integer.toString(abscence.getDureePerm())); motifLabel.setText(abscence.getMotif()); } else { nomPersLabel.setText(""); prenomPersLabel.setText(""); fonctionLabel.setText(""); dateDebutLabel.setText(""); dateFinLabel.setText(""); dureeLabel.setText(""); motifLabel.setText(""); } } public boolean showAbscenceEdit(Abscence abscence){ 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); controller.setAbscence(abscence); return controller.isValideClicked(); } catch (IOException e) { // TODO Bloc catch généré automatiquement e.printStackTrace(); return false; } } /** * Called when the user clicks the new button. * Opens a dialog to edit details for a new person. */ @FXML private void handleNouvelleAbscence() { Abscence tempAbscence = new Abscence(); boolean nouveauClicked = showAbscenceEdit(tempAbscence); if (nouveauClicked) { getAbscenceData().add(tempAbscence); } } /*@FXML private void handleNew() { mainApp.getFormPermissionData().clear(); mainApp.setFormPermissionFilePath(null); } */ /** * * @return table Abscence */ //protected Stage getPrimaryStageAbsc() { // TODO Stub de la méthode généré automatiquement //return primaryStageAbsc; //} /** * */ @FXML private void handleModifierAbscence() { Abscence selectedAbscence = abscenceTable.getSelectionModel().getSelectedItem(); if (selectedAbscence != null) { boolean okClicked = showAbscenceEdit(selectedAbscence); if (okClicked) { refreshAbscenceTable(); showAbscenceDetails(selectedAbscence); } } else { // Nothing selected Dialogs.showWarningDialog(null, "Veillez sélectionnez une personne dans la Table.", "Pas de Personne Sélectionnée", "Pas de Sélection"); } } /** * Called when the user clicks on the delete button. */ @FXML private void handleSupprimerAbscence() { int selectedIndex = abscenceTable.getSelectionModel().getSelectedIndex(); if (selectedIndex >= 0) { abscenceTable.getItems().remove(selectedIndex); } else { // Nothing selected Dialogs.showWarningDialog(null, "Please select a person in the table.", "No Person Selected", "No Selection"); } } /** * Refreshes the table. This is only necessary if an item that is already in * the table is changed. New and deleted items are refreshed automatically. * * This is a workaround because otherwise we would need to use property * bindings in the model class and add a *property() method for each * property. Maybe this will not be necessary in future versions of JavaFX * (see http://javafx-jira.kenai.com/browse/RT-22599) */ protected void refreshAbscenceTable() { int selectedIndex = abscenceTable.getSelectionModel().getSelectedIndex(); abscenceTable.setItems(null); abscenceTable.layout(); abscenceTable.setItems(getAbscenceData()); // Must set the selected index again (see http://javafx-jira.kenai.com/browse/RT-26291) abscenceTable.getSelectionModel().select(selectedIndex); } }// Fin de la classe PermissionListController