From 12fa70f22ebe8cc7c21cd758bf61815779f221a4 Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 16 Sep 2023 15:40:35 -0500 Subject: Improve setting value usability Some settings used to use long, sentence-like values in the config file. Now, they use simpler values that are easier to remember and specify in the manual. --- src/main/java/sevenUnitsGUI/Presenter.java | 81 +++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 13 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 0b5247d..2c8ae71 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -49,6 +49,9 @@ import sevenUnits.unit.UnitValue; import sevenUnits.utils.Nameable; import sevenUnits.utils.ObjectProduct; import sevenUnits.utils.UncertainDouble; +import sevenUnitsGUI.StandardDisplayRules.FixedDecimals; +import sevenUnitsGUI.StandardDisplayRules.FixedPrecision; +import sevenUnitsGUI.StandardDisplayRules.UncertaintyBased; /** * An object that handles interactions between the view and the backend code @@ -620,8 +623,7 @@ public final class Presenter { this.database.loadUnitsFile(pathFromConfig(value)); break; case "number_display_rule": - this.numberDisplayRule = StandardDisplayRules - .getStandardRule(value); + this.setDisplayRuleFromString(value); break; case "prefix_rule": this.prefixRepetitionRule = DefaultPrefixRepetitionRule @@ -635,14 +637,7 @@ public final class Presenter { this.showDuplicates = Boolean.valueOf(value); break; case "search_prefix_rule": - if (PrefixSearchRule.NO_PREFIXES.toString().equals(value)) { - this.searchRule = PrefixSearchRule.NO_PREFIXES; - } else if (PrefixSearchRule.COMMON_PREFIXES.toString() - .equals(value)) { - this.searchRule = PrefixSearchRule.COMMON_PREFIXES; - } else { - this.searchRule = this.getUniversalSearchRule(); - } + this.setSearchRuleFromString(value); break; default: System.err.printf("Warning: unrecognized setting \"%s\".%n", @@ -656,6 +651,42 @@ public final class Presenter { } catch (final IOException e) {} } + private void setSearchRuleFromString(String ruleString) { + switch (ruleString) { + case "NO_PREFIXES": + this.searchRule = PrefixSearchRule.NO_PREFIXES; + break; + case "COMMON_PREFIXES": + this.searchRule = PrefixSearchRule.COMMON_PREFIXES; + break; + case "ALL_METRIC_PREFIXES": + this.searchRule = PrefixSearchRule.ALL_METRIC_PREFIXES; + break; + default: + System.err.printf("Warning: unrecognized value for search_prefix_rule: %s\n", ruleString); + } + } + + private void setDisplayRuleFromString(String ruleString) { + String[] tokens = ruleString.split(" "); + switch (tokens[0]) { + case "FIXED_DECIMALS": + final int decimals = Integer.parseInt(tokens[1]); + this.numberDisplayRule = StandardDisplayRules.fixedDecimals(decimals); + break; + case "FIXED_PRECISION": + final int sigDigs = Integer.parseInt(tokens[1]); + this.numberDisplayRule = StandardDisplayRules.fixedPrecision(sigDigs); + break; + case "UNCERTAINTY_BASED": + this.numberDisplayRule = StandardDisplayRules.uncertaintyBased(); + break; + default: + this.numberDisplayRule = StandardDisplayRules.getStandardRule(ruleString); + break; + } + } + /** * @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 @@ -725,15 +756,15 @@ public final class Presenter { boolean writeSettings(Path settingsFile) { try (BufferedWriter writer = Files.newBufferedWriter(settingsFile)) { writer.write(String.format("number_display_rule=%s\n", - this.numberDisplayRule)); + displayRuleToString(this.numberDisplayRule))); writer.write( String.format("prefix_rule=%s\n", this.prefixRepetitionRule)); writer.write( String.format("one_way=%s\n", this.oneWayConversionEnabled)); writer.write( String.format("include_duplicates=%s\n", this.showDuplicates)); - writer.write( - String.format("search_prefix_rule=%s\n", this.searchRule)); + writer.write(String.format("search_prefix_rule=%s\n", + searchRuleToString(this.searchRule))); return true; } catch (final IOException e) { e.printStackTrace(); @@ -744,6 +775,30 @@ public final class Presenter { } } + private static String searchRuleToString(Function, Map> searchRule) { + if (PrefixSearchRule.NO_PREFIXES.equals(searchRule)) { + return "NO_PREFIXES"; + } else if (PrefixSearchRule.COMMON_PREFIXES.equals(searchRule)) { + return "COMMON_PREFIXES"; + } else if (PrefixSearchRule.ALL_METRIC_PREFIXES.equals(searchRule)) { + return "ALL_METRIC_PREFIXES"; + } else + return searchRule.toString(); + } + + private static String displayRuleToString(Function numberDisplayRule) { + if (numberDisplayRule instanceof FixedDecimals) { + return String.format("FIXED_DECIMALS %d", + ((FixedDecimals) numberDisplayRule) .decimalPlaces()); + } else if (numberDisplayRule instanceof FixedPrecision) { + return String.format("FIXED_PRECISION %d", + ((FixedPrecision) numberDisplayRule).significantFigures()); + } else if (numberDisplayRule instanceof UncertaintyBased) { + return "UNCERTAINTY_BASED"; + } else + return numberDisplayRule.toString(); + } + /** * @param numberDisplayRule the new rule that will be used by this presenter * to convert numbers into strings -- cgit v1.2.3