package com.megatim.fdxconvert.views; import com.megatimfx.common.customcontrols.AbstractSelectionItem; import com.megatim.fdxconvert.controller.table.TypeFichierTable; import com.megatim.fdxconvert.model.TypeFichier; import com.megatim.fdxconvert.model.TypeFichierJson; import com.megatim.fdxconvert.viewmodel.JsonStructureFormViewModel; import com.megatim.fdxconvert.viewmodel.JsonStructureViewModel; import com.megatim.fdxconvert.views.jsonstructure.JsonStructureController; import com.megatim.fdxconvert.views.jsonstructure.NestedJsonStructure; import com.megatim.fdxconvert.views.jsonstructure.NestedJsonStructureFromJsonStructure; import javafx.application.Platform; import javafx.beans.property.ObjectProperty; import javafx.collections.ListChangeListener; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javafx.util.Pair; import java.io.IOException; import java.net.URL; import java.util.Arrays; import java.util.ResourceBundle; import java.util.stream.Collectors; public class JsonStructureFormController implements Initializable { @FXML private VBox mainContainer; @FXML private AbstractSelectionItem typeFichierField; @FXML private HBox bottomContainer; @FXML private Button cancelButton; @FXML private Button editButton; private final JsonStructureFormViewModel viewModel; private final JsonStructureController jsonStructureController; public JsonStructureFormController(TypeFichierJson typeFichierJson) { NestedJsonStructure nestedJsonStructure = (typeFichierJson != null) ? new NestedJsonStructureFromJsonStructure(typeFichierJson.getJsonStructure()) : null; jsonStructureController = new JsonStructureController(new JsonStructureViewModel(true, 1, nestedJsonStructure)); viewModel = new JsonStructureFormViewModel(typeFichierJson); } @Override public void initialize(URL location, ResourceBundle resources) { viewModel.init(); //On ferme la fenêtre si un résultat vient d'être évalué viewModel.resultProperty().addListener((observable, oldValue, newValue) -> { if(newValue != null) { close(); } }); initTypeFichierField(); loadRootJsonStructureView(); editButton.disableProperty().bind(jsonStructureController.validProperty().not()); editButton.setOnAction(event -> { viewModel.createTypeFichierJSON(jsonStructureController.nestedJsonStructure()); }); } public ObjectProperty resultProperty() { return viewModel.resultProperty(); } public void close() { ((Stage) mainContainer.getScene().getWindow()).close(); } public void mouseDraggedTitleBar(MouseEvent mouseEvent) { } public void mousePressedTitleBar(MouseEvent mouseEvent) { } public void minimize(ActionEvent actionEvent) { } public void maximize(ActionEvent actionEvent) { } private void initTypeFichierField() { typeFichierField.setTitle("Choix du type de fichier de la tâche"); typeFichierField.setColumns(Arrays.asList(TypeFichierTable.codeColumn(), TypeFichierTable.libelleColumn(), TypeFichierTable.participantColumn())); typeFichierField.setSearchFieldPairs(Arrays.asList(new Pair<>("code", "Code"), new Pair<>("libelle", "Libellé"))); typeFichierField.setOldElement(viewModel.selectedTypeFichierProperty().get()); typeFichierField.selectedElementProperty().bindBidirectional(viewModel.selectedTypeFichierProperty()); typeFichierField.selectedElementProperty().addListener((observable, oldValue, newValue) -> { jsonStructureController.setRootStructureName((newValue != null) ? newValue.getCode() : ""); }); typeFichierField.setElements(viewModel.getTypeFichiers().stream().collect(Collectors.toSet())); viewModel.getTypeFichiers().addListener((ListChangeListener) c -> { while (c.next()) { Platform.runLater(() -> typeFichierField.setElements(viewModel.getTypeFichiers().stream().collect(Collectors.toSet()))); } }); } private void loadRootJsonStructureView() { try { FXMLLoader loader = new FXMLLoader(jsonStructureController.getClass().getResource("JsonStructureView.fxml")); loader.setController(jsonStructureController); Parent root = loader.load(); mainContainer.getChildren().add(root); } catch (IOException e) { throw new RuntimeException(e); } } }