summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java126
1 files changed, 74 insertions, 52 deletions
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index 2c8ae71..fb134bb 100644
--- a/src/main/java/sevenUnitsGUI/Presenter.java
+++ b/src/main/java/sevenUnitsGUI/Presenter.java
@@ -596,59 +596,69 @@ public final class Presenter {
* @since 2021-12-15
*/
void loadSettings(Path settingsFile) {
- try {
- // read file line by line
- final int lineNum = 0;
- for (final String line : Files.readAllLines(settingsFile)) {
- final int equalsIndex = line.indexOf('=');
- if (equalsIndex == -1)
- throw new IllegalStateException(
- "Settings file is malformed at line " + lineNum);
-
- final String param = line.substring(0, equalsIndex);
- final String value = line.substring(equalsIndex + 1);
-
- switch (param) {
- // set manually to avoid the unnecessary saving of the non-manual
- // methods
- case "custom_dimension_file":
- this.database.loadDimensionFile(pathFromConfig(value));
- break;
- case "custom_exception_file":
- Files.lines(pathFromConfig(value))
- .map(Presenter::withoutComments)
- .forEach(this.metricExceptions::add);
- break;
- case "custom_unit_file":
- this.database.loadUnitsFile(pathFromConfig(value));
- break;
- case "number_display_rule":
- this.setDisplayRuleFromString(value);
- break;
- case "prefix_rule":
- this.prefixRepetitionRule = DefaultPrefixRepetitionRule
- .valueOf(value);
- this.database.setPrefixRepetitionRule(this.prefixRepetitionRule);
- break;
- case "one_way":
- this.oneWayConversionEnabled = Boolean.valueOf(value);
- break;
- case "include_duplicates":
- this.showDuplicates = Boolean.valueOf(value);
- break;
- case "search_prefix_rule":
- this.setSearchRuleFromString(value);
- break;
- default:
- System.err.printf("Warning: unrecognized setting \"%s\".%n",
- param);
- break;
- }
- }
- if (this.view.getPresenter() != null) {
- this.updateView();
+ for (Map.Entry<String, String> setting : settingsFromFile(settingsFile)) {
+ final String value = setting.getValue();
+
+ switch (setting.getKey()) {
+ // set manually to avoid the unnecessary saving of the non-manual
+ // methods
+ case "custom_dimension_file":
+ this.database.loadDimensionFile(pathFromConfig(value));
+ break;
+ case "custom_exception_file":
+ this.loadExceptionFile(pathFromConfig(value));
+ break;
+ case "custom_unit_file":
+ this.database.loadUnitsFile(pathFromConfig(value));
+ break;
+ case "number_display_rule":
+ this.setDisplayRuleFromString(value);
+ break;
+ case "prefix_rule":
+ this.prefixRepetitionRule = DefaultPrefixRepetitionRule
+ .valueOf(value);
+ this.database.setPrefixRepetitionRule(this.prefixRepetitionRule);
+ break;
+ case "one_way":
+ this.oneWayConversionEnabled = Boolean.valueOf(value);
+ break;
+ case "include_duplicates":
+ this.showDuplicates = Boolean.valueOf(value);
+ break;
+ case "search_prefix_rule":
+ this.setSearchRuleFromString(value);
+ break;
+ default:
+ System.err.printf("Warning: unrecognized setting \"%s\".%n",
+ setting.getKey());
+ break;
}
- } catch (final IOException e) {}
+ }
+
+ if (this.view.getPresenter() != null) {
+ this.updateView();
+ }
+ }
+
+ private List<Map.Entry<String, String>> settingsFromFile(Path settingsFile) {
+ try (Stream<String> lines = Files.lines(settingsFile)) {
+ return lines.map(Presenter::parseSettingLine).toList();
+ } catch (final IOException e) {
+ this.view.showErrorMessage("Settings Loading Error", "Error loading settings file. Using default settings.");
+ return null;
+ }
+ }
+
+ private static Map.Entry<String, String> parseSettingLine(String line) {
+ final int equalsIndex = line.indexOf('=');
+ if (equalsIndex == -1)
+ throw new IllegalStateException(
+ "Settings file is malformed at line: " + line);
+
+ final String param = line.substring(0, equalsIndex);
+ final String value = line.substring(equalsIndex + 1);
+
+ return Map.entry(param, value);
}
private void setSearchRuleFromString(String ruleString) {
@@ -687,6 +697,18 @@ public final class Presenter {
}
}
+ private void loadExceptionFile(Path exceptionFile) {
+ try (Stream<String> lines = Files.lines(exceptionFile)) {
+ lines.map(Presenter::withoutComments)
+ .forEach(this.metricExceptions::add);
+ } catch (IOException e) {
+ this.view.showErrorMessage("File Load Error",
+ "Error loading configured metric exception file \""
+ + exceptionFile + "\": "
+ + e.getLocalizedMessage());
+ }
+ }
+
/**
* @return true iff the One-Way Conversion feature is available (views that
* show units as a list will have metric units removed from the From