From 8f4e8f66e19c78ce414cd946686547f42522c92c Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Sun, 12 Jun 2016 00:50:51 +0200 Subject: [PATCH] Import from CSV (comma separated without commas in the values) --- .../es/kauron/jstudy/controller/Controller.java | 15 ++++++++++++++- .../java/es/kauron/jstudy/model/TestItem.java | 8 +++++--- .../resources/es/kauron/jstudy/view/main.fxml | 8 ++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main/java/es/kauron/jstudy/controller/Controller.java b/src/main/java/es/kauron/jstudy/controller/Controller.java index ebc4147..b7f43f9 100644 --- a/src/main/java/es/kauron/jstudy/controller/Controller.java +++ b/src/main/java/es/kauron/jstudy/controller/Controller.java @@ -61,7 +61,7 @@ public class Controller implements Initializable { 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); + List aux = TestItem.loadFrom(file, TestItem.COLONS); if (aux != null) { tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux)); tabPane.getSelectionModel().selectLast(); @@ -77,6 +77,19 @@ public class Controller implements Initializable { TestItem.saveTo(file, tabMap.get(tabPane.getSelectionModel().getSelectedItem()).getData()); } + @FXML + private void onImportAction(ActionEvent event) { + FileChooser chooser = new FileChooser(); + chooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Data files (.csv)", "*.csv")); + File file = chooser.showOpenDialog(root.getScene().getWindow()); + if (file == null) return; + List aux = TestItem.loadFrom(file, TestItem.COMMA); + if (aux != null) { + tabPane.getTabs().add(createTableTab(file.getName().substring(0, file.getName().lastIndexOf('.')), aux)); + tabPane.getSelectionModel().selectLast(); + } + } + private Tab createTableTab(String name, List list) { try { Tab tab = new Tab(name); diff --git a/src/main/java/es/kauron/jstudy/model/TestItem.java b/src/main/java/es/kauron/jstudy/model/TestItem.java index c57c14a..d791797 100644 --- a/src/main/java/es/kauron/jstudy/model/TestItem.java +++ b/src/main/java/es/kauron/jstudy/model/TestItem.java @@ -10,6 +10,8 @@ import java.util.List; import java.util.Scanner; public class TestItem { + public static final String COMMA = ",", COLONS = "::"; + private StringProperty question, answer; public TestItem(SimpleStringProperty question, SimpleStringProperty answer) { @@ -62,15 +64,15 @@ public class TestItem { } } - public static List loadFrom(File file) { + public static List loadFrom(File file, String separator) { List list = new ArrayList<>(); try { Scanner in = new Scanner(file); while (in.hasNextLine()) { String line = in.nextLine(); - int index = line.indexOf("::"); + int index = line.indexOf(separator); String question = line.substring(0, index); - String answer = line.substring(index + 2); + String answer = line.substring(index + separator.length()); list.add(new TestItem(question, answer)); System.err.println(question + " :: " + answer); } diff --git a/src/main/resources/es/kauron/jstudy/view/main.fxml b/src/main/resources/es/kauron/jstudy/view/main.fxml index dd2e6fb..8866392 100644 --- a/src/main/resources/es/kauron/jstudy/view/main.fxml +++ b/src/main/resources/es/kauron/jstudy/view/main.fxml @@ -24,6 +24,7 @@