summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-16 15:40:35 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-16 16:09:39 -0500
commit12fa70f22ebe8cc7c21cd758bf61815779f221a4 (patch)
treef52fc6799281af802519b7c00e788b9f3ee49c23 /src/main
parent0b67f9500f8c9487b1e3c69209e44e00a0fdbd02 (diff)
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.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java81
1 files changed, 68 insertions, 13 deletions
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.Entry<String, LinearUnit>, Map<String, LinearUnit>> 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<UncertainDouble, String> 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