Merge branch 'main' into 'popup'

# Conflicts:
#   src/main/kotlin/main/Main.kt
main
Odabasioglu Cemal 2023-12-07 13:34:48 +00:00
commit ed4122c27b
2 changed files with 181 additions and 62 deletions

View File

@ -37,6 +37,8 @@ dependencies {
implementation("io.ktor:ktor-html-builder:1.6.3") implementation("io.ktor:ktor-html-builder:1.6.3")
implementation("junit:junit:4.13.1") implementation("junit:junit:4.13.1")
testImplementation("junit:junit:4.13.1") testImplementation("junit:junit:4.13.1")
implementation("org.apache.poi:poi:5.0.0")
implementation("org.apache.poi:poi-ooxml:5.0.0")

View File

@ -22,7 +22,14 @@ import java.awt.Frame
import java.io.File import java.io.File
import java.io.FileWriter import java.io.FileWriter
import java.io.IOException import java.io.IOException
<<<<<<< src/main/kotlin/main/Main.kt
=======
import org.apache.poi.ss.usermodel.CellType
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.FileInputStream
>>>>>>> src/main/kotlin/main/Main.kt
val customRedColor = Color(0xFFB70D1B) val customRedColor = Color(0xFFB70D1B)
@ -57,15 +64,18 @@ fun homePage(
Text("Glossaire") Text("Glossaire")
} }
Button(onClick = onCodeAVerifierClick, Button(
onClick = onCodeAVerifierClick,
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
) ) { )
) {
Text("Code à Vérifier") Text("Code à Vérifier")
} }
Button(onClick = { /* Action de Comparer */ }, Button(
onClick = { /* Action de Comparer */ },
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
@ -90,28 +100,14 @@ fun app() {
onCodeAVerifierClick = { currentPage.value = "choixLangage" } onCodeAVerifierClick = { currentPage.value = "choixLangage" }
) )
} }
"glossaire" -> { "glossaire" -> {
glossairePage( glossairePage(
onAjouterMotClick = { currentPage.value = "formulaire" }, onAjouterMotClick = { currentPage.value = "formulaire" },
onImporterClick = { onImporterClick = {
val fileDialog = FileDialog(Frame(), "Select a file", FileDialog.LOAD) selectFile(setOf("csv", "xlsx")) { filePath ->
fileDialog.file = "Untitled.csv" // Initial file name println("Importing file: $filePath")
fileDialog.isMultipleMode = false // To enable selecting only one file importFile(filePath)
fileDialog.setFile("*.csv")
fileDialog.isVisible = true
val selectedFile = fileDialog.file
val selectedDirectory = fileDialog.directory
if (selectedFile != null) {
val filePath = selectedDirectory + selectedFile
println("Opening: $filePath")
importCSV(filePath)
} else {
println("Open command cancelled by user.")
} }
}, },
onExporterClick = { onExporterClick = {
@ -131,9 +127,11 @@ fun app() {
onRetourClick = { currentPage.value = "accueil" } onRetourClick = { currentPage.value = "accueil" }
) )
} }
"formulaire" -> { "formulaire" -> {
formulairePage(onAnnulerClick = { currentPage.value = "glossaire" }) formulairePage(onAnnulerClick = { currentPage.value = "glossaire" })
} }
"choixLangage" -> { "choixLangage" -> {
val pythonExtensions = setOf("py") val pythonExtensions = setOf("py")
val javaExtensions = setOf("java") val javaExtensions = setOf("java")
@ -143,7 +141,8 @@ fun app() {
onPythonClick = { onPythonClick = {
selectFile(pythonExtensions) { filePath -> selectFile(pythonExtensions) { filePath ->
println("Python file selected: $filePath") // Change by parser functions println("Python file selected: $filePath") // Change by parser functions
} }, }
},
onJavaClick = { onJavaClick = {
selectFile(javaExtensions) { filePath -> selectFile(javaExtensions) { filePath ->
println("Java file selected: $filePath") // Change by parser functions println("Java file selected: $filePath") // Change by parser functions
@ -168,7 +167,6 @@ fun selectFile(extensions: Set<String>, onFileSelected: (String) -> Unit) {
fileDialog.isVisible = true fileDialog.isVisible = true
val selectedFile = fileDialog.file val selectedFile = fileDialog.file
val selectedDirectory = fileDialog.directory val selectedDirectory = fileDialog.directory
@ -208,7 +206,23 @@ fun exportToCSV(csvFilePath: String) {
} }
} }
fun importCSV(cheminFichier: String) { fun importFile(cheminFichier: String) {
val fileExtension = File(cheminFichier).extension.lowercase()
when {
fileExtension == "csv" -> {
importCSVFile(cheminFichier)
}
fileExtension == "xlsx" -> {
importXLSXFile(cheminFichier)
}
else -> {
println("Unsupported file format.")
}
}
}
fun importCSVFile(cheminFichier: String) {
val file = File(cheminFichier).readText(Charsets.UTF_8) val file = File(cheminFichier).readText(Charsets.UTF_8)
val lines = file.split("\n") val lines = file.split("\n")
@ -248,7 +262,63 @@ fun importCSV(cheminFichier: String) {
} }
} }
private fun importXLSXFile(cheminFichier: String) {
val workbook: Workbook
try {
FileInputStream(cheminFichier).use { fileInputStream ->
workbook = XSSFWorkbook(fileInputStream)
}
val sheet = workbook.getSheetAt(0) // Assuming the data is in the first sheet
val headerRow = sheet.getRow(0)
val header = mutableListOf<String>()
for (i in 0 until headerRow.lastCellNum) {
header.add(headerRow.getCell(i).stringCellValue)
}
val dataLines = mutableListOf<Map<String, String>>()
for (i in 1 until sheet.physicalNumberOfRows) {
val currentRow = sheet.getRow(i)
val data = mutableMapOf<String, String>()
for (j in 0 until currentRow.lastCellNum) {
val cell = currentRow.getCell(j)
val cellValue = when (cell.cellType) {
CellType.STRING -> cell.stringCellValue
CellType.NUMERIC -> cell.numericCellValue.toString()
else -> ""
}
data[header[j]] = cellValue
}
dataLines.add(data)
}
dataLines.forEach { line ->
// Check if the line is not empty or blank
if (line.isNotEmpty()) {
// Process each line as needed
val nouveauMot = Mot(
nom = line["Mot"] ?: "",
description = line["Description"] ?: "",
contextePrincipal = line["Contexte principal"] ?: "",
contexte2 = line["Contexte 2"] ?: "",
lieA = line["Lié à"] ?: "",
synonyme = line["Synonyme"] ?: "",
antonyme = line["Antonyme"] ?: ""
)
if (nouveauMot.nom.isNotBlank() && verifierChampsRequis(line)) {
ajouterMotAuGlossaire(nouveauMot)
}
}
}
} catch (e: IOException) {
e.printStackTrace()
}
}
fun verifierChampsRequis(mot: Map<String, String>): Boolean { fun verifierChampsRequis(mot: Map<String, String>): Boolean {
//Vérifier que les headers sont égales aux champs requis //Vérifier que les headers sont égales aux champs requis
@ -317,7 +387,8 @@ fun choixLangagePage(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
)) { )
) {
Text("Java") Text("Java")
} }
@ -326,7 +397,8 @@ fun choixLangagePage(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
)) { )
) {
Text("JavaScript") Text("JavaScript")
} }
} }
@ -342,7 +414,8 @@ fun choixLangagePage(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
)) { )
) {
Text("Retour") Text("Retour")
} }
} }
@ -379,7 +452,8 @@ fun glossairePage(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
)) { )
) {
Text("Ajouter un mot") Text("Ajouter un mot")
} }
@ -388,8 +462,9 @@ fun glossairePage(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
)) { )
Text("Importer un fichier CSV") ) {
Text("Importer un fichier CSV ou XLSX")
} }
Button( Button(
@ -397,7 +472,8 @@ fun glossairePage(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
)) { )
) {
Text("Exporter un fichier CSV") Text("Exporter un fichier CSV")
} }
@ -414,17 +490,23 @@ fun glossairePage(
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
contentColor = Color.White contentColor = Color.White
)) { )
) {
Text("Retour") Text("Retour")
} }
} }
} }
<<<<<<< src/main/kotlin/main/Main.kt
@OptIn(ExperimentalMaterialApi::class) @OptIn(ExperimentalMaterialApi::class)
=======
>>>>>>> src/main/kotlin/main/Main.kt
@Composable @Composable
fun formulairePage(onAnnulerClick: () -> Unit) { fun formulairePage(onAnnulerClick: () -> Unit) {
// State to track whether to show the snackbar
val snackbarVisibleState = remember { mutableStateOf(false) }
val nom = remember { mutableStateOf("") } val nom = remember { mutableStateOf("") }
val description = remember { mutableStateOf("") } val description = remember { mutableStateOf("") }
@ -477,16 +559,12 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
verticalArrangement = Arrangement.spacedBy(8.dp), verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
Text(text = "Nouveau contexte métier", style = MaterialTheme.typography.h5) Text(text = "Nouveau mot", style = MaterialTheme.typography.h5)
TextField( TextField(
value = nom.value, value = nom.value,
onValueChange = { nom.value = it }, onValueChange = { nom.value = it },
label = { Text("Mot") }, label = { Text("Mot *") },
colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor, // Change this to the color you want
unfocusedIndicatorColor = Color.Gray // Change this to the color you want
)
) )
TextField( TextField(
@ -494,18 +572,18 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
onValueChange = { description.value = it }, onValueChange = { description.value = it },
label = { Text("Description") }, label = { Text("Description") },
colors = TextFieldDefaults.textFieldColors( colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor, // Change this to the color you want focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray // Change this to the color you want unfocusedIndicatorColor = Color.Gray
) )
) )
TextField( TextField(
value = contextePrincipal.value, value = contextePrincipal.value,
onValueChange = { contextePrincipal.value = it }, onValueChange = { contextePrincipal.value = it },
label = { Text("Contexte principal") }, label = { Text("Contexte principal *") },
colors = TextFieldDefaults.textFieldColors( colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor, // Change this to the color you want focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray // Change this to the color you want unfocusedIndicatorColor = Color.Gray
) )
) )
@ -514,8 +592,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
onValueChange = { contexte2.value = it }, onValueChange = { contexte2.value = it },
label = { Text("Contexte 2") }, label = { Text("Contexte 2") },
colors = TextFieldDefaults.textFieldColors( colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor, // Change this to the color you want focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray // Change this to the color you want unfocusedIndicatorColor = Color.Gray
) )
) )
@ -524,8 +602,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
onValueChange = { lieA.value = it }, onValueChange = { lieA.value = it },
label = { Text("Lié à") }, label = { Text("Lié à") },
colors = TextFieldDefaults.textFieldColors( colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor, // Change this to the color you want focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray // Change this to the color you want unfocusedIndicatorColor = Color.Gray
) )
) )
@ -534,8 +612,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
onValueChange = { synonyme.value = it }, onValueChange = { synonyme.value = it },
label = { Text("Synonyme") }, label = { Text("Synonyme") },
colors = TextFieldDefaults.textFieldColors( colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor, // Change this to the color you want focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray // Change this to the color you want unfocusedIndicatorColor = Color.Gray
) )
) )
@ -544,8 +622,8 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
onValueChange = { antonyme.value = it }, onValueChange = { antonyme.value = it },
label = { Text("Antonyme") }, label = { Text("Antonyme") },
colors = TextFieldDefaults.textFieldColors( colors = TextFieldDefaults.textFieldColors(
focusedIndicatorColor = customRedColor, // Change this to the color you want focusedIndicatorColor = customRedColor,
unfocusedIndicatorColor = Color.Gray // Change this to the color you want unfocusedIndicatorColor = Color.Gray
) )
) )
@ -566,20 +644,34 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
Button( Button(
onClick = { onClick = {
// Validation du formulaire // Validation du formulaire
if (nom.value.isBlank() || contextePrincipal.value.isBlank()) {
// Show the snackbar when validation fails
snackbarVisibleState.value = true
return@Button
}
val nouveauMot = Mot( val nouveauMot = Mot(
nom=nom.value, nom = nom.value,
description=description.value, description = description.value,
contextePrincipal=contextePrincipal.value, contextePrincipal = contextePrincipal.value,
contexte2=contexte2.value, contexte2 = contexte2.value,
lieA=lieA.value, lieA = lieA.value,
synonyme=synonyme.value, synonyme = synonyme.value,
antonyme=antonyme.value antonyme = antonyme.value
) )
ajouterMotAuGlossaire(nouveauMot) ajouterMotAuGlossaire(nouveauMot)
openDialog.value = true openDialog.value = true
// Réinitialiser les champs après l'ajout
nom.value = ""
description.value = ""
contextePrincipal.value = ""
contexte2.value = ""
lieA.value = ""
synonyme.value = ""
antonyme.value = ""
}, },
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
backgroundColor = customRedColor, backgroundColor = customRedColor,
@ -591,10 +683,24 @@ fun formulairePage(onAnnulerClick: () -> Unit) {
} }
} }
// Show the snackbar when the state is true
if (snackbarVisibleState.value) {
Snackbar(
modifier = Modifier.padding(16.dp),
action = {
Button(onClick = { snackbarVisibleState.value = false }) {
Text("OK")
}
}
) {
Text("Les champs 'Mot' et 'Contexte principal' sont obligatoires.")
}
}
} }
} }
} }
fun chargerDonneesDepuisFichier(): List<Mot> { fun chargerDonneesDepuisFichier(): List<Mot> {
//if file is empty, return empty list //if file is empty, return empty list
if (!File("glossaire.json").exists() || File("glossaire.json").length() == 0L) { if (!File("glossaire.json").exists() || File("glossaire.json").length() == 0L) {
@ -609,20 +715,31 @@ fun chargerDonneesDepuisFichier(): List<Mot> {
} }
} }
private val json = Json { prettyPrint = true }
fun sauvegarderDonneesDansFichier(listeMots: List<Mot>) { fun sauvegarderDonneesDansFichier(listeMots: List<Mot>) {
try { try {
val content = Json.encodeToString(listeMots) val content = json.encodeToString(listeMots)
File("glossaire.json").writeText(content) File("glossaire.json").writeText(content)
} catch (e: IOException) { } catch (e: IOException) {
e.printStackTrace() e.printStackTrace()
// Handle the exception as needed
} }
} }
fun ajouterMotAuGlossaire(nouveauMot: Mot) { fun ajouterMotAuGlossaire(nouveauMot: Mot) {
val listeMots = chargerDonneesDepuisFichier().toMutableList() val listeMots = chargerDonneesDepuisFichier().toMutableList()
// Vérifier si le mot existe déjà dans le glossaire
if (listeMots.any { it.nom.equals(nouveauMot.nom, ignoreCase = true) }) {
println("Le mot '${nouveauMot.nom}' existe déjà dans le glossaire. Ajout annulé.")
return
}
// Ajouter le nouveau mot seulement s'il n'existe pas déjà
listeMots.add(nouveauMot) listeMots.add(nouveauMot)
sauvegarderDonneesDansFichier(listeMots) sauvegarderDonneesDansFichier(listeMots)
println("Mot ajouté avec succès : ${nouveauMot.nom}")
} }