summaryrefslogtreecommitdiff
path: root/src/main/java/controlleur/ControllerAdmin.java
blob: 9a31f0d20e17df2fbafcb539bd9255c47a470fb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package controlleur;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import modele.Evenement;

public class ControllerAdmin {
	private static String destinationDir;
	private static File destinationFile ;
	private static File file;
	
	public static void parcourir(Stage primaryStage, ImageView imageView) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Fichiers Images", "*.png", "*.jpg"));
        
        // Ouvrir le file chooser et obtenir le fichier sélectionné
        file = fileChooser.showOpenDialog(primaryStage);
        
       boolean nomFich = true;
        
        if (file != null) {

        	for (int i = 0 ; i < Connexion.eventC.getNbrEvt();i++) {
        		
        		if (file.getName().equals(Connexion.eventC.getNom_image().get(i))) {
        			
        	        Alert alert = new Alert(Alert.AlertType.ERROR);
        	        alert.setTitle("StatHammer : Nom d'image déjà existant");
        	        alert.setHeaderText(null);
        	        alert.setContentText("Le nom de l'image est déjà prise.");
        	        alert.showAndWait();
        	        nomFich = false;
        	        
        		}
        	}
        	if (nomFich) {
            	System.out.println("Fichier sélectionné : " + file.getName());
            	System.out.println("Chemin du fichier : " + file.getAbsolutePath());
                
                // Créer une nouvelle image à partir du chemin absolu du fichier
                Image image = new Image("file:" + file.getAbsolutePath());
                
                // Définir l'image dans l'ImageView
                imageView.setImage(image);

                // Destination pour sauvegarder l'image (hors de "src")
                destinationDir = "images";
                File destinationFolder = new File(destinationDir); // = Paths.get(destinationDir);
                
                // Créez le répertoire s'il n'existe pas
                if (!destinationFolder.exists()) {
                    destinationFolder.mkdirs();
                }
                
                File nouveauNom = new File(file.getName());
                destinationFile = new File(destinationFolder, nouveauNom.getName());
                		
        	}

        }
	}
	
	
	//méthode "valider"
	public static VBox valider(String nom_evt, String nom_img, String desc, String date) {
		Evenement evt = new Evenement(nom_evt, nom_img, desc, date);
		VBox event = new VBox();
		event.setPrefHeight(50);
        event.setAlignment(Pos.CENTER);
		try {
            // Copier le fichier dans le dossier de destination
            Files.copy(file.toPath(), destinationFile.toPath());
            System.out.println("Fichier enregistré à : " + destinationFile.getAbsolutePath());
            EvenementController.insererEvenement(evt);
			event = EvenementController.EvenementVBox();
			
        } catch (IOException ioException) {
        	System.out.println("Probleme sur Valider");
            ioException.printStackTrace();
        }
		return event;
    }
	
	public static File getFile() {
		return file;
	}


	public static String getDestinationDir() {
		return destinationDir;
	}
	

	
}