From 25a24353f72c67e659dda87591d0039031fe7166 Mon Sep 17 00:00:00 2001 From: CAPEL Maxime <83071634+fortyup@users.noreply.github.com> Date: Wed, 6 Dec 2023 13:25:34 +0100 Subject: [PATCH] Removing code duplication --- src/main/kotlin/main/Main.kt | 78 ++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 44 deletions(-) diff --git a/src/main/kotlin/main/Main.kt b/src/main/kotlin/main/Main.kt index db34a46..2e643dd 100644 --- a/src/main/kotlin/main/Main.kt +++ b/src/main/kotlin/main/Main.kt @@ -134,57 +134,23 @@ fun app() { formulairePage(onAnnulerClick = { currentPage.value = "glossaire" }) } "choixLangage" -> { + val jsExtensions = setOf("js", "html") + val javaExtensions = setOf("java") + val pythonExtensions = setOf("py") choixLangagePage( onRetourClick = { currentPage.value = "accueil" }, onPythonClick = { - val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) - fileDialog.file = "Untitled.py" // Initial file name - fileDialog.isMultipleMode = false // To enable selecting only one file - fileDialog.setFile("*.py") - fileDialog.isVisible = true - - val selectedFile = fileDialog.file - val selectedDirectory = fileDialog.directory - - if (selectedFile != null) { - val filePath = selectedDirectory + selectedFile - println("Opening: $filePath") - } else { - println("Open command cancelled by user.") - } - }, + selectFile(pythonExtensions) { filePath -> + println("Python file selected: $filePath") // Change by parser functions + } }, onJavaClick = { - val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) - fileDialog.file = "Untitled.java" // Initial file name - fileDialog.isMultipleMode = false // To enable selecting only one file - fileDialog.setFile("*.java") - fileDialog.isVisible = true - - val selectedFile = fileDialog.file - val selectedDirectory = fileDialog.directory - - if (selectedFile != null) { - val filePath = selectedDirectory + selectedFile - println("Opening: $filePath") - } else { - println("Open command cancelled by user.") + selectFile(javaExtensions) { filePath -> + println("Java file selected: $filePath") // Change by parser functions } }, onJavaScriptClick = { - val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) - fileDialog.file = "Untitled.js" // Initial file name - fileDialog.isMultipleMode = false // To enable selecting only one file - fileDialog.setFile("*.js") - fileDialog.isVisible = true - - val selectedFile = fileDialog.file - val selectedDirectory = fileDialog.directory - - if (selectedFile != null) { - val filePath = selectedDirectory + selectedFile - println("Opening: $filePath") - } else { - println("Open command cancelled by user.") + selectFile(jsExtensions) { filePath -> + println("JavaScript file selected: $filePath") // Change by parser functions } } ) @@ -194,6 +160,30 @@ fun app() { } } +fun selectFile(allowedExtensions: Set, onFileSelected: (String) -> Unit) { + val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) + fileDialog.isMultipleMode = false // To enable selecting only one file + fileDialog.isVisible = true + + val selectedFile = fileDialog.file + val selectedDirectory = fileDialog.directory + + if (selectedFile != null) { + val filePath = "$selectedDirectory$selectedFile" + + // Vérifier si l'extension est autorisée + val fileExtension = File(filePath).extension.toLowerCase() + if (allowedExtensions.contains(fileExtension)) { + println("Opening: $filePath") + onFileSelected(filePath) + } else { + println("Invalid file extension.") + } + } else { + println("Open command cancelled by user.") + } +} + fun exportToCSV(csvFilePath: String) { val glossary = chargerDonneesDepuisFichier() val csvContent = buildString {