package com.megatim.fdxgenerator.forms; import com.megatimfx.common.annontations.Champ; import com.megatimfx.common.customcontrols.AbstractNestedEntityTable; import com.megatimfx.common.customcontrols.AbstractSelectionItem; import com.megatimfx.components.customdialogs.AlertMessageUtil; import com.megatimfx.components.customdialogs.LoadinIndicatorDialogUtil; import com.megatim.fdxgenerator.App; import com.megatim.fdxgenerator.controller.table.StructureLigneTable; import com.megatim.fdxgenerator.controller.table.TypeFichierTable; import com.megatim.fdxgenerator.enums.DataType; import com.megatim.fdxgenerator.model.StructureLigne; import com.megatim.fdxgenerator.model.TypeFichier; import com.megatim.fdxgenerator.model.ValidateurFichier; import com.megatim.fdxgenerator.pojo.Delimiteur; import com.megatim.fdxgenerator.service.StructureLigneService; import com.megatim.fdxgenerator.service.TypeFichierService; import java.net.URL; import java.util.Arrays; import java.util.HashSet; import java.util.ResourceBundle; import java.util.Set; import javafx.collections.FXCollections; import javafx.concurrent.Task; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.CheckBox; import javafx.scene.control.ComboBox; import javafx.scene.control.TextField; import javafx.util.Pair; public class ValidateurFichierEditFormController implements Initializable { @FXML @Champ(mappedBy = "typeFichier", type = TypeFichier.class, update = false) private AbstractSelectionItem typeFichierAbstractSelectItem; @FXML @Champ(mappedBy = "nombrePosition", type = Integer.class, update = false) private TextField nombrePositionTextField; @FXML private CheckBox saisieStructureLigneChecBox; @FXML @Champ(mappedBy = "delimiteurLigne", type = Delimiteur.class) private ComboBox delimiteurLigne; @FXML @Champ(mappedBy = "delimiteurColonne", type = Delimiteur.class) private ComboBox delimiteurColonne; @FXML @Champ(mappedBy = "dataType", type = DataType.class) private ComboBox dataTypeComboBox; @FXML @Champ(mappedBy = "headerPresent", type = Boolean.class) private CheckBox headerPresentCheckbox; @FXML @Champ(mappedBy = "structureLignes", type = StructureLigne.class, update = true) private AbstractNestedEntityTable structureLigneAbstractTable; private TypeFichierService typeFichierService = TypeFichierService.getInstance(); private StructureLigneService structureLigneService = StructureLigneService.getInstance(); //ELEMENTS DE CHARGEMENT private final Set typeFichierSet = new HashSet<>(); @Override public void initialize(URL url, ResourceBundle rb) { saisieStructureLigneChecBox.setDisable(true); nombrePositionTextField.setText(""); typeFichierAbstractSelectItem.setTitle("Choix du type de fichier"); typeFichierAbstractSelectItem.setColumns(Arrays.asList( TypeFichierTable.codeColumn(), TypeFichierTable.libelleColumn() )); typeFichierAbstractSelectItem.setSearchFieldPairs(Arrays.asList( new Pair<>("code", "Code"), new Pair<>("libelle", "Libellé") )); structureLigneAbstractTable.setClazz(StructureLigne.class); structureLigneAbstractTable.setColums(Arrays.asList( StructureLigneTable.positionColumn(), StructureLigneTable.codeColumn(), StructureLigneTable.typeDonneeColumn(), StructureLigneTable.tailleColumn() )); structureLigneAbstractTable.setGenericCrudService(structureLigneService); saisieStructureLigneChecBox.setOnAction(event -> { typeFichierAbstractSelectItem.setDisable(saisieStructureLigneChecBox.isSelected()); nombrePositionTextField.setDisable(saisieStructureLigneChecBox.isSelected()); }); saisieStructureLigneChecBox.selectedProperty().addListener((observable, oldValue, newValue) -> { if (newValue) { typeFichierAbstractSelectItem.setDisable(true); nombrePositionTextField.setDisable(true); saisieStructureLigneChecBox.setDisable(true); } }); typeFichierAbstractSelectItem.selectedElementProperty().addListener((observable, oldValue, newValue) -> { if (newValue != null) { enableCheckBox(); } }); nombrePositionTextField.textProperty().addListener((observable, oldValue, newValue) -> { if (newValue.matches("\\d*")) { if (!newValue.isEmpty()) { enableCheckBox(); } else { saisieStructureLigneChecBox.setDisable(true); } } else { nombrePositionTextField.setText(oldValue); } }); delimiteurColonne.setPromptText("Sélectionnez un élément dans la liste"); delimiteurColonne.setItems(FXCollections.observableArrayList( App.DELIMITEURS_COLONNE.values() )); delimiteurColonne.setDisable(true); delimiteurLigne.setPromptText("Sélectionnez un élément dans la liste"); delimiteurLigne.setItems(FXCollections.observableArrayList( App.DELIMITEURS_LIGNE.values() )); delimiteurLigne.setDisable(true); dataTypeComboBox.setPromptText("Sélectionnez un élément dans la liste"); dataTypeComboBox.setItems(FXCollections.observableArrayList( DataType.values() )); initElements(); } private void initElements() { Task task = new Task() { @Override protected Object call() throws Exception { typeFichierSet.clear(); typeFichierSet.addAll(typeFichierService.getAllTypeFichWithoutValidateur()); return null; } }; task.setOnRunning(e -> LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().show()); task.setOnSucceeded(e -> { LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide(); typeFichierAbstractSelectItem.setElements(typeFichierSet); }); task.setOnFailed(e -> { LoadinIndicatorDialogUtil.getLoadingIndicatorDialog().hide(); AlertMessageUtil.showAlertException(task.getException(), "Une exception s'est produite pendant le traitement", "Erreur"); }); Thread thread = new Thread(task); thread.setDaemon(true); thread.start(); } public void enableCheckBox() { try { int position = Integer.valueOf(nombrePositionTextField.getText()); saisieStructureLigneChecBox.setDisable(typeFichierAbstractSelectItem == null || position <= 0); } catch (NumberFormatException e) { saisieStructureLigneChecBox.setDisable(true); } } public AbstractSelectionItem getTypeFichierAbstractSelectItem() { return typeFichierAbstractSelectItem; } public void setTypeFichierAbstractSelectItem(AbstractSelectionItem typeFichierAbstractSelectItem) { this.typeFichierAbstractSelectItem = typeFichierAbstractSelectItem; } public TextField getNombrePositionTextField() { return nombrePositionTextField; } public void setNombrePositionTextField(TextField nombrePositionTextField) { this.nombrePositionTextField = nombrePositionTextField; } public CheckBox getSaisieStructureLigneChecBox() { return saisieStructureLigneChecBox; } public void setSaisieStructureLigneChecBox(CheckBox saisieStructureLigneChecBox) { this.saisieStructureLigneChecBox = saisieStructureLigneChecBox; } public AbstractNestedEntityTable getStructureLigneAbstractTable() { return structureLigneAbstractTable; } public void setStructureLigneAbstractTable(AbstractNestedEntityTable structureLigneAbstractTable) { this.structureLigneAbstractTable = structureLigneAbstractTable; } public ComboBox getDelimiteurLigne() { return delimiteurLigne; } public ComboBox getDelimiteurColonne() { return delimiteurColonne; } public ComboBox getDataTypeComboBox() { return dataTypeComboBox; } public CheckBox getHeaderPresentCheckbox() { return headerPresentCheckbox; } }