1
0
Fork 0
mirror of https://gitlab.com/kauron/jstudy synced 2024-07-01 07:53:02 +02:00

Import from CSV (comma separated without commas in the values)

This commit is contained in:
Carlos Galindo 2016-06-12 00:50:51 +02:00
parent 9c80a1d722
commit 8f4e8f66e1
Signed by: kauron
GPG key ID: 83E68706DEE119A3
3 changed files with 27 additions and 4 deletions

View file

@ -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<TestItem> aux = TestItem.loadFrom(file);
List<TestItem> 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<TestItem> 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<TestItem> list) {
try {
Tab tab = new Tab(name);

View file

@ -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<TestItem> loadFrom(File file) {
public static List<TestItem> loadFrom(File file, String separator) {
List<TestItem> 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);
}

View file

@ -24,6 +24,7 @@
<children>
<Button mnemonicParsing="false" onAction="#onNewAction" prefWidth="100.0" text="New table" />
<Button mnemonicParsing="false" onAction="#onLoadAction" prefWidth="100.0" text="Load file" />
<Button layoutX="260.0" layoutY="187.0" mnemonicParsing="false" onAction="#onImportAction" prefWidth="100.0" text="Import data" />
</children>
</VBox>
</content>
@ -41,6 +42,7 @@
<KeyCodeCombination alt="UP" code="N" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem onAction="#onLoadAction" text="_Open">
<accelerator>
<KeyCodeCombination alt="UP" code="O" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
@ -51,6 +53,12 @@
<KeyCodeCombination alt="UP" code="S" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem onAction="#onImportAction" text="_Open">
<accelerator>
<KeyCodeCombination alt="UP" code="O" control="DOWN" meta="UP" shift="UP" shortcut="UP" />
</accelerator>
</MenuItem>
</items>
</Menu>
<Menu text="_Options">