JCash/src/me/teridax/jcash/gui/Loader.java

51 lines
1.9 KiB
Java

package me.teridax.jcash.gui;
import me.teridax.jcash.Logging;
import me.teridax.jcash.banking.management.BankingManagementSystem;
import me.teridax.jcash.lang.Translator;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import static javax.swing.JFileChooser.APPROVE_OPTION;
/**
* Utility class for loading a BMS configuration from a csv file.
*/
public class Loader {
/**
* Filter that only allows for files with *.csv extension
*/
private static final FileNameExtensionFilter FILE_FILTER = new FileNameExtensionFilter("Comma separated value spreadsheet", "csv", "CSV");
private Loader() {}
/**
* Load a BMS from a csv file. Opens up a dialog which prompts the user to select a single file.
* Once the file is selected this function will try to parse the contents to a BMS and return the instance.
* @return a valid BMS instance loaded from a file
* @throws IllegalStateException When either no file is selected or the selected files content is invalid
*/
public static BankingManagementSystem load() throws IllegalStateException {
var fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileChooser.setFileFilter(FILE_FILTER);
fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
fileChooser.setAcceptAllFileFilterUsed(false);
if (fileChooser.showDialog(null, Translator.translate("Load database")) == APPROVE_OPTION) {
// parse file content
try {
return BankingManagementSystem.loadFromCsv(fileChooser.getSelectedFile().toPath());
} catch (Exception e) {
throw new IllegalStateException("Unable to load database", e);
}
}
Logging.LOGGER.warning("no file selected");
throw new IllegalStateException("No file selected");
}
}