Kenmegne
6 days ago 908e81dd173f380879d5fabeb5794d5b7a76df0e
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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<TypeFichier> 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<TypeFichierJson> 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<TypeFichier>) 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);
        }
    }
 
}