From e02b863a39cc82f51459bb451990fddb77470406 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Thu, 8 Dec 2016 22:17:03 +0100 Subject: [PATCH] Main: added merge and multiple file opening --- .../kauron/jstudy/controller/Controller.java | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index d0f31a4..6cb02dd 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -83,12 +83,43 @@ public class Controller implements Initializable { private void onLoadAction(ActionEvent event) { FileChooser chooser = new FileChooser(); chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JStudy file", "*.jsdb")); - File file = chooser.showOpenDialog(root.getScene().getWindow()); - if (file == null) return; - List aux = TestItem.loadFrom(file, TestItem.COLONS); - if (aux != null) { - tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); - tabPane.getSelectionModel().selectLast(); + List list = chooser.showOpenMultipleDialog(root.getScene().getWindow()); + if (list == null || list.isEmpty()) return; + if (list.size() > 1) { + ButtonType mergeBT = new ButtonType("Merge", ButtonBar.ButtonData.APPLY); + ButtonType openBT = new ButtonType("Open", ButtonBar.ButtonData.OK_DONE); + Alert alert = new Alert(Alert.AlertType.CONFIRMATION); + alert.getDialogPane().getButtonTypes().clear(); + alert.getDialogPane().getButtonTypes().addAll(mergeBT, openBT, ButtonType.CANCEL); + alert.setTitle("Opening files..."); + alert.setHeaderText("Several files have been opened"); + alert.setContentText("Merge files or open them separately?"); + + Optional result = alert.showAndWait(); + + if (result.isPresent() && result.get().equals(mergeBT)) { + List aux = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + aux.addAll(TestItem.loadFrom(list.get(i), TestItem.COLONS)); + } + tabPane.getTabs().add(createTableTab("Merge table", aux, null)); + tabPane.getSelectionModel().selectLast(); + } else if (result.isPresent() && result.get().equals(openBT)){ + for (int i = 0; i < list.size(); i++) { + File file = list.get(i); + List aux = TestItem.loadFrom(file, TestItem.COLONS); + if (aux != null) { + tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); + } + } + } + } else { + File file = list.get(0); + List aux = TestItem.loadFrom(file, TestItem.COLONS); + if (aux != null) { + tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux, file)); + tabPane.getSelectionModel().selectLast(); + } } }